]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/test: rewrite generate tests using the new maps package
authorBryan C. Mills <bcmills@google.com>
Wed, 8 Feb 2023 20:49:03 +0000 (15:49 -0500)
committerGopher Robot <gobot@golang.org>
Thu, 9 Feb 2023 13:37:58 +0000 (13:37 +0000)
For #58415.

Change-Id: I13c00f28824087e1841a49ec35a3e2a990945137
Reviewed-on: https://go-review.googlesource.com/c/go/+/466695
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>

src/cmd/go/internal/test/flagdefs_test.go
src/cmd/go/internal/test/genflags.go
src/cmd/go/internal/test/internal/genflags/testflag.go [new file with mode: 0644]
src/cmd/go/internal/test/testflag.go

index 1c46d78b1bc1803653758a4eab72830ad7d34822..5461b2d1a5237831c548a32f7b62dfa1907c64b7 100644 (file)
@@ -7,11 +7,9 @@ package test
 import (
        "cmd/go/internal/cfg"
        "cmd/go/internal/test/internal/genflags"
-       "flag"
        "internal/testenv"
+       "maps"
        "os"
-       "reflect"
-       "strings"
        "testing"
 )
 
@@ -20,48 +18,59 @@ func TestMain(m *testing.M) {
        os.Exit(m.Run())
 }
 
-func TestPassFlagToTestIncludesAllTestFlags(t *testing.T) {
-       flag.VisitAll(func(f *flag.Flag) {
-               if !strings.HasPrefix(f.Name, "test.") {
-                       return
-               }
-               name := strings.TrimPrefix(f.Name, "test.")
-               switch name {
-               case "testlogfile", "paniconexit0", "fuzzcachedir", "fuzzworker",
-                       "gocoverdir":
-                       // These are internal flags.
-               default:
-                       if !passFlagToTest[name] {
-                               t.Errorf("passFlagToTest missing entry for %q (flag test.%s)", name, name)
-                               t.Logf("(Run 'go generate cmd/go/internal/test' if it should be added.)")
-                       }
-               }
-       })
+func TestPassFlagToTest(t *testing.T) {
+       wantNames := genflags.ShortTestFlags()
 
-       for name := range passFlagToTest {
-               if flag.Lookup("test."+name) == nil {
-                       t.Errorf("passFlagToTest contains %q, but flag -test.%s does not exist in test binary", name, name)
+       missing := map[string]bool{}
+       for _, name := range wantNames {
+               if !passFlagToTest[name] {
+                       missing[name] = true
                }
+       }
+       if len(missing) > 0 {
+               t.Errorf("passFlagToTest is missing entries: %v", missing)
+       }
 
-               if CmdTest.Flag.Lookup(name) == nil {
-                       t.Errorf("passFlagToTest contains %q, but flag -%s does not exist in 'go test' subcommand", name, name)
-               }
+       extra := maps.Clone(passFlagToTest)
+       for _, name := range wantNames {
+               delete(extra, name)
+       }
+       if len(extra) > 0 {
+               t.Errorf("passFlagToTest contains extra entries: %v", extra)
+       }
+
+       if t.Failed() {
+               t.Logf("To regenerate:\n\tgo generate cmd/go/internal/test")
        }
 }
 
-func TestVetAnalyzersSetIsCorrect(t *testing.T) {
-       vetAns, err := genflags.VetAnalyzers()
+func TestPassAnalyzersToVet(t *testing.T) {
+       testenv.MustHaveGoBuild(t) // runs 'go tool vet -flags'
+
+       wantNames, err := genflags.VetAnalyzers()
        if err != nil {
                t.Fatal(err)
        }
 
-       want := make(map[string]bool)
-       for _, a := range vetAns {
-               want[a] = true
+       missing := map[string]bool{}
+       for _, name := range wantNames {
+               if !passAnalyzersToVet[name] {
+                       missing[name] = true
+               }
+       }
+       if len(missing) > 0 {
+               t.Errorf("passAnalyzersToVet is missing entries: %v", missing)
+       }
+
+       extra := maps.Clone(passAnalyzersToVet)
+       for _, name := range wantNames {
+               delete(extra, name)
+       }
+       if len(extra) > 0 {
+               t.Errorf("passFlagToTest contains extra entries: %v", extra)
        }
 
-       if !reflect.DeepEqual(want, passAnalyzersToVet) {
-               t.Errorf("stale vet analyzers: want %v; got %v", want, passAnalyzersToVet)
-               t.Logf("(Run 'go generate cmd/go/internal/test' to refresh the set of analyzers.)")
+       if t.Failed() {
+               t.Logf("To regenerate:\n\tgo generate cmd/go/internal/test")
        }
 }
index 625f94133a147982902ad06474d61fb7773e9e16..bb5ceb647baf4ce508865f313255ff2d12aba71b 100644 (file)
@@ -8,12 +8,9 @@ package main
 
 import (
        "bytes"
-       "flag"
        "log"
        "os"
        "os/exec"
-       "strings"
-       "testing"
        "text/template"
 
        "cmd/go/internal/test/internal/genflags"
@@ -33,7 +30,7 @@ func regenerate() error {
 
        t := template.Must(template.New("fileTemplate").Parse(fileTemplate))
        tData := map[string][]string{
-               "testFlags":    testFlags(),
+               "testFlags":    genflags.ShortTestFlags(),
                "vetAnalyzers": vetAnalyzers,
        }
        buf := bytes.NewBuffer(nil)
@@ -63,28 +60,6 @@ func regenerate() error {
        return nil
 }
 
-func testFlags() []string {
-       testing.Init()
-
-       var names []string
-       flag.VisitAll(func(f *flag.Flag) {
-               var name string
-               var found bool
-               if name, found = strings.CutPrefix(f.Name, "test."); !found {
-                       return
-               }
-
-               switch name {
-               case "testlogfile", "paniconexit0", "fuzzcachedir", "fuzzworker", "gocoverdir":
-                       // These flags are only for use by cmd/go.
-               default:
-                       names = append(names, name)
-               }
-       })
-
-       return names
-}
-
 const fileTemplate = `// Copyright 2019 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
diff --git a/src/cmd/go/internal/test/internal/genflags/testflag.go b/src/cmd/go/internal/test/internal/genflags/testflag.go
new file mode 100644 (file)
index 0000000..712428d
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package genflags
+
+import (
+       "flag"
+       "strings"
+       "testing"
+)
+
+// ShortTestFlags returns the set of "-test." flag shorthand names that end
+// users may pass to 'go test'.
+func ShortTestFlags() []string {
+       testing.Init()
+
+       var names []string
+       flag.VisitAll(func(f *flag.Flag) {
+               var name string
+               var found bool
+               if name, found = strings.CutPrefix(f.Name, "test."); !found {
+                       return
+               }
+
+               switch name {
+               case "testlogfile", "paniconexit0", "fuzzcachedir", "fuzzworker", "gocoverdir":
+                       // These flags are only for use by cmd/go.
+               default:
+                       names = append(names, name)
+               }
+       })
+
+       return names
+}
index e69068977db5b61e5977a69924c0756cf03c191c..69c0a2872e4ebe9082a42a468ffbb277c201c358 100644 (file)
@@ -67,8 +67,10 @@ func init() {
        cf.Var(&testV, "v", "")
        cf.Var(&testShuffle, "shuffle", "")
 
-       for name := range passFlagToTest {
-               cf.Var(cf.Lookup(name).Value, "test."+name, "")
+       for name, ok := range passFlagToTest {
+               if ok {
+                       cf.Var(cf.Lookup(name).Value, "test."+name, "")
+               }
        }
 }