From f85d3d1f7ff891552c3bd54f8010cd9c401e8cd4 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 12 Feb 2017 17:26:34 +0100 Subject: [PATCH] Convert Requirement test to spec. --- Library/Homebrew/.rubocop.yml | 2 +- Library/Homebrew/test/requirement_spec.rb | 288 ++++++++++++++++++++++ Library/Homebrew/test/requirement_test.rb | 152 ------------ 3 files changed, 289 insertions(+), 153 deletions(-) create mode 100644 Library/Homebrew/test/requirement_spec.rb delete mode 100644 Library/Homebrew/test/requirement_test.rb diff --git a/Library/Homebrew/.rubocop.yml b/Library/Homebrew/.rubocop.yml index a85f0dcabb..938bf21b7c 100644 --- a/Library/Homebrew/.rubocop.yml +++ b/Library/Homebrew/.rubocop.yml @@ -12,7 +12,7 @@ AllCops: Style/BlockDelimiters: Exclude: - '**/cask/spec/**/*' - - '**/cask/test/**/*' + - '**/*_spec.rb' # so many of these in formulae but none in here Lint/AmbiguousRegexpLiteral: diff --git a/Library/Homebrew/test/requirement_spec.rb b/Library/Homebrew/test/requirement_spec.rb new file mode 100644 index 0000000000..110a7ac4fb --- /dev/null +++ b/Library/Homebrew/test/requirement_spec.rb @@ -0,0 +1,288 @@ +require "extend/ENV" +require "requirement" + +RSpec::Matchers.alias_matcher :have_a_default_formula, :be_a_default_formula +RSpec::Matchers.alias_matcher :be_a_build_requirement, :be_a_build + +describe Requirement do + subject { klass.new } + + let(:klass) { Class.new(described_class) } + + describe "#tags" do + subject { described_class.new(tags) } + + context "single tag" do + let(:tags) { ["bar"] } + + its(:tags) { are_expected.to eq(tags) } + end + + context "multiple tags" do + let(:tags) { ["bar", "baz"] } + + its(:tags) { are_expected.to eq(tags) } + end + + context "symbol tags" do + let(:tags) { [:build] } + + its(:tags) { are_expected.to eq(tags) } + end + + context "symbol and string tags" do + let(:tags) { [:build, "bar"] } + + its(:tags) { are_expected.to eq(tags) } + end + end + + describe "#fatal?" do + context "#fatal true is specified" do + let(:klass) do + Class.new(described_class) do + fatal true + end + end + + it { is_expected.to be_fatal } + end + + context "#fatal is ommitted" do + it { is_expected.not_to be_fatal } + end + end + + describe "#satisfied?" do + context "#satisfy with block and build_env returns true" do + let(:klass) do + Class.new(described_class) do + satisfy(build_env: false) do + true + end + end + end + + it { is_expected.to be_satisfied } + end + + context "#satisfy with block and build_env returns false" do + let(:klass) do + Class.new(described_class) do + satisfy(build_env: false) do + false + end + end + end + + it { is_expected.not_to be_satisfied } + end + + context "#satisfy returns true" do + let(:klass) do + Class.new(described_class) do + satisfy true + end + end + + it { is_expected.to be_satisfied } + end + + context "#satisfy returns false" do + let(:klass) do + Class.new(described_class) do + satisfy false + end + end + + it { is_expected.not_to be_satisfied } + end + + context "#satisfy with block returning true and without :build_env" do + let(:klass) do + Class.new(described_class) do + satisfy do + true + end + end + end + + it "sets up build environment" do + expect(ENV).to receive(:with_build_environment).and_call_original + subject.satisfied? + end + end + + context "#satisfy with block returning true and :build_env set to false" do + let(:klass) do + Class.new(described_class) do + satisfy(build_env: false) do + true + end + end + end + + it "skips setting up build environment" do + expect(ENV).not_to receive(:with_build_environment) + subject.satisfied? + end + end + + context "#satisfy with block returning path and without :build_env" do + let(:klass) do + Class.new(described_class) do + satisfy do + Pathname.new("/foo/bar/baz") + end + end + end + + it "infers path from #satisfy result" do + expect(ENV).to receive(:append_path).with("PATH", Pathname.new("/foo/bar")) + subject.satisfied? + subject.modify_build_environment + end + end + end + + describe "#build?" do + context "#build true is specified" do + let(:klass) do + Class.new(described_class) do + build true + end + end + + it { is_expected.to be_a_build_requirement } + end + + context "#build ommitted" do + it { is_expected.not_to be_a_build_requirement } + end + end + + describe "#name and #option_names" do + let(:const) { :FooRequirement } + let(:klass) { self.class.const_get(const) } + + before(:each) do + self.class.const_set(const, Class.new(described_class)) + end + + after(:each) do + self.class.send(:remove_const, const) + end + + its(:name) { is_expected.to eq("foo") } + its(:option_names) { are_expected.to eq(["foo"]) } + end + + describe "#default_formula?" do + context "#default_formula specified" do + let(:klass) do + Class.new(described_class) do + default_formula "foo" + end + end + + it { is_expected.to have_a_default_formula } + end + + context "#default_formula ommitted" do + it { is_expected.not_to have_a_default_formula } + end + end + + describe "#to_dependency" do + let(:klass) do + Class.new(described_class) do + default_formula "foo" + end + end + + it "returns a Dependency for its default Formula" do + expect(subject.to_dependency).to eq(Dependency.new("foo")) + end + + context "#modify_build_environment" do + context "with error" do + let(:klass) do + Class.new(described_class) do + class ModifyBuildEnvironmentError < StandardError; end + + default_formula "foo" + + satisfy do + true + end + + env do + raise ModifyBuildEnvironmentError + end + end + end + + it "raises an error" do + expect { + subject.to_dependency.modify_build_environment + }.to raise_error(klass.const_get(:ModifyBuildEnvironmentError)) + end + end + end + end + + describe "#modify_build_environment" do + context "without env proc" do + let(:klass) { Class.new(described_class) } + + it "returns nil" do + expect(subject.modify_build_environment).to be nil + end + end + end + + describe "#eql? and #==" do + subject { described_class.new } + + it "returns true if the names and tags are equal" do + other = described_class.new + + expect(subject).to eql(other) + expect(subject).to eq(other) + end + + it "returns false if names differ" do + other = described_class.new + allow(other).to receive(:name).and_return("foo") + expect(subject).not_to eql(other) + expect(subject).not_to eq(other) + end + + it "returns false if tags differ" do + other = described_class.new([:optional]) + + expect(subject).not_to eql(other) + expect(subject).not_to eq(other) + end + end + + describe "#hash" do + subject { described_class.new } + + it "is equal if names and tags are equal" do + other = described_class.new + expect(subject.hash).to eq(other.hash) + end + + it "differs if names differ" do + other = described_class.new + allow(other).to receive(:name).and_return("foo") + expect(subject.hash).not_to eq(other.hash) + end + + it "differs if tags differ" do + other = described_class.new([:optional]) + expect(subject.hash).not_to eq(other.hash) + end + end +end diff --git a/Library/Homebrew/test/requirement_test.rb b/Library/Homebrew/test/requirement_test.rb deleted file mode 100644 index 580d42bfa0..0000000000 --- a/Library/Homebrew/test/requirement_test.rb +++ /dev/null @@ -1,152 +0,0 @@ -require "testing_env" -require "requirement" - -class RequirementTests < Homebrew::TestCase - class TestRequirement < Requirement; end - - def test_accepts_single_tag - dep = Requirement.new(%w[bar]) - assert_equal %w[bar], dep.tags - end - - def test_accepts_multiple_tags - dep = Requirement.new(%w[bar baz]) - assert_equal %w[bar baz].sort, dep.tags.sort - end - - def test_option_names - dep = TestRequirement.new - assert_equal %w[test], dep.option_names - end - - def test_preserves_symbol_tags - dep = Requirement.new([:build]) - assert_equal [:build], dep.tags - end - - def test_accepts_symbol_and_string_tags - dep = Requirement.new([:build, "bar"]) - assert_equal [:build, "bar"], dep.tags - end - - def test_dsl_fatal - req = Class.new(Requirement) { fatal true }.new - assert_predicate req, :fatal? - end - - def test_satisfy_true - req = Class.new(Requirement) do - satisfy(build_env: false) { true } - end.new - assert_predicate req, :satisfied? - end - - def test_satisfy_false - req = Class.new(Requirement) do - satisfy(build_env: false) { false } - end.new - refute_predicate req, :satisfied? - end - - def test_satisfy_with_boolean - req = Class.new(Requirement) do - satisfy true - end.new - assert_predicate req, :satisfied? - end - - def test_satisfy_sets_up_build_env_by_default - req = Class.new(Requirement) do - satisfy { true } - end.new - - ENV.expects(:with_build_environment).yields.returns(true) - - assert_predicate req, :satisfied? - end - - def test_satisfy_build_env_can_be_disabled - req = Class.new(Requirement) do - satisfy(build_env: false) { true } - end.new - - ENV.expects(:with_build_environment).never - - assert_predicate req, :satisfied? - end - - def test_infers_path_from_satisfy_result - which_path = Pathname.new("/foo/bar/baz") - req = Class.new(Requirement) do - satisfy { which_path } - end.new - - ENV.expects(:with_build_environment).yields.returns(which_path) - ENV.expects(:append_path).with("PATH", which_path.parent) - - req.satisfied? - req.modify_build_environment - end - - def test_dsl_build - req = Class.new(Requirement) { build true }.new - assert_predicate req, :build? - end - - def test_infer_name_from_class - const = :FooRequirement - klass = self.class - - klass.const_set(const, Class.new(Requirement)) - - begin - assert_equal "foo", klass.const_get(const).new.name - ensure - klass.send(:remove_const, const) - end - end - - def test_dsl_default_formula - req = Class.new(Requirement) { default_formula "foo" }.new - assert_predicate req, :default_formula? - end - - def test_to_dependency - req = Class.new(Requirement) { default_formula "foo" }.new - assert_equal Dependency.new("foo"), req.to_dependency - end - - def test_to_dependency_calls_requirement_modify_build_environment - error = Class.new(StandardError) - - req = Class.new(Requirement) do - default_formula "foo" - satisfy { true } - env { raise error } - end.new - - assert_raises(error) do - req.to_dependency.modify_build_environment - end - end - - def test_modify_build_environment_without_env_proc - assert_nil Class.new(Requirement).new.modify_build_environment - end - - def test_eql - a = Requirement.new - b = Requirement.new - assert_equal a, b - assert_eql a, b - assert_equal a.hash, b.hash - end - - def test_not_eql - a = Requirement.new([:optional]) - b = Requirement.new - refute_equal a, b - refute_eql a, b - refute_equal a.hash, b.hash - end -end