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 #<MultipleVersionsInstalledError:0x00007fc6009d8870>
/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 `<main>'
```
This commit is contained in:
Misty De Meo 2020-09-09 11:02:43 -07:00
parent 1654de3279
commit c06848c487
No known key found for this signature in database
GPG Key ID: 76CF846A2F674B2C
3 changed files with 16 additions and 4 deletions

View File

@ -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)

View File

@ -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.

View File

@ -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