Skip to content

Commit

Permalink
(PE-27792) Fix version_is_less for current versioning scheme
Browse files Browse the repository at this point in the history
The previous version of this function did not handle RC versions or post-release but not-RC-tagged versions vs. RC versions correctly (this is rare, but it crops up occasionally). This will allow us to compare dev builds to dev builds to determine which build is earlier.
  • Loading branch information
Nick Burgan-Illig committed Mar 10, 2020
1 parent cf24436 commit 2808970
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
52 changes: 42 additions & 10 deletions lib/beaker/shared/semvar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ module Semvar
#@param [String] b A version of the form '\d.\d.\d.*'
#@return [Boolean] true if a is less than b, otherwise return false
#
#@note 3.0.0-160-gac44cfb is greater than 3.0.0, and 2.8.2
#@note -rc being less than final builds is not yet implemented.
#@note This has been updated for our current versioning scheme.
#@note 2019.5.0 is greater than 2019.5.0-rc0
#@note 2019.5.0-rc0-1-gabc1234 is greater than 2019.5.0-rc0
#@note 2019.5.0-rc1 is greater than 2019.5.0-rc0-1-gabc1234
#@note 2019.5.0-1-gabc1234 is greater than 2019.5.0
def version_is_less a, b
a_nums = a.split('-')[0].split('.')
b_nums = b.split('-')[0].split('.')
Expand All @@ -24,16 +27,45 @@ def version_is_less a, b
end
end
#checks all dots, they are equal so examine the rest
a_rest = a.split('-', 2)[1]
b_rest = b.split('-', 2)[1]
if a_rest and b_rest and a_rest < b_rest
return false
elsif a_rest and not b_rest
a_rest = a.split('-').drop(1)
a_is_release = a_rest.empty?
a_is_rc = !a_is_release && !!(a_rest[0] =~ /rc\d+/)
b_rest = b.split('-').drop(1)
b_is_release = b_rest.empty?
b_is_rc = !b_is_release && !!(b_rest[0] =~ /rc\d+/)

if a_is_release && b_is_release
# They are equal
return false
elsif not a_rest and b_rest
return true
elsif !a_is_release && !b_is_release
a_next = a_rest.shift
b_next = b_rest.shift
if a_is_rc && b_is_rc
a_rc = a_next.gsub('rc','').to_i
b_rc = b_next.gsub('rc','').to_i
if a_rc < b_rc
return true
elsif a_rc > b_rc
return false
else
a_next = a_rest.shift
b_next = b_rest.shift
if a_next && b_next
return a_next.to_i < b_next.to_i
else
# If a has nothing after -rc#, it is a tagged RC and
# b must be a later build after this tag.
return a_next.nil?
end
end
else
# If one of them is not an rc (and also not a release),
# that one is a post-release build. So if a is the RC, it is less.
return a_is_rc
end
else
return (b_is_release && a_is_rc) || (a_is_release && !b_is_rc)
end
return false
end

# Gets the max semver version from a list of them
Expand Down
24 changes: 22 additions & 2 deletions spec/beaker/shared/semvar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,24 @@ module Shared
expect( subject.version_is_less( '2015.3.0-rc0-8-gf80879a', '2016' ) ).to be === true
end

it 'reports 2015.3.0-rc0-8-gf80879a is not less than 2015.3.0' do
expect( subject.version_is_less( '2015.3.0-rc0-8-gf80879a', '2015.3.0' ) ).to be === false
it 'reports 2015.3.0-rc0-8-gf80879a is less than 2015.3.0' do
expect( subject.version_is_less( '2015.3.0-rc0-8-gf80879a', '2015.3.0' ) ).to be === true
end

it 'reports that 2015.3.0-rc0 is less than 2015.3.0-rc0-8-gf80879a' do
expect( subject.version_is_less( '2015.3.0-rc0', '2015.3.0-rc0-8-gf80879a' ) ).to be === true
end

it 'reports that 2015.3.0-rc2 is less than 2015.3.0-rc10 (not using string comparison)' do
expect( subject.version_is_less( '2015.3.0-rc2', '2015.3.0-rc10' ) ).to be === true
end

it 'reports that 2015.3.0 is less than 2015.3.0-1-gabc1234' do
expect( subject.version_is_less( '2015.3.0', '2015.3.0-1-gabc1234' ) ).to be === true
end

it 'reports that 2015.3.0-rc2 is less than 2015.3.0-1-gabc1234' do
expect( subject.version_is_less( '2015.3.0-rc2', '2015.3.0-1-gabc1234' ) ).to be === true
end

it 'reports 2015.3.0-rc0-8-gf80879a is not less than 3.0.0' do
Expand Down Expand Up @@ -41,6 +57,10 @@ module Shared
it 'reports 2.8 is less than 2.9' do
expect( subject.version_is_less( '2.8', '2.9' ) ).to be === true
end

it 'reports that 2015.3.0 is not less than 2015.3.0' do
expect( subject.version_is_less( '2015.3.0', '2015.3.0' ) ).to be == false
end
end

describe 'max_version' do
Expand Down

0 comments on commit 2808970

Please sign in to comment.