From 16529a4de5c24f62574ba0b7dbc033f5323e7afb Mon Sep 17 00:00:00 2001 From: Misty De Meo Date: Thu, 3 Nov 2016 16:47:18 -0700 Subject: [PATCH] Version: allow coercing non-versions in comparisons These are needed due to the raw string and fixnum comparisons which exist for legacy reasons, for instance compiler version and build comparisons. --- Library/Homebrew/test/test_versions.rb | 5 +++++ Library/Homebrew/version.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/Library/Homebrew/test/test_versions.rb b/Library/Homebrew/test/test_versions.rb index 3e1c97b03a..a6e922178f 100644 --- a/Library/Homebrew/test/test_versions.rb +++ b/Library/Homebrew/test/test_versions.rb @@ -149,6 +149,11 @@ class VersionComparisonTests < Homebrew::TestCase assert_operator version("2.1.0-p194"), :>, nil end + def test_comparing_against_strings + assert_operator version("2.1.0-p194"), :==, "2.1.0-p194" + assert_operator version("1"), :==, 1 + end + def test_comparison_returns_nil_for_non_version v = version("1.0") assert_nil v <=> Object.new diff --git a/Library/Homebrew/version.rb b/Library/Homebrew/version.rb index 56ffc9a642..f5fdb09669 100644 --- a/Library/Homebrew/version.rb +++ b/Library/Homebrew/version.rb @@ -213,6 +213,11 @@ class Version end def <=>(other) + # Needed to retain API compatibility with older string comparisons + # for compiler versions, etc. + other = Version.new(other) if other.is_a? String + # Used by the *_build_version comparisons, which formerly returned Fixnum + other = Version.new(other.to_s) if other.is_a? Integer return 1 if other.nil? return unless other.is_a?(Version)