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