From c06848c48795904a39fb5a0114b6ddc7b49ccbf4 Mon Sep 17 00:00:00 2001 From: Misty De Meo Date: Wed, 9 Sep 2020 11:02:43 -0700 Subject: [PATCH] Fix printing MultipleVersionsInstalledError details The refactor in 6e8f5d0958247e4b4d629866099ed2836a0e0863 means that the exception no longer exposes the name of the package with multiple versions, and as a result the rescuer is unable to print this information. Because we now have a path in which MultipleVersionsInstalledError doesn't have the name at all, we can't reasonably restore the old behaviour. And since rack resolution happens purely internal to the function that raises the exception, the caller has no way to know what name to use. However, since the exception text gets printed anyway, we can just move this text into the exception itself. Fixes the following error: ``` Error: mpd has multiple installed versions Error: undefined method `name' for # /usr/local/Homebrew/Library/Homebrew/cmd/uninstall.rb:137:in `rescue in uninstall' /usr/local/Homebrew/Library/Homebrew/cmd/uninstall.rb:135:in `uninstall' /usr/local/Homebrew/Library/Homebrew/brew.rb:119:in `
' ``` --- Library/Homebrew/cli/named_args.rb | 5 ++++- Library/Homebrew/cmd/uninstall.rb | 1 - Library/Homebrew/test/exceptions_spec.rb | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/cli/named_args.rb b/Library/Homebrew/cli/named_args.rb index ec3812d445..4f245e9744 100644 --- a/Library/Homebrew/cli/named_args.rb +++ b/Library/Homebrew/cli/named_args.rb @@ -164,7 +164,10 @@ module Homebrew end unless (prefix = f.latest_installed_prefix).directory? - raise MultipleVersionsInstalledError, "#{rack.basename} has multiple installed versions" + raise MultipleVersionsInstalledError, <<~EOS + #{rack.basename} has multiple installed versions + Run `brew uninstall --force #{rack.basename}` to remove all versions. + EOS end Keg.new(prefix) diff --git a/Library/Homebrew/cmd/uninstall.rb b/Library/Homebrew/cmd/uninstall.rb index ee65afb1b8..2cfe6c600b 100644 --- a/Library/Homebrew/cmd/uninstall.rb +++ b/Library/Homebrew/cmd/uninstall.rb @@ -134,7 +134,6 @@ module Homebrew ) rescue MultipleVersionsInstalledError => e ofail e - puts "Run `brew uninstall --force #{e.name}` to remove all versions." ensure # If we delete Cellar/newname, then Cellar/oldname symlink # can become broken and we have to remove it. diff --git a/Library/Homebrew/test/exceptions_spec.rb b/Library/Homebrew/test/exceptions_spec.rb index 536c63baa1..8bb4266ad7 100644 --- a/Library/Homebrew/test/exceptions_spec.rb +++ b/Library/Homebrew/test/exceptions_spec.rb @@ -3,9 +3,19 @@ require "exceptions" describe MultipleVersionsInstalledError do - subject { described_class.new("foo has multiple installed versions") } + subject { + described_class.new <<~EOS + foo has multiple installed versions + Run `brew uninstall --force foo` to remove all versions. + EOS + } - its(:to_s) { is_expected.to eq("foo has multiple installed versions") } + its(:to_s) { + is_expected.to eq <<~EOS + foo has multiple installed versions + Run `brew uninstall --force foo` to remove all versions. + EOS + } end describe NoSuchKegError do