Fix goMinorVersion on non-final Go releases
authorElliott Sales de Andrade <quantum.analyst@gmail.com>
Wed, 19 Feb 2020 07:46:21 +0000 (02:46 -0500)
committerGitHub <noreply@github.com>
Wed, 19 Feb 2020 07:46:21 +0000 (08:46 +0100)
This should work for alpha/beta/rc releases.

common/hugo/version.go
common/hugo/version_test.go

index 848393f970dd30172b16696a2e5d2d06806e0b05..038537fc04e2d7ad014a443f145d9941f898a732 100644 (file)
@@ -15,7 +15,7 @@ package hugo
 
 import (
        "fmt"
-       "strconv"
+       "io"
 
        "runtime"
        "strings"
@@ -245,7 +245,15 @@ func goMinorVersion(version string) int {
        if strings.HasPrefix(version, "devel") {
                return 9999 // magic
        }
-       i, _ := strconv.Atoi(strings.Split(version, ".")[1])
-       return i
-
+       var major, minor int
+       var trailing string
+       n, err := fmt.Sscanf(version, "go%d.%d%s", &major, &minor, &trailing)
+       if n == 2 && err == io.EOF {
+               // Means there were no trailing characters (i.e., not an alpha/beta)
+               err = nil
+       }
+       if err != nil {
+               return 0
+       }
+       return minor
 }
index e0cd0e6e886082b51643c5a1a5fc199d5a02005b..9e0ebb2952fac0a22519a75df91883304eff0693 100644 (file)
@@ -84,5 +84,6 @@ func TestParseHugoVersion(t *testing.T) {
 func TestGoMinorVersion(t *testing.T) {
        c := qt.New(t)
        c.Assert(goMinorVersion("go1.12.5"), qt.Equals, 12)
+       c.Assert(goMinorVersion("go1.14rc1"), qt.Equals, 14)
        c.Assert(GoMinorVersion() >= 11, qt.Equals, true)
 }