From fb3b13e3c365c303aae2066d1533283e085060c2 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Thu, 27 Jun 2024 20:51:10 -0700 Subject: [PATCH] Add tests Check for the following: - Tap migration rename to core tap can be loaded by short name - Tap migration rename to core tap can be loaded by long name - Tap migration renam that clashes with existing core tap short name is ignored in favor of loading the cask/formula from the core tap --- .../cask/cask_loader/from_api_loader_spec.rb | 60 +++++++++++++++++-- Library/Homebrew/test/formulary_spec.rb | 46 ++++++++++++++ 2 files changed, 101 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/test/cask/cask_loader/from_api_loader_spec.rb b/Library/Homebrew/test/cask/cask_loader/from_api_loader_spec.rb index f6c60ff203..624c1f3cbf 100644 --- a/Library/Homebrew/test/cask/cask_loader/from_api_loader_spec.rb +++ b/Library/Homebrew/test/cask/cask_loader/from_api_loader_spec.rb @@ -14,12 +14,14 @@ RSpec.describe Cask::CaskLoader::FromAPILoader, :cask do before do allow(Homebrew::API::Cask) - .to receive(:all_casks) - .and_return(casks_from_api_hash) + .to receive_messages(all_casks: casks_from_api_hash, all_renames: {}) + + # The call to `Cask::CaskLoader.load` above sets the Tap cache prematurely. + Tap.clear_cache end end - describe ".can_load?" do + describe ".try_new" do include_context "with API setup", "test-opera" context "when not using the API", :no_api do @@ -34,16 +36,64 @@ RSpec.describe Cask::CaskLoader::FromAPILoader, :cask do end it "returns a loader for valid token" do - expect(described_class.try_new(token)).not_to be_nil + expect(described_class.try_new(token)) + .to be_a(described_class) + .and have_attributes(token:) end it "returns a loader for valid full name" do - expect(described_class.try_new("homebrew/cask/#{token}")).not_to be_nil + expect(described_class.try_new("homebrew/cask/#{token}")) + .to be_a(described_class) + .and have_attributes(token:) end it "returns nil for full name with invalid tap" do expect(described_class.try_new("homebrew/foo/#{token}")).to be_nil end + + context "with core tap migration renames" do + let(:foo_tap) { Tap.fetch("homebrew", "foo") } + + before do + foo_tap.path.mkpath + end + + after do + FileUtils.rm_rf foo_tap.path + end + + it "returns the core cask if the short name clashes with a tap migration rename" do + (foo_tap.path/"tap_migrations.json").write <<~JSON + { "#{token}": "homebrew/cask/#{token}-v2" } + JSON + + expect(Cask::CaskLoader::FromNameLoader.try_new(token)) + .to be_a(Cask::CaskLoader::FromNameLoader) + .and have_attributes(token:) + end + + it "returns the tap migration rename by old token" do + old_token = "#{token}-old" + (foo_tap.path/"tap_migrations.json").write <<~JSON + { "#{old_token}": "homebrew/cask/#{token}" } + JSON + + expect(Cask::CaskLoader::FromNameLoader.try_new(old_token)) + .to be_a(described_class) + .and have_attributes(token:) + end + + it "returns the tap migration rename by old full name" do + old_token = "#{token}-old" + (foo_tap.path/"tap_migrations.json").write <<~JSON + { "#{old_token}": "homebrew/cask/#{token}" } + JSON + + expect(Cask::CaskLoader::FromTapLoader.try_new("#{foo_tap}/#{old_token}")) + .to be_a(described_class) + .and have_attributes(token:) + end + end end end diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb index e5293ffc7a..43fb416bd1 100644 --- a/Library/Homebrew/test/formulary_spec.rb +++ b/Library/Homebrew/test/formulary_spec.rb @@ -383,6 +383,7 @@ RSpec.describe Formulary do # avoid unnecessary network calls allow(Homebrew::API::Formula).to receive_messages(all_aliases: {}, all_renames: {}) allow(CoreTap.instance).to receive(:tap_migrations).and_return({}) + allow(CoreCaskTap.instance).to receive(:tap_migrations).and_return({}) # don't try to load/fetch gcc/glibc allow(DevelopmentTools).to receive_messages(needs_libc_formula?: false, needs_compiler_formula?: false) @@ -482,6 +483,51 @@ RSpec.describe Formulary do expect(formula.deps.count).to eq 5 expect(formula.deps.map(&:name).include?("uses_from_macos_dep")).to be true end + + context "with core tap migration renames" do + let(:foo_tap) { Tap.fetch("homebrew", "foo") } + + before do + allow(Homebrew::API::Formula).to receive(:all_formulae).and_return formula_json_contents + foo_tap.path.mkpath + end + + after do + FileUtils.rm_rf foo_tap.path + end + + it "returns the core formula if the short name clashes with a tap migration rename" do + (foo_tap.path/"tap_migrations.json").write <<~JSON + { "#{formula_name}": "homebrew/core/#{formula_name}-v2" } + JSON + + expect(described_class::FromNameLoader.try_new(formula_name)) + .to be_a(described_class::FromNameLoader) + .and have_attributes(name: formula_name) + end + + it "returns the tap migration rename by old formula_name" do + old_formula_name = "#{formula_name}-old" + (foo_tap.path/"tap_migrations.json").write <<~JSON + { "#{old_formula_name}": "homebrew/core/#{formula_name}" } + JSON + + expect(described_class::FromNameLoader.try_new(old_formula_name)) + .to be_a(described_class::FromAPILoader) + .and have_attributes(name: formula_name) + end + + it "returns the tap migration rename by old full name" do + old_formula_name = "#{formula_name}-old" + (foo_tap.path/"tap_migrations.json").write <<~JSON + { "#{old_formula_name}": "homebrew/core/#{formula_name}" } + JSON + + expect(described_class::FromTapLoader.try_new("#{foo_tap}/#{old_formula_name}")) + .to be_a(described_class::FromAPILoader) + .and have_attributes(name: formula_name) + end + end end end