From 6e0f1366b014b285799c9bb9c311c6345bcd666b Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sat, 21 Jan 2017 14:56:58 +0000 Subject: [PATCH 01/12] tests: extract a common using_git_env method --- .../Homebrew/test/download_strategies_test.rb | 12 ----- Library/Homebrew/test/formula_test.rb | 46 +++++++------------ Library/Homebrew/test/install_test.rb | 28 ++++------- Library/Homebrew/test/support/helper/env.rb | 28 +++++++++-- Library/Homebrew/test/tap_test.rb | 23 ++++------ 5 files changed, 57 insertions(+), 80 deletions(-) diff --git a/Library/Homebrew/test/download_strategies_test.rb b/Library/Homebrew/test/download_strategies_test.rb index 604671b1e7..0a2068bd98 100644 --- a/Library/Homebrew/test/download_strategies_test.rb +++ b/Library/Homebrew/test/download_strategies_test.rb @@ -157,18 +157,6 @@ class GitDownloadStrategyTests < Homebrew::TestCase end end - def using_git_env - initial_env = ENV.to_hash - %w[AUTHOR COMMITTER].each do |role| - ENV["GIT_#{role}_NAME"] = "brew tests" - ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" - ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" - end - yield - ensure - ENV.replace(initial_env) - end - def setup_git_repo using_git_env do @cached_location.cd do diff --git a/Library/Homebrew/test/formula_test.rb b/Library/Homebrew/test/formula_test.rb index a043423114..74db4ad6c2 100644 --- a/Library/Homebrew/test/formula_test.rb +++ b/Library/Homebrew/test/formula_test.rb @@ -526,8 +526,6 @@ class FormulaTests < Homebrew::TestCase end def test_update_head_version - initial_env = ENV.to_hash - f = formula do head "foo", using: :git end @@ -535,25 +533,19 @@ class FormulaTests < Homebrew::TestCase cached_location = f.head.downloader.cached_location cached_location.mkpath - %w[AUTHOR COMMITTER].each do |role| - ENV["GIT_#{role}_NAME"] = "brew tests" - ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" - ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" - end - - cached_location.cd do - FileUtils.touch "LICENSE" - shutup do - system "git", "init" - system "git", "add", "--all" - system "git", "commit", "-m", "Initial commit" + using_git_env do + cached_location.cd do + FileUtils.touch "LICENSE" + shutup do + system "git", "init" + system "git", "add", "--all" + system "git", "commit", "-m", "Initial commit" + end end end f.update_head_version assert_equal Version.create("HEAD-5658946"), f.head.version - ensure - ENV.replace(initial_env) end def test_legacy_options @@ -1104,7 +1096,6 @@ class OutdatedVersionsTests < Homebrew::TestCase tab_a = setup_tab_for_prefix(head_prefix_a, versions: { "stable" => "1.0" }) setup_tab_for_prefix(head_prefix_b) - initial_env = ENV.to_hash testball_repo = HOMEBREW_PREFIX.join("testball_repo") testball_repo.mkdir @@ -1114,18 +1105,14 @@ class OutdatedVersionsTests < Homebrew::TestCase head "file://#{testball_repo}", using: :git end - %w[AUTHOR COMMITTER].each do |role| - ENV["GIT_#{role}_NAME"] = "brew tests" - ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" - ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" - end - - testball_repo.cd do - FileUtils.touch "LICENSE" - shutup do - system "git", "init" - system "git", "add", "--all" - system "git", "commit", "-m", "Initial commit" + using_git_env do + testball_repo.cd do + FileUtils.touch "LICENSE" + shutup do + system "git", "init" + system "git", "add", "--all" + system "git", "commit", "-m", "Initial commit" + end end end @@ -1144,7 +1131,6 @@ class OutdatedVersionsTests < Homebrew::TestCase reset_outdated_kegs assert_predicate f.outdated_kegs(fetch_head: true), :empty? ensure - ENV.replace(initial_env) testball_repo.rmtree if testball_repo.exist? end diff --git a/Library/Homebrew/test/install_test.rb b/Library/Homebrew/test/install_test.rb index fa0ef5ffee..2ca5bf4ca1 100644 --- a/Library/Homebrew/test/install_test.rb +++ b/Library/Homebrew/test/install_test.rb @@ -73,24 +73,19 @@ class IntegrationCommandTestInstall < IntegrationCommandTestCase end def test_install_head_installed - initial_env = ENV.to_hash - %w[AUTHOR COMMITTER].each do |role| - ENV["GIT_#{role}_NAME"] = "brew tests" - ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" - ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" - end - repo_path = HOMEBREW_CACHE.join("repo") repo_path.join("bin").mkpath - repo_path.cd do - shutup do - system "git", "init" - system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" - FileUtils.touch "bin/something.bin" - FileUtils.touch "README" - system "git", "add", "--all" - system "git", "commit", "-m", "Initial repo commit" + using_git_env do + repo_path.cd do + shutup do + system "git", "init" + system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" + FileUtils.touch "bin/something.bin" + FileUtils.touch "README" + system "git", "add", "--all" + system "git", "commit", "-m", "Initial repo commit" + end end end @@ -110,9 +105,6 @@ class IntegrationCommandTestInstall < IntegrationCommandTestCase cmd("install", "testball1", "--HEAD", "--ignore-dependencies") assert_match "#{HOMEBREW_CELLAR}/testball1/HEAD-2ccdf4f", cmd("unlink", "testball1") assert_match "#{HOMEBREW_CELLAR}/testball1/1.0", cmd("install", "testball1") - - ensure - ENV.replace(initial_env) end def test_install_with_invalid_option diff --git a/Library/Homebrew/test/support/helper/env.rb b/Library/Homebrew/test/support/helper/env.rb index 904a1d4c71..6c69b335d5 100644 --- a/Library/Homebrew/test/support/helper/env.rb +++ b/Library/Homebrew/test/support/helper/env.rb @@ -1,14 +1,32 @@ module Test module Helper module Env + def copy_env + ENV.to_hash + end + + def restore_env(env) + ENV.replace(env) + end + def with_environment(partial_env) - old = ENV.to_hash + old = copy_env ENV.update partial_env - begin - yield - ensure - ENV.replace old + yield + ensure + restore_env old + end + + def using_git_env + initial_env = copy_env + %w[AUTHOR COMMITTER].each do |role| + ENV["GIT_#{role}_NAME"] = "brew tests" + ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" + ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" end + yield + ensure + restore_env initial_env end end end diff --git a/Library/Homebrew/test/tap_test.rb b/Library/Homebrew/test/tap_test.rb index 2ab003fd27..6656017f3b 100644 --- a/Library/Homebrew/test/tap_test.rb +++ b/Library/Homebrew/test/tap_test.rb @@ -66,23 +66,16 @@ class TapTest < Homebrew::TestCase end def setup_git_repo - env = ENV.to_hash - %w[AUTHOR COMMITTER].each do |role| - ENV["GIT_#{role}_NAME"] = "brew tests" - ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" - ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" - end - - @path.cd do - shutup do - system "git", "init" - system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" - system "git", "add", "--all" - system "git", "commit", "-m", "init" + using_git_env do + @path.cd do + shutup do + system "git", "init" + system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" + system "git", "add", "--all" + system "git", "commit", "-m", "init" + end end end - ensure - ENV.replace(env) end def test_fetch From 116ed3ec80b6d1b9d6178f25793cfcaaaa161308 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sat, 21 Jan 2017 14:57:44 +0000 Subject: [PATCH 02/12] tests: automatically restore ENV in teardown --- Library/Homebrew/test/audit_test.rb | 3 --- Library/Homebrew/test/commands_test.rb | 4 ---- Library/Homebrew/test/diagnostic_test.rb | 6 ------ Library/Homebrew/test/os/mac/diagnostic_test.rb | 6 ------ Library/Homebrew/test/shell_test.rb | 3 --- Library/Homebrew/test/support/helper/test_case.rb | 3 +++ Library/Homebrew/test/utils_test.rb | 6 ------ 7 files changed, 3 insertions(+), 28 deletions(-) diff --git a/Library/Homebrew/test/audit_test.rb b/Library/Homebrew/test/audit_test.rb index f8d1374822..75a882d693 100644 --- a/Library/Homebrew/test/audit_test.rb +++ b/Library/Homebrew/test/audit_test.rb @@ -361,13 +361,10 @@ class FormulaAuditorTests < Homebrew::TestCase end EOS - original_value = ENV["HOMEBREW_NO_GITHUB_API"] ENV["HOMEBREW_NO_GITHUB_API"] = "1" fa.audit_github_repository assert_equal [], fa.problems - ensure - ENV["HOMEBREW_NO_GITHUB_API"] = original_value end def test_audit_caveats diff --git a/Library/Homebrew/test/commands_test.rb b/Library/Homebrew/test/commands_test.rb index 3d7c16e58f..5f5dc95865 100644 --- a/Library/Homebrew/test/commands_test.rb +++ b/Library/Homebrew/test/commands_test.rb @@ -47,8 +47,6 @@ class CommandsTests < Homebrew::TestCase end def test_external_commands - env = ENV.to_hash - mktmpdir do |dir| %w[brew-t1 brew-t2.rb brew-t3.py].each do |file| path = "#{dir}/#{file}" @@ -67,8 +65,6 @@ class CommandsTests < Homebrew::TestCase "Executable files with a non Ruby extension shoudn't be included" refute cmds.include?("t4"), "Non-executable files shouldn't be included" end - ensure - ENV.replace(env) end def test_internal_command_path diff --git a/Library/Homebrew/test/diagnostic_test.rb b/Library/Homebrew/test/diagnostic_test.rb index 2d12868273..7a1fb25f79 100644 --- a/Library/Homebrew/test/diagnostic_test.rb +++ b/Library/Homebrew/test/diagnostic_test.rb @@ -6,15 +6,9 @@ require "diagnostic" class DiagnosticChecksTest < Homebrew::TestCase def setup super - @env = ENV.to_hash @checks = Homebrew::Diagnostic::Checks.new end - def teardown - ENV.replace(@env) - super - end - def test_inject_file_list assert_equal "foo:\n", @checks.inject_file_list([], "foo:\n") diff --git a/Library/Homebrew/test/os/mac/diagnostic_test.rb b/Library/Homebrew/test/os/mac/diagnostic_test.rb index 284d293ca9..5f467e87be 100644 --- a/Library/Homebrew/test/os/mac/diagnostic_test.rb +++ b/Library/Homebrew/test/os/mac/diagnostic_test.rb @@ -6,15 +6,9 @@ require "diagnostic" class OSMacDiagnosticChecksTest < Homebrew::TestCase def setup super - @env = ENV.to_hash @checks = Homebrew::Diagnostic::Checks.new end - def teardown - ENV.replace(@env) - super - end - def test_check_for_other_package_managers MacOS.stubs(:macports_or_fink).returns ["fink"] assert_match "You have MacPorts or Fink installed:", diff --git a/Library/Homebrew/test/shell_test.rb b/Library/Homebrew/test/shell_test.rb index 877acb5c8b..a32d098638 100644 --- a/Library/Homebrew/test/shell_test.rb +++ b/Library/Homebrew/test/shell_test.rb @@ -37,7 +37,6 @@ class ShellSmokeTest < Homebrew::TestCase end def prepend_path_shell(shell, path, fragment) - original_shell = ENV["SHELL"] ENV["SHELL"] = shell prepend_message = Utils::Shell.prepend_path_in_shell_profile(path) @@ -45,8 +44,6 @@ class ShellSmokeTest < Homebrew::TestCase prepend_message.start_with?(fragment), "#{shell}: expected #{prepend_message} to match #{fragment}" ) - - ENV["SHELL"] = original_shell end def test_prepend_path_in_shell_profile diff --git a/Library/Homebrew/test/support/helper/test_case.rb b/Library/Homebrew/test/support/helper/test_case.rb index 53d4e70317..c4c7f87275 100644 --- a/Library/Homebrew/test/support/helper/test_case.rb +++ b/Library/Homebrew/test/support/helper/test_case.rb @@ -16,11 +16,14 @@ module Homebrew def setup super + @__argv = ARGV.dup + @__env = copy_env # Call #to_hash to duplicate ENV end def teardown ARGV.replace(@__argv) + restore_env @__env Tab.clear_cache diff --git a/Library/Homebrew/test/utils_test.rb b/Library/Homebrew/test/utils_test.rb index 7099e332c2..7ff4e192ac 100644 --- a/Library/Homebrew/test/utils_test.rb +++ b/Library/Homebrew/test/utils_test.rb @@ -7,12 +7,6 @@ class UtilTests < Homebrew::TestCase def setup super @dir = Pathname.new(mktmpdir) - @env = ENV.to_hash - end - - def teardown - ENV.replace @env - super end def test_ofail From 0c1d665568875ffccb660a65ff248b7ab98ec77c Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sat, 21 Jan 2017 15:11:49 +0000 Subject: [PATCH 03/12] tests: set developer in ENV rather than stubbing --- Library/Homebrew/test/audit_test.rb | 6 +++--- Library/Homebrew/test/os/mac/diagnostic_test.rb | 2 +- Library/Homebrew/test/utils_test.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/test/audit_test.rb b/Library/Homebrew/test/audit_test.rb index 75a882d693..60cf276106 100644 --- a/Library/Homebrew/test/audit_test.rb +++ b/Library/Homebrew/test/audit_test.rb @@ -243,7 +243,7 @@ class FormulaAuditorTests < Homebrew::TestCase needs_compat require "compat/formula_specialties" - ARGV.stubs(:homebrew_developer?).returns false + ENV.delete("HOMEBREW_DEVELOPER") fa = shutup do formula_auditor "foo", <<-EOS.undent class Foo < GithubGistFormula @@ -260,7 +260,7 @@ class FormulaAuditorTests < Homebrew::TestCase needs_compat require "compat/formula_specialties" - ARGV.stubs(:homebrew_developer?).returns false + ENV.delete("HOMEBREW_DEVELOPER") fa = formula_auditor "foo", <<-EOS.undent class Foo < ScriptFileFormula url "http://example.com/foo-1.0.tgz" @@ -275,7 +275,7 @@ class FormulaAuditorTests < Homebrew::TestCase needs_compat require "compat/formula_specialties" - ARGV.stubs(:homebrew_developer?).returns false + ENV.delete("HOMEBREW_DEVELOPER") fa = formula_auditor "foo", <<-EOS.undent class Foo < AmazonWebServicesFormula url "http://example.com/foo-1.0.tgz" diff --git a/Library/Homebrew/test/os/mac/diagnostic_test.rb b/Library/Homebrew/test/os/mac/diagnostic_test.rb index 5f467e87be..704235b01c 100644 --- a/Library/Homebrew/test/os/mac/diagnostic_test.rb +++ b/Library/Homebrew/test/os/mac/diagnostic_test.rb @@ -16,7 +16,7 @@ class OSMacDiagnosticChecksTest < Homebrew::TestCase end def test_check_for_unsupported_macos - ARGV.stubs(:homebrew_developer?).returns false + ENV.delete("HOMEBREW_DEVELOPER") OS::Mac.stubs(:prerelease?).returns true assert_match "We do not provide support for this pre-release version.", @checks.check_for_unsupported_macos diff --git a/Library/Homebrew/test/utils_test.rb b/Library/Homebrew/test/utils_test.rb index 7ff4e192ac..520cd4fcdd 100644 --- a/Library/Homebrew/test/utils_test.rb +++ b/Library/Homebrew/test/utils_test.rb @@ -207,7 +207,7 @@ class UtilTests < Homebrew::TestCase end def test_odeprecated - ARGV.stubs(:homebrew_developer?).returns false + ENV.delete("HOMEBREW_DEVELOPER") e = assert_raises(MethodDeprecatedError) do odeprecated("method", "replacement", caller: ["#{HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-core/"], From a736c7e3173fd7a9727026b5b968ac5f067a906d Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sat, 21 Jan 2017 15:16:32 +0000 Subject: [PATCH 04/12] tests: set verbose in ENV instead of stubbing ARGV --- Library/Homebrew/test/sandbox_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/test/sandbox_test.rb b/Library/Homebrew/test/sandbox_test.rb index 2e97b5d6a2..21a3ca8c40 100644 --- a/Library/Homebrew/test/sandbox_test.rb +++ b/Library/Homebrew/test/sandbox_test.rb @@ -47,7 +47,7 @@ class SandboxTest < Homebrew::TestCase def test_complains_on_failure Utils.expects(popen_read: "foo") - ARGV.stubs(verbose?: true) + ENV["HOMEBREW_VERBOSE"] = "1" out, _err = capture_io do assert_raises(ErrorDuringExecution) { @sandbox.exec "false" } end @@ -61,7 +61,7 @@ class SandboxTest < Homebrew::TestCase bar EOS Utils.expects(popen_read: with_bogus_error) - ARGV.stubs(verbose?: true) + ENV["HOMEBREW_VERBOSE"] = "1" out, _err = capture_io do assert_raises(ErrorDuringExecution) { @sandbox.exec "false" } end From 1e1de8c111b5c3a36e4bdf886d6f2e736890ba56 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sat, 21 Jan 2017 15:46:28 +0000 Subject: [PATCH 05/12] tests: remove remainder of ARGV stubs I was waiting for confirmation that there wasn't a good reason for these to be stubbed before I removed them. --- Library/Homebrew/test/sandbox_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/test/sandbox_test.rb b/Library/Homebrew/test/sandbox_test.rb index 21a3ca8c40..a633defce2 100644 --- a/Library/Homebrew/test/sandbox_test.rb +++ b/Library/Homebrew/test/sandbox_test.rb @@ -15,11 +15,11 @@ class SandboxTest < Homebrew::TestCase f2 = formula { url "bar-1.0" } f2.stubs(:tap).returns(Tap.fetch("test/tap")) - ARGV.stubs(:sandbox?).returns true + ENV["HOMEBREW_SANDBOX"] = "1" assert Sandbox.formula?(f), "Formulae should be sandboxed if --sandbox was passed." - ARGV.stubs(:sandbox?).returns false + ENV.delete("HOMEBREW_SANDBOX") assert Sandbox.formula?(f), "Formulae should be sandboxed if in a sandboxed tap." refute Sandbox.formula?(f2), @@ -27,7 +27,7 @@ class SandboxTest < Homebrew::TestCase end def test_test? - ARGV.stubs(:no_sandbox?).returns false + ENV.delete("HOMEBREW_NO_SANDBOX") assert Sandbox.test?, "Tests should be sandboxed unless --no-sandbox was passed." end From cbc9e5dd3b0b47a98e3976ae1683641dbe94b57d Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 22 Jan 2017 11:10:06 +0000 Subject: [PATCH 06/12] tests: re-use with_environment in using_git_env Proposed by @reitermarkus in https://github.com/Homebrew/brew/pull/1890#discussion_r97210285. I made one slight adjustment of preserving the previous date string in case anything was relying on it. --- Library/Homebrew/test/support/helper/env.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/test/support/helper/env.rb b/Library/Homebrew/test/support/helper/env.rb index 6c69b335d5..7e6429e743 100644 --- a/Library/Homebrew/test/support/helper/env.rb +++ b/Library/Homebrew/test/support/helper/env.rb @@ -18,15 +18,15 @@ module Test end def using_git_env - initial_env = copy_env - %w[AUTHOR COMMITTER].each do |role| - ENV["GIT_#{role}_NAME"] = "brew tests" - ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" - ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" + git_env = ["AUTHOR", "COMMITTER"].each_with_object({}) do |role, env| + env["GIT_#{role}_NAME"] = "brew tests" + env["GIT_#{role}_EMAIL"] = "brew-tests@localhost" + env["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" + end + + with_environment(git_env) do + yield end - yield - ensure - restore_env initial_env end end end From 9c3d01c682056367f59d557720c7a08ca67f48c4 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sun, 22 Jan 2017 19:59:13 +0000 Subject: [PATCH 07/12] tests: remove unnecessary comment See https://github.com/Homebrew/brew/pull/1890#discussion_r97229473. --- Library/Homebrew/test/support/helper/test_case.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/test/support/helper/test_case.rb b/Library/Homebrew/test/support/helper/test_case.rb index c4c7f87275..801fa08c77 100644 --- a/Library/Homebrew/test/support/helper/test_case.rb +++ b/Library/Homebrew/test/support/helper/test_case.rb @@ -18,7 +18,7 @@ module Homebrew super @__argv = ARGV.dup - @__env = copy_env # Call #to_hash to duplicate ENV + @__env = copy_env end def teardown From b53ce62ffb193748266c63d7b13fd023c0764ff0 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sun, 22 Jan 2017 20:45:15 +0000 Subject: [PATCH 08/12] tests: use more recent git committer date This will allow the `brew bundle` test to use the default git environment. --- Library/Homebrew/test/download_strategies_test.rb | 6 +++--- Library/Homebrew/test/formula_test.rb | 2 +- Library/Homebrew/test/install_test.rb | 6 +++--- Library/Homebrew/test/support/helper/env.rb | 2 +- Library/Homebrew/test/tap_test.rb | 8 ++++---- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Library/Homebrew/test/download_strategies_test.rb b/Library/Homebrew/test/download_strategies_test.rb index 0a2068bd98..3c4272c749 100644 --- a/Library/Homebrew/test/download_strategies_test.rb +++ b/Library/Homebrew/test/download_strategies_test.rb @@ -180,7 +180,7 @@ class GitDownloadStrategyTests < Homebrew::TestCase def test_source_modified_time setup_git_repo - assert_equal 1_242_860_651, @strategy.source_modified_time.to_i + assert_equal 1_485_115_153, @strategy.source_modified_time.to_i end def test_last_commit @@ -191,7 +191,7 @@ class GitDownloadStrategyTests < Homebrew::TestCase git_commit_all end end - assert_equal "c50c79b", @strategy.last_commit + assert_equal "f68266e", @strategy.last_commit end def test_fetch_last_commit @@ -216,7 +216,7 @@ class GitDownloadStrategyTests < Homebrew::TestCase end @strategy.shutup! - assert_equal "c50c79b", @strategy.fetch_last_commit + assert_equal "f68266e", @strategy.fetch_last_commit ensure remote_repo.rmtree if remote_repo.directory? end diff --git a/Library/Homebrew/test/formula_test.rb b/Library/Homebrew/test/formula_test.rb index 74db4ad6c2..b061113708 100644 --- a/Library/Homebrew/test/formula_test.rb +++ b/Library/Homebrew/test/formula_test.rb @@ -1090,7 +1090,7 @@ class OutdatedVersionsTests < Homebrew::TestCase outdated_stable_prefix = HOMEBREW_CELLAR.join("testball/1.0") head_prefix_a = HOMEBREW_CELLAR.join("testball/HEAD") head_prefix_b = HOMEBREW_CELLAR.join("testball/HEAD-aaaaaaa_1") - head_prefix_c = HOMEBREW_CELLAR.join("testball/HEAD-5658946") + head_prefix_c = HOMEBREW_CELLAR.join("testball/HEAD-18a7103") setup_tab_for_prefix(outdated_stable_prefix) tab_a = setup_tab_for_prefix(head_prefix_a, versions: { "stable" => "1.0" }) diff --git a/Library/Homebrew/test/install_test.rb b/Library/Homebrew/test/install_test.rb index 2ca5bf4ca1..3f3df6099b 100644 --- a/Library/Homebrew/test/install_test.rb +++ b/Library/Homebrew/test/install_test.rb @@ -100,10 +100,10 @@ class IntegrationCommandTestInstall < IntegrationCommandTestCase # Ignore dependencies, because we'll try to resolve requirements in build.rb # and there will be the git requirement, but we cannot instantiate git # formula since we only have testball1 formula. - assert_match "#{HOMEBREW_CELLAR}/testball1/HEAD-2ccdf4f", cmd("install", "testball1", "--HEAD", "--ignore-dependencies") - assert_match "testball1-HEAD-2ccdf4f already installed", + assert_match "#{HOMEBREW_CELLAR}/testball1/HEAD-d5eb689", cmd("install", "testball1", "--HEAD", "--ignore-dependencies") + assert_match "testball1-HEAD-d5eb689 already installed", cmd("install", "testball1", "--HEAD", "--ignore-dependencies") - assert_match "#{HOMEBREW_CELLAR}/testball1/HEAD-2ccdf4f", cmd("unlink", "testball1") + assert_match "#{HOMEBREW_CELLAR}/testball1/HEAD-d5eb689", cmd("unlink", "testball1") assert_match "#{HOMEBREW_CELLAR}/testball1/1.0", cmd("install", "testball1") end diff --git a/Library/Homebrew/test/support/helper/env.rb b/Library/Homebrew/test/support/helper/env.rb index 7e6429e743..7958a041ef 100644 --- a/Library/Homebrew/test/support/helper/env.rb +++ b/Library/Homebrew/test/support/helper/env.rb @@ -21,7 +21,7 @@ module Test git_env = ["AUTHOR", "COMMITTER"].each_with_object({}) do |role, env| env["GIT_#{role}_NAME"] = "brew tests" env["GIT_#{role}_EMAIL"] = "brew-tests@localhost" - env["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" + env["GIT_#{role}_DATE"] = "Sun Jan 22 19:59:13 2017 +0000" end with_environment(git_env) do diff --git a/Library/Homebrew/test/tap_test.rb b/Library/Homebrew/test/tap_test.rb index 6656017f3b..a268ab0ae9 100644 --- a/Library/Homebrew/test/tap_test.rb +++ b/Library/Homebrew/test/tap_test.rb @@ -177,10 +177,10 @@ class TapTest < Homebrew::TestCase touch @path/"README" setup_git_repo - assert_equal "e1893a6bd191ba895c71b652ff8376a6114c7fa7", @tap.git_head - assert_equal "e189", @tap.git_short_head - assert_match "years ago", @tap.git_last_commit - assert_equal "2009-05-21", @tap.git_last_commit_date + assert_equal "0453e16c8e3fac73104da50927a86221ca0740c2", @tap.git_head + assert_equal "0453", @tap.git_short_head + assert_match "ago", @tap.git_last_commit + assert_equal "2017-01-22", @tap.git_last_commit_date end def test_private_remote From 19e61355b38b8ba96db0ca71849bb536af0490bf Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sun, 22 Jan 2017 20:54:37 +0000 Subject: [PATCH 09/12] tests: remove with_git_env method A common git environment is now used in all tests, so this is no longer required. --- Library/Homebrew/dev-cmd/tests.rb | 1 + .../Homebrew/test/download_strategies_test.rb | 40 ++++++++----------- Library/Homebrew/test/formula_test.rb | 28 ++++++------- Library/Homebrew/test/install_test.rb | 18 ++++----- Library/Homebrew/test/support/helper/env.rb | 12 ------ Library/Homebrew/test/tap_test.rb | 14 +++---- 6 files changed, 44 insertions(+), 69 deletions(-) diff --git a/Library/Homebrew/dev-cmd/tests.rb b/Library/Homebrew/dev-cmd/tests.rb index b4f3c2d40d..05bdda8d2e 100644 --- a/Library/Homebrew/dev-cmd/tests.rb +++ b/Library/Homebrew/dev-cmd/tests.rb @@ -34,6 +34,7 @@ module Homebrew %w[AUTHOR COMMITTER].each do |role| ENV["GIT_#{role}_NAME"] = "brew tests" ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" + ENV["GIT_#{role}_DATE"] = "Sun Jan 22 19:59:13 2017 +0000" end Homebrew.install_gem_setup_path! "bundler" diff --git a/Library/Homebrew/test/download_strategies_test.rb b/Library/Homebrew/test/download_strategies_test.rb index 3c4272c749..40236b420a 100644 --- a/Library/Homebrew/test/download_strategies_test.rb +++ b/Library/Homebrew/test/download_strategies_test.rb @@ -158,15 +158,13 @@ class GitDownloadStrategyTests < Homebrew::TestCase end def setup_git_repo - using_git_env do - @cached_location.cd do - shutup do - system "git", "init" - system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" - end - touch "README" - git_commit_all + @cached_location.cd do + shutup do + system "git", "init" + system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" end + touch "README" + git_commit_all end end @@ -185,11 +183,9 @@ class GitDownloadStrategyTests < Homebrew::TestCase def test_last_commit setup_git_repo - using_git_env do - @cached_location.cd do - touch "LICENSE" - git_commit_all - end + @cached_location.cd do + touch "LICENSE" + git_commit_all end assert_equal "f68266e", @strategy.last_commit end @@ -202,17 +198,15 @@ class GitDownloadStrategyTests < Homebrew::TestCase resource.instance_variable_set(:@version, Version.create("HEAD")) @strategy = GitDownloadStrategy.new("baz", resource) - using_git_env do - remote_repo.cd do - shutup do - system "git", "init" - system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" - end - touch "README" - git_commit_all - touch "LICENSE" - git_commit_all + remote_repo.cd do + shutup do + system "git", "init" + system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" end + touch "README" + git_commit_all + touch "LICENSE" + git_commit_all end @strategy.shutup! diff --git a/Library/Homebrew/test/formula_test.rb b/Library/Homebrew/test/formula_test.rb index b061113708..ecdd1847b9 100644 --- a/Library/Homebrew/test/formula_test.rb +++ b/Library/Homebrew/test/formula_test.rb @@ -533,14 +533,12 @@ class FormulaTests < Homebrew::TestCase cached_location = f.head.downloader.cached_location cached_location.mkpath - using_git_env do - cached_location.cd do - FileUtils.touch "LICENSE" - shutup do - system "git", "init" - system "git", "add", "--all" - system "git", "commit", "-m", "Initial commit" - end + cached_location.cd do + FileUtils.touch "LICENSE" + shutup do + system "git", "init" + system "git", "add", "--all" + system "git", "commit", "-m", "Initial commit" end end @@ -1105,14 +1103,12 @@ class OutdatedVersionsTests < Homebrew::TestCase head "file://#{testball_repo}", using: :git end - using_git_env do - testball_repo.cd do - FileUtils.touch "LICENSE" - shutup do - system "git", "init" - system "git", "add", "--all" - system "git", "commit", "-m", "Initial commit" - end + testball_repo.cd do + FileUtils.touch "LICENSE" + shutup do + system "git", "init" + system "git", "add", "--all" + system "git", "commit", "-m", "Initial commit" end end diff --git a/Library/Homebrew/test/install_test.rb b/Library/Homebrew/test/install_test.rb index 3f3df6099b..da6c1863f5 100644 --- a/Library/Homebrew/test/install_test.rb +++ b/Library/Homebrew/test/install_test.rb @@ -76,16 +76,14 @@ class IntegrationCommandTestInstall < IntegrationCommandTestCase repo_path = HOMEBREW_CACHE.join("repo") repo_path.join("bin").mkpath - using_git_env do - repo_path.cd do - shutup do - system "git", "init" - system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" - FileUtils.touch "bin/something.bin" - FileUtils.touch "README" - system "git", "add", "--all" - system "git", "commit", "-m", "Initial repo commit" - end + repo_path.cd do + shutup do + system "git", "init" + system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" + FileUtils.touch "bin/something.bin" + FileUtils.touch "README" + system "git", "add", "--all" + system "git", "commit", "-m", "Initial repo commit" end end diff --git a/Library/Homebrew/test/support/helper/env.rb b/Library/Homebrew/test/support/helper/env.rb index 7958a041ef..88b25e237e 100644 --- a/Library/Homebrew/test/support/helper/env.rb +++ b/Library/Homebrew/test/support/helper/env.rb @@ -16,18 +16,6 @@ module Test ensure restore_env old end - - def using_git_env - git_env = ["AUTHOR", "COMMITTER"].each_with_object({}) do |role, env| - env["GIT_#{role}_NAME"] = "brew tests" - env["GIT_#{role}_EMAIL"] = "brew-tests@localhost" - env["GIT_#{role}_DATE"] = "Sun Jan 22 19:59:13 2017 +0000" - end - - with_environment(git_env) do - yield - end - end end end end diff --git a/Library/Homebrew/test/tap_test.rb b/Library/Homebrew/test/tap_test.rb index a268ab0ae9..9979be43d4 100644 --- a/Library/Homebrew/test/tap_test.rb +++ b/Library/Homebrew/test/tap_test.rb @@ -66,14 +66,12 @@ class TapTest < Homebrew::TestCase end def setup_git_repo - using_git_env do - @path.cd do - shutup do - system "git", "init" - system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" - system "git", "add", "--all" - system "git", "commit", "-m", "init" - end + @path.cd do + shutup do + system "git", "init" + system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" + system "git", "add", "--all" + system "git", "commit", "-m", "init" end end end From 7d131de57dac25dbb7cf9a8f986c291de87f7e65 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 23 Jan 2017 12:01:47 +0000 Subject: [PATCH 10/12] tests: slightly stricter match in tab test Suggested by @reitermarkus in https://github.com/Homebrew/brew/pull/1890#discussion_r97234536, and then I added the \A and \Z. --- Library/Homebrew/test/tap_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/test/tap_test.rb b/Library/Homebrew/test/tap_test.rb index 9979be43d4..578326cea8 100644 --- a/Library/Homebrew/test/tap_test.rb +++ b/Library/Homebrew/test/tap_test.rb @@ -177,7 +177,7 @@ class TapTest < Homebrew::TestCase assert_equal "0453e16c8e3fac73104da50927a86221ca0740c2", @tap.git_head assert_equal "0453", @tap.git_short_head - assert_match "ago", @tap.git_last_commit + assert_match(/\A\d+ .+ ago\Z/, @tap.git_last_commit) assert_equal "2017-01-22", @tap.git_last_commit_date end From b7dc56a8aa08e5783072ac728ed08ca597262835 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 22 Jan 2017 23:17:09 +0100 Subject: [PATCH 11/12] Delete with_environment from cask tests. --- Library/Homebrew/cask/spec/cask/cli_spec.rb | 16 ++++++++-------- Library/Homebrew/cask/test/cask/dsl_test.rb | 6 +++++- Library/Homebrew/cask/test/test_helper.rb | 2 -- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Library/Homebrew/cask/spec/cask/cli_spec.rb b/Library/Homebrew/cask/spec/cask/cli_spec.rb index 9964275f10..6b2313a41c 100644 --- a/Library/Homebrew/cask/spec/cask/cli_spec.rb +++ b/Library/Homebrew/cask/spec/cask/cli_spec.rb @@ -39,17 +39,17 @@ describe Hbc::CLI do end it "respects the env variable when choosing what appdir to create" do - with_environment "HOMEBREW_CASK_OPTS" => "--appdir=/custom/appdir" do - expect(Hbc).to receive(:appdir=).with(Pathname("/custom/appdir")) - described_class.process("noop") - end + allow(ENV).to receive(:[]) + allow(ENV).to receive(:[]).with("HOMEBREW_CASK_OPTS").and_return("--appdir=/custom/appdir") + expect(Hbc).to receive(:appdir=).with(Pathname.new("/custom/appdir")) + described_class.process("noop") end it "respects the env variable when choosing a non-default Caskroom location" do - with_environment "HOMEBREW_CASK_OPTS" => "--caskroom=/custom/caskdir" do - expect(Hbc).to receive(:caskroom=).with(Pathname("/custom/caskdir")) - described_class.process("noop") - end + allow(ENV).to receive(:[]) + allow(ENV).to receive(:[]).with("HOMEBREW_CASK_OPTS").and_return("--caskroom=/custom/caskdir") + expect(Hbc).to receive(:caskroom=).with(Pathname.new("/custom/caskdir")) + described_class.process("noop") end it "exits with a status of 1 when something goes wrong" do diff --git a/Library/Homebrew/cask/test/cask/dsl_test.rb b/Library/Homebrew/cask/test/cask/dsl_test.rb index 96d24a1a14..0ea928f40e 100644 --- a/Library/Homebrew/cask/test/cask/dsl_test.rb +++ b/Library/Homebrew/cask/test/cask/dsl_test.rb @@ -69,7 +69,11 @@ describe Hbc::DSL do end it "may use deprecated DSL version hash syntax" do - with_environment "HOMEBREW_DEVELOPER" => nil do + stub = proc do |arg| + arg == "HOMEBREW_DEVELOPER" ? nil : ENV[arg] + end + + ENV.stub :[], stub do shutup do test_cask = Hbc.load("with-dsl-version") test_cask.token.must_equal "with-dsl-version" diff --git a/Library/Homebrew/cask/test/test_helper.rb b/Library/Homebrew/cask/test/test_helper.rb index 275ede3048..7315839f58 100644 --- a/Library/Homebrew/cask/test/test_helper.rb +++ b/Library/Homebrew/cask/test/test_helper.rb @@ -13,9 +13,7 @@ require "global" # add Homebrew-Cask to load path $LOAD_PATH.push(HOMEBREW_LIBRARY_PATH.join("cask", "lib").to_s) -require "test/support/helper/env" require "test/support/helper/shutup" -include Test::Helper::Env include Test::Helper::Shutup def sudo(*args) From 62a0c3a6f5cd6577cf9e628524b88b91b9419df7 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Mon, 23 Jan 2017 12:26:34 +0000 Subject: [PATCH 12/12] tests: remove env helper --- Library/Homebrew/cask/spec/spec_helper.rb | 2 -- .../Homebrew/test/gpg2_requirement_test.rb | 5 ++--- Library/Homebrew/test/gpg_test.rb | 7 +++---- Library/Homebrew/test/support/helper/env.rb | 21 ------------------- .../Homebrew/test/support/helper/test_case.rb | 6 ++---- 5 files changed, 7 insertions(+), 34 deletions(-) delete mode 100644 Library/Homebrew/test/support/helper/env.rb diff --git a/Library/Homebrew/cask/spec/spec_helper.rb b/Library/Homebrew/cask/spec/spec_helper.rb index 458fe00f4f..162cb3b8f3 100644 --- a/Library/Homebrew/cask/spec/spec_helper.rb +++ b/Library/Homebrew/cask/spec/spec_helper.rb @@ -15,7 +15,6 @@ require "global" # add Homebrew-Cask to load path $LOAD_PATH.push(HOMEBREW_LIBRARY_PATH.join("cask", "lib").to_s) -require "test/support/helper/env" require "test/support/helper/shutup" Pathname.glob(HOMEBREW_LIBRARY_PATH.join("cask", "spec", "support", "*.rb")).each(&method(:require)) @@ -38,6 +37,5 @@ end RSpec.configure do |config| config.order = :random - config.include(Test::Helper::Env) config.include(Test::Helper::Shutup) end diff --git a/Library/Homebrew/test/gpg2_requirement_test.rb b/Library/Homebrew/test/gpg2_requirement_test.rb index b45798b420..3297c28512 100644 --- a/Library/Homebrew/test/gpg2_requirement_test.rb +++ b/Library/Homebrew/test/gpg2_requirement_test.rb @@ -19,8 +19,7 @@ class GPG2RequirementTests < Homebrew::TestCase end def test_satisfied - with_environment("PATH" => @dir/"bin") do - assert_predicate GPG2Requirement.new, :satisfied? - end + ENV["PATH"] = @dir/"bin" + assert_predicate GPG2Requirement.new, :satisfied? end end diff --git a/Library/Homebrew/test/gpg_test.rb b/Library/Homebrew/test/gpg_test.rb index d3a6372971..ea43725491 100644 --- a/Library/Homebrew/test/gpg_test.rb +++ b/Library/Homebrew/test/gpg_test.rb @@ -10,10 +10,9 @@ class GpgTest < Homebrew::TestCase def test_create_test_key Dir.chdir(@dir) do - with_environment("HOME" => @dir) do - shutup { Gpg.create_test_key(@dir) } - assert_predicate @dir/".gnupg/secring.gpg", :exist? - end + ENV["HOME"] = @dir + shutup { Gpg.create_test_key(@dir) } + assert_predicate @dir/".gnupg/secring.gpg", :exist? end end end diff --git a/Library/Homebrew/test/support/helper/env.rb b/Library/Homebrew/test/support/helper/env.rb deleted file mode 100644 index 88b25e237e..0000000000 --- a/Library/Homebrew/test/support/helper/env.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Test - module Helper - module Env - def copy_env - ENV.to_hash - end - - def restore_env(env) - ENV.replace(env) - end - - def with_environment(partial_env) - old = copy_env - ENV.update partial_env - yield - ensure - restore_env old - end - end - end -end diff --git a/Library/Homebrew/test/support/helper/test_case.rb b/Library/Homebrew/test/support/helper/test_case.rb index 801fa08c77..ab97ef7589 100644 --- a/Library/Homebrew/test/support/helper/test_case.rb +++ b/Library/Homebrew/test/support/helper/test_case.rb @@ -1,11 +1,9 @@ module Homebrew class TestCase < ::Minitest::Test - require "test/support/helper/env" require "test/support/helper/fs_leak_logger" require "test/support/helper/lifecycle_enforcer" require "test/support/helper/shutup" require "test/support/helper/version_assertions" - include Test::Helper::Env include Test::Helper::FSLeakLogger include Test::Helper::LifecycleEnforcer include Test::Helper::Shutup @@ -18,12 +16,12 @@ module Homebrew super @__argv = ARGV.dup - @__env = copy_env + @__env = ENV.to_hash # dup doesn't work on ENV end def teardown ARGV.replace(@__argv) - restore_env @__env + ENV.replace(@__env) Tab.clear_cache