Skip to content

Commit

Permalink
Fix comparison bug when comparing non-numeric prereleases
Browse files Browse the repository at this point in the history
Reference: hashicorp#61
This fixes a bug when comparing non-numeric prereleases of different set
sizes. Makes it so that X.Y-alpha is considered smaller than X.Y-alpha.beta
because a larger set of pre-release fields should have a higher
precedence than a smaller set if all of the preceding identifiers are
equal.

When considering prerelease parts that are empty, use the other to
decide regardless if it is numeric or not. If our self part is empty
then the other part must be the larger set. If the other part is empty
then the self part must be the larger set.

Also fix and add a few additional test cases.
  • Loading branch information
Vicken Simonian committed Sep 11, 2019
1 parent 192140e commit 4fb518e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
10 changes: 2 additions & 8 deletions version.go
Expand Up @@ -212,17 +212,11 @@ func comparePart(preSelf string, preOther string) int {

// if a part is empty, we use the other to decide
if preSelf == "" {
if otherNumeric {
return -1
}
return 1
return -1
}

if preOther == "" {
if selfNumeric {
return 1
}
return -1
return 1
}

if selfNumeric && !otherNumeric {
Expand Down
4 changes: 3 additions & 1 deletion version_test.go
Expand Up @@ -190,7 +190,9 @@ func TestComparePreReleases(t *testing.T) {
{"3.0-alpha.3", "3.0-rc.1", -1},
{"3.0-alpha3", "3.0-rc1", -1},
{"3.0-alpha.1", "3.0-alpha.beta", -1},
{"5.4-alpha", "5.4-alpha.beta", 1},
{"5.4-alpha", "5.4-alpha.beta", -1},
{"5.4-beta", "5.4-alpha.beta", 1},
{"5.4-beta.2", "5.4-alpha.beta", 1},
{"v1.2-beta.2", "v1.2-beta.2", 0},
{"v1.2-beta.1", "v1.2-beta.2", -1},
{"v3.2-alpha.1", "v3.2-alpha", 1},
Expand Down

0 comments on commit 4fb518e

Please sign in to comment.