Merge pull request #1890 from alyssais/global_teardown_env

tests: automatically restore ENV in teardown
This commit is contained in:
Alyssa Ross 2017-01-23 17:54:31 +00:00 committed by GitHub
commit 2c1fbe1693
20 changed files with 60 additions and 158 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -157,28 +157,14 @@ 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
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
@ -192,18 +178,16 @@ 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
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 "c50c79b", @strategy.last_commit
assert_equal "f68266e", @strategy.last_commit
end
def test_fetch_last_commit
@ -214,21 +198,19 @@ 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!
assert_equal "c50c79b", @strategy.fetch_last_commit
assert_equal "f68266e", @strategy.fetch_last_commit
ensure
remote_repo.rmtree if remote_repo.directory?
end

View File

@ -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,12 +533,6 @@ 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
@ -552,8 +544,6 @@ class FormulaTests < Homebrew::TestCase
f.update_head_version
assert_equal Version.create("HEAD-5658946"), f.head.version
ensure
ENV.replace(initial_env)
end
def test_legacy_options
@ -1098,13 +1088,12 @@ 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" })
setup_tab_for_prefix(head_prefix_b)
initial_env = ENV.to_hash
testball_repo = HOMEBREW_PREFIX.join("testball_repo")
testball_repo.mkdir
@ -1114,12 +1103,6 @@ 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
@ -1144,7 +1127,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

View File

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

View File

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

View File

@ -73,13 +73,6 @@ 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
@ -105,14 +98,11 @@ 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")
ensure
ENV.replace(initial_env)
end
def test_install_with_invalid_option

View File

@ -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:",
@ -22,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

View File

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

View File

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

View File

@ -1,15 +0,0 @@
module Test
module Helper
module Env
def with_environment(partial_env)
old = ENV.to_hash
ENV.update partial_env
begin
yield
ensure
ENV.replace old
end
end
end
end
end

View File

@ -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
@ -16,11 +14,14 @@ module Homebrew
def setup
super
@__argv = ARGV.dup
@__env = ENV.to_hash # dup doesn't work on ENV
end
def teardown
ARGV.replace(@__argv)
ENV.replace(@__env)
Tab.clear_cache

View File

@ -66,13 +66,6 @@ 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"
@ -81,8 +74,6 @@ class TapTest < Homebrew::TestCase
system "git", "commit", "-m", "init"
end
end
ensure
ENV.replace(env)
end
def test_fetch
@ -184,10 +175,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(/\A\d+ .+ ago\Z/, @tap.git_last_commit)
assert_equal "2017-01-22", @tap.git_last_commit_date
end
def test_private_remote

View File

@ -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
@ -213,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/"],