From 07d206f7698f4d45544a2f9b6051ede594ba04cc Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 28 Oct 2020 10:44:24 -0400 Subject: [PATCH] cmd/go: use internal/testenv instead of computing canRun and skipExternal ad-hoc Fixes #42223 Change-Id: Icf9bb61d48f0a6c7fd6f74e80e333a4837aa52ab Reviewed-on: https://go-review.googlesource.com/c/go/+/265781 Trust: Bryan C. Mills Run-TryBot: Bryan C. Mills Reviewed-by: Ian Lance Taylor --- src/cmd/go/go_test.go | 71 +++++---------------------------- src/cmd/go/script_test.go | 4 +- src/internal/testenv/testenv.go | 20 ++++++++++ 3 files changed, 32 insertions(+), 63 deletions(-) diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index d1bd516a5d..c7ca73b5b5 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -35,72 +35,29 @@ import ( ) var ( - canRun = true // whether we can run go or ./testgo canRace = false // whether we can run the race detector canCgo = false // whether we can use cgo canMSan = false // whether we can run the memory sanitizer - - exeSuffix string // ".exe" on Windows - - skipExternal = false // skip external tests ) +var exeSuffix string = func() string { + if runtime.GOOS == "windows" { + return ".exe" + } + return "" +}() + func tooSlow(t *testing.T) { if testing.Short() { // In -short mode; skip test, except run it on the {darwin,linux,windows}/amd64 builders. if testenv.Builder() != "" && runtime.GOARCH == "amd64" && (runtime.GOOS == "linux" || runtime.GOOS == "darwin" || runtime.GOOS == "windows") { return } + t.Helper() t.Skip("skipping test in -short mode") } } -func init() { - switch runtime.GOOS { - case "android", "js": - canRun = false - case "darwin": - // nothing to do - case "ios": - canRun = false - case "linux": - switch runtime.GOARCH { - case "arm": - // many linux/arm machines are too slow to run - // the full set of external tests. - skipExternal = true - case "mips", "mipsle", "mips64", "mips64le": - // Also slow. - skipExternal = true - if testenv.Builder() != "" { - // On the builders, skip the cmd/go - // tests. They're too slow and already - // covered by other ports. There's - // nothing os/arch specific in the - // tests. - canRun = false - } - } - case "freebsd": - switch runtime.GOARCH { - case "arm": - // many freebsd/arm machines are too slow to run - // the full set of external tests. - skipExternal = true - canRun = false - } - case "plan9": - switch runtime.GOARCH { - case "arm": - // many plan9/arm machines are too slow to run - // the full set of external tests. - skipExternal = true - } - case "windows": - exeSuffix = ".exe" - } -} - // testGOROOT is the GOROOT to use when running testgo, a cmd/go binary // build from this process's current GOROOT, but run from a different // (temp) directory. @@ -153,7 +110,7 @@ func TestMain(m *testing.M) { } testGOCACHE = cache.DefaultDir() - if canRun { + if testenv.HasGoBuild() { testBin = filepath.Join(testTmpDir, "testbin") if err := os.Mkdir(testBin, 0777); err != nil { log.Fatal(err) @@ -224,7 +181,7 @@ func TestMain(m *testing.M) { cmd.Stderr = new(strings.Builder) if out, err := cmd.Output(); err != nil { fmt.Fprintf(os.Stderr, "running testgo failed: %v\n%s", err, cmd.Stderr) - canRun = false + os.Exit(2) } else { canCgo, err = strconv.ParseBool(strings.TrimSpace(string(out))) if err != nil { @@ -324,10 +281,7 @@ func skipIfGccgo(t *testing.T, msg string) { func testgo(t *testing.T) *testgoData { t.Helper() testenv.MustHaveGoBuild(t) - - if skipExternal { - t.Skipf("skipping external tests on %s/%s", runtime.GOOS, runtime.GOARCH) - } + testenv.SkipIfShortAndSlow(t) return &testgoData{t: t} } @@ -416,9 +370,6 @@ func (tg *testgoData) goTool() string { // returning exit status. func (tg *testgoData) doRun(args []string) error { tg.t.Helper() - if !canRun { - panic("testgoData.doRun called but canRun false") - } if tg.inParallel { for _, arg := range args { if strings.HasPrefix(arg, "testdata") || strings.HasPrefix(arg, "./testdata") { diff --git a/src/cmd/go/script_test.go b/src/cmd/go/script_test.go index a31561cd86..d81f299c3c 100644 --- a/src/cmd/go/script_test.go +++ b/src/cmd/go/script_test.go @@ -40,9 +40,7 @@ import ( // TestScript runs the tests in testdata/script/*.txt. func TestScript(t *testing.T) { testenv.MustHaveGoBuild(t) - if skipExternal { - t.Skipf("skipping external tests on %s/%s", runtime.GOOS, runtime.GOARCH) - } + testenv.SkipIfShortAndSlow(t) files, err := filepath.Glob("testdata/script/*.txt") if err != nil { diff --git a/src/internal/testenv/testenv.go b/src/internal/testenv/testenv.go index dff68869bd..c902b1404f 100644 --- a/src/internal/testenv/testenv.go +++ b/src/internal/testenv/testenv.go @@ -286,3 +286,23 @@ func CleanCmdEnv(cmd *exec.Cmd) *exec.Cmd { } return cmd } + +// CPUIsSlow reports whether the CPU running the test is suspected to be slow. +func CPUIsSlow() bool { + switch runtime.GOARCH { + case "arm", "mips", "mipsle", "mips64", "mips64le": + return true + } + return false +} + +// SkipIfShortAndSlow skips t if -short is set and the CPU running the test is +// suspected to be slow. +// +// (This is useful for CPU-intensive tests that otherwise complete quickly.) +func SkipIfShortAndSlow(t testing.TB) { + if testing.Short() && CPUIsSlow() { + t.Helper() + t.Skipf("skipping test in -short mode on %s", runtime.GOARCH) + } +} -- 2.48.1