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

Add type signature for Tap::fetch. #16832

Merged
merged 1 commit into from
Mar 6, 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
7 changes: 2 additions & 5 deletions Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
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 @@ -543,9 +542,7 @@ def self.tap_cask_token_type(tapped_token, warn:)
new_token = tap.core_cask_tap? ? token : "#{tap}/#{token}"
type = :rename
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, new_token = Tap.with_cask_token(new_tap_name) || [Tap.fetch(new_tap_name), token]
new_tap.ensure_installed!
new_tapped_token = "#{new_tap}/#{new_token}"

Expand Down
4 changes: 1 addition & 3 deletions Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1151,9 +1151,7 @@ def self.tap_formula_name_type(tapped_name, warn:)
new_name = tap.core_tap? ? name : "#{tap}/#{name}"
type = :rename
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, new_name = Tap.with_formula_name(new_tap_name) || [Tap.fetch(new_tap_name), name]
new_tap.ensure_installed!
new_tapped_name = "#{new_tap}/#{new_name}"

Expand Down
33 changes: 18 additions & 15 deletions Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
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)
Comment on lines -72 to 76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it itself uses fetch, so can only return Tap.

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 @@ -878,7 +881,7 @@ def should_report_analytics?
sig { params(other: T.nilable(T.any(String, Tap))).returns(T::Boolean) }
def ==(other)
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

def self.each(&block)
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/test/cli/named_args_spec.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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