From 237a42974b0e3fac629f4ffba640a519ef80f346 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 13 Nov 2023 13:40:44 -0500 Subject: [PATCH 1/4] Pass original tap to formula when loaded from the API via `TapLoader` --- Library/Homebrew/formulary.rb | 8 ++--- Library/Homebrew/test/formulary_spec.rb | 42 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 9d621eb8f6425..337abfef99528 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -667,8 +667,8 @@ def klass(flags:, ignore_errors:) # Load formulae from the API. class FormulaAPILoader < FormulaLoader - def initialize(name) - super name, Formulary.core_path(name) + def initialize(name, tap: CoreTap.instance) + super name, Formulary.core_path(name), tap: tap end def klass(flags:, ignore_errors:) @@ -926,9 +926,9 @@ def self.tap_formula_name_path(tapped_name, warn:) def self.tap_loader_for(tapped_name, warn:) name, path, tap = Formulary.tap_formula_name_path(tapped_name, warn: warn) - if name.exclude?("/") && !Homebrew::EnvConfig.no_install_from_api? && + if Tap.from_path(path).core_tap? && !Homebrew::EnvConfig.no_install_from_api? && Homebrew::API::Formula.all_formulae.key?(name) - FormulaAPILoader.new(name) + FormulaAPILoader.new(name, tap: tap) else TapLoader.new(name, path, tap: tap) end diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb index 7c237cc4b429b..e7c4c39805b8f 100644 --- a/Library/Homebrew/test/formulary_spec.rb +++ b/Library/Homebrew/test/formulary_spec.rb @@ -186,6 +186,48 @@ class Wrong#{described_class.class_s(formula_name)} < Formula end end + context "when migrating from a Tap" do + let(:tap) { Tap.new("homebrew", "foo") } + let(:another_tap) { Tap.new("homebrew", "bar") } + let(:tap_migrations_path) { tap.path/"tap_migrations.json" } + let(:another_tap_formula_path) { another_tap.path/"Formula/#{formula_name}.rb" } + + before do + tap.path.mkpath + another_tap_formula_path.dirname.mkpath + another_tap_formula_path.write formula_content + end + + after do + FileUtils.rm_rf tap.path + FileUtils.rm_rf another_tap.path + end + + it "returns a Formula that has gone through a tap migration into homebrew/core" do + tap_migrations_path.write <<~EOS + { + "#{formula_name}": "homebrew/core" + } + EOS + formula = described_class.factory("#{tap}/#{formula_name}") + expect(formula).to be_a(Formula) + expect(formula.tap).to eq(tap) + expect(formula.path).to eq(formula_path) + end + + it "returns a Formula that has gone through a tap migration into another tap" do + tap_migrations_path.write <<~EOS + { + "#{formula_name}": "#{another_tap}" + } + EOS + formula = described_class.factory("#{tap}/#{formula_name}") + expect(formula).to be_a(Formula) + expect(formula.tap).to eq(tap) + expect(formula.path).to eq(another_tap_formula_path) + end + end + context "when loading from Tap" do let(:tap) { Tap.new("homebrew", "foo") } let(:another_tap) { Tap.new("homebrew", "bar") } From aa316109d9ec80424c10d66f49ee524cedd30c37 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 13 Nov 2023 14:00:22 -0500 Subject: [PATCH 2/4] Pass new tap to `TapLoader` Co-authored-by: Alexander Mancevice --- Library/Homebrew/formulary.rb | 5 +++-- Library/Homebrew/test/formulary_spec.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 337abfef99528..f01d144e5b6eb 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -915,6 +915,7 @@ def self.tap_formula_name_path(tapped_name, warn:) name, path = Formulary.tap_formula_name_path(new_tapped_name, warn: false) old_name = tapped_name new_name = new_tap.core_tap? ? name : new_tapped_name + tap = new_tap end opoo "Formula #{old_name} was renamed to #{new_name}." if warn && old_name && new_name @@ -926,9 +927,9 @@ def self.tap_formula_name_path(tapped_name, warn:) def self.tap_loader_for(tapped_name, warn:) name, path, tap = Formulary.tap_formula_name_path(tapped_name, warn: warn) - if Tap.from_path(path).core_tap? && !Homebrew::EnvConfig.no_install_from_api? && + if tap.core_tap? && !Homebrew::EnvConfig.no_install_from_api? && Homebrew::API::Formula.all_formulae.key?(name) - FormulaAPILoader.new(name, tap: tap) + FormulaAPILoader.new(name) else TapLoader.new(name, path, tap: tap) end diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb index e7c4c39805b8f..9dfe65d4c3d1f 100644 --- a/Library/Homebrew/test/formulary_spec.rb +++ b/Library/Homebrew/test/formulary_spec.rb @@ -211,7 +211,7 @@ class Wrong#{described_class.class_s(formula_name)} < Formula EOS formula = described_class.factory("#{tap}/#{formula_name}") expect(formula).to be_a(Formula) - expect(formula.tap).to eq(tap) + expect(formula.tap).to eq(CoreTap.instance) expect(formula.path).to eq(formula_path) end @@ -223,7 +223,7 @@ class Wrong#{described_class.class_s(formula_name)} < Formula EOS formula = described_class.factory("#{tap}/#{formula_name}") expect(formula).to be_a(Formula) - expect(formula.tap).to eq(tap) + expect(formula.tap).to eq(another_tap) expect(formula.path).to eq(another_tap_formula_path) end end From 74996ab32ce64046802c75b9e3dd5d92a2e7acb6 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 13 Nov 2023 14:07:34 -0500 Subject: [PATCH 3/4] Remove new `tap` parameter to `FormulaAPILoader` --- Library/Homebrew/formulary.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index f01d144e5b6eb..813853c8484b9 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -667,8 +667,8 @@ def klass(flags:, ignore_errors:) # Load formulae from the API. class FormulaAPILoader < FormulaLoader - def initialize(name, tap: CoreTap.instance) - super name, Formulary.core_path(name), tap: tap + def initialize(name) + super name, Formulary.core_path(name) end def klass(flags:, ignore_errors:) From fb50cfa13dc89a2e875996b51e4f9d7621ce6c3f Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 13 Nov 2023 14:12:35 -0500 Subject: [PATCH 4/4] Simplify tap assignment --- Library/Homebrew/formulary.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 813853c8484b9..8fd5697067a43 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -912,10 +912,9 @@ def self.tap_formula_name_path(tapped_name, warn:) new_tap = Tap.fetch new_tap_name new_tap.ensure_installed! new_tapped_name = "#{new_tap_name}/#{name}" - name, path = Formulary.tap_formula_name_path(new_tapped_name, warn: false) + name, path, tap = Formulary.tap_formula_name_path(new_tapped_name, warn: false) old_name = tapped_name new_name = new_tap.core_tap? ? name : new_tapped_name - tap = new_tap end opoo "Formula #{old_name} was renamed to #{new_name}." if warn && old_name && new_name