Add type signature for Tap::fetch.

This commit is contained in:
Markus Reiter 2024-02-23 15:02:10 +01:00
parent d55fa09d1e
commit a851bb86ef
No known key found for this signature in database
GPG Key ID: 245293B51702655B
5 changed files with 26 additions and 28 deletions

View File

@ -215,8 +215,7 @@ module Cask
sig { params(tapped_token: String).void } sig { params(tapped_token: String).void }
def initialize(tapped_token) def initialize(tapped_token)
user, repo, token = tapped_token.split("/", 3) tap, token = Tap.with_cask_token(tapped_token)
tap = Tap.fetch(user, repo)
cask = CaskLoader.find_cask_in_tap(token, tap) cask = CaskLoader.find_cask_in_tap(token, tap)
super cask super cask
end end
@ -543,9 +542,7 @@ module Cask
new_token = tap.core_cask_tap? ? token : "#{tap}/#{token}" new_token = tap.core_cask_tap? ? token : "#{tap}/#{token}"
type = :rename type = :rename
elsif (new_tap_name = tap.tap_migrations[token].presence) elsif (new_tap_name = tap.tap_migrations[token].presence)
new_tap_user, new_tap_repo, new_token = new_tap_name.split("/", 3) new_tap, new_token = Tap.with_cask_token(new_tap_name) || [Tap.fetch(new_tap_name), token]
new_token ||= token
new_tap = Tap.fetch(new_tap_user, new_tap_repo)
new_tap.ensure_installed! new_tap.ensure_installed!
new_tapped_token = "#{new_tap}/#{new_token}" new_tapped_token = "#{new_tap}/#{new_token}"

View File

@ -1151,9 +1151,7 @@ module Formulary
new_name = tap.core_tap? ? name : "#{tap}/#{name}" new_name = tap.core_tap? ? name : "#{tap}/#{name}"
type = :rename type = :rename
elsif (new_tap_name = tap.tap_migrations[name].presence) elsif (new_tap_name = tap.tap_migrations[name].presence)
new_tap_user, new_tap_repo, new_name = new_tap_name.split("/", 3) new_tap, new_name = Tap.with_formula_name(new_tap_name) || [Tap.fetch(new_tap_name), name]
new_name ||= name
new_tap = Tap.fetch(new_tap_user, new_tap_repo)
new_tap.ensure_installed! new_tap.ensure_installed!
new_tapped_name = "#{new_tap}/#{new_name}" new_tapped_name = "#{new_tap}/#{new_name}"

View File

@ -39,19 +39,19 @@ class Tap
#{HOMEBREW_TAP_STYLE_EXCEPTIONS_DIR}/*.json #{HOMEBREW_TAP_STYLE_EXCEPTIONS_DIR}/*.json
].freeze ].freeze
def self.fetch(*args) sig { params(user: String, repo: String).returns(Tap) }
case args.length def self.fetch(user, repo = T.unsafe(nil))
when 1 user, repo = user.split("/", 2) if repo.nil?
user, repo = args.first.split("/", 2)
when 2 if [user, repo].any? { |part| part.nil? || part.include?("/") }
user = args.first raise ArgumentError, "Invalid tap name: '#{[*user, *repo].join("/")}'"
repo = args.second
end end
raise "Invalid tap name '#{args.join("/")}'" if [user, repo].any? { |part| part.nil? || part.include?("/") } user = T.must(user)
repo = T.must(repo)
# We special case homebrew and linuxbrew so that users don't have to shift in a terminal. # We special case homebrew and linuxbrew so that users don't have to shift in a terminal.
user = user.capitalize if ["homebrew", "linuxbrew"].include? user user = user.capitalize if ["homebrew", "linuxbrew"].include?(user)
repo = repo.sub(HOMEBREW_OFFICIAL_REPO_PREFIXES_REGEX, "") repo = repo.sub(HOMEBREW_OFFICIAL_REPO_PREFIXES_REGEX, "")
return CoreTap.instance if ["Homebrew", "Linuxbrew"].include?(user) && ["core", "homebrew"].include?(repo) return CoreTap.instance if ["Homebrew", "Linuxbrew"].include?(user) && ["core", "homebrew"].include?(repo)
@ -63,13 +63,16 @@ class Tap
def self.from_path(path) def self.from_path(path)
match = File.expand_path(path).match(HOMEBREW_TAP_PATH_REGEX) match = File.expand_path(path).match(HOMEBREW_TAP_PATH_REGEX)
return if match.blank? || match[:user].blank? || match[:repo].blank?
fetch(match[:user], match[:repo]) return unless match
return unless (user = match[:user])
return unless (repo = match[:repo])
fetch(user, repo)
end end
# @private # @private
sig { params(name: String).returns(T.nilable([T.attached_class, String])) } sig { params(name: String).returns(T.nilable([Tap, String])) }
def self.with_formula_name(name) def self.with_formula_name(name)
return unless (match = name.match(HOMEBREW_TAP_FORMULA_REGEX)) return unless (match = name.match(HOMEBREW_TAP_FORMULA_REGEX))
@ -85,7 +88,7 @@ class Tap
end end
# @private # @private
sig { params(token: String).returns(T.nilable([T.attached_class, String])) } sig { params(token: String).returns(T.nilable([Tap, String])) }
def self.with_cask_token(token) def self.with_cask_token(token)
return unless (match = token.match(HOMEBREW_TAP_CASK_REGEX)) return unless (match = token.match(HOMEBREW_TAP_CASK_REGEX))
@ -823,7 +826,7 @@ class Tap
new_tap_user, new_tap_repo, new_name = new_name.split("/", 3) new_tap_user, new_tap_repo, new_name = new_name.split("/", 3)
next unless new_name next unless new_name
new_tap = Tap.fetch(new_tap_user, new_tap_repo) new_tap = Tap.fetch(T.must(new_tap_user), T.must(new_tap_repo))
hash["#{new_tap}/#{new_name}"] ||= [] hash["#{new_tap}/#{new_name}"] ||= []
hash["#{new_tap}/#{new_name}"] << old_name hash["#{new_tap}/#{new_name}"] << old_name
@ -878,7 +881,7 @@ class Tap
sig { params(other: T.nilable(T.any(String, Tap))).returns(T::Boolean) } sig { params(other: T.nilable(T.any(String, Tap))).returns(T::Boolean) }
def ==(other) def ==(other)
other = Tap.fetch(other) if other.is_a?(String) other = Tap.fetch(other) if other.is_a?(String)
self.class == other.class && name == other.name other.is_a?(self.class) && name == other.name
end end
def self.each(&block) def self.each(&block)

View File

@ -312,7 +312,7 @@ RSpec.describe Homebrew::CLI::NamedArgs do
it "raises an error for invalid tap" do it "raises an error for invalid tap" do
taps = described_class.new("homebrew/foo", "barbaz") taps = described_class.new("homebrew/foo", "barbaz")
expect { taps.to_taps }.to raise_error(RuntimeError, /Invalid tap name/) expect { taps.to_taps }.to raise_error(ArgumentError, /Invalid tap name/)
end end
end end
@ -333,7 +333,7 @@ RSpec.describe Homebrew::CLI::NamedArgs do
it "raises an error for invalid tap" do it "raises an error for invalid tap" do
taps = described_class.new("homebrew/foo", "barbaz") taps = described_class.new("homebrew/foo", "barbaz")
expect { taps.to_installed_taps }.to raise_error(RuntimeError, /Invalid tap name/) expect { taps.to_installed_taps }.to raise_error(ArgumentError, /Invalid tap name/)
end end
end end
end end

View File

@ -103,15 +103,15 @@ RSpec.describe Tap do
expect do expect do
described_class.fetch("foo") described_class.fetch("foo")
end.to raise_error(/Invalid tap name/) end.to raise_error(ArgumentError, /Invalid tap name/)
expect do expect do
described_class.fetch("homebrew/homebrew/bar") described_class.fetch("homebrew/homebrew/bar")
end.to raise_error(/Invalid tap name/) end.to raise_error(ArgumentError, /Invalid tap name/)
expect do expect do
described_class.fetch("homebrew", "homebrew/baz") described_class.fetch("homebrew", "homebrew/baz")
end.to raise_error(/Invalid tap name/) end.to raise_error(ArgumentError, /Invalid tap name/)
end end
describe "::from_path" do describe "::from_path" do