forked from neucn/ipgw
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(update): correct semver comparison behaviour
- Loading branch information
Showing
3 changed files
with
142 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package utils | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type Semver struct { | ||
Major int | ||
Minor int | ||
Patch int | ||
Prerelease string | ||
} | ||
|
||
// CompareVersion compares strings like "v1.1.1", returns true when versionA > versionB | ||
func CompareVersion(versionA, versionB *Semver) bool { | ||
if versionA == nil { | ||
return false | ||
} | ||
if versionB == nil { | ||
return true | ||
} | ||
// semver: | ||
// v1.0.1 > v1.0.0 | ||
// v1.0.0 > v1.0.0-beta | ||
// v1.0.0-beta.1 > v1.0.0-beta | ||
// v1.0.0-beta > v1.0.0-alpha | ||
if versionA.Major != versionB.Major { | ||
return versionA.Major > versionB.Major | ||
} | ||
|
||
if versionA.Minor != versionB.Minor { | ||
return versionA.Minor > versionB.Minor | ||
} | ||
|
||
if versionA.Patch != versionB.Patch { | ||
return versionA.Patch > versionB.Patch | ||
} | ||
|
||
if versionA.Prerelease != "" && versionB.Prerelease != "" { | ||
return versionA.Prerelease > versionB.Prerelease | ||
} | ||
|
||
if versionA.Prerelease != "" { | ||
// versionB.Prerelease == "" | ||
return false | ||
} | ||
|
||
if versionB.Prerelease != "" { | ||
// versionA.Prerelease == "" | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func ParseVersion(version string) *Semver { | ||
if version == "" { | ||
return nil | ||
} | ||
// remove leading 'v' | ||
version = strings.TrimPrefix(version, "v") | ||
parts := strings.Split(version, "-") | ||
if len(parts) < 1 { | ||
return nil | ||
} | ||
dot := strings.Split(parts[0], ".") | ||
if len(dot) < 3 { | ||
return nil | ||
} | ||
preRelease := "" | ||
if len(parts) > 1 { | ||
preRelease = parts[1] | ||
} | ||
major, _ := strconv.Atoi(dot[0]) | ||
minor, _ := strconv.Atoi(dot[1]) | ||
patch, _ := strconv.Atoi(dot[2]) | ||
return &Semver{ | ||
Major: major, | ||
Minor: minor, | ||
Patch: patch, | ||
Prerelease: preRelease, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestParseVersion(t *testing.T) { | ||
a := assert.New(t) | ||
cases := []struct { | ||
v string | ||
parsed *Semver | ||
}{ | ||
{v: "v1.0.1", parsed: &Semver{ | ||
Major: 1, | ||
Minor: 0, | ||
Patch: 1, | ||
Prerelease: "", | ||
}}, | ||
{v: "v1.0.1-beta", parsed: &Semver{ | ||
Major: 1, | ||
Minor: 0, | ||
Patch: 1, | ||
Prerelease: "beta", | ||
}}, | ||
{v: "0.2.0-alpha.3", parsed: &Semver{ | ||
Major: 0, | ||
Minor: 2, | ||
Patch: 0, | ||
Prerelease: "alpha.3", | ||
}}, | ||
{v: "0.1", parsed: nil}, | ||
} | ||
for _, c := range cases { | ||
a.Equal(ParseVersion(c.v), c.parsed) | ||
} | ||
} | ||
|
||
func TestCompareVersion(t *testing.T) { | ||
a := assert.New(t) | ||
cases := []struct { | ||
a string | ||
b string | ||
expected bool | ||
}{ | ||
{a: "v1.0.0", b: "v0.2.2", expected: true}, | ||
{a: "v1.0.0-alpha", b: "v1.2.2", expected: false}, | ||
{a: "v1.0.0-alpha", b: "v1.0.0", expected: false}, | ||
{a: "v1.0.0", b: "v1.0.0-beta.2", expected: true}, | ||
{a: "v1.0.0-beta", b: "v1.0.0-alpha", expected: true}, | ||
{a: "v1", b: "v1.0.0-alpha", expected: false}, | ||
{a: "1.0.0-beta", b: "", expected: true}, | ||
} | ||
for _, c := range cases { | ||
a.Equal(CompareVersion(ParseVersion(c.a), ParseVersion(c.b)), c.expected) | ||
} | ||
} |