Skip to content

Commit

Permalink
Ignore build metadata for sort order
Browse files Browse the repository at this point in the history
Use sort.Stable for repeatable sorts of semantically-equal versions
  • Loading branch information
cjnosal committed Sep 29, 2023
1 parent 5409682 commit 410ce6b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
14 changes: 8 additions & 6 deletions v4/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,31 @@ var (
v1 = MustParse("1.2.2")
v2 = MustParse("1.2.3")
v3 = MustParse("1.2.4")
v4 = MustParse("1.2.4+ignoreme")
)

func testEQ(f comparator) bool {
return f(v1, v1) && !f(v1, v2)
return f(v1, v1) && !f(v1, v2) && f(v3, v4)
}

func testNE(f comparator) bool {
return !f(v1, v1) && f(v1, v2)
return !f(v1, v1) && f(v1, v2) && !f(v3, v4)
}

func testGT(f comparator) bool {
return f(v2, v1) && f(v3, v2) && !f(v1, v2) && !f(v1, v1)
return f(v2, v1) && f(v3, v2) && !f(v1, v2) && !f(v1, v1) && !f(v3, v4)
}

func testGE(f comparator) bool {
return f(v2, v1) && f(v3, v2) && !f(v1, v2)
return f(v2, v1) && f(v3, v2) && !f(v1, v2) && f(v3, v4)
}

func testLT(f comparator) bool {
return f(v1, v2) && f(v2, v3) && !f(v2, v1) && !f(v1, v1)
return f(v1, v2) && f(v2, v3) && !f(v2, v1) && !f(v1, v1) && !f(v3, v4)
}

func testLE(f comparator) bool {
return f(v1, v2) && f(v2, v3) && !f(v2, v1)
return f(v1, v2) && f(v2, v3) && !f(v2, v1) && f(v3, v4)
}

func TestSplitAndTrim(t *testing.T) {
Expand Down Expand Up @@ -385,6 +386,7 @@ func TestParseRange(t *testing.T) {
{"1.2.3", []tv{
{"1.2.2", false},
{"1.2.3", true},
{"1.2.3+ignoreme", true},
{"1.2.4", false},
}},
{"=1.2.3", []tv{
Expand Down
6 changes: 1 addition & 5 deletions v4/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ func (v Version) Compare(o Version) int {
return -1
}

if comp := v.Pre.Compare(o.Pre); comp != 0 {
return comp
}

return v.Build.Compare(o.Build)
return v.Pre.Compare(o.Pre)
}

// IncrementPatch increments the patch version
Expand Down
10 changes: 5 additions & 5 deletions v4/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ var compareTests = []compareTest{
{Version{1, 0, 0, []versionExtension{extstr("rc"), extnum(1)}, nil}, Version{1, 0, 0, nil, nil}, -1},

// Ignore Build metadata
{Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2), extnum(3)}}, Version{1, 0, 0, nil, nil}, 1},
{Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2), extnum(3)}}, Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2)}}, 1},
{Version{1, 0, 0, nil, []versionExtension{extstr("a")}}, Version{1, 0, 0, nil, []versionExtension{extnum(1)}}, 1},
{Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2), extnum(3)}}, Version{1, 0, 0, nil, nil}, 0},
{Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2), extnum(3)}}, Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2)}}, 0},
{Version{1, 0, 0, nil, []versionExtension{extstr("a")}}, Version{1, 0, 0, nil, []versionExtension{extnum(1)}}, 0},
{Version{1, 0, 0, nil, []versionExtension{extstr("a")}}, Version{1, 0, 0, nil, []versionExtension{extstr("a")}}, 0},
{Version{1, 0, 0, nil, []versionExtension{extnum(1)}}, Version{1, 0, 0, nil, []versionExtension{extnum(2)}}, -1},
{Version{1, 0, 0, []versionExtension{extnum(2)}, []versionExtension{extnum(2)}}, Version{1, 0, 0, []versionExtension{extnum(3)}, []versionExtension{extnum(1)}}, -1},
{Version{1, 0, 0, nil, []versionExtension{extnum(1)}}, Version{1, 0, 0, nil, []versionExtension{extnum(2)}}, 0},
{Version{1, 0, 0, []versionExtension{extnum(2)}, []versionExtension{extnum(2)}}, Version{1, 0, 0, []versionExtension{extnum(2)}, []versionExtension{extnum(1)}}, 0},
}

func TestCompare(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion v4/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func (s Versions) Less(i, j int) bool {

// Sort sorts a slice of versions
func Sort(versions []Version) {
sort.Sort(Versions(versions))
sort.Stable(Versions(versions))
}
10 changes: 7 additions & 3 deletions v4/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
func TestSort(t *testing.T) {
v100, _ := Parse("1.0.0")
v010, _ := Parse("0.1.0")
v001b, _ := Parse("0.0.1+b")
v001, _ := Parse("0.0.1")
versions := []Version{v010, v100, v001}
v001a, _ := Parse("0.0.1+a")
versions := []Version{v010, v100, v001b, v001, v001a}
Sort(versions)

correct := []Version{v001, v010, v100}
correct := []Version{v001b, v001, v001a, v010, v100}
if !reflect.DeepEqual(versions, correct) {
t.Fatalf("Sort returned wrong order: %s", versions)
}
Expand All @@ -21,10 +23,12 @@ func TestSort(t *testing.T) {
func BenchmarkSort(b *testing.B) {
v100, _ := Parse("1.0.0")
v010, _ := Parse("0.1.0")
v001b, _ := Parse("0.0.1+b")
v001, _ := Parse("0.0.1")
v001a, _ := Parse("0.0.1+a")
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
Sort([]Version{v010, v100, v001})
Sort([]Version{v010, v100, v001b, v001, v001a})
}
}

0 comments on commit 410ce6b

Please sign in to comment.