tests: extract a common using_git_env method

This commit is contained in:
Alyssa Ross 2017-01-21 14:56:58 +00:00
parent 6f305ad3dc
commit 6e0f1366b0
5 changed files with 57 additions and 80 deletions

View File

@ -157,18 +157,6 @@ class GitDownloadStrategyTests < Homebrew::TestCase
end end
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 def setup_git_repo
using_git_env do using_git_env do
@cached_location.cd do @cached_location.cd do

View File

@ -526,8 +526,6 @@ class FormulaTests < Homebrew::TestCase
end end
def test_update_head_version def test_update_head_version
initial_env = ENV.to_hash
f = formula do f = formula do
head "foo", using: :git head "foo", using: :git
end end
@ -535,25 +533,19 @@ class FormulaTests < Homebrew::TestCase
cached_location = f.head.downloader.cached_location cached_location = f.head.downloader.cached_location
cached_location.mkpath cached_location.mkpath
%w[AUTHOR COMMITTER].each do |role| using_git_env do
ENV["GIT_#{role}_NAME"] = "brew tests" cached_location.cd do
ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" FileUtils.touch "LICENSE"
ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" shutup do
end system "git", "init"
system "git", "add", "--all"
cached_location.cd do system "git", "commit", "-m", "Initial commit"
FileUtils.touch "LICENSE" end
shutup do
system "git", "init"
system "git", "add", "--all"
system "git", "commit", "-m", "Initial commit"
end end
end end
f.update_head_version f.update_head_version
assert_equal Version.create("HEAD-5658946"), f.head.version assert_equal Version.create("HEAD-5658946"), f.head.version
ensure
ENV.replace(initial_env)
end end
def test_legacy_options 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" }) tab_a = setup_tab_for_prefix(head_prefix_a, versions: { "stable" => "1.0" })
setup_tab_for_prefix(head_prefix_b) setup_tab_for_prefix(head_prefix_b)
initial_env = ENV.to_hash
testball_repo = HOMEBREW_PREFIX.join("testball_repo") testball_repo = HOMEBREW_PREFIX.join("testball_repo")
testball_repo.mkdir testball_repo.mkdir
@ -1114,18 +1105,14 @@ class OutdatedVersionsTests < Homebrew::TestCase
head "file://#{testball_repo}", using: :git head "file://#{testball_repo}", using: :git
end end
%w[AUTHOR COMMITTER].each do |role| using_git_env do
ENV["GIT_#{role}_NAME"] = "brew tests" testball_repo.cd do
ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" FileUtils.touch "LICENSE"
ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" shutup do
end system "git", "init"
system "git", "add", "--all"
testball_repo.cd do system "git", "commit", "-m", "Initial commit"
FileUtils.touch "LICENSE" end
shutup do
system "git", "init"
system "git", "add", "--all"
system "git", "commit", "-m", "Initial commit"
end end
end end
@ -1144,7 +1131,6 @@ class OutdatedVersionsTests < Homebrew::TestCase
reset_outdated_kegs reset_outdated_kegs
assert_predicate f.outdated_kegs(fetch_head: true), :empty? assert_predicate f.outdated_kegs(fetch_head: true), :empty?
ensure ensure
ENV.replace(initial_env)
testball_repo.rmtree if testball_repo.exist? testball_repo.rmtree if testball_repo.exist?
end end

View File

@ -73,24 +73,19 @@ class IntegrationCommandTestInstall < IntegrationCommandTestCase
end end
def test_install_head_installed 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 = HOMEBREW_CACHE.join("repo")
repo_path.join("bin").mkpath repo_path.join("bin").mkpath
repo_path.cd do using_git_env do
shutup do repo_path.cd do
system "git", "init" shutup do
system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" system "git", "init"
FileUtils.touch "bin/something.bin" system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
FileUtils.touch "README" FileUtils.touch "bin/something.bin"
system "git", "add", "--all" FileUtils.touch "README"
system "git", "commit", "-m", "Initial repo commit" system "git", "add", "--all"
system "git", "commit", "-m", "Initial repo commit"
end
end end
end end
@ -110,9 +105,6 @@ class IntegrationCommandTestInstall < IntegrationCommandTestCase
cmd("install", "testball1", "--HEAD", "--ignore-dependencies") cmd("install", "testball1", "--HEAD", "--ignore-dependencies")
assert_match "#{HOMEBREW_CELLAR}/testball1/HEAD-2ccdf4f", cmd("unlink", "testball1") assert_match "#{HOMEBREW_CELLAR}/testball1/HEAD-2ccdf4f", cmd("unlink", "testball1")
assert_match "#{HOMEBREW_CELLAR}/testball1/1.0", cmd("install", "testball1") assert_match "#{HOMEBREW_CELLAR}/testball1/1.0", cmd("install", "testball1")
ensure
ENV.replace(initial_env)
end end
def test_install_with_invalid_option def test_install_with_invalid_option

View File

@ -1,14 +1,32 @@
module Test module Test
module Helper module Helper
module Env module Env
def copy_env
ENV.to_hash
end
def restore_env(env)
ENV.replace(env)
end
def with_environment(partial_env) def with_environment(partial_env)
old = ENV.to_hash old = copy_env
ENV.update partial_env ENV.update partial_env
begin yield
yield ensure
ensure restore_env old
ENV.replace 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 end
yield
ensure
restore_env initial_env
end end
end end
end end

View File

@ -66,23 +66,16 @@ class TapTest < Homebrew::TestCase
end end
def setup_git_repo def setup_git_repo
env = ENV.to_hash using_git_env do
%w[AUTHOR COMMITTER].each do |role| @path.cd do
ENV["GIT_#{role}_NAME"] = "brew tests" shutup do
ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" system "git", "init"
ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
end system "git", "add", "--all"
system "git", "commit", "-m", "init"
@path.cd do end
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 end
ensure
ENV.replace(env)
end end
def test_fetch def test_fetch