diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 45c9561c62..f164a0fc60 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -251,11 +251,9 @@ module Homebrew problem "#{formula} is versioned but no #{unversioned_name} formula exists" end elsif ARGV.build_stable? && formula.stable? && - !(versioned_formulae = Dir[formula.path.to_s.gsub(/\.rb$/, "@*.rb")]).empty? + !(versioned_formulae = formula.versioned_formulae).empty? versioned_aliases = formula.aliases.grep(/.@\d/) - _, last_alias_version = - File.basename(versioned_formulae.sort.reverse.first) - .gsub(/\.rb$/, "").split("@") + _, last_alias_version = versioned_formulae.map(&:name).last.split("@") major, minor, = formula.version.to_s.split(".") alias_name_major = "#{formula.name}@#{major}" alias_name_major_minor = "#{alias_name_major}.#{minor}" diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 1d5a5b25d5..fcc965fd18 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -370,6 +370,15 @@ class Formula name.include?("@") end + # Returns any `@`-versioned formulae for an non-`@`-versioned formula. + def versioned_formulae + return [] if versioned_formula? + + Pathname.glob(path.to_s.gsub(/\.rb$/, "@*.rb")).map do |path| + Formula[path.basename(".rb").to_s] + end.sort + end + # A named Resource for the currently active {SoftwareSpec}. # Additional downloads can be defined as {#resource}s. # {Resource#stage} will create a temporary directory and yield to a block. diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 6ad102756e..da17a22fe4 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -107,6 +107,56 @@ describe Formula do end end + describe "#versioned_formula?" do + let(:f) do + formula "foo" do + url "foo-1.0" + end + end + + let(:f2) do + formula "foo@2.0" do + url "foo-2.0" + end + end + + it "returns true for @-versioned formulae" do + expect(f2.versioned_formula?).to be true + end + + it "returns false for non-@-versioned formulae" do + expect(f.versioned_formula?).to be false + end + end + + describe "#versioned_formulae" do + let(:f) do + formula "foo" do + url "foo-1.0" + end + end + + let(:f2) do + formula "foo@2.0" do + url "foo-2.0" + end + end + + it "returns true by default" do + FileUtils.touch f.path + FileUtils.touch f2.path + allow(Formulary).to receive(:load_formula_from_path).with(f2.name, f2.path).and_return(f2) + allow(Formulary).to receive(:factory).with(f2.name).and_return(f2) + expect(f.versioned_formulae).to eq [f2] + end + + it "returns empty array for non-@-versioned formulae" do + FileUtils.touch f.path + FileUtils.touch f2.path + expect(f2.versioned_formulae).to be_empty + end + end + example "installed alias with core" do f = formula do url "foo-1.0" @@ -708,7 +758,7 @@ describe Formula do expect(f3.runtime_dependencies.map(&:name)).to eq(["foo/bar/f1", "baz/qux/f2"]) end - it "includes non-declared direct dependencies", :focus do + it "includes non-declared direct dependencies" do formula = Class.new(Testball).new dependency = formula("dependency") { url "f-1.0" }