Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
699756b7
Commit
699756b7
authored
Sep 26, 2017
by
Alexander Kanevskiy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix version comparison for versions with preRelease components
Improve unit testing, so reverse comparison of versions also works Fixes #53055
parent
b188868f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
11 deletions
+48
-11
version.go
pkg/util/version/version.go
+30
-9
version_test.go
pkg/util/version/version_test.go
+18
-2
No files found.
pkg/util/version/version.go
View file @
699756b7
...
@@ -181,12 +181,11 @@ func (v *Version) String() string {
...
@@ -181,12 +181,11 @@ func (v *Version) String() string {
// compareInternal returns -1 if v is less than other, 1 if it is greater than other, or 0
// compareInternal returns -1 if v is less than other, 1 if it is greater than other, or 0
// if they are equal
// if they are equal
func
(
v
*
Version
)
compareInternal
(
other
*
Version
)
int
{
func
(
v
*
Version
)
compareInternal
(
other
*
Version
)
int
{
for
i
:=
range
v
.
components
{
vLen
:=
len
(
v
.
components
)
oLen
:=
len
(
other
.
components
)
for
i
:=
0
;
i
<
vLen
&&
i
<
oLen
;
i
++
{
switch
{
switch
{
case
i
>=
len
(
other
.
components
)
:
if
v
.
components
[
i
]
!=
0
{
return
1
}
case
other
.
components
[
i
]
<
v
.
components
[
i
]
:
case
other
.
components
[
i
]
<
v
.
components
[
i
]
:
return
1
return
1
case
other
.
components
[
i
]
>
v
.
components
[
i
]
:
case
other
.
components
[
i
]
>
v
.
components
[
i
]
:
...
@@ -194,6 +193,14 @@ func (v *Version) compareInternal(other *Version) int {
...
@@ -194,6 +193,14 @@ func (v *Version) compareInternal(other *Version) int {
}
}
}
}
// If components are common but one has more items and they are not zeros, it is bigger
switch
{
case
oLen
<
vLen
&&
!
onlyZeros
(
v
.
components
[
oLen
:
])
:
return
1
case
oLen
>
vLen
&&
!
onlyZeros
(
other
.
components
[
vLen
:
])
:
return
-
1
}
if
!
v
.
semver
||
!
other
.
semver
{
if
!
v
.
semver
||
!
other
.
semver
{
return
0
return
0
}
}
...
@@ -209,10 +216,7 @@ func (v *Version) compareInternal(other *Version) int {
...
@@ -209,10 +216,7 @@ func (v *Version) compareInternal(other *Version) int {
vPR
:=
strings
.
Split
(
v
.
preRelease
,
"."
)
vPR
:=
strings
.
Split
(
v
.
preRelease
,
"."
)
oPR
:=
strings
.
Split
(
other
.
preRelease
,
"."
)
oPR
:=
strings
.
Split
(
other
.
preRelease
,
"."
)
for
i
:=
range
vPR
{
for
i
:=
0
;
i
<
len
(
vPR
)
&&
i
<
len
(
oPR
);
i
++
{
if
i
>=
len
(
oPR
)
{
return
1
}
vNum
,
err
:=
strconv
.
ParseUint
(
vPR
[
i
],
10
,
0
)
vNum
,
err
:=
strconv
.
ParseUint
(
vPR
[
i
],
10
,
0
)
if
err
==
nil
{
if
err
==
nil
{
oNum
,
err
:=
strconv
.
ParseUint
(
oPR
[
i
],
10
,
0
)
oNum
,
err
:=
strconv
.
ParseUint
(
oPR
[
i
],
10
,
0
)
...
@@ -234,9 +238,26 @@ func (v *Version) compareInternal(other *Version) int {
...
@@ -234,9 +238,26 @@ func (v *Version) compareInternal(other *Version) int {
}
}
}
}
switch
{
case
len
(
oPR
)
<
len
(
vPR
)
:
return
1
case
len
(
oPR
)
>
len
(
vPR
)
:
return
-
1
}
return
0
return
0
}
}
// returns false if array contain any non-zero element
func
onlyZeros
(
array
[]
uint
)
bool
{
for
_
,
num
:=
range
array
{
if
num
!=
0
{
return
false
}
}
return
true
}
// AtLeast tests if a version is at least equal to a given minimum version. If both
// AtLeast tests if a version is at least equal to a given minimum version. If both
// Versions are Semantic Versions, this will use the Semantic Version comparison
// Versions are Semantic Versions, this will use the Semantic Version comparison
// algorithm. Otherwise, it will compare only the numeric components, with non-present
// algorithm. Otherwise, it will compare only the numeric components, with non-present
...
...
pkg/util/version/version_test.go
View file @
699756b7
...
@@ -45,13 +45,24 @@ func testOne(v *Version, item, prev testItem) error {
...
@@ -45,13 +45,24 @@ func testOne(v *Version, item, prev testItem) error {
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"unexpected parse error: %v"
,
err
)
return
fmt
.
Errorf
(
"unexpected parse error: %v"
,
err
)
}
}
rv
,
err
:=
parse
(
prev
.
version
,
v
.
semver
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"unexpected parse error: %v"
,
err
)
}
rcmp
,
err
:=
rv
.
Compare
(
item
.
version
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"unexpected parse error: %v"
,
err
)
}
switch
{
switch
{
case
cmp
==
-
1
:
case
cmp
==
-
1
:
return
fmt
.
Errorf
(
"unexpected ordering %q < %q"
,
item
.
version
,
prev
.
version
)
return
fmt
.
Errorf
(
"unexpected ordering %q < %q"
,
item
.
version
,
prev
.
version
)
case
cmp
==
0
&&
!
item
.
equalsPrev
:
case
cmp
==
0
&&
!
item
.
equalsPrev
:
return
fmt
.
Errorf
(
"unexpected comparison %q == %q"
,
item
.
version
,
item
.
version
)
return
fmt
.
Errorf
(
"unexpected comparison %q == %q"
,
item
.
version
,
prev
.
version
)
case
cmp
==
1
&&
item
.
equalsPrev
:
case
cmp
==
1
&&
item
.
equalsPrev
:
return
fmt
.
Errorf
(
"unexpected comparison %q != %q"
,
item
.
version
,
item
.
version
)
return
fmt
.
Errorf
(
"unexpected comparison %q != %q"
,
item
.
version
,
prev
.
version
)
case
cmp
!=
-
rcmp
:
return
fmt
.
Errorf
(
"unexpected reverse comparison %q <=> %q %v %v %v %v"
,
item
.
version
,
prev
.
version
,
cmp
,
rcmp
,
v
.
Components
(),
rv
.
Components
())
}
}
}
}
...
@@ -76,6 +87,8 @@ func TestSemanticVersions(t *testing.T) {
...
@@ -76,6 +87,8 @@ func TestSemanticVersions(t *testing.T) {
{
version
:
"1.0.0-x.7.z.92"
},
{
version
:
"1.0.0-x.7.z.92"
},
{
version
:
"1.0.0"
},
{
version
:
"1.0.0"
},
{
version
:
"1.0.0+20130313144700"
,
equalsPrev
:
true
},
{
version
:
"1.0.0+20130313144700"
,
equalsPrev
:
true
},
{
version
:
"1.8.0-alpha.3"
},
{
version
:
"1.8.0-alpha.3.673+73326ef01d2d7c"
},
{
version
:
"1.9.0"
},
{
version
:
"1.9.0"
},
{
version
:
"1.10.0"
},
{
version
:
"1.10.0"
},
{
version
:
"1.11.0"
},
{
version
:
"1.11.0"
},
...
@@ -187,6 +200,7 @@ func TestGenericVersions(t *testing.T) {
...
@@ -187,6 +200,7 @@ func TestGenericVersions(t *testing.T) {
{
version
:
"1.2"
,
unparsed
:
"1.2"
},
{
version
:
"1.2"
,
unparsed
:
"1.2"
},
{
version
:
"1.2a.3"
,
unparsed
:
"1.2"
,
equalsPrev
:
true
},
{
version
:
"1.2a.3"
,
unparsed
:
"1.2"
,
equalsPrev
:
true
},
{
version
:
"1.2.3"
,
unparsed
:
"1.2.3"
},
{
version
:
"1.2.3"
,
unparsed
:
"1.2.3"
},
{
version
:
"1.2.3.0"
,
unparsed
:
"1.2.3.0"
,
equalsPrev
:
true
},
{
version
:
"1.2.3a"
,
unparsed
:
"1.2.3"
,
equalsPrev
:
true
},
{
version
:
"1.2.3a"
,
unparsed
:
"1.2.3"
,
equalsPrev
:
true
},
{
version
:
"1.2.3-foo."
,
unparsed
:
"1.2.3"
,
equalsPrev
:
true
},
{
version
:
"1.2.3-foo."
,
unparsed
:
"1.2.3"
,
equalsPrev
:
true
},
{
version
:
"1.2.3-.foo"
,
unparsed
:
"1.2.3"
,
equalsPrev
:
true
},
{
version
:
"1.2.3-.foo"
,
unparsed
:
"1.2.3"
,
equalsPrev
:
true
},
...
@@ -201,8 +215,10 @@ func TestGenericVersions(t *testing.T) {
...
@@ -201,8 +215,10 @@ func TestGenericVersions(t *testing.T) {
{
version
:
"1.2.3.4b3"
,
unparsed
:
"1.2.3.4"
,
equalsPrev
:
true
},
{
version
:
"1.2.3.4b3"
,
unparsed
:
"1.2.3.4"
,
equalsPrev
:
true
},
{
version
:
"1.2.3.4.5"
,
unparsed
:
"1.2.3.4.5"
},
{
version
:
"1.2.3.4.5"
,
unparsed
:
"1.2.3.4.5"
},
{
version
:
"1.9.0"
,
unparsed
:
"1.9.0"
},
{
version
:
"1.9.0"
,
unparsed
:
"1.9.0"
},
{
version
:
"1.9.0.0.0.0.0.0"
,
unparsed
:
"1.9.0.0.0.0.0.0"
,
equalsPrev
:
true
},
{
version
:
"1.10.0"
,
unparsed
:
"1.10.0"
},
{
version
:
"1.10.0"
,
unparsed
:
"1.10.0"
},
{
version
:
"1.11.0"
,
unparsed
:
"1.11.0"
},
{
version
:
"1.11.0"
,
unparsed
:
"1.11.0"
},
{
version
:
"1.11.0.0.5"
,
unparsed
:
"1.11.0.0.5"
},
{
version
:
"2.0.0"
,
unparsed
:
"2.0.0"
},
{
version
:
"2.0.0"
,
unparsed
:
"2.0.0"
},
{
version
:
"2.1.0"
,
unparsed
:
"2.1.0"
},
{
version
:
"2.1.0"
,
unparsed
:
"2.1.0"
},
{
version
:
"2.1.1"
,
unparsed
:
"2.1.1"
},
{
version
:
"2.1.1"
,
unparsed
:
"2.1.1"
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment