diff --git a/Library/Homebrew/requirement.rb b/Library/Homebrew/requirement.rb index b4acf2255f..38ebe87721 100644 --- a/Library/Homebrew/requirement.rb +++ b/Library/Homebrew/requirement.rb @@ -20,6 +20,10 @@ class Requirement attr_reader :tags, :name, :cask, :download def initialize(tags = []) + # Only allow instances of subclasses. This base class enforces no constraints on its own. + # Individual subclasses use the `satisfy` DSL to define those constraints. + raise "Do not call `Requirement.new' directly without a subclass." unless self.class < Requirement + @cask = self.class.cask @download = self.class.download tags.each do |tag| diff --git a/Library/Homebrew/test/requirement_spec.rb b/Library/Homebrew/test/requirement_spec.rb index 0cd1c0db19..0daefeecef 100644 --- a/Library/Homebrew/test/requirement_spec.rb +++ b/Library/Homebrew/test/requirement_spec.rb @@ -12,7 +12,7 @@ describe Requirement do let(:klass) { Class.new(described_class) } describe "#tags" do - subject { described_class.new(tags) } + subject { klass.new(tags) } context "with a single tag" do let(:tags) { ["bar"] } @@ -149,7 +149,7 @@ describe Requirement do describe "#build?" do context "when the :build tag is specified" do - subject { described_class.new([:build]) } + subject { klass.new([:build]) } it { is_expected.to be_a_build_requirement } end @@ -186,24 +186,24 @@ describe Requirement do end describe "#eql? and #==" do - subject(:requirement) { described_class.new } + subject(:requirement) { klass.new } it "returns true if the names and tags are equal" do - other = described_class.new + other = klass.new expect(requirement).to eql(other) expect(requirement).to eq(other) end it "returns false if names differ" do - other = described_class.new + other = klass.new allow(other).to receive(:name).and_return("foo") expect(requirement).not_to eql(other) expect(requirement).not_to eq(other) end it "returns false if tags differ" do - other = described_class.new([:optional]) + other = klass.new([:optional]) expect(requirement).not_to eql(other) expect(requirement).not_to eq(other) @@ -211,21 +211,21 @@ describe Requirement do end describe "#hash" do - subject(:requirement) { described_class.new } + subject(:requirement) { klass.new } it "is equal if names and tags are equal" do - other = described_class.new + other = klass.new expect(requirement.hash).to eq(other.hash) end it "differs if names differ" do - other = described_class.new + other = klass.new allow(other).to receive(:name).and_return("foo") expect(requirement.hash).not_to eq(other.hash) end it "differs if tags differ" do - other = described_class.new([:optional]) + other = klass.new([:optional]) expect(requirement.hash).not_to eq(other.hash) end end diff --git a/Library/Homebrew/test/requirements_spec.rb b/Library/Homebrew/test/requirements_spec.rb index a915c926cd..e271a0073e 100644 --- a/Library/Homebrew/test/requirements_spec.rb +++ b/Library/Homebrew/test/requirements_spec.rb @@ -12,7 +12,8 @@ describe Requirements do end it "merges duplicate requirements" do - requirements << Requirement.new << Requirement.new + klass = Class.new(Requirement) + requirements << klass.new << klass.new expect(requirements.count).to eq(1) end end