From fba00f2bbfd970ece1d2503a333a9a2730ae5b63 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 19 Oct 2016 12:32:48 -0400 Subject: [PATCH 1/4] testing_env: extract with_environment to helper module --- Library/Homebrew/test/helper/env.rb | 15 +++++++++++++++ Library/Homebrew/test/testing_env.rb | 12 ++---------- 2 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 Library/Homebrew/test/helper/env.rb diff --git a/Library/Homebrew/test/helper/env.rb b/Library/Homebrew/test/helper/env.rb new file mode 100644 index 0000000000..904a1d4c71 --- /dev/null +++ b/Library/Homebrew/test/helper/env.rb @@ -0,0 +1,15 @@ +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 diff --git a/Library/Homebrew/test/testing_env.rb b/Library/Homebrew/test/testing_env.rb index 02264aa392..5f98ace15d 100644 --- a/Library/Homebrew/test/testing_env.rb +++ b/Library/Homebrew/test/testing_env.rb @@ -63,7 +63,9 @@ module Homebrew end class TestCase < ::Minitest::Test + require "test/helper/env" require "test/helper/shutup" + include Test::Helper::Env include Test::Helper::Shutup include VersionAssertions @@ -112,16 +114,6 @@ module Homebrew Pathname.new("#{TEST_DIRECTORY}/mach/#{name}.bundle") end - def with_environment(partial_env) - old = ENV.to_hash - ENV.update partial_env - begin - yield - ensure - ENV.replace old - end - end - # Use a stubbed {Formulary::FormulaLoader} to make a given formula be found # when loading from {Formulary} with `ref`. def stub_formula_loader(formula, ref = formula.full_name) From e8b6aa4ed16b662cf7a3847e6f7995ca8c8e0194 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 19 Oct 2016 12:34:03 -0400 Subject: [PATCH 2/4] cask/spec: replace with_env_var with with_environment --- Library/Homebrew/cask/spec/cask/cli_spec.rb | 4 ++-- Library/Homebrew/cask/spec/spec_helper.rb | 2 ++ Library/Homebrew/cask/spec/support/env_helper.rb | 16 ---------------- 3 files changed, 4 insertions(+), 18 deletions(-) delete mode 100644 Library/Homebrew/cask/spec/support/env_helper.rb diff --git a/Library/Homebrew/cask/spec/cask/cli_spec.rb b/Library/Homebrew/cask/spec/cask/cli_spec.rb index cb21dbd25c..1bdc57b6e0 100644 --- a/Library/Homebrew/cask/spec/cask/cli_spec.rb +++ b/Library/Homebrew/cask/spec/cask/cli_spec.rb @@ -41,14 +41,14 @@ describe Hbc::CLI do end it "respects the env variable when choosing what appdir to create" do - EnvHelper.with_env_var("HOMEBREW_CASK_OPTS", "--appdir=/custom/appdir") do + with_environment "HOMEBREW_CASK_OPTS" => "--appdir=/custom/appdir" do expect(Hbc).to receive(:appdir=).with(Pathname("/custom/appdir")) described_class.process("noop") end end it "respects the env variable when choosing a non-default Caskroom location" do - EnvHelper.with_env_var "HOMEBREW_CASK_OPTS", "--caskroom=/custom/caskdir" do + with_environment "HOMEBREW_CASK_OPTS" => "--caskroom=/custom/caskdir" do expect(Hbc).to receive(:caskroom=).with(Pathname("/custom/caskdir")) described_class.process("noop") end diff --git a/Library/Homebrew/cask/spec/spec_helper.rb b/Library/Homebrew/cask/spec/spec_helper.rb index 7dadc6b5c2..ebd60124ce 100644 --- a/Library/Homebrew/cask/spec/spec_helper.rb +++ b/Library/Homebrew/cask/spec/spec_helper.rb @@ -16,6 +16,7 @@ require "global" # add Homebrew-Cask to load path $LOAD_PATH.push(project_root.join("lib").to_s) +require "test/helper/env" require "test/helper/shutup" Dir["#{project_root}/spec/support/*.rb"].each(&method(:require)) @@ -61,5 +62,6 @@ Hbc.caskroom = Hbc.homebrew_prefix.join("TestCaskroom") RSpec.configure do |config| config.order = :random + config.include(Test::Helper::Env) config.include(Test::Helper::Shutup) end diff --git a/Library/Homebrew/cask/spec/support/env_helper.rb b/Library/Homebrew/cask/spec/support/env_helper.rb deleted file mode 100644 index 0a302ef45f..0000000000 --- a/Library/Homebrew/cask/spec/support/env_helper.rb +++ /dev/null @@ -1,16 +0,0 @@ -module EnvHelper - class << self - def with_env_var(key, val) - was_defined = ENV.key? "key" - old_value = ENV["key"] - ENV[key] = val - yield - ensure - if was_defined - ENV[key] = old_value - else - ENV.delete(key) - end - end - end -end From fef8f143e8a97141b747e9288100d4e8dbd3fc19 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 19 Oct 2016 12:35:12 -0400 Subject: [PATCH 3/4] compliance_test: unset HOMEBREW_DEVELOPER to avoid deprecation errors --- .../Homebrew/cask/test/Casks/compliance_test.rb | 16 +++++++++------- Library/Homebrew/cask/test/test_helper.rb | 2 ++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/cask/test/Casks/compliance_test.rb b/Library/Homebrew/cask/test/Casks/compliance_test.rb index fff3e07985..1ab5819ed0 100644 --- a/Library/Homebrew/cask/test/Casks/compliance_test.rb +++ b/Library/Homebrew/cask/test/Casks/compliance_test.rb @@ -1,13 +1,15 @@ require 'test_helper' describe "Casks" do - Hbc.all.reject {|c| c.is_a?(Hbc::TestCask) }.each do |cask| - describe "#{cask}" do - it "passes audit" do - audit = Hbc::Audit.new(cask) - audit.run! - audit.errors.must_equal [], "[#{cask}] Cask audit must be error free" - audit.warnings.must_equal [], "[#{cask}] Cask audit must be warning free" + with_environment "HOMEBREW_DEVELOPER" => nil do + Hbc.all.reject {|c| c.is_a?(Hbc::TestCask) }.each do |cask| + describe "#{cask}" do + it "passes audit" do + audit = Hbc::Audit.new(cask) + audit.run! + audit.errors.must_equal [], "[#{cask}] Cask audit must be error free" + audit.warnings.must_equal [], "[#{cask}] Cask audit must be warning free" + end end end end diff --git a/Library/Homebrew/cask/test/test_helper.rb b/Library/Homebrew/cask/test/test_helper.rb index 25b32d2de0..8f5e25777a 100644 --- a/Library/Homebrew/cask/test/test_helper.rb +++ b/Library/Homebrew/cask/test/test_helper.rb @@ -17,7 +17,9 @@ require "global" # add Homebrew-Cask to load path $LOAD_PATH.push(project_root.join("lib").to_s) +require "test/helper/env" require "test/helper/shutup" +include Test::Helper::Env include Test::Helper::Shutup def sudo(*args) From eebf535beee726ccbbeeb756ade86f216a0fcbc0 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 19 Oct 2016 12:35:25 -0400 Subject: [PATCH 4/4] dsl_test: unset HOMEBREW_DEVELOPER to avoid deprecation errors --- Library/Homebrew/cask/test/cask/dsl_test.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/cask/test/cask/dsl_test.rb b/Library/Homebrew/cask/test/cask/dsl_test.rb index 2d51a20f13..ecd1cb72ff 100644 --- a/Library/Homebrew/cask/test/cask/dsl_test.rb +++ b/Library/Homebrew/cask/test/cask/dsl_test.rb @@ -69,11 +69,13 @@ describe Hbc::DSL do end it "may use deprecated DSL version hash syntax" do - test_cask = Hbc.load("with-dsl-version") - test_cask.token.must_equal "with-dsl-version" - test_cask.url.to_s.must_equal "http://example.com/TestCask.dmg" - test_cask.homepage.must_equal "http://example.com/" - test_cask.version.to_s.must_equal "1.2.3" + with_environment "HOMEBREW_DEVELOPER" => nil do + test_cask = Hbc.load("with-dsl-version") + test_cask.token.must_equal "with-dsl-version" + test_cask.url.to_s.must_equal "http://example.com/TestCask.dmg" + test_cask.homepage.must_equal "http://example.com/" + test_cask.version.to_s.must_equal "1.2.3" + end end end