Fix BundleVersion comparison.

This commit is contained in:
Markus Reiter 2023-05-16 14:06:24 +02:00
parent 53de9f38a2
commit a7cc9a8b41
No known key found for this signature in database
GPG Key ID: 245293B51702655B
2 changed files with 40 additions and 8 deletions

View File

@ -62,8 +62,24 @@ module Homebrew
end end
def <=>(other) def <=>(other)
[version, short_version].map { |v| v&.yield_self(&Version.public_method(:new)) || Version::NULL } <=> return super unless instance_of?(other.class)
[other.version, other.short_version].map { |v| v&.yield_self(&Version.public_method(:new)) || Version::NULL }
make_version = ->(v) { v ? Version.new(v) : Version::NULL }
version = self.version.then(&make_version)
other_version = other.version.then(&make_version)
difference = version <=> other_version
# If `version` is equal or cannot be compared, compare `short_version` instead.
if difference.nil? || difference.zero?
short_version = self.short_version.then(&make_version)
other_short_version = other.short_version.then(&make_version)
return short_version <=> other_short_version
end
difference
end end
def ==(other) def ==(other)

View File

@ -3,6 +3,28 @@
require "bundle_version" require "bundle_version"
describe Homebrew::BundleVersion do describe Homebrew::BundleVersion do
describe "#<=>" do
it "compares both the `short_version` and `version`" do
expect(described_class.new("1.2.3", "3000")).to be < described_class.new("1.2.3", "4000")
expect(described_class.new("1.2.3", "4000")).to be <= described_class.new("1.2.3", "4000")
expect(described_class.new("1.2.3", "4000")).to be >= described_class.new("1.2.3", "4000")
expect(described_class.new("1.2.4", "4000")).to be > described_class.new("1.2.3", "4000")
end
it "compares `version` first" do
expect(described_class.new("1.2.4", "3000")).to be < described_class.new("1.2.3", "4000")
end
it "does not fail when `short_version` or `version` is missing" do
expect(described_class.new("1.06", nil)).to be < described_class.new("1.12", "1.12")
expect(described_class.new("1.06", "471")).to be > described_class.new(nil, "311")
expect(described_class.new("1.2.3", nil)).to be < described_class.new("1.2.4", nil)
expect(described_class.new(nil, "1.2.3")).to be < described_class.new(nil, "1.2.4")
expect(described_class.new("1.2.3", nil)).to be < described_class.new(nil, "1.2.3")
expect(described_class.new(nil, "1.2.3")).to be > described_class.new("1.2.3", nil)
end
end
describe "#nice_version" do describe "#nice_version" do
expected_mappings = { expected_mappings = {
["1.2", nil] => "1.2", ["1.2", nil] => "1.2",
@ -25,10 +47,4 @@ 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