From 0dcf719a5c66eb694f64d6476165b60ad2d0323d Mon Sep 17 00:00:00 2001 From: Vicken Simonian Date: Wed, 11 Sep 2019 11:35:04 -0700 Subject: [PATCH] Fix comparison bug when comparing non-numeric prereleases Reference: #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. Source: https://semver.org/#spec-item-11 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 round out some additional test cases. --- version.go | 10 ++-------- version_test.go | 4 +++- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/version.go b/version.go index 30454e1..dbd39e3 100644 --- a/version.go +++ b/version.go @@ -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 { diff --git a/version_test.go b/version_test.go index bd3534a..33f4c96 100644 --- a/version_test.go +++ b/version_test.go @@ -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},