Skip to content

Commit

Permalink
Add type signature for Tap::fetch.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Mar 6, 2024
1 parent d55fa09 commit a592593
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
5 changes: 2 additions & 3 deletions Library/Homebrew/cask/cask_loader.rb
Expand Up @@ -215,8 +215,7 @@ def self.try_new(ref, warn: false)

sig { params(tapped_token: String).void }
def initialize(tapped_token)
user, repo, token = tapped_token.split("/", 3)
tap = Tap.fetch(user, repo)
tap, token = Tap.with_cask_token(tapped_token)
cask = CaskLoader.find_cask_in_tap(token, tap)
super cask
end
Expand Down Expand Up @@ -545,7 +544,7 @@ def self.tap_cask_token_type(tapped_token, warn:)
elsif (new_tap_name = tap.tap_migrations[token].presence)
new_tap_user, new_tap_repo, new_token = new_tap_name.split("/", 3)
new_token ||= token
new_tap = Tap.fetch(new_tap_user, new_tap_repo)
new_tap = Tap.fetch(T.must(new_tap_user), T.must(new_tap_repo))
new_tap.ensure_installed!
new_tapped_token = "#{new_tap}/#{new_token}"

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/formulary.rb
Expand Up @@ -1153,7 +1153,7 @@ def self.tap_formula_name_type(tapped_name, warn:)
elsif (new_tap_name = tap.tap_migrations[name].presence)
new_tap_user, new_tap_repo, new_name = new_tap_name.split("/", 3)
new_name ||= 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))
new_tap.ensure_installed!
new_tapped_name = "#{new_tap}/#{new_name}"

Expand Down
33 changes: 19 additions & 14 deletions Library/Homebrew/tap.rb
Expand Up @@ -39,19 +39,19 @@ class Tap
#{HOMEBREW_TAP_STYLE_EXCEPTIONS_DIR}/*.json
].freeze

def self.fetch(*args)
case args.length
when 1
user, repo = args.first.split("/", 2)
when 2
user = args.first
repo = args.second
sig { params(user: String, repo: String).returns(Tap) }
def self.fetch(user, repo = T.unsafe(nil))
user, repo = user.split("/", 2) if repo.nil?

if [user, repo].any? { |part| part.nil? || part.include?("/") }
raise ArgumentError, "Invalid tap name: '#{[*user, *repo].join("/")}'"
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.
user = user.capitalize if ["homebrew", "linuxbrew"].include? user
user = user.capitalize if ["homebrew", "linuxbrew"].include?(user)
repo = repo.sub(HOMEBREW_OFFICIAL_REPO_PREFIXES_REGEX, "")

return CoreTap.instance if ["Homebrew", "Linuxbrew"].include?(user) && ["core", "homebrew"].include?(repo)
Expand All @@ -63,13 +63,16 @@ def self.fetch(*args)

def self.from_path(path)
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

# @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)
return unless (match = name.match(HOMEBREW_TAP_FORMULA_REGEX))

Expand All @@ -85,7 +88,7 @@ def self.with_formula_name(name)
end

# @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)
return unless (match = token.match(HOMEBREW_TAP_CASK_REGEX))

Expand Down Expand Up @@ -823,7 +826,7 @@ def self.reverse_tap_migrations_renames
new_tap_user, new_tap_repo, new_name = new_name.split("/", 3)
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}"] << old_name
Expand Down Expand Up @@ -877,6 +880,8 @@ def should_report_analytics?

sig { params(other: T.nilable(T.any(String, Tap))).returns(T::Boolean) }
def ==(other)
return false if other.nil?

other = Tap.fetch(other) if other.is_a?(String)
self.class == other.class && name == other.name
end
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/test/cli/named_args_spec.rb
Expand Up @@ -312,7 +312,7 @@ def setup_unredable_cask(name)

it "raises an error for invalid tap" do
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

Expand All @@ -333,7 +333,7 @@ def setup_unredable_cask(name)

it "raises an error for invalid tap" do
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
6 changes: 3 additions & 3 deletions Library/Homebrew/test/tap_spec.rb
Expand Up @@ -103,15 +103,15 @@ def setup_completion(link:)

expect do
described_class.fetch("foo")
end.to raise_error(/Invalid tap name/)
end.to raise_error(ArgumentError, /Invalid tap name/)

expect do
described_class.fetch("homebrew/homebrew/bar")
end.to raise_error(/Invalid tap name/)
end.to raise_error(ArgumentError, /Invalid tap name/)

expect do
described_class.fetch("homebrew", "homebrew/baz")
end.to raise_error(/Invalid tap name/)
end.to raise_error(ArgumentError, /Invalid tap name/)
end

describe "::from_path" do
Expand Down

0 comments on commit a592593

Please sign in to comment.