]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/fix: support go versions with patch release
authorAlex Bozhenko <alexbozhenko@gmail.com>
Wed, 7 Feb 2024 15:19:06 +0000 (15:19 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 22 Feb 2024 05:31:47 +0000 (05:31 +0000)
Support go version with patch release(e.g. 1.21.0)
and release candidates(e.g. 1.21rc1)
when parsing the go version in the fix command
by using new "go/version" package.

Fixes #62584

Change-Id: I0ec16137c7a396c68039d374c770c4021fb54b4e
GitHub-Last-Rev: 76bced5c48334c0937289bce8bcf50f82e3f0b98
GitHub-Pull-Request: golang/go#62586
Reviewed-on: https://go-review.googlesource.com/c/go/+/527342
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Alex Bozhenko <alexbozhenko@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/cmd/fix/buildtag.go
src/cmd/fix/buildtag_test.go
src/cmd/fix/main.go
src/cmd/fix/main_test.go

index 5f4fbfef16f15b99d78ff34d2f2c0a02334760db..6b706c4cb5a7fb890cedc70287ebfc65230ac1ea 100644 (file)
@@ -6,6 +6,7 @@ package main
 
 import (
        "go/ast"
+       "go/version"
        "strings"
 )
 
@@ -13,7 +14,7 @@ func init() {
        register(buildtagFix)
 }
 
-const buildtagGoVersionCutoff = 1_18
+const buildtagGoVersionCutoff = "go1.18"
 
 var buildtagFix = fix{
        name: "buildtag",
@@ -23,7 +24,7 @@ var buildtagFix = fix{
 }
 
 func buildtag(f *ast.File) bool {
-       if goVersion < buildtagGoVersionCutoff {
+       if version.Compare(*goVersion, buildtagGoVersionCutoff) < 0 {
                return false
        }
 
index 1c6efbe9e0326232a59a9548e2cde10fb73e2bc0..e5997043c2115e3543e11043d2bb88d2dd902af8 100644 (file)
@@ -11,7 +11,7 @@ func init() {
 var buildtagTests = []testCase{
        {
                Name:    "buildtag.oldGo",
-               Version: 1_10,
+               Version: "go1.10",
                In: `//go:build yes
 // +build yes
 
@@ -20,7 +20,7 @@ package main
        },
        {
                Name:    "buildtag.new",
-               Version: 1_99,
+               Version: "go1.99",
                In: `//go:build yes
 // +build yes
 
index 0f36fcc3123202c4f23f68c229a3a100f7ccfce0..db67b4ba07a55e343d8da645d65464b37306908c 100644 (file)
@@ -13,13 +13,13 @@ import (
        "go/parser"
        "go/scanner"
        "go/token"
+       "go/version"
        "internal/diff"
        "io"
        "io/fs"
        "os"
        "path/filepath"
        "sort"
-       "strconv"
        "strings"
 )
 
@@ -37,10 +37,8 @@ var forceRewrites = flag.String("force", "",
 var allowed, force map[string]bool
 
 var (
-       doDiff       = flag.Bool("diff", false, "display diffs instead of rewriting files")
-       goVersionStr = flag.String("go", "", "go language version for files")
-
-       goVersion int // 115 for go1.15
+       doDiff    = flag.Bool("diff", false, "display diffs instead of rewriting files")
+       goVersion = flag.String("go", "", "go language version for files")
 )
 
 // enable for debugging fix failures
@@ -68,24 +66,9 @@ func main() {
        flag.Usage = usage
        flag.Parse()
 
-       if *goVersionStr != "" {
-               if !strings.HasPrefix(*goVersionStr, "go") {
-                       report(fmt.Errorf("invalid -go=%s", *goVersionStr))
-                       os.Exit(exitCode)
-               }
-               majorStr := (*goVersionStr)[len("go"):]
-               minorStr := "0"
-               if before, after, found := strings.Cut(majorStr, "."); found {
-                       majorStr, minorStr = before, after
-               }
-               major, err1 := strconv.Atoi(majorStr)
-               minor, err2 := strconv.Atoi(minorStr)
-               if err1 != nil || err2 != nil || major < 0 || major >= 100 || minor < 0 || minor >= 100 {
-                       report(fmt.Errorf("invalid -go=%s", *goVersionStr))
-                       os.Exit(exitCode)
-               }
-
-               goVersion = major*100 + minor
+       if !version.IsValid(*goVersion) {
+               report(fmt.Errorf("invalid -go=%s", *goVersion))
+               os.Exit(exitCode)
        }
 
        sort.Sort(byDate(fixes))
index cafd116cfd6b357f5737d2a77db7bb4bd6a0cb93..8d841b101fe4c0749cf31c47f92d7712d6009831 100644 (file)
@@ -17,7 +17,7 @@ import (
 type testCase struct {
        Name    string
        Fn      func(*ast.File) bool
-       Version int
+       Version string
        In      string
        Out     string
 }
@@ -96,7 +96,7 @@ func TestRewrite(t *testing.T) {
        for _, tt := range testCases {
                tt := tt
                t.Run(tt.Name, func(t *testing.T) {
-                       if tt.Version == 0 {
+                       if tt.Version == "" {
                                if testing.Verbose() {
                                        // Don't run in parallel: cmd/fix sometimes writes directly to stderr,
                                        // and since -v prints which test is currently running we want that
@@ -105,10 +105,10 @@ func TestRewrite(t *testing.T) {
                                        t.Parallel()
                                }
                        } else {
-                               old := goVersion
-                               goVersion = tt.Version
+                               old := *goVersion
+                               *goVersion = tt.Version
                                defer func() {
-                                       goVersion = old
+                                       *goVersion = old
                                }()
                        }