Skip to content

Commit eed0ea3

Browse files
committed
Ignore build metadata for sort order
Use sort.Stable for repeatable sorts of semantically-equal versions Signed-off-by: Conor Nosal <[email protected]>
1 parent 5409682 commit eed0ea3

File tree

5 files changed

+22
-20
lines changed

5 files changed

+22
-20
lines changed

v4/range_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,31 @@ var (
4848
v1 = MustParse("1.2.2")
4949
v2 = MustParse("1.2.3")
5050
v3 = MustParse("1.2.4")
51+
v4 = MustParse("1.2.4+ignoreme")
5152
)
5253

5354
func testEQ(f comparator) bool {
54-
return f(v1, v1) && !f(v1, v2)
55+
return f(v1, v1) && !f(v1, v2) && f(v3, v4)
5556
}
5657

5758
func testNE(f comparator) bool {
58-
return !f(v1, v1) && f(v1, v2)
59+
return !f(v1, v1) && f(v1, v2) && !f(v3, v4)
5960
}
6061

6162
func testGT(f comparator) bool {
62-
return f(v2, v1) && f(v3, v2) && !f(v1, v2) && !f(v1, v1)
63+
return f(v2, v1) && f(v3, v2) && !f(v1, v2) && !f(v1, v1) && !f(v3, v4)
6364
}
6465

6566
func testGE(f comparator) bool {
66-
return f(v2, v1) && f(v3, v2) && !f(v1, v2)
67+
return f(v2, v1) && f(v3, v2) && !f(v1, v2) && f(v3, v4)
6768
}
6869

6970
func testLT(f comparator) bool {
70-
return f(v1, v2) && f(v2, v3) && !f(v2, v1) && !f(v1, v1)
71+
return f(v1, v2) && f(v2, v3) && !f(v2, v1) && !f(v1, v1) && !f(v3, v4)
7172
}
7273

7374
func testLE(f comparator) bool {
74-
return f(v1, v2) && f(v2, v3) && !f(v2, v1)
75+
return f(v1, v2) && f(v2, v3) && !f(v2, v1) && f(v3, v4)
7576
}
7677

7778
func TestSplitAndTrim(t *testing.T) {
@@ -385,6 +386,7 @@ func TestParseRange(t *testing.T) {
385386
{"1.2.3", []tv{
386387
{"1.2.2", false},
387388
{"1.2.3", true},
389+
{"1.2.3+ignoreme", true},
388390
{"1.2.4", false},
389391
}},
390392
{"=1.2.3", []tv{

v4/semver.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ func (v Version) Compare(o Version) int {
148148
return -1
149149
}
150150

151-
if comp := v.Pre.Compare(o.Pre); comp != 0 {
152-
return comp
153-
}
154-
155-
return v.Build.Compare(o.Build)
151+
return v.Pre.Compare(o.Pre)
156152
}
157153

158154
// IncrementPatch increments the patch version

v4/semver_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ var compareTests = []compareTest{
152152
{Version{1, 0, 0, []versionExtension{extstr("rc"), extnum(1)}, nil}, Version{1, 0, 0, nil, nil}, -1},
153153

154154
// Ignore Build metadata
155-
{Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2), extnum(3)}}, Version{1, 0, 0, nil, nil}, 1},
156-
{Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2), extnum(3)}}, Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2)}}, 1},
157-
{Version{1, 0, 0, nil, []versionExtension{extstr("a")}}, Version{1, 0, 0, nil, []versionExtension{extnum(1)}}, 1},
155+
{Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2), extnum(3)}}, Version{1, 0, 0, nil, nil}, 0},
156+
{Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2), extnum(3)}}, Version{1, 0, 0, nil, []versionExtension{extnum(1), extnum(2)}}, 0},
157+
{Version{1, 0, 0, nil, []versionExtension{extstr("a")}}, Version{1, 0, 0, nil, []versionExtension{extnum(1)}}, 0},
158158
{Version{1, 0, 0, nil, []versionExtension{extstr("a")}}, Version{1, 0, 0, nil, []versionExtension{extstr("a")}}, 0},
159-
{Version{1, 0, 0, nil, []versionExtension{extnum(1)}}, Version{1, 0, 0, nil, []versionExtension{extnum(2)}}, -1},
160-
{Version{1, 0, 0, []versionExtension{extnum(2)}, []versionExtension{extnum(2)}}, Version{1, 0, 0, []versionExtension{extnum(3)}, []versionExtension{extnum(1)}}, -1},
159+
{Version{1, 0, 0, nil, []versionExtension{extnum(1)}}, Version{1, 0, 0, nil, []versionExtension{extnum(2)}}, 0},
160+
{Version{1, 0, 0, []versionExtension{extnum(2)}, []versionExtension{extnum(2)}}, Version{1, 0, 0, []versionExtension{extnum(2)}, []versionExtension{extnum(1)}}, 0},
161161
}
162162

163163
func TestCompare(t *testing.T) {

v4/sort.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ func (s Versions) Less(i, j int) bool {
2424

2525
// Sort sorts a slice of versions
2626
func Sort(versions []Version) {
27-
sort.Sort(Versions(versions))
27+
sort.Stable(Versions(versions))
2828
}

v4/sort_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
func TestSort(t *testing.T) {
99
v100, _ := Parse("1.0.0")
1010
v010, _ := Parse("0.1.0")
11+
v001b, _ := Parse("0.0.1+b")
1112
v001, _ := Parse("0.0.1")
12-
versions := []Version{v010, v100, v001}
13+
v001a, _ := Parse("0.0.1+a")
14+
versions := []Version{v010, v100, v001b, v001, v001a}
1315
Sort(versions)
1416

15-
correct := []Version{v001, v010, v100}
17+
correct := []Version{v001b, v001, v001a, v010, v100}
1618
if !reflect.DeepEqual(versions, correct) {
1719
t.Fatalf("Sort returned wrong order: %s", versions)
1820
}
@@ -21,10 +23,12 @@ func TestSort(t *testing.T) {
2123
func BenchmarkSort(b *testing.B) {
2224
v100, _ := Parse("1.0.0")
2325
v010, _ := Parse("0.1.0")
26+
v001b, _ := Parse("0.0.1+b")
2427
v001, _ := Parse("0.0.1")
28+
v001a, _ := Parse("0.0.1+a")
2529
b.ReportAllocs()
2630
b.ResetTimer()
2731
for n := 0; n < b.N; n++ {
28-
Sort([]Version{v010, v100, v001})
32+
Sort([]Version{v010, v100, v001b, v001, v001a})
2933
}
3034
}

0 commit comments

Comments
 (0)