Properly handle -DEV suffix when comparing Hugo versions

See #4443
This commit is contained in:
Bjørn Erik Pedersen 2018-02-22 17:16:42 +01:00
parent 0602135fd4
commit 19d9a46f63
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
3 changed files with 45 additions and 22 deletions

View file

@ -14,7 +14,6 @@
package helpers package helpers
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
@ -57,7 +56,7 @@ func (h HugoVersionString) String() string {
// Implements compare.Comparer // Implements compare.Comparer
func (h HugoVersionString) Compare(other interface{}) int { func (h HugoVersionString) Compare(other interface{}) int {
v := MustParseHugoVersion(h.String()) v := MustParseHugoVersion(h.String())
return compareVersions(v.Number, v.PatchLevel, other) return compareVersionsWithSuffix(v.Number, v.PatchLevel, v.Suffix, other)
} }
// Implements compare.Eqer // Implements compare.Eqer
@ -69,16 +68,16 @@ func (h HugoVersionString) Eq(other interface{}) bool {
return s == h.String() return s == h.String()
} }
var versionSuffixes = []string{"-test", "-DEV"}
// ParseHugoVersion parses a version string. // ParseHugoVersion parses a version string.
func ParseHugoVersion(s string) (HugoVersion, error) { func ParseHugoVersion(s string) (HugoVersion, error) {
var vv HugoVersion var vv HugoVersion
if strings.HasSuffix(s, "-test") { for _, suffix := range versionSuffixes {
vv.Suffix = "-test" if strings.HasSuffix(s, suffix) {
s = strings.TrimSuffix(s, "-test") vv.Suffix = suffix
} s = strings.TrimSuffix(s, suffix)
}
if strings.Contains(s, "DEV") {
return vv, errors.New("DEV versions not supported by parse")
} }
v, p := parseVersion(s) v, p := parseVersion(s)
@ -141,39 +140,53 @@ func hugoVersion(version float32, patchVersion int, suffix string) string {
// It returns -1 if the given version is less than, 0 if equal and 1 if greater than // It returns -1 if the given version is less than, 0 if equal and 1 if greater than
// the running version. // the running version.
func CompareVersion(version interface{}) int { func CompareVersion(version interface{}) int {
return compareVersions(CurrentHugoVersion.Number, CurrentHugoVersion.PatchLevel, version) return compareVersionsWithSuffix(CurrentHugoVersion.Number, CurrentHugoVersion.PatchLevel, CurrentHugoVersion.Suffix, version)
} }
func compareVersions(inVersion float32, inPatchVersion int, in interface{}) int { func compareVersions(inVersion float32, inPatchVersion int, in interface{}) int {
return compareVersionsWithSuffix(inVersion, inPatchVersion, "", in)
}
func compareVersionsWithSuffix(inVersion float32, inPatchVersion int, suffix string, in interface{}) int {
var c int
switch d := in.(type) { switch d := in.(type) {
case float64: case float64:
return compareFloatVersions(inVersion, float32(d)) c = compareFloatVersions(inVersion, float32(d))
case float32: case float32:
return compareFloatVersions(inVersion, d) c = compareFloatVersions(inVersion, d)
case int: case int:
return compareFloatVersions(inVersion, float32(d)) c = compareFloatVersions(inVersion, float32(d))
case int32: case int32:
return compareFloatVersions(inVersion, float32(d)) c = compareFloatVersions(inVersion, float32(d))
case int64: case int64:
return compareFloatVersions(inVersion, float32(d)) c = compareFloatVersions(inVersion, float32(d))
default: default:
s, err := cast.ToStringE(in) s, err := cast.ToStringE(in)
if err != nil { if err != nil {
return -1 return -1
} }
v, p := parseVersion(s) v, err := ParseHugoVersion(s)
if err != nil {
if v == inVersion && p == inPatchVersion { return -1
return 0
} }
if v < inVersion || (v == inVersion && p < inPatchVersion) { if v.Number == inVersion && v.PatchLevel == inPatchVersion {
return strings.Compare(suffix, v.Suffix)
}
if v.Number < inVersion || (v.Number == inVersion && v.PatchLevel < inPatchVersion) {
return -1 return -1
} }
return 1 return 1
} }
if c == 0 && suffix != "" {
return 1
}
return c
} }
func parseVersion(s string) (float32, int) { func parseVersion(s string) (float32, int) {

View file

@ -53,12 +53,18 @@ func TestCompareVersions(t *testing.T) {
require.Equal(t, 1, compareVersions(0.20, 0, "0.20.1")) require.Equal(t, 1, compareVersions(0.20, 0, "0.20.1"))
require.Equal(t, 1, compareVersions(0.20, 1, "0.20.2")) require.Equal(t, 1, compareVersions(0.20, 1, "0.20.2"))
require.Equal(t, 1, compareVersions(0.21, 1, "0.22.1")) require.Equal(t, 1, compareVersions(0.21, 1, "0.22.1"))
require.Equal(t, -1, compareVersions(0.22, 0, "0.22-DEV"))
require.Equal(t, 1, compareVersions(0.22, 0, "0.22.1-DEV"))
require.Equal(t, 1, compareVersionsWithSuffix(0.22, 0, "-DEV", "0.22"))
require.Equal(t, -1, compareVersionsWithSuffix(0.22, 1, "-DEV", "0.22"))
require.Equal(t, 0, compareVersionsWithSuffix(0.22, 1, "-DEV", "0.22.1-DEV"))
} }
func TestParseHugoVersion(t *testing.T) { func TestParseHugoVersion(t *testing.T) {
require.Equal(t, "0.25", MustParseHugoVersion("0.25").String()) require.Equal(t, "0.25", MustParseHugoVersion("0.25").String())
require.Equal(t, "0.25.2", MustParseHugoVersion("0.25.2").String()) require.Equal(t, "0.25.2", MustParseHugoVersion("0.25.2").String())
require.Equal(t, "0.25-test", MustParseHugoVersion("0.25-test").String()) require.Equal(t, "0.25-test", MustParseHugoVersion("0.25-test").String())
_, err := ParseHugoVersion("0.25-DEV") require.Equal(t, "0.25-DEV", MustParseHugoVersion("0.25-DEV").String())
require.Error(t, err)
} }

View file

@ -180,6 +180,10 @@ func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b inte
{helpers.MustParseHugoVersion("0.32").Version(), "0.36", -1}, {helpers.MustParseHugoVersion("0.32").Version(), "0.36", -1},
{"0.36", helpers.MustParseHugoVersion("0.32").Version(), 1}, {"0.36", helpers.MustParseHugoVersion("0.32").Version(), 1},
{"0.36", helpers.MustParseHugoVersion("0.36").Version(), 0}, {"0.36", helpers.MustParseHugoVersion("0.36").Version(), 0},
{"0.37", helpers.MustParseHugoVersion("0.37-DEV").Version(), 1},
{"0.37-DEV", helpers.MustParseHugoVersion("0.37").Version(), -1},
{"0.36", helpers.MustParseHugoVersion("0.37-DEV").Version(), -1},
{"0.37-DEV", helpers.MustParseHugoVersion("0.37-DEV").Version(), 0},
} { } {
result := funcUnderTest(test.left, test.right) result := funcUnderTest(test.left, test.right)
success := false success := false