From f3e0f1c077c778104bc1b56a490f4233569b87a9 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 8 Feb 2023 15:49:03 -0500 Subject: [PATCH] cmd/go/internal/test: rewrite generate tests using the new maps package For #58415. Change-Id: I13c00f28824087e1841a49ec35a3e2a990945137 Reviewed-on: https://go-review.googlesource.com/c/go/+/466695 Auto-Submit: Bryan Mills Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot Run-TryBot: Bryan Mills --- src/cmd/go/internal/test/flagdefs_test.go | 77 +++++++++++-------- src/cmd/go/internal/test/genflags.go | 27 +------ .../test/internal/genflags/testflag.go | 35 +++++++++ src/cmd/go/internal/test/testflag.go | 6 +- 4 files changed, 83 insertions(+), 62 deletions(-) create mode 100644 src/cmd/go/internal/test/internal/genflags/testflag.go diff --git a/src/cmd/go/internal/test/flagdefs_test.go b/src/cmd/go/internal/test/flagdefs_test.go index 1c46d78b1b..5461b2d1a5 100644 --- a/src/cmd/go/internal/test/flagdefs_test.go +++ b/src/cmd/go/internal/test/flagdefs_test.go @@ -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") } } diff --git a/src/cmd/go/internal/test/genflags.go b/src/cmd/go/internal/test/genflags.go index 625f94133a..bb5ceb647b 100644 --- a/src/cmd/go/internal/test/genflags.go +++ b/src/cmd/go/internal/test/genflags.go @@ -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 index 0000000000..712428d86a --- /dev/null +++ b/src/cmd/go/internal/test/internal/genflags/testflag.go @@ -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 +} diff --git a/src/cmd/go/internal/test/testflag.go b/src/cmd/go/internal/test/testflag.go index e69068977d..69c0a2872e 100644 --- a/src/cmd/go/internal/test/testflag.go +++ b/src/cmd/go/internal/test/testflag.go @@ -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, "") + } } } -- 2.50.0