Merge pull request #2232 from reitermarkus/spec-mktmpdir

Add `mktmpdir` helper method.
This commit is contained in:
Markus Reiter 2017-02-28 16:04:04 +01:00 committed by GitHub
commit fa34aa2a21
19 changed files with 47 additions and 77 deletions

View File

@ -10,9 +10,9 @@ describe "brew bundle", :integration_test, :needs_test_cmd_taps do
end end
end end
Dir.mktmpdir do |path| mktmpdir do |path|
FileUtils.touch "#{path}/Brewfile" FileUtils.touch "#{path}/Brewfile"
Dir.chdir path do path.cd do
expect { brew "bundle", "check" } expect { brew "bundle", "check" }
.to output("The Brewfile's dependencies are satisfied.\n").to_stdout .to output("The Brewfile's dependencies are satisfied.\n").to_stdout
.and not_to_output.to_stderr .and not_to_output.to_stderr

View File

@ -54,7 +54,7 @@ describe Homebrew do
end end
specify "::external_commands" do specify "::external_commands" do
Dir.mktmpdir do |dir| mktmpdir do |dir|
%w[brew-t1 brew-t2.rb brew-t3.py].each do |file| %w[brew-t1 brew-t2.rb brew-t3.py].each do |file|
path = "#{dir}/#{file}" path = "#{dir}/#{file}"
FileUtils.touch path FileUtils.touch path

View File

@ -1,8 +1,6 @@
describe "brew custom-external-command", :integration_test do describe "brew custom-external-command", :integration_test do
it "is supported" do it "is supported" do
Dir.mktmpdir do |path| mktmpdir do |path|
path = Pathname.new(path)
cmd = "custom-external-command-#{rand}" cmd = "custom-external-command-#{rand}"
file = path/"brew-#{cmd}" file = path/"brew-#{cmd}"

View File

@ -1,11 +1,7 @@
describe "brew linkapps", :integration_test do 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" } let(:apps_dir) { home_dir/"Applications" }
after(:each) do
home_dir.rmtree unless @home_dir.nil?
end
it "symlinks applications" do it "symlinks applications" do
apps_dir.mkpath apps_dir.mkpath

View File

@ -1,11 +1,7 @@
describe "brew unlinkapps", :integration_test do 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" } let(:apps_dir) { home_dir/"Applications" }
after(:each) do
home_dir.rmtree unless @home_dir.nil?
end
it "unlinks symlinked applications" do it "unlinks symlinked applications" do
apps_dir.mkpath apps_dir.mkpath

View File

@ -2,9 +2,7 @@ describe "brew unpack", :integration_test do
it "unpacks a given Formula's archive" do it "unpacks a given Formula's archive" do
setup_test_formula "testball" setup_test_formula "testball"
Dir.mktmpdir do |path| mktmpdir do |path|
path = Pathname.new(path)
shutup do shutup do
expect { brew "unpack", "testball", "--destdir=#{path}" } expect { brew "unpack", "testball", "--destdir=#{path}" }
.to be_a_success .to be_a_success

View File

@ -6,11 +6,7 @@ RSpec::Matchers.alias_matcher :have_end, :be_end
RSpec::Matchers.alias_matcher :have_trailing_newline, :be_trailing_newline RSpec::Matchers.alias_matcher :have_trailing_newline, :be_trailing_newline
describe FormulaText do describe FormulaText do
let(:dir) { @dir = Pathname.new(Dir.mktmpdir) } let(:dir) { mktmpdir }
after(:each) do
dir.rmtree unless @dir.nil?
end
def formula_text(name, body = nil, options = {}) def formula_text(name, body = nil, options = {})
path = dir/"#{name}.rb" path = dir/"#{name}.rb"
@ -70,11 +66,7 @@ describe FormulaAuditor do
described_class.new(Formulary.factory(path), options) described_class.new(Formulary.factory(path), options)
end end
let(:dir) { @dir = Pathname.new(Dir.mktmpdir) } let(:dir) { mktmpdir }
after(:each) do
dir.rmtree unless @dir.nil?
end
describe "#problems" do describe "#problems" do
it "is empty by default" do it "is empty by default" do

View File

@ -13,7 +13,7 @@ describe Homebrew::Diagnostic::Checks do
end end
specify "#check_for_anaconda" do specify "#check_for_anaconda" do
Dir.mktmpdir do |path| mktmpdir do |path|
anaconda = "#{path}/anaconda" anaconda = "#{path}/anaconda"
python = "#{path}/python" python = "#{path}/python"
FileUtils.touch anaconda FileUtils.touch anaconda
@ -23,7 +23,7 @@ describe Homebrew::Diagnostic::Checks do
FileUtils.chmod 0755, anaconda FileUtils.chmod 0755, anaconda
FileUtils.chmod 0755, python 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") expect(subject.check_for_anaconda).to match("Anaconda")
end end
@ -124,7 +124,7 @@ describe Homebrew::Diagnostic::Checks do
end end
specify "#check_user_curlrc" do specify "#check_user_curlrc" do
Dir.mktmpdir do |path| mktmpdir do |path|
FileUtils.touch "#{path}/.curlrc" FileUtils.touch "#{path}/.curlrc"
ENV["CURL_HOME"] = path ENV["CURL_HOME"] = path
@ -133,7 +133,7 @@ describe Homebrew::Diagnostic::Checks do
end end
specify "#check_for_config_scripts" do specify "#check_for_config_scripts" do
Dir.mktmpdir do |path| mktmpdir do |path|
file = "#{path}/foo-config" file = "#{path}/foo-config"
FileUtils.touch file FileUtils.touch file
FileUtils.chmod 0755, file FileUtils.chmod 0755, file
@ -153,7 +153,7 @@ describe Homebrew::Diagnostic::Checks do
begin begin
HOMEBREW_CELLAR.rmtree HOMEBREW_CELLAR.rmtree
Dir.mktmpdir do |path| mktmpdir do |path|
FileUtils.ln_s path, HOMEBREW_CELLAR FileUtils.ln_s path, HOMEBREW_CELLAR
expect(subject.check_for_symlinked_cellar).to match(path) expect(subject.check_for_symlinked_cellar).to match(path)
@ -170,8 +170,8 @@ describe Homebrew::Diagnostic::Checks do
end end
specify "#check_for_external_cmd_name_conflict" do specify "#check_for_external_cmd_name_conflict" do
Dir.mktmpdir do |path1| mktmpdir do |path1|
Dir.mktmpdir do |path2| mktmpdir do |path2|
[path1, path2].each do |path| [path1, path2].each do |path|
cmd = "#{path}/brew-foo" cmd = "#{path}/brew-foo"
FileUtils.touch cmd FileUtils.touch cmd

View File

@ -2,11 +2,7 @@ require "requirements/gpg2_requirement"
require "fileutils" require "fileutils"
describe GPG2Requirement do describe GPG2Requirement do
let(:dir) { @dir = Pathname.new(Dir.mktmpdir) } let(:dir) { mktmpdir }
after(:each) do
FileUtils.rm_rf dir unless @dir.nil?
end
describe "#satisfied?" do describe "#satisfied?" do
it "returns true if GPG2 is installed" do it "returns true if GPG2 is installed" do

View File

@ -7,9 +7,8 @@ describe Gpg do
it "creates a test key in the home directory" do it "creates a test key in the home directory" do
skip "GPG Unavailable" unless subject.available? skip "GPG Unavailable" unless subject.available?
Dir.mktmpdir do |dir| mktmpdir do |dir|
ENV["HOME"] = dir ENV["HOME"] = dir
dir = Pathname.new(dir)
shutup do shutup do
subject.create_test_key(dir) subject.create_test_key(dir)

View File

@ -46,7 +46,7 @@ describe JavaRequirement do
end end
context "when #possible_javas contains paths" do context "when #possible_javas contains paths" do
let(:path) { Pathname.new(Dir.mktmpdir) } let(:path) { mktmpdir }
let(:java) { path/"java" } let(:java) { path/"java" }
def setup_java_with_version(version) def setup_java_with_version(version)
@ -61,10 +61,6 @@ describe JavaRequirement do
allow(subject).to receive(:possible_javas).and_return([java]) allow(subject).to receive(:possible_javas).and_return([java])
end end
after(:each) do
path.rmtree
end
context "and 1.7 is required" do context "and 1.7 is required" do
subject { described_class.new(%w[1.7]) } subject { described_class.new(%w[1.7]) }

View File

@ -6,7 +6,7 @@ describe Language::Go do
expect(described_class).to receive(:opoo).once expect(described_class).to receive(:opoo).once
Dir.mktmpdir do |path| mktmpdir do |path|
shutup do shutup do
described_class.stage_deps [], path described_class.stage_deps [], path
end end

View File

@ -4,14 +4,12 @@ require "resource"
describe Language::Python::Virtualenv::Virtualenv do describe Language::Python::Virtualenv::Virtualenv do
subject { described_class.new(formula, dir, "python") } subject { described_class.new(formula, dir, "python") }
let(:dir) { @dir = Pathname.new(Dir.mktmpdir) } let(:dir) { mktmpdir }
let(:resource) { double("resource", stage: true) } let(:resource) { double("resource", stage: true) }
let(:formula_bin) { dir/"formula_bin" } let(:formula_bin) { dir/"formula_bin" }
let(:formula) { double("formula", resource: resource, bin: formula_bin) } let(:formula) { double("formula", resource: resource, bin: formula_bin) }
after(:each) { dir.rmtree unless @dir.nil? }
describe "#create" do describe "#create" do
it "creates a virtual environment" do it "creates a virtual environment" do
expect(formula).to receive(:resource).with("homebrew-virtualenv").and_return(resource) expect(formula).to receive(:resource).with("homebrew-virtualenv").and_return(resource)

View File

@ -3,32 +3,29 @@ require "fileutils"
describe JavaRequirement do describe JavaRequirement do
subject { described_class.new(%w[1.8]) } subject { described_class.new(%w[1.8]) }
let(:java_home) { Dir.mktmpdir } let(:java_home) { mktmpdir }
let(:java_home_path) { Pathname.new(java_home) }
before(:each) do before(:each) do
FileUtils.mkdir java_home_path/"bin" FileUtils.mkdir java_home/"bin"
FileUtils.touch java_home_path/"bin/java" FileUtils.touch java_home/"bin/java"
allow(subject).to receive(:preferred_java).and_return(java_home_path/"bin/java") allow(subject).to receive(:preferred_java).and_return(java_home/"bin/java")
expect(subject).to be_satisfied expect(subject).to be_satisfied
end end
after(:each) { java_home_path.rmtree }
specify "Apple Java environment" do specify "Apple Java environment" do
expect(ENV).to receive(:prepend_path) expect(ENV).to receive(:prepend_path)
expect(ENV).to receive(:append_to_cflags) expect(ENV).to receive(:append_to_cflags)
subject.modify_build_environment subject.modify_build_environment
expect(ENV["JAVA_HOME"]).to eq(java_home) expect(ENV["JAVA_HOME"]).to eq(java_home.to_s)
end end
specify "Oracle Java environment" do 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(:prepend_path)
expect(ENV).to receive(:append_to_cflags).twice expect(ENV).to receive(:append_to_cflags).twice
subject.modify_build_environment subject.modify_build_environment
expect(ENV["JAVA_HOME"]).to eq(java_home) expect(ENV["JAVA_HOME"]).to eq(java_home.to_s)
end end
end end

View File

@ -5,13 +5,11 @@ require "install_renamed"
describe Pathname do describe Pathname do
include FileUtils include FileUtils
let(:src) { Pathname.new(Dir.mktmpdir) } let(:src) { mktmpdir }
let(:dst) { Pathname.new(Dir.mktmpdir) } let(:dst) { mktmpdir }
let(:file) { src/"foo" } let(:file) { src/"foo" }
let(:dir) { src/"bar" } let(:dir) { src/"bar" }
after(:each) { rm_rf [src, dst] }
describe DiskUsageExtension do describe DiskUsageExtension do
before(:each) do before(:each) do
mkdir_p dir/"a-directory" mkdir_p dir/"a-directory"
@ -294,7 +292,7 @@ describe Pathname do
end end
describe FileUtils do describe FileUtils do
let(:dst) { Pathname.new(Dir.mktmpdir) } let(:dst) { mktmpdir }
describe "#mkdir" do describe "#mkdir" do
it "creates indermediate directories" do it "creates indermediate directories" do

View File

@ -3,17 +3,13 @@ require "sandbox"
RSpec::Matchers.define_negated_matcher :not_matching, :matching RSpec::Matchers.define_negated_matcher :not_matching, :matching
describe Sandbox do describe Sandbox do
let(:dir) { @dir = Pathname.new(Dir.mktmpdir) } let(:dir) { mktmpdir }
let(:file) { dir/"foo" } let(:file) { dir/"foo" }
before(:each) do before(:each) do
skip "Sandbox not implemented." unless described_class.available? skip "Sandbox not implemented." unless described_class.available?
end end
after(:each) do
dir.rmtree unless @dir.nil?
end
specify "#formula?" do specify "#formula?" do
f = formula { url "foo-1.0" } f = formula { url "foo-1.0" }
f2 = formula { url "bar-1.0" } f2 = formula { url "bar-1.0" }

View File

@ -17,6 +17,7 @@ require "tap"
require "test/support/helper/shutup" require "test/support/helper/shutup"
require "test/support/helper/fixtures" require "test/support/helper/fixtures"
require "test/support/helper/formula" require "test/support/helper/formula"
require "test/support/helper/mktmpdir"
require "test/support/helper/spec/shared_context/integration_test" require "test/support/helper/spec/shared_context/integration_test"
TEST_DIRECTORIES = [ TEST_DIRECTORIES = [
@ -35,6 +36,7 @@ RSpec.configure do |config|
config.include(Test::Helper::Shutup) config.include(Test::Helper::Shutup)
config.include(Test::Helper::Fixtures) config.include(Test::Helper::Fixtures)
config.include(Test::Helper::Formula) config.include(Test::Helper::Formula)
config.include(Test::Helper::MkTmpDir)
config.before(:each, :needs_compat) do config.before(:each, :needs_compat) do
skip "Requires compatibility layer." if ENV["HOMEBREW_NO_COMPAT"] skip "Requires compatibility layer." if ENV["HOMEBREW_NO_COMPAT"]

View File

@ -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

View File

@ -1,9 +1,7 @@
require "utils" require "utils"
describe "globally-scoped helper methods" do describe "globally-scoped helper methods" do
let(:dir) { @dir = Pathname.new(Dir.mktmpdir) } let(:dir) { mktmpdir }
after(:each) { dir.rmtree unless @dir.nil? }
def esc(code) def esc(code)
/(\e\[\d+m)*\e\[#{code}m/ /(\e\[\d+m)*\e\[#{code}m/
@ -195,8 +193,7 @@ describe "globally-scoped helper methods" do
end end
specify "#gzip" do specify "#gzip" do
Dir.mktmpdir do |path| mktmpdir do |path|
path = Pathname.new(path)
somefile = path/"somefile" somefile = path/"somefile"
FileUtils.touch somefile FileUtils.touch somefile
expect(gzip(somefile)[0].to_s).to eq("#{somefile}.gz") expect(gzip(somefile)[0].to_s).to eq("#{somefile}.gz")