Skip to content

Commit 0847482

Browse files
authored
Merge pull request #69 from zeralight/fix-version-sorting
fix version comparison & sorting
2 parents 37279a9 + ba8df0e commit 0847482

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

cmd/remote/version.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ func (version defaultVersion) String() string {
5151
}
5252

5353
func Less(l defaultVersion, r defaultVersion) bool {
54-
if l.Major < r.Major {
55-
return true
56-
} else if l.Minor < r.Minor {
57-
return true
58-
} else if l.Patch < r.Patch {
59-
return true
54+
lseg := [3]int{l.Major, l.Minor, l.Patch}
55+
rseg := [3]int{r.Major, r.Minor, r.Patch}
56+
for i := 0; i < 3; i++ {
57+
if lseg[i] != rseg[i] {
58+
return lseg[i] < rseg[i]
59+
}
6060
}
6161

62-
return false
62+
// If segments are equal, then compare the prerelease info
63+
return l.Tag < r.Tag
6364
}
6465

6566
func IsVersionSmaller(v1 string, v2 string) bool {

cmd/remote/version_test.go

+32-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package remote
22

33
import (
4+
"fmt"
45
"sort"
56
"testing"
7+
8+
"github.com/stretchr/testify/assert"
69
)
710

811
func TestToString(t *testing.T) {
@@ -63,10 +66,13 @@ func TestParseVersions(t *testing.T) {
6366

6467
func TestSorter(t *testing.T) {
6568
values := []string{
69+
"2.0.2",
70+
"2.0.1",
6671
"2",
6772
"1.2",
6873
"1",
6974
"1.2.3",
75+
"1.0.9",
7076
}
7177

7278
var versions []defaultVersion
@@ -77,19 +83,36 @@ func TestSorter(t *testing.T) {
7783
}
7884

7985
sort.Sort(defaultVersionList(versions))
80-
if versions[0].Major != 1 {
81-
t.Log(versions)
82-
}
8386

84-
if versions[1].Minor != 2 {
85-
t.Log(versions)
87+
expected_versions := []defaultVersion{
88+
{1, 0, 0, ""},
89+
{1, 0, 9, ""},
90+
{1, 2, 0, ""},
91+
{1, 2, 3, ""},
92+
{2, 0, 0, ""},
93+
{2, 0, 1, ""},
94+
{2, 0, 2, ""},
8695
}
8796

88-
if versions[2].Patch != 3 {
89-
t.Log(versions)
97+
for i := 0; i < len(expected_versions); i++ {
98+
assert.Equal(t, expected_versions[i], versions[i], fmt.Sprintf("versions[%d] should be %v", i, expected_versions[i]))
9099
}
100+
}
91101

92-
if versions[3].Major != 2 {
93-
t.Log(versions)
102+
func TestLess(t *testing.T) {
103+
test_cases := []struct {
104+
l, r defaultVersion
105+
compare int
106+
}{
107+
{defaultVersion{1, 0, 0, ""}, defaultVersion{1, 0, 1, ""}, -1},
108+
{defaultVersion{1, 0, 0, ""}, defaultVersion{1, 0, 0, ""}, 0},
109+
{defaultVersion{1, 0, 2, ""}, defaultVersion{1, 1, 0, ""}, -1},
110+
{defaultVersion{1, 1, 0, ""}, defaultVersion{1, 0, 1, ""}, 1},
111+
{defaultVersion{1, 0, 0, "rc1"}, defaultVersion{1, 0, 0, "rc2"}, -1},
112+
{defaultVersion{1, 0, 0, "rc2"}, defaultVersion{1, 0, 0, "rc2"}, 0},
113+
}
114+
for _, tc := range test_cases {
115+
assert.Equal(t, tc.compare < 0, Less(tc.l, tc.r), fmt.Sprintf("Less(%v, %v) should be %v", tc.l, tc.r, tc.compare < 0))
116+
assert.Equal(t, tc.compare > 0, Less(tc.r, tc.l), fmt.Sprintf("Less(%v, %v) should be %v", tc.r, tc.l, tc.compare > 0))
94117
}
95118
}

0 commit comments

Comments
 (0)