From 7bc3e5880675ce4aae245f46d193924cff5efdfb Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 5 Jun 2015 11:01:53 -0400 Subject: [PATCH] all: extract "can I exec?" check from tests into internal/testenv Change-Id: I7b54be9d8b50b39e01c6be21f310ae9a10404e9d Reviewed-on: https://go-review.googlesource.com/10753 Reviewed-by: Brad Fitzpatrick Reviewed-by: David Crawshaw Reviewed-by: Ian Lance Taylor --- src/cmd/addr2line/addr2line_test.go | 17 ++--- src/cmd/cover/cover_test.go | 13 +--- src/cmd/go/go_test.go | 48 ++++---------- src/cmd/go/note_test.go | 9 +-- src/cmd/go/vcs_test.go | 11 +--- src/cmd/internal/obj/x86/obj6_test.go | 8 +-- src/cmd/nm/nm_test.go | 11 +--- src/cmd/objdump/objdump_test.go | 11 +--- src/cmd/pack/pack_test.go | 22 +------ src/cmd/vet/vet_test.go | 23 ++----- src/crypto/x509/x509_test.go | 12 +--- src/go/build/deps_test.go | 1 + src/internal/testenv/testenv.go | 90 +++++++++++++++++++++++++++ src/net/http/cgi/matryoshka_test.go | 17 ++--- src/os/exec/exec_test.go | 21 +++---- src/os/os_test.go | 45 +++----------- src/runtime/crash_test.go | 11 +--- src/runtime/pprof/pprof_test.go | 8 +-- src/runtime/pprof/trace_test.go | 2 + src/syscall/exec_unix_test.go | 7 +-- src/syscall/syscall_unix_test.go | 9 ++- 21 files changed, 163 insertions(+), 233 deletions(-) create mode 100644 src/internal/testenv/testenv.go diff --git a/src/cmd/addr2line/addr2line_test.go b/src/cmd/addr2line/addr2line_test.go index c83a883b8c..e8234e2da2 100644 --- a/src/cmd/addr2line/addr2line_test.go +++ b/src/cmd/addr2line/addr2line_test.go @@ -7,6 +7,7 @@ package main import ( "bufio" "bytes" + "internal/testenv" "io/ioutil" "os" "os/exec" @@ -85,22 +86,14 @@ func testAddr2Line(t *testing.T, exepath, addr string) { if !os.SameFile(fi1, fi2) { t.Fatalf("addr2line_test.go and %s are not same file", srcPath) } - if srcLineNo != "94" { - t.Fatalf("line number = %v; want 94", srcLineNo) + if srcLineNo != "95" { + t.Fatalf("line number = %v; want 95", srcLineNo) } } -// This is line 93. The test depends on that. +// This is line 94. The test depends on that. func TestAddr2Line(t *testing.T) { - switch runtime.GOOS { - case "nacl", "android": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveGoBuild(t) syms := loadSyms(t) diff --git a/src/cmd/cover/cover_test.go b/src/cmd/cover/cover_test.go index af7ea443cd..f01f138cd0 100644 --- a/src/cmd/cover/cover_test.go +++ b/src/cmd/cover/cover_test.go @@ -7,11 +7,11 @@ package main_test import ( "bytes" "fmt" + "internal/testenv" "io/ioutil" "os" "os/exec" "path/filepath" - "runtime" "testing" ) @@ -41,15 +41,8 @@ var debug = false // Keeps the rewritten files around if set. // go run ./testdata/main.go ./testdata/test.go // func TestCover(t *testing.T) { - switch runtime.GOOS { - case "nacl": - t.Skipf("skipping; %v/%v no support for forking", runtime.GOOS, runtime.GOARCH) - case "darwin", "android": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping; %v/%v no support for forking", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveGoBuild(t) + // Read in the test file (testTest) and write it, with LINEs specified, to coverInput. file, err := ioutil.ReadFile(testTest) if err != nil { diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index b6d2705309..c36e59586b 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -8,6 +8,7 @@ import ( "bytes" "flag" "fmt" + "internal/testenv" "io/ioutil" "os" "os/exec" @@ -129,19 +130,7 @@ type testgoData struct { // testgo sets up for a test that runs testgo. func testgo(t *testing.T) *testgoData { - if !canRun { - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, no fork", runtime.GOOS, runtime.GOARCH) - } - default: - t.Skip("skipping for unknown reason") - } - } + testenv.MustHaveGoBuild(t) return &testgoData{t: t} } @@ -835,9 +824,8 @@ func TestInternalPackagesOutsideGOROOTAreRespected(t *testing.T) { } func testMove(t *testing.T, vcs, url, base, config string) { - if testing.Short() { - t.Skip("skipping test that uses network in short mode") - } + testenv.MustHaveExternalNetwork(t) + tg := testgo(t) defer tg.cleanup() tg.parallel() @@ -1053,9 +1041,7 @@ func TestInstallToGOBINCommandLinePackage(t *testing.T) { } func TestGodocInstalls(t *testing.T) { - if testing.Short() { - t.Skip("skipping test that uses network in short mode") - } + testenv.MustHaveExternalNetwork(t) // godoc installs into GOBIN tg := testgo(t) @@ -1248,9 +1234,7 @@ func TestMissingGOPATHIsReported(t *testing.T) { // Issue 4186. go get cannot be used to download packages to $GOROOT. // Test that without GOPATH set, go get should fail. func TestWithoutGOPATHGoGetFails(t *testing.T) { - if testing.Short() { - t.Skip("skipping test that uses network in short mode") - } + testenv.MustHaveExternalNetwork(t) tg := testgo(t) defer tg.cleanup() @@ -1263,9 +1247,7 @@ func TestWithoutGOPATHGoGetFails(t *testing.T) { // Test that with GOPATH=$GOROOT, go get should fail. func TestWithGOPATHEqualsGOROOTGoGetFails(t *testing.T) { - if testing.Short() { - t.Skip("skipping test that uses network in short mode") - } + testenv.MustHaveExternalNetwork(t) tg := testgo(t) defer tg.cleanup() @@ -1799,9 +1781,7 @@ func TestGoGenerateRunFlag(t *testing.T) { } func TestGoGetWorksWithVanityWildcards(t *testing.T) { - if testing.Short() { - t.Skip("skipping test that uses network in short mode") - } + testenv.MustHaveExternalNetwork(t) tg := testgo(t) defer tg.cleanup() @@ -1812,9 +1792,7 @@ func TestGoGetWorksWithVanityWildcards(t *testing.T) { } func TestGoVetWithExternalTests(t *testing.T) { - if testing.Short() { - t.Skip("skipping test that uses network in short mode") - } + testenv.MustHaveExternalNetwork(t) tg := testgo(t) defer tg.cleanup() @@ -1827,9 +1805,7 @@ func TestGoVetWithExternalTests(t *testing.T) { } func TestGoVetWithTags(t *testing.T) { - if testing.Short() { - t.Skip("skipping test that uses network in short mode") - } + testenv.MustHaveExternalNetwork(t) tg := testgo(t) defer tg.cleanup() @@ -1843,9 +1819,7 @@ func TestGoVetWithTags(t *testing.T) { // Issue 9767. func TestGoGetRscIoToolstash(t *testing.T) { - if testing.Short() { - t.Skip("skipping test that uses network in short mode") - } + testenv.MustHaveExternalNetwork(t) tg := testgo(t) defer tg.cleanup() diff --git a/src/cmd/go/note_test.go b/src/cmd/go/note_test.go index f2390cbae5..ae5514188d 100644 --- a/src/cmd/go/note_test.go +++ b/src/cmd/go/note_test.go @@ -13,14 +13,7 @@ import ( ) func TestNoteReading(t *testing.T) { - // No file system access on these systems. - switch sys := runtime.GOOS + "/" + runtime.GOARCH; sys { - case "darwin/arm", "darwin/arm64", "nacl/386", "nacl/amd64p32", "nacl/arm": - t.Skipf("skipping on %s/%s - no file system", runtime.GOOS, runtime.GOARCH) - } - if runtime.GOOS == "android" { - t.Skipf("skipping; requires go tool") - } + testenv.MustHaveGoBuild(t) // TODO: Replace with new test scaffolding by iant. d, err := ioutil.TempDir("", "go-test-") diff --git a/src/cmd/go/vcs_test.go b/src/cmd/go/vcs_test.go index 14d681ba6a..7c7adbe5e9 100644 --- a/src/cmd/go/vcs_test.go +++ b/src/cmd/go/vcs_test.go @@ -5,20 +5,15 @@ package main import ( - "runtime" + "internal/testenv" "testing" ) // Test that RepoRootForImportPath creates the correct RepoRoot for a given importPath. // TODO(cmang): Add tests for SVN and BZR. func TestRepoRootForImportPath(t *testing.T) { - if testing.Short() { - t.Skip("skipping test to avoid external network") - } - switch runtime.GOOS { - case "nacl", "android": - t.Skipf("no networking available on %s", runtime.GOOS) - } + testenv.MustHaveExternalNetwork(t) + tests := []struct { path string want *repoRoot diff --git a/src/cmd/internal/obj/x86/obj6_test.go b/src/cmd/internal/obj/x86/obj6_test.go index b4526fc9a6..6302107536 100644 --- a/src/cmd/internal/obj/x86/obj6_test.go +++ b/src/cmd/internal/obj/x86/obj6_test.go @@ -5,12 +5,12 @@ import ( "bytes" "fmt" "go/build" + "internal/testenv" "io/ioutil" "os" "os/exec" "path/filepath" "regexp" - "runtime" "strconv" "strings" "testing" @@ -148,10 +148,8 @@ func parseOutput(t *testing.T, td *ParsedTestData, asmout []byte) { } func TestDynlink(t *testing.T) { - iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") - if runtime.GOOS == "nacl" || runtime.GOOS == "android" || iOS { - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } + testenv.MustHaveGoBuild(t) + testdata := parseTestData(t) asmout := asmOutput(t, testdata.input) parseOutput(t, testdata, asmout) diff --git a/src/cmd/nm/nm_test.go b/src/cmd/nm/nm_test.go index 155d103ee7..602a288104 100644 --- a/src/cmd/nm/nm_test.go +++ b/src/cmd/nm/nm_test.go @@ -8,6 +8,7 @@ import ( "bufio" "bytes" "fmt" + "internal/testenv" "io/ioutil" "os" "os/exec" @@ -55,15 +56,7 @@ func checkSymbols(t *testing.T, nmoutput []byte) { } func TestNM(t *testing.T) { - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveGoBuild(t) tmpDir, err := ioutil.TempDir("", "TestNM") if err != nil { diff --git a/src/cmd/objdump/objdump_test.go b/src/cmd/objdump/objdump_test.go index 9bd71495af..54e26552e0 100644 --- a/src/cmd/objdump/objdump_test.go +++ b/src/cmd/objdump/objdump_test.go @@ -5,6 +5,7 @@ package main import ( + "internal/testenv" "io/ioutil" "os" "os/exec" @@ -15,15 +16,7 @@ import ( ) func buildObjdump(t *testing.T) (tmp, exe string) { - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveGoBuild(t) tmp, err := ioutil.TempDir("", "TestObjDump") if err != nil { diff --git a/src/cmd/pack/pack_test.go b/src/cmd/pack/pack_test.go index cd32020501..c305a87b57 100644 --- a/src/cmd/pack/pack_test.go +++ b/src/cmd/pack/pack_test.go @@ -8,12 +8,12 @@ import ( "bufio" "bytes" "fmt" + "internal/testenv" "io" "io/ioutil" "os" "os/exec" "path/filepath" - "runtime" "testing" "time" "unicode/utf8" @@ -198,15 +198,7 @@ func TestExtract(t *testing.T) { // Test that pack-created archives can be understood by the tools. func TestHello(t *testing.T) { - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveGoBuild(t) dir := tmpDir(t) defer os.RemoveAll(dir) @@ -238,15 +230,7 @@ func TestHello(t *testing.T) { // Test that pack works with very long lines in PKGDEF. func TestLargeDefs(t *testing.T) { - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveGoBuild(t) dir := tmpDir(t) defer os.RemoveAll(dir) diff --git a/src/cmd/vet/vet_test.go b/src/cmd/vet/vet_test.go index af374787f0..9aae8dd930 100644 --- a/src/cmd/vet/vet_test.go +++ b/src/cmd/vet/vet_test.go @@ -6,11 +6,11 @@ package main_test import ( "bytes" + "internal/testenv" "os" "os/exec" "path/filepath" "runtime" - "strings" "testing" ) @@ -25,18 +25,12 @@ const ( // rm testvet // func TestVet(t *testing.T) { + testenv.MustHaveGoBuild(t) + switch runtime.GOOS { case "plan9", "windows": // Plan 9 and Windows systems can't be guaranteed to have Perl and so can't run errchk. t.Skipf("skipping test; no Perl on %q", runtime.GOOS) - case "nacl": - t.Skip("skipping test; no command execution on nacl") - case "darwin": - if strings.HasPrefix(runtime.GOARCH, "arm") { - t.Skipf("skipping test; no command execution on darwin/%s", runtime.GOARCH) - } - case "android": - t.Skip("skipping test; no go toolchain available") } // go build @@ -84,16 +78,7 @@ func run(c *exec.Cmd, t *testing.T) bool { // TestTags verifies that the -tags argument controls which files to check. func TestTags(t *testing.T) { - switch runtime.GOOS { - case "nacl": - t.Skip("skipping test; no command execution on nacl") - case "darwin": - if strings.HasPrefix(runtime.GOARCH, "arm") { - t.Skip("skipping test; no command execution on darwin/%s", runtime.GOARCH) - } - case "android": - t.Skip("skipping test; no go toolchain available") - } + testenv.MustHaveGoBuild(t) // go build cmd := exec.Command("go", "build", "-o", binary) diff --git a/src/crypto/x509/x509_test.go b/src/crypto/x509/x509_test.go index 86a8b16cba..d83147bb29 100644 --- a/src/crypto/x509/x509_test.go +++ b/src/crypto/x509/x509_test.go @@ -18,11 +18,11 @@ import ( "encoding/base64" "encoding/hex" "encoding/pem" + "internal/testenv" "math/big" "net" "os/exec" "reflect" - "runtime" "testing" "time" ) @@ -848,15 +848,7 @@ func TestParsePEMCRL(t *testing.T) { } func TestImports(t *testing.T) { - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveGoRun(t) if err := exec.Command("go", "run", "x509_test_import.go").Run(); err != nil { t.Errorf("failed to run x509_test_import.go: %s", err) diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go index 8e985aa05b..187e9d1bb5 100644 --- a/src/go/build/deps_test.go +++ b/src/go/build/deps_test.go @@ -352,6 +352,7 @@ var pkgDeps = map[string][]string{ "internal/syscall/unix": {"runtime", "sync/atomic", "syscall", "unsafe"}, "internal/syscall/windows": {"syscall", "unsafe"}, "internal/syscall/windows/registry": {"errors", "io", "syscall", "unicode/utf16", "unsafe"}, + "internal/testenv": {"runtime", "strings", "testing"}, "internal/trace": {"bufio", "bytes", "fmt", "io", "os", "os/exec", "sort", "strconv", "strings"}, "net/http/cookiejar": {"errors", "fmt", "net", "net/http", "net/url", "sort", "strings", "sync", "time", "unicode/utf8"}, "net/http/internal": {"bufio", "bytes", "errors", "fmt", "io"}, diff --git a/src/internal/testenv/testenv.go b/src/internal/testenv/testenv.go new file mode 100644 index 0000000000..a1de61ff48 --- /dev/null +++ b/src/internal/testenv/testenv.go @@ -0,0 +1,90 @@ +// Copyright 2015 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 testenv provides information about what functionality +// is available in different testing environments run by the Go team. +// +// It is an internal package because these details are specific +// to the Go team's test setup (on build.golang.org) and not +// fundamental to tests in general. +package testenv + +import ( + "runtime" + "strings" + "testing" +) + +// HasGoBuild reports whether the current system can build programs with ``go build'' +// and then run them with os.StartProcess or exec.Command. +func HasGoBuild() bool { + switch runtime.GOOS { + case "android", "nacl": + return false + case "darwin": + if strings.HasPrefix(runtime.GOARCH, "arm") { + return false + } + } + return true +} + +// MustHaveGoBuild checks that the current system can build programs with ``go build'' +// and then run them with os.StartProcess or exec.Command. +// If not, MustHaveGoBuild calls t.Skip with an explanation. +func MustHaveGoBuild(t *testing.T) { + if !HasGoBuild() { + t.Skipf("skipping test: 'go build' not available on %s/%s", runtime.GOOS, runtime.GOARCH) + } +} + +// HasGoRun reports whether the current system can run programs with ``go run.'' +func HasGoRun() bool { + // For now, having go run and having go build are the same. + return HasGoBuild() +} + +// MustHaveGoRun checks that the current system can run programs with ``go run.'' +// If not, MustHaveGoRun calls t.Skip with an explanation. +func MustHaveGoRun(t *testing.T) { + if !HasGoRun() { + t.Skipf("skipping test: 'go run' not available on %s/%s", runtime.GOOS, runtime.GOARCH) + } +} + +// HasExec reports whether the current system can start new processes +// using os.StartProcess or (more commonly) exec.Command. +func HasExec() bool { + switch runtime.GOOS { + case "nacl": + return false + case "darwin": + if strings.HasPrefix(runtime.GOARCH, "arm") { + return false + } + } + return true +} + +// MustHaveExec checks that the current system can start new processes +// using os.StartProcess or (more commonly) exec.Command. +// If not, MustHaveExec calls t.Skip with an explanation. +func MustHaveExec(t *testing.T) { + if !HasExec() { + t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH) + } +} + +// HasExternalNetwork reports whether the current system can use +// external (non-localhost) networks. +func HasExternalNetwork() bool { + return !testing.Short() +} + +// MustHaveExternalNetwork checks that the current system can use +// external (non-localhost) networks. +// If not, MustHaveExternalNetwork calls t.Skip with an explanation. +func MustHaveExternalNetwork(t *testing.T) { + t.Skipf("skipping test: no external network in -short mode") +} diff --git a/src/net/http/cgi/matryoshka_test.go b/src/net/http/cgi/matryoshka_test.go index c89c6d525e..32d59c09a3 100644 --- a/src/net/http/cgi/matryoshka_test.go +++ b/src/net/http/cgi/matryoshka_test.go @@ -12,24 +12,19 @@ import ( "bytes" "errors" "fmt" + "internal/testenv" "io" "net/http" "net/http/httptest" "os" - "runtime" "testing" "time" ) -// iOS cannot fork, so we skip some tests -var iOS = runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") - // This test is a CGI host (testing host.go) that runs its own binary // as a child process testing the other half of CGI (child.go). func TestHostingOurselves(t *testing.T) { - if runtime.GOOS == "nacl" || iOS { - t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) - } + testenv.MustHaveExec(t) h := &Handler{ Path: os.Args[0], @@ -96,9 +91,7 @@ func (w *limitWriter) Write(p []byte) (n int, err error) { // If there's an error copying the child's output to the parent, test // that we kill the child. func TestKillChildAfterCopyError(t *testing.T) { - if runtime.GOOS == "nacl" || iOS { - t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) - } + testenv.MustHaveExec(t) defer func() { testHookStartProcess = nil }() proc := make(chan *os.Process, 1) @@ -143,9 +136,7 @@ func TestKillChildAfterCopyError(t *testing.T) { // Test that a child handler writing only headers works. // golang.org/issue/7196 func TestChildOnlyHeaders(t *testing.T) { - if runtime.GOOS == "nacl" || iOS { - t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) - } + testenv.MustHaveExec(t) h := &Handler{ Path: os.Args[0], diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index db84eabf26..3773963cb0 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -11,6 +11,7 @@ import ( "bufio" "bytes" "fmt" + "internal/testenv" "io" "io/ioutil" "log" @@ -27,13 +28,9 @@ import ( "time" ) -// iOS cannot fork -var iOS = runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") - func helperCommand(t *testing.T, s ...string) *exec.Cmd { - if runtime.GOOS == "nacl" || iOS { - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } + testenv.MustHaveExec(t) + cs := []string{"-test.run=TestHelperProcess", "--"} cs = append(cs, s...) cmd := exec.Command(os.Args[0], cs...) @@ -52,9 +49,7 @@ func TestEcho(t *testing.T) { } func TestCommandRelativeName(t *testing.T) { - if iOS { - t.Skip("skipping on darwin/%s, cannot fork", runtime.GOARCH) - } + testenv.MustHaveExec(t) // Run our own binary as a relative path // (e.g. "_test/exec.test") our parent directory. @@ -427,13 +422,11 @@ func TestExtraFilesFDShuffle(t *testing.T) { } func TestExtraFiles(t *testing.T) { - switch runtime.GOOS { - case "nacl", "windows": + testenv.MustHaveExec(t) + + if runtime.GOOS == "windows" { t.Skipf("skipping test on %q", runtime.GOOS) } - if iOS { - t.Skipf("skipping test on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } // Ensure that file descriptors have not already been leaked into // our environment. diff --git a/src/os/os_test.go b/src/os/os_test.go index b1fc998a90..3c0dfd194d 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -9,6 +9,7 @@ import ( "errors" "flag" "fmt" + "internal/testenv" "io" "io/ioutil" . "os" @@ -794,15 +795,7 @@ func exec(t *testing.T, dir, cmd string, args []string, expect string) { } func TestStartProcess(t *testing.T) { - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveExec(t) var dir, cmd string var args []string @@ -1258,19 +1251,13 @@ func testWindowsHostname(t *testing.T) { func TestHostname(t *testing.T) { // There is no other way to fetch hostname on windows, but via winapi. // On Plan 9 it can be taken from #c/sysname as Hostname() does. - switch runtime.GOOS { - case "android", "nacl", "plan9": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } - case "windows": + if runtime.GOOS == "windows" { testWindowsHostname(t) return } + testenv.MustHaveExec(t) + // Check internal Hostname() against the output of /bin/hostname. // Allow that the internal Hostname returns a Fully Qualified Domain Name // and the /bin/hostname only returns the first component @@ -1529,15 +1516,7 @@ func TestReadAtEOF(t *testing.T) { } func testKillProcess(t *testing.T, processKiller func(p *Process)) { - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveExec(t) // Re-exec the test binary itself to emulate "sleep 1". cmd := osexec.Command(Args[0], "-test.run", "TestSleep") @@ -1574,19 +1553,13 @@ func TestKillStartProcess(t *testing.T) { } func TestGetppid(t *testing.T) { - switch runtime.GOOS { - case "nacl": - t.Skip("skipping on nacl") - case "plan9": + if runtime.GOOS == "plan9" { // TODO: golang.org/issue/8206 t.Skipf("skipping test on plan9; see issue 8206") - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping test on %s/%s, no fork", runtime.GOOS, runtime.GOARCH) - } } + testenv.MustHaveExec(t) + if Getenv("GO_WANT_HELPER_PROCESS") == "1" { fmt.Print(Getppid()) Exit(0) diff --git a/src/runtime/crash_test.go b/src/runtime/crash_test.go index 975defdb60..8efce4da2d 100644 --- a/src/runtime/crash_test.go +++ b/src/runtime/crash_test.go @@ -6,6 +6,7 @@ package runtime_test import ( "fmt" + "internal/testenv" "io/ioutil" "os" "os/exec" @@ -38,15 +39,7 @@ func testEnv(cmd *exec.Cmd) *exec.Cmd { } func executeTest(t *testing.T, templ string, data interface{}, extra ...string) string { - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, no fork", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveGoBuild(t) checkStaleRuntime(t) diff --git a/src/runtime/pprof/pprof_test.go b/src/runtime/pprof/pprof_test.go index 0189f457a7..e9fd1466ee 100644 --- a/src/runtime/pprof/pprof_test.go +++ b/src/runtime/pprof/pprof_test.go @@ -9,6 +9,7 @@ package pprof_test import ( "bytes" "fmt" + "internal/testenv" "math/big" "os" "os/exec" @@ -209,12 +210,7 @@ func testCPUProfile(t *testing.T, need []string, f func()) { // Fork can hang if preempted with signals frequently enough (see issue 5517). // Ensure that we do not do this. func TestCPUProfileWithFork(t *testing.T) { - if runtime.GOOS == "darwin" { - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } - } + testenv.MustHaveExec(t) heap := 1 << 30 if runtime.GOOS == "android" { diff --git a/src/runtime/pprof/trace_test.go b/src/runtime/pprof/trace_test.go index a333a3f83a..ba6eb66948 100644 --- a/src/runtime/pprof/trace_test.go +++ b/src/runtime/pprof/trace_test.go @@ -24,6 +24,8 @@ func skipTraceTestsIfNeeded(t *testing.T) { case "darwin": switch runtime.GOARCH { case "arm", "arm64": + // TODO(rsc): What does this have to do with the trace tests? + // There is no forking here. t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) } } diff --git a/src/syscall/exec_unix_test.go b/src/syscall/exec_unix_test.go index ff8261111e..9bb95c0f39 100644 --- a/src/syscall/exec_unix_test.go +++ b/src/syscall/exec_unix_test.go @@ -7,11 +7,11 @@ package syscall_test import ( + "internal/testenv" "io" "os" "os/exec" "os/signal" - "runtime" "syscall" "testing" "unsafe" @@ -48,9 +48,8 @@ func (c *command) Stop() { } func create(t *testing.T) *command { - if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { - t.Skipf("skipping on %s/%s, cannot fork", runtime.GOOS, runtime.GOARCH) - } + testenv.MustHaveExec(t) + proc := exec.Command("cat") stdin, err := proc.StdinPipe() if err != nil { diff --git a/src/syscall/syscall_unix_test.go b/src/syscall/syscall_unix_test.go index af92013739..6895458165 100644 --- a/src/syscall/syscall_unix_test.go +++ b/src/syscall/syscall_unix_test.go @@ -9,6 +9,7 @@ package syscall_test import ( "flag" "fmt" + "internal/testenv" "io/ioutil" "net" "os" @@ -130,12 +131,10 @@ func TestPassFD(t *testing.T) { case "solaris": // TODO(aram): Figure out why ReadMsgUnix is returning empty message. t.Skip("skipping test on solaris, see issue 7402") - case "darwin": - switch runtime.GOARCH { - case "arm", "arm64": - t.Skipf("skipping test on %d/%s, no fork", runtime.GOOS, runtime.GOARCH) - } } + + testenv.MustHaveExec(t) + if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" { passFDChild() return -- 2.50.0