From 890631dc29cbf0035566d5c5d1e74ddf43684bc2 Mon Sep 17 00:00:00 2001 From: Markus Reiter <me@reitermark.us> Date: Sat, 25 Feb 2017 10:09:26 +0100 Subject: [PATCH] Convert ENV test to spec. --- Library/Homebrew/test/ARGV_spec.rb | 2 +- Library/Homebrew/test/ENV_spec.rb | 189 +++++++++++++++++++++++++++++ Library/Homebrew/test/ENV_test.rb | 175 -------------------------- 3 files changed, 190 insertions(+), 176 deletions(-) create mode 100644 Library/Homebrew/test/ENV_spec.rb delete mode 100644 Library/Homebrew/test/ENV_test.rb diff --git a/Library/Homebrew/test/ARGV_spec.rb b/Library/Homebrew/test/ARGV_spec.rb index bcb7c3f70e..89ef9cabc6 100644 --- a/Library/Homebrew/test/ARGV_spec.rb +++ b/Library/Homebrew/test/ARGV_spec.rb @@ -1,7 +1,7 @@ require "extend/ARGV" describe HomebrewArgvExtension do - subject { argv.extend(HomebrewArgvExtension) } + subject { argv.extend(described_class) } let(:argv) { ["mxcl"] } describe "#formulae" do diff --git a/Library/Homebrew/test/ENV_spec.rb b/Library/Homebrew/test/ENV_spec.rb new file mode 100644 index 0000000000..7b50ca1d4a --- /dev/null +++ b/Library/Homebrew/test/ENV_spec.rb @@ -0,0 +1,189 @@ +require "extend/ENV" + +shared_examples EnvActivation do + subject { env.extend(described_class) } + let(:env) { {}.extend(EnvActivation) } + + it "supports switching compilers" do + subject.clang + expect(subject["LD"]).to be nil + expect(subject["CC"]).to eq(subject["OBJC"]) + end + + describe "#with_build_environment" do + it "restores the environment" do + before = subject.dup + + subject.with_build_environment do + subject["foo"] = "bar" + end + + expect(subject["foo"]).to be nil + expect(subject).to eq(before) + end + + it "ensures the environment is restored" do + before = subject.dup + + expect { + subject.with_build_environment do + subject["foo"] = "bar" + raise StandardError + end + }.to raise_error(StandardError) + + expect(subject["foo"]).to be nil + expect(subject).to eq(before) + end + + it "returns the value of the block" do + expect(subject.with_build_environment { 1 }).to eq(1) + end + + it "does not mutate the interface" do + expected = subject.methods + + subject.with_build_environment do + expect(subject.methods).to eq(expected) + end + + expect(subject.methods).to eq(expected) + end + end + + describe "#append" do + it "appends to an existing key" do + subject["foo"] = "bar" + subject.append "foo", "1" + expect(subject["foo"]).to eq("bar 1") + end + + it "appends to an existing empty key" do + subject["foo"] = "" + subject.append "foo", "1" + expect(subject["foo"]).to eq("1") + end + + it "appends to a non-existant key" do + subject.append "foo", "1" + expect(subject["foo"]).to eq("1") + end + + # NOTE: this may be a wrong behavior; we should probably reject objects that + # do not respond to #to_str. For now this documents existing behavior. + it "coerces a value to a string" do + subject.append "foo", 42 + expect(subject["foo"]).to eq("42") + end + end + + describe "#prepend" do + it "prepends to an existing key" do + subject["foo"] = "bar" + subject.prepend "foo", "1" + expect(subject["foo"]).to eq("1 bar") + end + + it "prepends to an existing empty key" do + subject["foo"] = "" + subject.prepend "foo", "1" + expect(subject["foo"]).to eq("1") + end + + it "prepends to a non-existant key" do + subject.prepend "foo", "1" + expect(subject["foo"]).to eq("1") + end + + # NOTE: this may be a wrong behavior; we should probably reject objects that + # do not respond to #to_str. For now this documents existing behavior. + it "coerces a value to a string" do + subject.prepend "foo", 42 + expect(subject["foo"]).to eq("42") + end + end + + describe "#append_path" do + it "appends to a path" do + subject.append_path "FOO", "/usr/bin" + expect(subject["FOO"]).to eq("/usr/bin") + + subject.append_path "FOO", "/bin" + expect(subject["FOO"]).to eq("/usr/bin#{File::PATH_SEPARATOR}/bin") + end + end + + describe "#prepend_path" do + it "prepends to a path" do + subject.prepend_path "FOO", "/usr/bin" + expect(subject["FOO"]).to eq("/usr/bin") + + subject.prepend_path "FOO", "/bin" + expect(subject["FOO"]).to eq("/bin#{File::PATH_SEPARATOR}/usr/bin") + end + end + + describe "#compiler" do + it "allows switching compilers" do + [:clang, :gcc_4_2, :gcc_4_0].each do |compiler| + subject.public_send(compiler) + expect(subject.compiler).to eq(compiler) + end + end + end + + example "deparallelize_block_form_restores_makeflags" do + subject["MAKEFLAGS"] = "-j4" + + subject.deparallelize do + expect(subject["MAKEFLAGS"]).to be nil + end + + expect(subject["MAKEFLAGS"]).to eq("-j4") + end +end + +describe Stdenv do + include_examples EnvActivation +end + +describe Superenv do + include_examples EnvActivation + + it "initializes deps" do + expect(subject.deps).to eq([]) + expect(subject.keg_only_deps).to eq([]) + end + + describe "#cxx11" do + it "raises an error when the compiler isn't supported" do + %w[gcc gcc-4.7].each do |compiler| + subject["HOMEBREW_CC"] = compiler + + expect { subject.cxx11 } + .to raise_error(/The selected compiler doesn't support C\+\+11:/) + + expect(subject["HOMEBREW_CCCFG"]).to be nil + end + end + + it "supports gcc-5" do + subject["HOMEBREW_CC"] = "gcc-5" + subject.cxx11 + expect(subject["HOMEBREW_CCCFG"]).to include("x") + end + + example "supports gcc-6" do + subject["HOMEBREW_CC"] = "gcc-6" + subject.cxx11 + expect(subject["HOMEBREW_CCCFG"]).to include("x") + end + + it "supports clang" do + subject["HOMEBREW_CC"] = "clang" + subject.cxx11 + expect(subject["HOMEBREW_CCCFG"]).to include("x") + expect(subject["HOMEBREW_CCCFG"]).to include("g") + end + end +end diff --git a/Library/Homebrew/test/ENV_test.rb b/Library/Homebrew/test/ENV_test.rb deleted file mode 100644 index 66b59d8a4f..0000000000 --- a/Library/Homebrew/test/ENV_test.rb +++ /dev/null @@ -1,175 +0,0 @@ -require "testing_env" -require "extend/ENV" -require "testing_env" - -module SharedEnvTests - def setup - super - @env = {}.extend(EnvActivation) - end - - def test_switching_compilers - @env.clang - assert_nil @env["LD"] - assert_equal @env["OBJC"], @env["CC"] - end - - def test_with_build_environment_restores_env - before = @env.dup - @env.with_build_environment do - @env["foo"] = "bar" - end - assert_nil @env["foo"] - assert_equal before, @env - end - - def test_with_build_environment_ensures_env_restored - before = @env.dup - begin - @env.with_build_environment do - @env["foo"] = "bar" - raise Exception - end - rescue Exception - end - assert_nil @env["foo"] - assert_equal before, @env - end - - def test_with_build_environment_returns_block_value - assert_equal 1, @env.with_build_environment { 1 } - end - - def test_with_build_environment_does_not_mutate_interface - expected = @env.methods - @env.with_build_environment { assert_equal expected, @env.methods } - assert_equal expected, @env.methods - end - - def test_append_existing_key - @env["foo"] = "bar" - @env.append "foo", "1" - assert_equal "bar 1", @env["foo"] - end - - def test_append_existing_key_empty - @env["foo"] = "" - @env.append "foo", "1" - assert_equal "1", @env["foo"] - end - - def test_append_missing_key - @env.append "foo", "1" - assert_equal "1", @env["foo"] - end - - def test_prepend_existing_key - @env["foo"] = "bar" - @env.prepend "foo", "1" - assert_equal "1 bar", @env["foo"] - end - - def test_prepend_existing_key_empty - @env["foo"] = "" - @env.prepend "foo", "1" - assert_equal "1", @env["foo"] - end - - def test_prepend_missing_key - @env.prepend "foo", "1" - assert_equal "1", @env["foo"] - end - - # NOTE: this may be a wrong behavior; we should probably reject objects that - # do not respond to #to_str. For now this documents existing behavior. - def test_append_coerces_value_to_string - @env.append "foo", 42 - assert_equal "42", @env["foo"] - end - - def test_prepend_coerces_value_to_string - @env.prepend "foo", 42 - assert_equal "42", @env["foo"] - end - - def test_append_path - @env.append_path "FOO", "/usr/bin" - assert_equal "/usr/bin", @env["FOO"] - @env.append_path "FOO", "/bin" - assert_equal "/usr/bin#{File::PATH_SEPARATOR}/bin", @env["FOO"] - end - - def test_prepend_path - @env.prepend_path "FOO", "/usr/bin" - assert_equal "/usr/bin", @env["FOO"] - @env.prepend_path "FOO", "/bin" - assert_equal "/bin#{File::PATH_SEPARATOR}/usr/bin", @env["FOO"] - end - - def test_switching_compilers_updates_compiler - [:clang, :gcc_4_2, :gcc_4_0].each do |compiler| - @env.send(compiler) - assert_equal compiler, @env.compiler - end - end - - def test_deparallelize_block_form_restores_makeflags - @env["MAKEFLAGS"] = "-j4" - @env.deparallelize do - assert_nil @env["MAKEFLAGS"] - end - assert_equal "-j4", @env["MAKEFLAGS"] - end -end - -class StdenvTests < Homebrew::TestCase - include SharedEnvTests - - def setup - super - @env.extend(Stdenv) - end -end - -class SuperenvTests < Homebrew::TestCase - include SharedEnvTests - - def setup - super - @env.extend(Superenv) - end - - def test_initializes_deps - assert_equal [], @env.deps - assert_equal [], @env.keg_only_deps - end - - def test_unsupported_cxx11 - %w[gcc gcc-4.7].each do |compiler| - @env["HOMEBREW_CC"] = compiler - assert_raises do - @env.cxx11 - end - refute_match "x", @env["HOMEBREW_CCCFG"] - end - end - - def test_supported_cxx11_gcc_5 - @env["HOMEBREW_CC"] = "gcc-5" - @env.cxx11 - assert_match "x", @env["HOMEBREW_CCCFG"] - end - - def test_supported_cxx11_gcc_6 - @env["HOMEBREW_CC"] = "gcc-6" - @env.cxx11 - assert_match "x", @env["HOMEBREW_CCCFG"] - end - - def test_supported_cxx11_clang - @env["HOMEBREW_CC"] = "clang" - @env.cxx11 - assert_match "x", @env["HOMEBREW_CCCFG"] - assert_match "g", @env["HOMEBREW_CCCFG"] - end -end -- GitLab