Keg.for: handle non-existent path.

Otherwise `path.realpath` will raise `Errno::ENOENT` rather than the
expected `NotAKegError`.

Fixes https://github.com/Homebrew/brew/issues/9015
This commit is contained in:
Mike McQuaid 2020-11-02 12:56:59 +00:00
parent 4ecd5d9824
commit b54b022f73
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70

View File

@ -183,13 +183,15 @@ class Keg
# if path is a file in a keg then this will return the containing Keg object # if path is a file in a keg then this will return the containing Keg object
def self.for(path) def self.for(path)
path = path.realpath original_path = path
until path.root? if original_path.exist? && (path = original_path.realpath)
return Keg.new(path) if path.parent.parent == HOMEBREW_CELLAR.realpath until path.root?
return Keg.new(path) if path.parent.parent == HOMEBREW_CELLAR.realpath
path = path.parent.realpath # realpath() prevents root? failing path = path.parent.realpath # realpath() prevents root? failing
end
end end
raise NotAKegError, "#{path} is not inside a keg" raise NotAKegError, "#{original_path} is not inside a keg"
end end
def self.all def self.all