Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cask migration warnings. #16654

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,23 @@ def from_h_gsubs(value, appdir)
end
end

# Loader which tries loading casks from the default tap.
class FromDefaultNameLoader < FromTapLoader
sig {
params(ref: T.any(String, Pathname, Cask, URI::Generic), warn: T::Boolean)
.returns(T.nilable(T.attached_class))
}
def self.try_new(ref, warn: false)
return unless ref.is_a?(String)
return unless (token = ref[HOMEBREW_DEFAULT_TAP_CASK_REGEX, :token])
return unless (tap = CoreCaskTap.instance).installed?

return unless (loader = super("#{tap}/#{token}", warn: warn))

loader if loader.path.exist?
end
end

# Loader which tries loading casks from tap paths, failing
# if the same token exists in multiple taps.
class FromNameLoader < FromTapLoader
Expand All @@ -458,11 +475,6 @@ def self.try_new(ref, warn: false)
when 1
loaders.first
when 2..Float::INFINITY
# Always prefer the default tap, i.e. behave the same as if loading from the API.
if (default_tap_loader = loaders.find { _1.tap.core_cask_tap? })
return default_tap_loader
end

raise TapCaskAmbiguityError.new(token, loaders.map(&:tap))
end
end
Expand Down Expand Up @@ -555,6 +567,7 @@ def self.for(ref, need_path: false, warn: true)
FromURILoader,
FromAPILoader,
FromTapLoader,
FromDefaultNameLoader,
FromNameLoader,
FromPathLoader,
FromInstalledPathLoader,
Expand Down
40 changes: 40 additions & 0 deletions Library/Homebrew/test/cask/cask_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,45 @@
end
end
end

context "when not using the API" do
before do
ENV["HOMEBREW_NO_INSTALL_FROM_API"] = "1"
end

context "when a cask is migrated to the default tap" do
let(:token) { "local-caffeine" }
let(:tap_migrations) do
{
token => default_tap.name,
}
end
let(:old_tap) { CoreTap.instance }
let(:default_tap) { CoreCaskTap.instance }

before do
(old_tap.path/"tap_migrations.json").write tap_migrations.to_json
old_tap.clear_cache
end

it "does not warn when loading the short token" do
expect do
described_class.for(token)
end.not_to output.to_stderr
end

it "does not warn when loading the full token in the default tap" do
expect do
described_class.for("#{default_tap}/#{token}")
end.not_to output.to_stderr
end

it "warns when loading the full token in the old tap" do
expect do
described_class.for("#{old_tap}/#{token}")
end.to output(%r{Cask #{old_tap}/#{token} was renamed to #{token}\.}).to_stderr
end
end
end
end
end
43 changes: 43 additions & 0 deletions Library/Homebrew/test/formulary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,47 @@ def formula_json_contents(extra_items = {})
expect(described_class.convert_to_string_or_symbol(":foo")).to eq :foo
end
end

describe "::loader_for" do
context "when not using the API" do
before do
ENV["HOMEBREW_NO_INSTALL_FROM_API"] = "1"
end

context "when a formula is migrated to the default tap" do
let(:token) { "local-caffeine" }
let(:tap_migrations) do
{
token => default_tap.name,
}
end
let(:old_tap) { CoreCaskTap.instance }
let(:default_tap) { CoreTap.instance }

before do
old_tap.path.mkpath
(old_tap.path/"tap_migrations.json").write tap_migrations.to_json
old_tap.clear_cache
end

it "does not warn when loading the short token" do
expect do
described_class.loader_for(token)
end.not_to output.to_stderr
end

it "does not warn when loading the full token in the default tap" do
expect do
described_class.loader_for("#{default_tap}/#{token}")
end.not_to output.to_stderr
end

it "warns when loading the full token in the old tap" do
expect do
described_class.loader_for("#{old_tap}/#{token}")
end.to output(%r{Formula #{old_tap}/#{token} was renamed to #{token}\.}).to_stderr
end
end
end
end
end