Merge pull request #10955 from reitermarkus/bundle-version-comparison

Fix `BundleVersion` comparison when one part is `nil`.
This commit is contained in:
Markus Reiter 2021-03-28 04:35:38 +02:00 committed by GitHub
commit 3bfa59bd2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -10,6 +10,8 @@ module Homebrew
class BundleVersion class BundleVersion
extend T::Sig extend T::Sig
include Comparable
extend SystemCommand::Mixin extend SystemCommand::Mixin
sig { params(info_plist_path: Pathname).returns(T.nilable(T.attached_class)) } sig { params(info_plist_path: Pathname).returns(T.nilable(T.attached_class)) }
@ -55,10 +57,15 @@ module Homebrew
end end
def <=>(other) def <=>(other)
[version, short_version].map { |v| v&.yield_self(&Version.public_method(:new)) } <=> [version, short_version].map { |v| v&.yield_self(&Version.public_method(:new)) || Version::NULL } <=>
[other.version, other.short_version].map { |v| v&.yield_self(&Version.public_method(:new)) } [other.version, other.short_version].map { |v| v&.yield_self(&Version.public_method(:new)) || Version::NULL }
end end
def ==(other)
instance_of?(other.class) && short_version == other.short_version && version == other.version
end
alias eql? ==
# Create a nicely formatted version (on a best effort basis). # Create a nicely formatted version (on a best effort basis).
sig { returns(String) } sig { returns(String) }
def nice_version def nice_version

View File

@ -26,4 +26,10 @@ describe Homebrew::BundleVersion do
end end
end end
end end
describe "#<=>" do
it "does not fail when a `version` is nil" do
expect(described_class.new("1.06", nil)).to be < described_class.new("1.12", "1.12")
end
end
end end