From a5549023804a8cef50f71dc2d7a723be8e83f3d1 Mon Sep 17 00:00:00 2001
From: Markus Reiter <me@reitermark.us>
Date: Tue, 28 Feb 2017 14:50:46 +0100
Subject: [PATCH] Add `mktmpdir` helper method.

---
 Library/Homebrew/test/cmd/bundle_spec.rb        |  4 ++--
 Library/Homebrew/test/cmd/commands_spec.rb      |  2 +-
 .../test/cmd/custom-external-command_spec.rb    |  4 +---
 Library/Homebrew/test/cmd/linkapps_spec.rb      |  6 +-----
 Library/Homebrew/test/cmd/unlinkapps_spec.rb    |  6 +-----
 Library/Homebrew/test/cmd/unpack_spec.rb        |  4 +---
 Library/Homebrew/test/dev-cmd/audit_spec.rb     | 12 ++----------
 Library/Homebrew/test/diagnostic_spec.rb        | 14 +++++++-------
 Library/Homebrew/test/gpg2_requirement_spec.rb  |  6 +-----
 Library/Homebrew/test/gpg_spec.rb               |  3 +--
 Library/Homebrew/test/java_requirement_spec.rb  |  6 +-----
 Library/Homebrew/test/language/go_spec.rb       |  2 +-
 Library/Homebrew/test/language/python_spec.rb   |  4 +---
 .../test/os/mac/java_requirement_spec.rb        | 17 +++++++----------
 Library/Homebrew/test/pathname_spec.rb          |  8 +++-----
 Library/Homebrew/test/sandbox_spec.rb           |  6 +-----
 Library/Homebrew/test/spec_helper.rb            |  2 ++
 .../Homebrew/test/support/helper/mktmpdir.rb    | 11 +++++++++++
 Library/Homebrew/test/utils_spec.rb             |  7 ++-----
 19 files changed, 47 insertions(+), 77 deletions(-)
 create mode 100644 Library/Homebrew/test/support/helper/mktmpdir.rb

diff --git a/Library/Homebrew/test/cmd/bundle_spec.rb b/Library/Homebrew/test/cmd/bundle_spec.rb
index 755f9ab3d8..13f13485ce 100644
--- a/Library/Homebrew/test/cmd/bundle_spec.rb
+++ b/Library/Homebrew/test/cmd/bundle_spec.rb
@@ -10,9 +10,9 @@ describe "brew bundle", :integration_test, :needs_test_cmd_taps do
         end
       end
 
-      Dir.mktmpdir do |path|
+      mktmpdir do |path|
         FileUtils.touch "#{path}/Brewfile"
-        Dir.chdir path do
+        path.cd do
           expect { brew "bundle", "check" }
             .to output("The Brewfile's dependencies are satisfied.\n").to_stdout
             .and not_to_output.to_stderr
diff --git a/Library/Homebrew/test/cmd/commands_spec.rb b/Library/Homebrew/test/cmd/commands_spec.rb
index 32d07a0bc0..cf6f567404 100644
--- a/Library/Homebrew/test/cmd/commands_spec.rb
+++ b/Library/Homebrew/test/cmd/commands_spec.rb
@@ -54,7 +54,7 @@ describe Homebrew do
   end
 
   specify "::external_commands" do
-    Dir.mktmpdir do |dir|
+    mktmpdir do |dir|
       %w[brew-t1 brew-t2.rb brew-t3.py].each do |file|
         path = "#{dir}/#{file}"
         FileUtils.touch path
diff --git a/Library/Homebrew/test/cmd/custom-external-command_spec.rb b/Library/Homebrew/test/cmd/custom-external-command_spec.rb
index 8ccc21fa79..d649786ec8 100644
--- a/Library/Homebrew/test/cmd/custom-external-command_spec.rb
+++ b/Library/Homebrew/test/cmd/custom-external-command_spec.rb
@@ -1,8 +1,6 @@
 describe "brew custom-external-command", :integration_test do
   it "is supported" do
-    Dir.mktmpdir do |path|
-      path = Pathname.new(path)
-
+    mktmpdir do |path|
       cmd = "custom-external-command-#{rand}"
       file = path/"brew-#{cmd}"
 
diff --git a/Library/Homebrew/test/cmd/linkapps_spec.rb b/Library/Homebrew/test/cmd/linkapps_spec.rb
index 42118a215e..2bca978228 100644
--- a/Library/Homebrew/test/cmd/linkapps_spec.rb
+++ b/Library/Homebrew/test/cmd/linkapps_spec.rb
@@ -1,11 +1,7 @@
 describe "brew linkapps", :integration_test do
-  let(:home_dir) { @home_dir = Pathname.new(Dir.mktmpdir) }
+  let(:home_dir) { mktmpdir }
   let(:apps_dir) { home_dir/"Applications" }
 
-  after(:each) do
-    home_dir.rmtree unless @home_dir.nil?
-  end
-
   it "symlinks applications" do
     apps_dir.mkpath
 
diff --git a/Library/Homebrew/test/cmd/unlinkapps_spec.rb b/Library/Homebrew/test/cmd/unlinkapps_spec.rb
index 1e21bd851a..e1170f4355 100644
--- a/Library/Homebrew/test/cmd/unlinkapps_spec.rb
+++ b/Library/Homebrew/test/cmd/unlinkapps_spec.rb
@@ -1,11 +1,7 @@
 describe "brew unlinkapps", :integration_test do
-  let(:home_dir) { @home_dir = Pathname.new(Dir.mktmpdir) }
+  let(:home_dir) { mktmpdir }
   let(:apps_dir) { home_dir/"Applications" }
 
-  after(:each) do
-    home_dir.rmtree unless @home_dir.nil?
-  end
-
   it "unlinks symlinked applications" do
     apps_dir.mkpath
 
diff --git a/Library/Homebrew/test/cmd/unpack_spec.rb b/Library/Homebrew/test/cmd/unpack_spec.rb
index 244fc08522..9b2b801bc1 100644
--- a/Library/Homebrew/test/cmd/unpack_spec.rb
+++ b/Library/Homebrew/test/cmd/unpack_spec.rb
@@ -2,9 +2,7 @@ describe "brew unpack", :integration_test do
   it "unpacks a given Formula's archive" do
     setup_test_formula "testball"
 
-    Dir.mktmpdir do |path|
-      path = Pathname.new(path)
-
+    mktmpdir do |path|
       shutup do
         expect { brew "unpack", "testball", "--destdir=#{path}" }
           .to be_a_success
diff --git a/Library/Homebrew/test/dev-cmd/audit_spec.rb b/Library/Homebrew/test/dev-cmd/audit_spec.rb
index c4afe65924..ec1a34fb4a 100644
--- a/Library/Homebrew/test/dev-cmd/audit_spec.rb
+++ b/Library/Homebrew/test/dev-cmd/audit_spec.rb
@@ -6,11 +6,7 @@ RSpec::Matchers.alias_matcher :have_end, :be_end
 RSpec::Matchers.alias_matcher :have_trailing_newline, :be_trailing_newline
 
 describe FormulaText do
-  let(:dir) { @dir = Pathname.new(Dir.mktmpdir) }
-
-  after(:each) do
-    dir.rmtree unless @dir.nil?
-  end
+  let(:dir) { mktmpdir }
 
   def formula_text(name, body = nil, options = {})
     path = dir/"#{name}.rb"
@@ -70,11 +66,7 @@ describe FormulaAuditor do
     described_class.new(Formulary.factory(path), options)
   end
 
-  let(:dir) { @dir = Pathname.new(Dir.mktmpdir) }
-
-  after(:each) do
-    dir.rmtree unless @dir.nil?
-  end
+  let(:dir) { mktmpdir }
 
   describe "#problems" do
     it "is empty by default" do
diff --git a/Library/Homebrew/test/diagnostic_spec.rb b/Library/Homebrew/test/diagnostic_spec.rb
index e749a3b0f4..59560127cb 100644
--- a/Library/Homebrew/test/diagnostic_spec.rb
+++ b/Library/Homebrew/test/diagnostic_spec.rb
@@ -13,7 +13,7 @@ describe Homebrew::Diagnostic::Checks do
   end
 
   specify "#check_for_anaconda" do
-    Dir.mktmpdir do |path|
+    mktmpdir do |path|
       anaconda = "#{path}/anaconda"
       python = "#{path}/python"
       FileUtils.touch anaconda
@@ -23,7 +23,7 @@ describe Homebrew::Diagnostic::Checks do
       FileUtils.chmod 0755, anaconda
       FileUtils.chmod 0755, python
 
-      ENV["PATH"] = path + File::PATH_SEPARATOR + ENV["PATH"]
+      ENV["PATH"] = "#{path}#{File::PATH_SEPARATOR}#{ENV["PATH"]}"
 
       expect(subject.check_for_anaconda).to match("Anaconda")
     end
@@ -124,7 +124,7 @@ describe Homebrew::Diagnostic::Checks do
   end
 
   specify "#check_user_curlrc" do
-    Dir.mktmpdir do |path|
+    mktmpdir do |path|
       FileUtils.touch "#{path}/.curlrc"
       ENV["CURL_HOME"] = path
 
@@ -133,7 +133,7 @@ describe Homebrew::Diagnostic::Checks do
   end
 
   specify "#check_for_config_scripts" do
-    Dir.mktmpdir do |path|
+    mktmpdir do |path|
       file = "#{path}/foo-config"
       FileUtils.touch file
       FileUtils.chmod 0755, file
@@ -153,7 +153,7 @@ describe Homebrew::Diagnostic::Checks do
     begin
       HOMEBREW_CELLAR.rmtree
 
-      Dir.mktmpdir do |path|
+      mktmpdir do |path|
         FileUtils.ln_s path, HOMEBREW_CELLAR
 
         expect(subject.check_for_symlinked_cellar).to match(path)
@@ -170,8 +170,8 @@ describe Homebrew::Diagnostic::Checks do
   end
 
   specify "#check_for_external_cmd_name_conflict" do
-    Dir.mktmpdir do |path1|
-      Dir.mktmpdir do |path2|
+    mktmpdir do |path1|
+      mktmpdir do |path2|
         [path1, path2].each do |path|
           cmd = "#{path}/brew-foo"
           FileUtils.touch cmd
diff --git a/Library/Homebrew/test/gpg2_requirement_spec.rb b/Library/Homebrew/test/gpg2_requirement_spec.rb
index f46b31196f..d7767abd33 100644
--- a/Library/Homebrew/test/gpg2_requirement_spec.rb
+++ b/Library/Homebrew/test/gpg2_requirement_spec.rb
@@ -2,11 +2,7 @@ require "requirements/gpg2_requirement"
 require "fileutils"
 
 describe GPG2Requirement do
-  let(:dir) { @dir = Pathname.new(Dir.mktmpdir) }
-
-  after(:each) do
-    FileUtils.rm_rf dir unless @dir.nil?
-  end
+  let(:dir) { mktmpdir }
 
   describe "#satisfied?" do
     it "returns true if GPG2 is installed" do
diff --git a/Library/Homebrew/test/gpg_spec.rb b/Library/Homebrew/test/gpg_spec.rb
index 9809dccb5e..aa00d79f51 100644
--- a/Library/Homebrew/test/gpg_spec.rb
+++ b/Library/Homebrew/test/gpg_spec.rb
@@ -7,9 +7,8 @@ describe Gpg do
     it "creates a test key in the home directory" do
       skip "GPG Unavailable" unless subject.available?
 
-      Dir.mktmpdir do |dir|
+      mktmpdir do |dir|
         ENV["HOME"] = dir
-        dir = Pathname.new(dir)
 
         shutup do
           subject.create_test_key(dir)
diff --git a/Library/Homebrew/test/java_requirement_spec.rb b/Library/Homebrew/test/java_requirement_spec.rb
index 5adf64c7c2..05d4f3cdac 100644
--- a/Library/Homebrew/test/java_requirement_spec.rb
+++ b/Library/Homebrew/test/java_requirement_spec.rb
@@ -46,7 +46,7 @@ describe JavaRequirement do
     end
 
     context "when #possible_javas contains paths" do
-      let(:path) { Pathname.new(Dir.mktmpdir) }
+      let(:path) { mktmpdir }
       let(:java) { path/"java" }
 
       def setup_java_with_version(version)
@@ -61,10 +61,6 @@ describe JavaRequirement do
         allow(subject).to receive(:possible_javas).and_return([java])
       end
 
-      after(:each) do
-        path.rmtree
-      end
-
       context "and 1.7 is required" do
         subject { described_class.new(%w[1.7]) }
 
diff --git a/Library/Homebrew/test/language/go_spec.rb b/Library/Homebrew/test/language/go_spec.rb
index 24db78594f..fb8c978294 100644
--- a/Library/Homebrew/test/language/go_spec.rb
+++ b/Library/Homebrew/test/language/go_spec.rb
@@ -6,7 +6,7 @@ describe Language::Go do
 
     expect(described_class).to receive(:opoo).once
 
-    Dir.mktmpdir do |path|
+    mktmpdir do |path|
       shutup do
         described_class.stage_deps [], path
       end
diff --git a/Library/Homebrew/test/language/python_spec.rb b/Library/Homebrew/test/language/python_spec.rb
index c785208977..02f6bf8d24 100644
--- a/Library/Homebrew/test/language/python_spec.rb
+++ b/Library/Homebrew/test/language/python_spec.rb
@@ -4,14 +4,12 @@ require "resource"
 describe Language::Python::Virtualenv::Virtualenv do
   subject { described_class.new(formula, dir, "python") }
 
-  let(:dir) { @dir = Pathname.new(Dir.mktmpdir) }
+  let(:dir) { mktmpdir }
 
   let(:resource) { double("resource", stage: true) }
   let(:formula_bin) { dir/"formula_bin" }
   let(:formula) { double("formula", resource: resource, bin: formula_bin) }
 
-  after(:each) { dir.rmtree unless @dir.nil? }
-
   describe "#create" do
     it "creates a virtual environment" do
       expect(formula).to receive(:resource).with("homebrew-virtualenv").and_return(resource)
diff --git a/Library/Homebrew/test/os/mac/java_requirement_spec.rb b/Library/Homebrew/test/os/mac/java_requirement_spec.rb
index f6404db926..1b46fe6b64 100644
--- a/Library/Homebrew/test/os/mac/java_requirement_spec.rb
+++ b/Library/Homebrew/test/os/mac/java_requirement_spec.rb
@@ -3,32 +3,29 @@ require "fileutils"
 
 describe JavaRequirement do
   subject { described_class.new(%w[1.8]) }
-  let(:java_home) { Dir.mktmpdir }
-  let(:java_home_path) { Pathname.new(java_home) }
+  let(:java_home) { mktmpdir }
 
   before(:each) do
-    FileUtils.mkdir java_home_path/"bin"
-    FileUtils.touch java_home_path/"bin/java"
-    allow(subject).to receive(:preferred_java).and_return(java_home_path/"bin/java")
+    FileUtils.mkdir java_home/"bin"
+    FileUtils.touch java_home/"bin/java"
+    allow(subject).to receive(:preferred_java).and_return(java_home/"bin/java")
     expect(subject).to be_satisfied
   end
 
-  after(:each) { java_home_path.rmtree }
-
   specify "Apple Java environment" do
     expect(ENV).to receive(:prepend_path)
     expect(ENV).to receive(:append_to_cflags)
 
     subject.modify_build_environment
-    expect(ENV["JAVA_HOME"]).to eq(java_home)
+    expect(ENV["JAVA_HOME"]).to eq(java_home.to_s)
   end
 
   specify "Oracle Java environment" do
-    FileUtils.mkdir java_home_path/"include"
+    FileUtils.mkdir java_home/"include"
     expect(ENV).to receive(:prepend_path)
     expect(ENV).to receive(:append_to_cflags).twice
 
     subject.modify_build_environment
-    expect(ENV["JAVA_HOME"]).to eq(java_home)
+    expect(ENV["JAVA_HOME"]).to eq(java_home.to_s)
   end
 end
diff --git a/Library/Homebrew/test/pathname_spec.rb b/Library/Homebrew/test/pathname_spec.rb
index 21e14479f0..77cb6cfed1 100644
--- a/Library/Homebrew/test/pathname_spec.rb
+++ b/Library/Homebrew/test/pathname_spec.rb
@@ -5,13 +5,11 @@ require "install_renamed"
 describe Pathname do
   include FileUtils
 
-  let(:src) { Pathname.new(Dir.mktmpdir) }
-  let(:dst) { Pathname.new(Dir.mktmpdir) }
+  let(:src) { mktmpdir }
+  let(:dst) { mktmpdir }
   let(:file) { src/"foo" }
   let(:dir) { src/"bar" }
 
-  after(:each) { rm_rf [src, dst] }
-
   describe DiskUsageExtension do
     before(:each) do
       mkdir_p dir/"a-directory"
@@ -294,7 +292,7 @@ describe Pathname do
 end
 
 describe FileUtils do
-  let(:dst) { Pathname.new(Dir.mktmpdir) }
+  let(:dst) { mktmpdir }
 
   describe "#mkdir" do
     it "creates indermediate directories" do
diff --git a/Library/Homebrew/test/sandbox_spec.rb b/Library/Homebrew/test/sandbox_spec.rb
index 98634bf3cf..0d349f6eb5 100644
--- a/Library/Homebrew/test/sandbox_spec.rb
+++ b/Library/Homebrew/test/sandbox_spec.rb
@@ -3,17 +3,13 @@ require "sandbox"
 RSpec::Matchers.define_negated_matcher :not_matching, :matching
 
 describe Sandbox do
-  let(:dir) { @dir = Pathname.new(Dir.mktmpdir) }
+  let(:dir) { mktmpdir }
   let(:file) { dir/"foo" }
 
   before(:each) do
     skip "Sandbox not implemented." unless described_class.available?
   end
 
-  after(:each) do
-    dir.rmtree unless @dir.nil?
-  end
-
   specify "#formula?" do
     f = formula { url "foo-1.0" }
     f2 = formula { url "bar-1.0" }
diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb
index 7905142a80..122aaba46d 100644
--- a/Library/Homebrew/test/spec_helper.rb
+++ b/Library/Homebrew/test/spec_helper.rb
@@ -17,6 +17,7 @@ require "tap"
 require "test/support/helper/shutup"
 require "test/support/helper/fixtures"
 require "test/support/helper/formula"
+require "test/support/helper/mktmpdir"
 require "test/support/helper/spec/shared_context/integration_test"
 
 TEST_DIRECTORIES = [
@@ -35,6 +36,7 @@ RSpec.configure do |config|
   config.include(Test::Helper::Shutup)
   config.include(Test::Helper::Fixtures)
   config.include(Test::Helper::Formula)
+  config.include(Test::Helper::MkTmpDir)
 
   config.before(:each, :needs_compat) do
     skip "Requires compatibility layer." if ENV["HOMEBREW_NO_COMPAT"]
diff --git a/Library/Homebrew/test/support/helper/mktmpdir.rb b/Library/Homebrew/test/support/helper/mktmpdir.rb
new file mode 100644
index 0000000000..f08fd386b4
--- /dev/null
+++ b/Library/Homebrew/test/support/helper/mktmpdir.rb
@@ -0,0 +1,11 @@
+module Test
+  module Helper
+    module MkTmpDir
+      def mktmpdir(prefix_suffix = nil)
+        new_dir = Pathname.new(Dir.mktmpdir(prefix_suffix, HOMEBREW_TEMP))
+        return yield new_dir if block_given?
+        new_dir
+      end
+    end
+  end
+end
diff --git a/Library/Homebrew/test/utils_spec.rb b/Library/Homebrew/test/utils_spec.rb
index b3fdedcb98..90eed7d88a 100644
--- a/Library/Homebrew/test/utils_spec.rb
+++ b/Library/Homebrew/test/utils_spec.rb
@@ -1,9 +1,7 @@
 require "utils"
 
 describe "globally-scoped helper methods" do
-  let(:dir) { @dir = Pathname.new(Dir.mktmpdir) }
-
-  after(:each) { dir.rmtree unless @dir.nil? }
+  let(:dir) { mktmpdir }
 
   def esc(code)
     /(\e\[\d+m)*\e\[#{code}m/
@@ -195,8 +193,7 @@ describe "globally-scoped helper methods" do
   end
 
   specify "#gzip" do
-    Dir.mktmpdir do |path|
-      path = Pathname.new(path)
+    mktmpdir do |path|
       somefile = path/"somefile"
       FileUtils.touch somefile
       expect(gzip(somefile)[0].to_s).to eq("#{somefile}.gz")
-- 
GitLab