Skip to content

Commit

Permalink
cmd/untap: fix untapping syntax failure.
Browse files Browse the repository at this point in the history
If an installed cask is invalid on attempting an untap: it will
prevent untapping that cask.

Fix this in two ways: one more specific to `untap` and one more
generally to other commands too:
- specific: only read the actual formulae/casks from the tap we're
  untapping instead of all of those that are installed
- general: rescue more exceptions in `Cask::Caskroom.casks` (like we
  already do for `Formula.installed`
  • Loading branch information
MikeMcQuaid committed Dec 13, 2023
1 parent cd8a9c0 commit beb9799
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
26 changes: 14 additions & 12 deletions Library/Homebrew/cask/caskroom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def self.paths
end
private_class_method :paths

# Return all tokens for installed casks.
sig { returns(T::Array[String]) }
def self.tokens
paths.map(&:basename).map(&:to_s)
end

sig { returns(T::Boolean) }
def self.any_casks_installed?
paths.any?
Expand All @@ -46,18 +52,14 @@ def self.ensure_caskroom_exists

sig { params(config: T.nilable(Config)).returns(T::Array[Cask]) }
def self.casks(config: nil)
paths.sort.map do |path|
token = path.basename.to_s

begin
CaskLoader.load(token, config: config)
rescue TapCaskAmbiguityError
tap_path = CaskLoader.tap_paths(token).first
CaskLoader::FromTapPathLoader.new(tap_path).load(config: config)
rescue CaskUnavailableError
# Don't blow up because of a single unavailable cask.
nil
end
tokens.sort.map do |token|
CaskLoader.load(token, config: config)
rescue TapCaskAmbiguityError
tap_path = CaskLoader.tap_paths(token).first
CaskLoader::FromTapPathLoader.new(tap_path).load(config: config)

Check warning on line 59 in Library/Homebrew/cask/caskroom.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/caskroom.rb#L58-L59

Added lines #L58 - L59 were not covered by tests
rescue
# Don't blow up because of a single unavailable cask.
nil

Check warning on line 62 in Library/Homebrew/cask/caskroom.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/caskroom.rb#L62

Added line #L62 was not covered by tests
end.compact
end
end
Expand Down
38 changes: 34 additions & 4 deletions Library/Homebrew/cmd/untap.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "cli/parser"
Expand All @@ -19,15 +19,45 @@ def untap_args
end
end

sig { void }
def untap
args = untap_args.parse

args.named.to_installed_taps.each do |tap|
odie "Untapping #{tap} is not allowed" if tap.core_tap? && Homebrew::EnvConfig.no_install_from_api?

if Homebrew::EnvConfig.no_install_from_api? || (!tap.core_tap? && tap != "homebrew/cask")
installed_tap_formulae = Formula.installed.select { |formula| formula.tap == tap }
installed_tap_casks = Cask::Caskroom.casks.select { |cask| cask.tap == tap }
if Homebrew::EnvConfig.no_install_from_api? || (!tap.core_tap? && !tap.core_cask_tap?)
installed_formula_names = T.let(nil, T.nilable(T::Set[String]))
installed_tap_formulae = tap.formula_names.map do |formula_name|
# initialise lazily in case there's no formulae in this tap
installed_formula_names ||= Set.new(Formula.installed_formula_names)

Check warning on line 33 in Library/Homebrew/cmd/untap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/untap.rb#L33

Added line #L33 was not covered by tests
next unless installed_formula_names.include?(formula_name)

formula = begin
Formulary.factory("#{tap.name}/#{formula_name}")

Check warning on line 37 in Library/Homebrew/cmd/untap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/untap.rb#L37

Added line #L37 was not covered by tests
rescue
# Don't blow up because of a single unavailable formula.
next

Check warning on line 40 in Library/Homebrew/cmd/untap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/untap.rb#L40

Added line #L40 was not covered by tests
end

formula if formula.any_version_installed?
end.compact

installed_cask_tokens = T.let(nil, T.nilable(T::Set[String]))
installed_tap_casks = tap.cask_tokens.map do |cask_token|
# initialise lazily in case there's no casks in this tap
installed_cask_tokens ||= Set.new(Cask::Caskroom.tokens)

Check warning on line 49 in Library/Homebrew/cmd/untap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/untap.rb#L49

Added line #L49 was not covered by tests
next unless installed_cask_tokens.include?(cask_token)

cask = begin
Cask::CaskLoader.load("#{tap.name}/#{cask_token}")

Check warning on line 53 in Library/Homebrew/cmd/untap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/untap.rb#L53

Added line #L53 was not covered by tests
rescue
# Don't blow up because of a single unavailable cask.
next

Check warning on line 56 in Library/Homebrew/cmd/untap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/untap.rb#L56

Added line #L56 was not covered by tests
end

cask if cask.installed?
end.compact

if installed_tap_formulae.present? || installed_tap_casks.present?
installed_names = (installed_tap_formulae + installed_tap_casks.map(&:token)).join("\n")
Expand Down
7 changes: 7 additions & 0 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,13 @@ def self.racks
end
end

# An array of all currently installed formula names.
# @private
sig { returns(T::Array[String]) }
def self.installed_formula_names
racks.map(&:basename).map(&:to_s)

Check warning on line 1957 in Library/Homebrew/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula.rb#L1957

Added line #L1957 was not covered by tests
end

# An array of all installed {Formula}
# @private
def self.installed
Expand Down

0 comments on commit beb9799

Please sign in to comment.