diff --git a/Library/Homebrew/cli/named_args.rb b/Library/Homebrew/cli/named_args.rb index 9eadc7f6ec4c6..529ea7e906dd3 100644 --- a/Library/Homebrew/cli/named_args.rb +++ b/Library/Homebrew/cli/named_args.rb @@ -423,7 +423,7 @@ def resolve_kegs(name) keg.tab.tap == requested_tap end - raise NoSuchKegFromTapError.new(requested_formula, requested_tap) if kegs.none? + raise NoSuchKegError.new(requested_formula, tap: requested_tap) if kegs.none? end raise NoSuchKegError, name if kegs.none? diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index 7c55f0172a7d0..7b3158f347e57 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -53,23 +53,14 @@ class NotAKegError < RuntimeError; end # Raised when a keg doesn't exist. class NoSuchKegError < RuntimeError - attr_reader :name - - def initialize(name) - @name = name - super "No such keg: #{HOMEBREW_CELLAR}/#{name}" - end -end - -# Raised when a keg from a specific tap doesn't exist. -class NoSuchKegFromTapError < RuntimeError attr_reader :name, :tap - sig { params(name: String, tap: Tap).void } - def initialize(name, tap) + def initialize(name, tap: nil) @name = name @tap = tap - super "No such keg: #{HOMEBREW_CELLAR}/#{name} from tap #{tap}" + message = "No such keg: #{HOMEBREW_CELLAR}/#{name}" + message += " from tap #{tap}" if tap + super message end end diff --git a/Library/Homebrew/test/cli/named_args_spec.rb b/Library/Homebrew/test/cli/named_args_spec.rb index fd9e9c135fe9c..30bf1e890ced6 100644 --- a/Library/Homebrew/test/cli/named_args_spec.rb +++ b/Library/Homebrew/test/cli/named_args_spec.rb @@ -244,7 +244,7 @@ def setup_unredable_cask(name) it "raises an error if there is no tap match" do stub_formula_loader bar, "other/tap/bar" - expect { described_class.new("other/tap/bar").to_kegs }.to raise_error(NoSuchKegFromTapError) + expect { described_class.new("other/tap/bar").to_kegs }.to raise_error(NoSuchKegError, %r{from tap other/tap}) end end end diff --git a/Library/Homebrew/test/exceptions_spec.rb b/Library/Homebrew/test/exceptions_spec.rb index 86a67332d63a7..1ad4c18ab5cac 100644 --- a/Library/Homebrew/test/exceptions_spec.rb +++ b/Library/Homebrew/test/exceptions_spec.rb @@ -19,18 +19,20 @@ end end - describe NoSuchKegFromTapError do - subject(:error) { described_class.new("foo", tap) } + describe NoSuchKegError do + context "without a tap" do + subject(:error) { described_class.new("foo") } - let(:tap) { instance_double(Tap, to_s: "u/r") } + it(:to_s) { expect(error.to_s).to eq("No such keg: #{HOMEBREW_CELLAR}/foo") } + end - it(:to_s) { expect(error.to_s).to eq("No such keg: #{HOMEBREW_CELLAR}/foo from tap u/r") } - end + context "with a tap" do + subject(:error) { described_class.new("foo", tap:) } - describe NoSuchKegError do - subject(:error) { described_class.new("foo") } + let(:tap) { instance_double(Tap, to_s: "u/r") } - it(:to_s) { expect(error.to_s).to eq("No such keg: #{HOMEBREW_CELLAR}/foo") } + it(:to_s) { expect(error.to_s).to eq("No such keg: #{HOMEBREW_CELLAR}/foo from tap u/r") } + end end describe FormulaValidationError do