]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use internal/testenv instead of computing canRun and skipExternal ad-hoc
authorBryan C. Mills <bcmills@google.com>
Wed, 28 Oct 2020 14:44:24 +0000 (10:44 -0400)
committerBryan C. Mills <bcmills@google.com>
Wed, 28 Oct 2020 19:37:03 +0000 (19:37 +0000)
Fixes #42223

Change-Id: Icf9bb61d48f0a6c7fd6f74e80e333a4837aa52ab
Reviewed-on: https://go-review.googlesource.com/c/go/+/265781
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/go_test.go
src/cmd/go/script_test.go
src/internal/testenv/testenv.go

index d1bd516a5dd621353b75ecb865237f248a26857d..c7ca73b5b5fee7a8608ad6d73e6a38276e623221 100644 (file)
@@ -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") {
index a31561cd869c9b598b531ef976fbddb4e4b7bca4..d81f299c3ccfc22243100134e5c35fd9360687ba 100644 (file)
@@ -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 {
index dff68869bd26bd5c20f2cc54de0cd6c79229981d..c902b1404f246ac4b4791517e2a9ad2d661be5ff 100644 (file)
@@ -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)
+       }
+}