summaryrefslogtreecommitdiffstats
path: root/src/internal/testenv
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
commitf6ad4dcef54c5ce997a4bad5a6d86de229015700 (patch)
tree7cfa4e31ace5c2bd95c72b154d15af494b2bcbef /src/internal/testenv
parentInitial commit. (diff)
downloadgolang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.tar.xz
golang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.zip
Adding upstream version 1.22.1.upstream/1.22.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/internal/testenv')
-rw-r--r--src/internal/testenv/exec.go222
-rw-r--r--src/internal/testenv/noopt.go12
-rw-r--r--src/internal/testenv/opt.go12
-rw-r--r--src/internal/testenv/testenv.go506
-rw-r--r--src/internal/testenv/testenv_notunix.go21
-rw-r--r--src/internal/testenv/testenv_notwin.go46
-rw-r--r--src/internal/testenv/testenv_test.go208
-rw-r--r--src/internal/testenv/testenv_unix.go43
-rw-r--r--src/internal/testenv/testenv_windows.go47
9 files changed, 1117 insertions, 0 deletions
diff --git a/src/internal/testenv/exec.go b/src/internal/testenv/exec.go
new file mode 100644
index 0000000..7f6ad5c
--- /dev/null
+++ b/src/internal/testenv/exec.go
@@ -0,0 +1,222 @@
+// 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
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "os"
+ "os/exec"
+ "runtime"
+ "strconv"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+)
+
+// 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.
+//
+// On some platforms MustHaveExec checks for exec support by re-executing the
+// current executable, which must be a binary built by 'go test'.
+// We intentionally do not provide a HasExec function because of the risk of
+// inappropriate recursion in TestMain functions.
+//
+// To check for exec support outside of a test, just try to exec the command.
+// If exec is not supported, testenv.SyscallIsNotSupported will return true
+// for the resulting error.
+func MustHaveExec(t testing.TB) {
+ tryExecOnce.Do(func() {
+ tryExecErr = tryExec()
+ })
+ if tryExecErr != nil {
+ t.Skipf("skipping test: cannot exec subprocess on %s/%s: %v", runtime.GOOS, runtime.GOARCH, tryExecErr)
+ }
+}
+
+var (
+ tryExecOnce sync.Once
+ tryExecErr error
+)
+
+func tryExec() error {
+ switch runtime.GOOS {
+ case "wasip1", "js", "ios":
+ default:
+ // Assume that exec always works on non-mobile platforms and Android.
+ return nil
+ }
+
+ // ios has an exec syscall but on real iOS devices it might return a
+ // permission error. In an emulated environment (such as a Corellium host)
+ // it might succeed, so if we need to exec we'll just have to try it and
+ // find out.
+ //
+ // As of 2023-04-19 wasip1 and js don't have exec syscalls at all, but we
+ // may as well use the same path so that this branch can be tested without
+ // an ios environment.
+
+ if !testing.Testing() {
+ // This isn't a standard 'go test' binary, so we don't know how to
+ // self-exec in a way that should succeed without side effects.
+ // Just forget it.
+ return errors.New("can't probe for exec support with a non-test executable")
+ }
+
+ // We know that this is a test executable. We should be able to run it with a
+ // no-op flag to check for overall exec support.
+ exe, err := os.Executable()
+ if err != nil {
+ return fmt.Errorf("can't probe for exec support: %w", err)
+ }
+ cmd := exec.Command(exe, "-test.list=^$")
+ cmd.Env = origEnv
+ return cmd.Run()
+}
+
+var execPaths sync.Map // path -> error
+
+// MustHaveExecPath checks that the current system can start the named executable
+// using os.StartProcess or (more commonly) exec.Command.
+// If not, MustHaveExecPath calls t.Skip with an explanation.
+func MustHaveExecPath(t testing.TB, path string) {
+ MustHaveExec(t)
+
+ err, found := execPaths.Load(path)
+ if !found {
+ _, err = exec.LookPath(path)
+ err, _ = execPaths.LoadOrStore(path, err)
+ }
+ if err != nil {
+ t.Skipf("skipping test: %s: %s", path, err)
+ }
+}
+
+// CleanCmdEnv will fill cmd.Env with the environment, excluding certain
+// variables that could modify the behavior of the Go tools such as
+// GODEBUG and GOTRACEBACK.
+//
+// If the caller wants to set cmd.Dir, set it before calling this function,
+// so PWD will be set correctly in the environment.
+func CleanCmdEnv(cmd *exec.Cmd) *exec.Cmd {
+ if cmd.Env != nil {
+ panic("environment already set")
+ }
+ for _, env := range cmd.Environ() {
+ // Exclude GODEBUG from the environment to prevent its output
+ // from breaking tests that are trying to parse other command output.
+ if strings.HasPrefix(env, "GODEBUG=") {
+ continue
+ }
+ // Exclude GOTRACEBACK for the same reason.
+ if strings.HasPrefix(env, "GOTRACEBACK=") {
+ continue
+ }
+ cmd.Env = append(cmd.Env, env)
+ }
+ return cmd
+}
+
+// CommandContext is like exec.CommandContext, but:
+// - skips t if the platform does not support os/exec,
+// - sends SIGQUIT (if supported by the platform) instead of SIGKILL
+// in its Cancel function
+// - if the test has a deadline, adds a Context timeout and WaitDelay
+// for an arbitrary grace period before the test's deadline expires,
+// - fails the test if the command does not complete before the test's deadline, and
+// - sets a Cleanup function that verifies that the test did not leak a subprocess.
+func CommandContext(t testing.TB, ctx context.Context, name string, args ...string) *exec.Cmd {
+ t.Helper()
+ MustHaveExec(t)
+
+ var (
+ cancelCtx context.CancelFunc
+ gracePeriod time.Duration // unlimited unless the test has a deadline (to allow for interactive debugging)
+ )
+
+ if t, ok := t.(interface {
+ testing.TB
+ Deadline() (time.Time, bool)
+ }); ok {
+ if td, ok := t.Deadline(); ok {
+ // Start with a minimum grace period, just long enough to consume the
+ // output of a reasonable program after it terminates.
+ gracePeriod = 100 * time.Millisecond
+ if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
+ scale, err := strconv.Atoi(s)
+ if err != nil {
+ t.Fatalf("invalid GO_TEST_TIMEOUT_SCALE: %v", err)
+ }
+ gracePeriod *= time.Duration(scale)
+ }
+
+ // If time allows, increase the termination grace period to 5% of the
+ // test's remaining time.
+ testTimeout := time.Until(td)
+ if gp := testTimeout / 20; gp > gracePeriod {
+ gracePeriod = gp
+ }
+
+ // When we run commands that execute subprocesses, we want to reserve two
+ // grace periods to clean up: one for the delay between the first
+ // termination signal being sent (via the Cancel callback when the Context
+ // expires) and the process being forcibly terminated (via the WaitDelay
+ // field), and a second one for the delay between the process being
+ // terminated and the test logging its output for debugging.
+ //
+ // (We want to ensure that the test process itself has enough time to
+ // log the output before it is also terminated.)
+ cmdTimeout := testTimeout - 2*gracePeriod
+
+ if cd, ok := ctx.Deadline(); !ok || time.Until(cd) > cmdTimeout {
+ // Either ctx doesn't have a deadline, or its deadline would expire
+ // after (or too close before) the test has already timed out.
+ // Add a shorter timeout so that the test will produce useful output.
+ ctx, cancelCtx = context.WithTimeout(ctx, cmdTimeout)
+ }
+ }
+ }
+
+ cmd := exec.CommandContext(ctx, name, args...)
+ cmd.Cancel = func() error {
+ if cancelCtx != nil && ctx.Err() == context.DeadlineExceeded {
+ // The command timed out due to running too close to the test's deadline.
+ // There is no way the test did that intentionally — it's too close to the
+ // wire! — so mark it as a test failure. That way, if the test expects the
+ // command to fail for some other reason, it doesn't have to distinguish
+ // between that reason and a timeout.
+ t.Errorf("test timed out while running command: %v", cmd)
+ } else {
+ // The command is being terminated due to ctx being canceled, but
+ // apparently not due to an explicit test deadline that we added.
+ // Log that information in case it is useful for diagnosing a failure,
+ // but don't actually fail the test because of it.
+ t.Logf("%v: terminating command: %v", ctx.Err(), cmd)
+ }
+ return cmd.Process.Signal(Sigquit)
+ }
+ cmd.WaitDelay = gracePeriod
+
+ t.Cleanup(func() {
+ if cancelCtx != nil {
+ cancelCtx()
+ }
+ if cmd.Process != nil && cmd.ProcessState == nil {
+ t.Errorf("command was started, but test did not wait for it to complete: %v", cmd)
+ }
+ })
+
+ return cmd
+}
+
+// Command is like exec.Command, but applies the same changes as
+// testenv.CommandContext (with a default Context).
+func Command(t testing.TB, name string, args ...string) *exec.Cmd {
+ t.Helper()
+ return CommandContext(t, context.Background(), name, args...)
+}
diff --git a/src/internal/testenv/noopt.go b/src/internal/testenv/noopt.go
new file mode 100644
index 0000000..ae2a3d0
--- /dev/null
+++ b/src/internal/testenv/noopt.go
@@ -0,0 +1,12 @@
+// Copyright 2022 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.
+
+//go:build noopt
+
+package testenv
+
+// OptimizationOff reports whether optimization is disabled.
+func OptimizationOff() bool {
+ return true
+}
diff --git a/src/internal/testenv/opt.go b/src/internal/testenv/opt.go
new file mode 100644
index 0000000..1bb96f7
--- /dev/null
+++ b/src/internal/testenv/opt.go
@@ -0,0 +1,12 @@
+// Copyright 2022 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.
+
+//go:build !noopt
+
+package testenv
+
+// OptimizationOff reports whether optimization is disabled.
+func OptimizationOff() bool {
+ return false
+}
diff --git a/src/internal/testenv/testenv.go b/src/internal/testenv/testenv.go
new file mode 100644
index 0000000..5c80137
--- /dev/null
+++ b/src/internal/testenv/testenv.go
@@ -0,0 +1,506 @@
+// 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 (
+ "bytes"
+ "errors"
+ "flag"
+ "fmt"
+ "internal/cfg"
+ "internal/platform"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "runtime"
+ "strconv"
+ "strings"
+ "sync"
+ "testing"
+)
+
+// Save the original environment during init for use in checks. A test
+// binary may modify its environment before calling HasExec to change its
+// behavior (such as mimicking a command-line tool), and that modified
+// environment might cause environment checks to behave erratically.
+var origEnv = os.Environ()
+
+// Builder reports the name of the builder running this test
+// (for example, "linux-amd64" or "windows-386-gce").
+// If the test is not running on the build infrastructure,
+// Builder returns the empty string.
+func Builder() string {
+ return os.Getenv("GO_BUILDER_NAME")
+}
+
+// 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 {
+ if os.Getenv("GO_GCFLAGS") != "" {
+ // It's too much work to require every caller of the go command
+ // to pass along "-gcflags="+os.Getenv("GO_GCFLAGS").
+ // For now, if $GO_GCFLAGS is set, report that we simply can't
+ // run go build.
+ return false
+ }
+
+ goBuildOnce.Do(func() {
+ // To run 'go build', we need to be able to exec a 'go' command.
+ // We somewhat arbitrarily choose to exec 'go tool -n compile' because that
+ // also confirms that cmd/go can find the compiler. (Before CL 472096,
+ // we sometimes ended up with cmd/go installed in the test environment
+ // without a cmd/compile it could use to actually build things.)
+ cmd := exec.Command("go", "tool", "-n", "compile")
+ cmd.Env = origEnv
+ out, err := cmd.Output()
+ if err != nil {
+ goBuildErr = fmt.Errorf("%v: %w", cmd, err)
+ return
+ }
+ out = bytes.TrimSpace(out)
+ if len(out) == 0 {
+ goBuildErr = fmt.Errorf("%v: no tool reported", cmd)
+ return
+ }
+ if _, err := exec.LookPath(string(out)); err != nil {
+ goBuildErr = err
+ return
+ }
+
+ if platform.MustLinkExternal(runtime.GOOS, runtime.GOARCH, false) {
+ // We can assume that we always have a complete Go toolchain available.
+ // However, this platform requires a C linker to build even pure Go
+ // programs, including tests. Do we have one in the test environment?
+ // (On Android, for example, the device running the test might not have a
+ // C toolchain installed.)
+ //
+ // If CC is set explicitly, assume that we do. Otherwise, use 'go env CC'
+ // to determine which toolchain it would use by default.
+ if os.Getenv("CC") == "" {
+ cmd := exec.Command("go", "env", "CC")
+ cmd.Env = origEnv
+ out, err := cmd.Output()
+ if err != nil {
+ goBuildErr = fmt.Errorf("%v: %w", cmd, err)
+ return
+ }
+ out = bytes.TrimSpace(out)
+ if len(out) == 0 {
+ goBuildErr = fmt.Errorf("%v: no CC reported", cmd)
+ return
+ }
+ _, goBuildErr = exec.LookPath(string(out))
+ }
+ }
+ })
+
+ return goBuildErr == nil
+}
+
+var (
+ goBuildOnce sync.Once
+ goBuildErr error
+)
+
+// 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.TB) {
+ if os.Getenv("GO_GCFLAGS") != "" {
+ t.Helper()
+ t.Skipf("skipping test: 'go build' not compatible with setting $GO_GCFLAGS")
+ }
+ if !HasGoBuild() {
+ t.Helper()
+ t.Skipf("skipping test: 'go build' unavailable: %v", goBuildErr)
+ }
+}
+
+// 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.TB) {
+ if !HasGoRun() {
+ t.Skipf("skipping test: 'go run' not available on %s/%s", runtime.GOOS, runtime.GOARCH)
+ }
+}
+
+// HasParallelism reports whether the current system can execute multiple
+// threads in parallel.
+// There is a copy of this function in cmd/dist/test.go.
+func HasParallelism() bool {
+ switch runtime.GOOS {
+ case "js", "wasip1":
+ return false
+ }
+ return true
+}
+
+// MustHaveParallelism checks that the current system can execute multiple
+// threads in parallel. If not, MustHaveParallelism calls t.Skip with an explanation.
+func MustHaveParallelism(t testing.TB) {
+ if !HasParallelism() {
+ t.Skipf("skipping test: no parallelism available on %s/%s", runtime.GOOS, runtime.GOARCH)
+ }
+}
+
+// GoToolPath reports the path to the Go tool.
+// It is a convenience wrapper around GoTool.
+// If the tool is unavailable GoToolPath calls t.Skip.
+// If the tool should be available and isn't, GoToolPath calls t.Fatal.
+func GoToolPath(t testing.TB) string {
+ MustHaveGoBuild(t)
+ path, err := GoTool()
+ if err != nil {
+ t.Fatal(err)
+ }
+ // Add all environment variables that affect the Go command to test metadata.
+ // Cached test results will be invalidate when these variables change.
+ // See golang.org/issue/32285.
+ for _, envVar := range strings.Fields(cfg.KnownEnv) {
+ os.Getenv(envVar)
+ }
+ return path
+}
+
+var (
+ gorootOnce sync.Once
+ gorootPath string
+ gorootErr error
+)
+
+func findGOROOT() (string, error) {
+ gorootOnce.Do(func() {
+ gorootPath = runtime.GOROOT()
+ if gorootPath != "" {
+ // If runtime.GOROOT() is non-empty, assume that it is valid.
+ //
+ // (It might not be: for example, the user may have explicitly set GOROOT
+ // to the wrong directory, or explicitly set GOROOT_FINAL but not GOROOT
+ // and hasn't moved the tree to GOROOT_FINAL yet. But those cases are
+ // rare, and if that happens the user can fix what they broke.)
+ return
+ }
+
+ // runtime.GOROOT doesn't know where GOROOT is (perhaps because the test
+ // binary was built with -trimpath, or perhaps because GOROOT_FINAL was set
+ // without GOROOT and the tree hasn't been moved there yet).
+ //
+ // Since this is internal/testenv, we can cheat and assume that the caller
+ // is a test of some package in a subdirectory of GOROOT/src. ('go test'
+ // runs the test in the directory containing the packaged under test.) That
+ // means that if we start walking up the tree, we should eventually find
+ // GOROOT/src/go.mod, and we can report the parent directory of that.
+ //
+ // Notably, this works even if we can't run 'go env GOROOT' as a
+ // subprocess.
+
+ cwd, err := os.Getwd()
+ if err != nil {
+ gorootErr = fmt.Errorf("finding GOROOT: %w", err)
+ return
+ }
+
+ dir := cwd
+ for {
+ parent := filepath.Dir(dir)
+ if parent == dir {
+ // dir is either "." or only a volume name.
+ gorootErr = fmt.Errorf("failed to locate GOROOT/src in any parent directory")
+ return
+ }
+
+ if base := filepath.Base(dir); base != "src" {
+ dir = parent
+ continue // dir cannot be GOROOT/src if it doesn't end in "src".
+ }
+
+ b, err := os.ReadFile(filepath.Join(dir, "go.mod"))
+ if err != nil {
+ if os.IsNotExist(err) {
+ dir = parent
+ continue
+ }
+ gorootErr = fmt.Errorf("finding GOROOT: %w", err)
+ return
+ }
+ goMod := string(b)
+
+ for goMod != "" {
+ var line string
+ line, goMod, _ = strings.Cut(goMod, "\n")
+ fields := strings.Fields(line)
+ if len(fields) >= 2 && fields[0] == "module" && fields[1] == "std" {
+ // Found "module std", which is the module declaration in GOROOT/src!
+ gorootPath = parent
+ return
+ }
+ }
+ }
+ })
+
+ return gorootPath, gorootErr
+}
+
+// GOROOT reports the path to the directory containing the root of the Go
+// project source tree. This is normally equivalent to runtime.GOROOT, but
+// works even if the test binary was built with -trimpath and cannot exec
+// 'go env GOROOT'.
+//
+// If GOROOT cannot be found, GOROOT skips t if t is non-nil,
+// or panics otherwise.
+func GOROOT(t testing.TB) string {
+ path, err := findGOROOT()
+ if err != nil {
+ if t == nil {
+ panic(err)
+ }
+ t.Helper()
+ t.Skip(err)
+ }
+ return path
+}
+
+// GoTool reports the path to the Go tool.
+func GoTool() (string, error) {
+ if !HasGoBuild() {
+ return "", errors.New("platform cannot run go tool")
+ }
+ goToolOnce.Do(func() {
+ goToolPath, goToolErr = exec.LookPath("go")
+ })
+ return goToolPath, goToolErr
+}
+
+var (
+ goToolOnce sync.Once
+ goToolPath string
+ goToolErr error
+)
+
+// HasSrc reports whether the entire source tree is available under GOROOT.
+func HasSrc() bool {
+ switch runtime.GOOS {
+ case "ios":
+ return false
+ }
+ return true
+}
+
+// HasExternalNetwork reports whether the current system can use
+// external (non-localhost) networks.
+func HasExternalNetwork() bool {
+ return !testing.Short() && runtime.GOOS != "js" && runtime.GOOS != "wasip1"
+}
+
+// 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.TB) {
+ if runtime.GOOS == "js" || runtime.GOOS == "wasip1" {
+ t.Helper()
+ t.Skipf("skipping test: no external network on %s", runtime.GOOS)
+ }
+ if testing.Short() {
+ t.Helper()
+ t.Skipf("skipping test: no external network in -short mode")
+ }
+}
+
+// HasCGO reports whether the current system can use cgo.
+func HasCGO() bool {
+ hasCgoOnce.Do(func() {
+ goTool, err := GoTool()
+ if err != nil {
+ return
+ }
+ cmd := exec.Command(goTool, "env", "CGO_ENABLED")
+ cmd.Env = origEnv
+ out, err := cmd.Output()
+ if err != nil {
+ panic(fmt.Sprintf("%v: %v", cmd, out))
+ }
+ hasCgo, err = strconv.ParseBool(string(bytes.TrimSpace(out)))
+ if err != nil {
+ panic(fmt.Sprintf("%v: non-boolean output %q", cmd, out))
+ }
+ })
+ return hasCgo
+}
+
+var (
+ hasCgoOnce sync.Once
+ hasCgo bool
+)
+
+// MustHaveCGO calls t.Skip if cgo is not available.
+func MustHaveCGO(t testing.TB) {
+ if !HasCGO() {
+ t.Skipf("skipping test: no cgo")
+ }
+}
+
+// CanInternalLink reports whether the current system can link programs with
+// internal linking.
+func CanInternalLink(withCgo bool) bool {
+ return !platform.MustLinkExternal(runtime.GOOS, runtime.GOARCH, withCgo)
+}
+
+// MustInternalLink checks that the current system can link programs with internal
+// linking.
+// If not, MustInternalLink calls t.Skip with an explanation.
+func MustInternalLink(t testing.TB, withCgo bool) {
+ if !CanInternalLink(withCgo) {
+ if withCgo && CanInternalLink(false) {
+ t.Skipf("skipping test: internal linking on %s/%s is not supported with cgo", runtime.GOOS, runtime.GOARCH)
+ }
+ t.Skipf("skipping test: internal linking on %s/%s is not supported", runtime.GOOS, runtime.GOARCH)
+ }
+}
+
+// MustHaveBuildMode reports whether the current system can build programs in
+// the given build mode.
+// If not, MustHaveBuildMode calls t.Skip with an explanation.
+func MustHaveBuildMode(t testing.TB, buildmode string) {
+ if !platform.BuildModeSupported(runtime.Compiler, buildmode, runtime.GOOS, runtime.GOARCH) {
+ t.Skipf("skipping test: build mode %s on %s/%s is not supported by the %s compiler", buildmode, runtime.GOOS, runtime.GOARCH, runtime.Compiler)
+ }
+}
+
+// HasSymlink reports whether the current system can use os.Symlink.
+func HasSymlink() bool {
+ ok, _ := hasSymlink()
+ return ok
+}
+
+// MustHaveSymlink reports whether the current system can use os.Symlink.
+// If not, MustHaveSymlink calls t.Skip with an explanation.
+func MustHaveSymlink(t testing.TB) {
+ ok, reason := hasSymlink()
+ if !ok {
+ t.Skipf("skipping test: cannot make symlinks on %s/%s: %s", runtime.GOOS, runtime.GOARCH, reason)
+ }
+}
+
+// HasLink reports whether the current system can use os.Link.
+func HasLink() bool {
+ // From Android release M (Marshmallow), hard linking files is blocked
+ // and an attempt to call link() on a file will return EACCES.
+ // - https://code.google.com/p/android-developer-preview/issues/detail?id=3150
+ return runtime.GOOS != "plan9" && runtime.GOOS != "android"
+}
+
+// MustHaveLink reports whether the current system can use os.Link.
+// If not, MustHaveLink calls t.Skip with an explanation.
+func MustHaveLink(t testing.TB) {
+ if !HasLink() {
+ t.Skipf("skipping test: hardlinks are not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
+ }
+}
+
+var flaky = flag.Bool("flaky", false, "run known-flaky tests too")
+
+func SkipFlaky(t testing.TB, issue int) {
+ t.Helper()
+ if !*flaky {
+ t.Skipf("skipping known flaky test without the -flaky flag; see golang.org/issue/%d", issue)
+ }
+}
+
+func SkipFlakyNet(t testing.TB) {
+ t.Helper()
+ if v, _ := strconv.ParseBool(os.Getenv("GO_BUILDER_FLAKY_NET")); v {
+ t.Skip("skipping test on builder known to have frequent network failures")
+ }
+}
+
+// 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", "wasm":
+ 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)
+ }
+}
+
+// SkipIfOptimizationOff skips t if optimization is disabled.
+func SkipIfOptimizationOff(t testing.TB) {
+ if OptimizationOff() {
+ t.Helper()
+ t.Skip("skipping test with optimization disabled")
+ }
+}
+
+// WriteImportcfg writes an importcfg file used by the compiler or linker to
+// dstPath containing entries for the file mappings in packageFiles, as well
+// as for the packages transitively imported by the package(s) in pkgs.
+//
+// pkgs may include any package pattern that is valid to pass to 'go list',
+// so it may also be a list of Go source files all in the same directory.
+func WriteImportcfg(t testing.TB, dstPath string, packageFiles map[string]string, pkgs ...string) {
+ t.Helper()
+
+ icfg := new(bytes.Buffer)
+ icfg.WriteString("# import config\n")
+ for k, v := range packageFiles {
+ fmt.Fprintf(icfg, "packagefile %s=%s\n", k, v)
+ }
+
+ if len(pkgs) > 0 {
+ // Use 'go list' to resolve any missing packages and rewrite the import map.
+ cmd := Command(t, GoToolPath(t), "list", "-export", "-deps", "-f", `{{if ne .ImportPath "command-line-arguments"}}{{if .Export}}{{.ImportPath}}={{.Export}}{{end}}{{end}}`)
+ cmd.Args = append(cmd.Args, pkgs...)
+ cmd.Stderr = new(strings.Builder)
+ out, err := cmd.Output()
+ if err != nil {
+ t.Fatalf("%v: %v\n%s", cmd, err, cmd.Stderr)
+ }
+
+ for _, line := range strings.Split(string(out), "\n") {
+ if line == "" {
+ continue
+ }
+ importPath, export, ok := strings.Cut(line, "=")
+ if !ok {
+ t.Fatalf("invalid line in output from %v:\n%s", cmd, line)
+ }
+ if packageFiles[importPath] == "" {
+ fmt.Fprintf(icfg, "packagefile %s=%s\n", importPath, export)
+ }
+ }
+ }
+
+ if err := os.WriteFile(dstPath, icfg.Bytes(), 0666); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// SyscallIsNotSupported reports whether err may indicate that a system call is
+// not supported by the current platform or execution environment.
+func SyscallIsNotSupported(err error) bool {
+ return syscallIsNotSupported(err)
+}
diff --git a/src/internal/testenv/testenv_notunix.go b/src/internal/testenv/testenv_notunix.go
new file mode 100644
index 0000000..a7df5f5
--- /dev/null
+++ b/src/internal/testenv/testenv_notunix.go
@@ -0,0 +1,21 @@
+// Copyright 2021 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.
+
+//go:build windows || plan9 || (js && wasm) || wasip1
+
+package testenv
+
+import (
+ "errors"
+ "io/fs"
+ "os"
+)
+
+// Sigquit is the signal to send to kill a hanging subprocess.
+// On Unix we send SIGQUIT, but on non-Unix we only have os.Kill.
+var Sigquit = os.Kill
+
+func syscallIsNotSupported(err error) bool {
+ return errors.Is(err, fs.ErrPermission) || errors.Is(err, errors.ErrUnsupported)
+}
diff --git a/src/internal/testenv/testenv_notwin.go b/src/internal/testenv/testenv_notwin.go
new file mode 100644
index 0000000..30e159a
--- /dev/null
+++ b/src/internal/testenv/testenv_notwin.go
@@ -0,0 +1,46 @@
+// Copyright 2016 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.
+
+//go:build !windows
+
+package testenv
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "runtime"
+)
+
+func hasSymlink() (ok bool, reason string) {
+ switch runtime.GOOS {
+ case "plan9":
+ return false, ""
+ case "android", "wasip1":
+ // For wasip1, some runtimes forbid absolute symlinks,
+ // or symlinks that escape the current working directory.
+ // Perform a simple test to see whether the runtime
+ // supports symlinks or not. If we get a permission
+ // error, the runtime does not support symlinks.
+ dir, err := os.MkdirTemp("", "")
+ if err != nil {
+ return false, ""
+ }
+ defer func() {
+ _ = os.RemoveAll(dir)
+ }()
+ fpath := filepath.Join(dir, "testfile.txt")
+ if err := os.WriteFile(fpath, nil, 0644); err != nil {
+ return false, ""
+ }
+ if err := os.Symlink(fpath, filepath.Join(dir, "testlink")); err != nil {
+ if SyscallIsNotSupported(err) {
+ return false, fmt.Sprintf("symlinks unsupported: %s", err.Error())
+ }
+ return false, ""
+ }
+ }
+
+ return true, ""
+}
diff --git a/src/internal/testenv/testenv_test.go b/src/internal/testenv/testenv_test.go
new file mode 100644
index 0000000..769db3a
--- /dev/null
+++ b/src/internal/testenv/testenv_test.go
@@ -0,0 +1,208 @@
+// Copyright 2022 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_test
+
+import (
+ "internal/platform"
+ "internal/testenv"
+ "os"
+ "path/filepath"
+ "runtime"
+ "strings"
+ "testing"
+)
+
+func TestGoToolLocation(t *testing.T) {
+ testenv.MustHaveGoBuild(t)
+
+ var exeSuffix string
+ if runtime.GOOS == "windows" {
+ exeSuffix = ".exe"
+ }
+
+ // Tests are defined to run within their package source directory,
+ // and this package's source directory is $GOROOT/src/internal/testenv.
+ // The 'go' command is installed at $GOROOT/bin/go, so if the environment
+ // is correct then testenv.GoTool() should be identical to ../../../bin/go.
+
+ relWant := "../../../bin/go" + exeSuffix
+ absWant, err := filepath.Abs(relWant)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ wantInfo, err := os.Stat(absWant)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Logf("found go tool at %q (%q)", relWant, absWant)
+
+ goTool, err := testenv.GoTool()
+ if err != nil {
+ t.Fatalf("testenv.GoTool(): %v", err)
+ }
+ t.Logf("testenv.GoTool() = %q", goTool)
+
+ gotInfo, err := os.Stat(goTool)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !os.SameFile(wantInfo, gotInfo) {
+ t.Fatalf("%q is not the same file as %q", absWant, goTool)
+ }
+}
+
+func TestHasGoBuild(t *testing.T) {
+ if !testenv.HasGoBuild() {
+ switch runtime.GOOS {
+ case "js", "wasip1":
+ // No exec syscall, so these shouldn't be able to 'go build'.
+ t.Logf("HasGoBuild is false on %s", runtime.GOOS)
+ return
+ }
+
+ b := testenv.Builder()
+ if b == "" {
+ // We shouldn't make assumptions about what kind of sandbox or build
+ // environment external Go users may be running in.
+ t.Skipf("skipping: 'go build' unavailable")
+ }
+
+ // Since we control the Go builders, we know which ones ought
+ // to be able to run 'go build'. Check that they can.
+ //
+ // (Note that we don't verify that any builders *can't* run 'go build'.
+ // If a builder starts running 'go build' tests when it shouldn't,
+ // we will presumably find out about it when those tests fail.)
+ switch runtime.GOOS {
+ case "ios":
+ if isCorelliumBuilder(b) {
+ // The corellium environment is self-hosting, so it should be able
+ // to build even though real "ios" devices can't exec.
+ } else {
+ // The usual iOS sandbox does not allow the app to start another
+ // process. If we add builders on stock iOS devices, they presumably
+ // will not be able to exec, so we may as well allow that now.
+ t.Logf("HasGoBuild is false on %s", b)
+ return
+ }
+ case "android":
+ if isEmulatedBuilder(b) && platform.MustLinkExternal(runtime.GOOS, runtime.GOARCH, false) {
+ // As of 2023-05-02, the test environment on the emulated builders is
+ // missing a C linker.
+ t.Logf("HasGoBuild is false on %s", b)
+ return
+ }
+ }
+
+ if strings.Contains(b, "-noopt") {
+ // The -noopt builder sets GO_GCFLAGS, which causes tests of 'go build' to
+ // be skipped.
+ t.Logf("HasGoBuild is false on %s", b)
+ return
+ }
+
+ t.Fatalf("HasGoBuild unexpectedly false on %s", b)
+ }
+
+ t.Logf("HasGoBuild is true; checking consistency with other functions")
+
+ hasExec := false
+ hasExecGo := false
+ t.Run("MustHaveExec", func(t *testing.T) {
+ testenv.MustHaveExec(t)
+ hasExec = true
+ })
+ t.Run("MustHaveExecPath", func(t *testing.T) {
+ testenv.MustHaveExecPath(t, "go")
+ hasExecGo = true
+ })
+ if !hasExec {
+ t.Errorf(`MustHaveExec(t) skipped unexpectedly`)
+ }
+ if !hasExecGo {
+ t.Errorf(`MustHaveExecPath(t, "go") skipped unexpectedly`)
+ }
+
+ dir := t.TempDir()
+ mainGo := filepath.Join(dir, "main.go")
+ if err := os.WriteFile(mainGo, []byte("package main\nfunc main() {}\n"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ cmd := testenv.Command(t, "go", "build", "-o", os.DevNull, mainGo)
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("%v: %v\n%s", cmd, err, out)
+ }
+}
+
+func TestMustHaveExec(t *testing.T) {
+ hasExec := false
+ t.Run("MustHaveExec", func(t *testing.T) {
+ testenv.MustHaveExec(t)
+ t.Logf("MustHaveExec did not skip")
+ hasExec = true
+ })
+
+ switch runtime.GOOS {
+ case "js", "wasip1":
+ if hasExec {
+ // js and wasip1 lack an “exec” syscall.
+ t.Errorf("expected MustHaveExec to skip on %v", runtime.GOOS)
+ }
+ case "ios":
+ if b := testenv.Builder(); isCorelliumBuilder(b) && !hasExec {
+ // Most ios environments can't exec, but the corellium builder can.
+ t.Errorf("expected MustHaveExec not to skip on %v", b)
+ }
+ default:
+ if b := testenv.Builder(); b != "" && !hasExec {
+ t.Errorf("expected MustHaveExec not to skip on %v", b)
+ }
+ }
+}
+
+func TestCleanCmdEnvPWD(t *testing.T) {
+ // Test that CleanCmdEnv sets PWD if cmd.Dir is set.
+ switch runtime.GOOS {
+ case "plan9", "windows":
+ t.Skipf("PWD is not used on %s", runtime.GOOS)
+ }
+ dir := t.TempDir()
+ cmd := testenv.Command(t, testenv.GoToolPath(t), "help")
+ cmd.Dir = dir
+ cmd = testenv.CleanCmdEnv(cmd)
+
+ for _, env := range cmd.Env {
+ if strings.HasPrefix(env, "PWD=") {
+ pwd := strings.TrimPrefix(env, "PWD=")
+ if pwd != dir {
+ t.Errorf("unexpected PWD: want %s, got %s", dir, pwd)
+ }
+ return
+ }
+ }
+ t.Error("PWD not set in cmd.Env")
+}
+
+func isCorelliumBuilder(builderName string) bool {
+ // Support both the old infra's builder names and the LUCI builder names.
+ // The former's names are ad-hoc so we could maintain this invariant on
+ // the builder side. The latter's names are structured, and "corellium" will
+ // appear as a "host" suffix after the GOOS and GOARCH, which always begin
+ // with an underscore.
+ return strings.HasSuffix(builderName, "-corellium") || strings.Contains(builderName, "_corellium")
+}
+
+func isEmulatedBuilder(builderName string) bool {
+ // Support both the old infra's builder names and the LUCI builder names.
+ // The former's names are ad-hoc so we could maintain this invariant on
+ // the builder side. The latter's names are structured, and the signifier
+ // of emulation "emu" will appear as a "host" suffix after the GOOS and
+ // GOARCH because it modifies the run environment in such a way that it
+ // the target GOOS and GOARCH may not match the host. This suffix always
+ // begins with an underscore.
+ return strings.HasSuffix(builderName, "-emu") || strings.Contains(builderName, "_emu")
+}
diff --git a/src/internal/testenv/testenv_unix.go b/src/internal/testenv/testenv_unix.go
new file mode 100644
index 0000000..a629078
--- /dev/null
+++ b/src/internal/testenv/testenv_unix.go
@@ -0,0 +1,43 @@
+// Copyright 2021 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.
+
+//go:build unix
+
+package testenv
+
+import (
+ "errors"
+ "io/fs"
+ "syscall"
+)
+
+// Sigquit is the signal to send to kill a hanging subprocess.
+// Send SIGQUIT to get a stack trace.
+var Sigquit = syscall.SIGQUIT
+
+func syscallIsNotSupported(err error) bool {
+ if err == nil {
+ return false
+ }
+
+ var errno syscall.Errno
+ if errors.As(err, &errno) {
+ switch errno {
+ case syscall.EPERM, syscall.EROFS:
+ // User lacks permission: either the call requires root permission and the
+ // user is not root, or the call is denied by a container security policy.
+ return true
+ case syscall.EINVAL:
+ // Some containers return EINVAL instead of EPERM if a system call is
+ // denied by security policy.
+ return true
+ }
+ }
+
+ if errors.Is(err, fs.ErrPermission) || errors.Is(err, errors.ErrUnsupported) {
+ return true
+ }
+
+ return false
+}
diff --git a/src/internal/testenv/testenv_windows.go b/src/internal/testenv/testenv_windows.go
new file mode 100644
index 0000000..4802b13
--- /dev/null
+++ b/src/internal/testenv/testenv_windows.go
@@ -0,0 +1,47 @@
+// Copyright 2016 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
+
+import (
+ "os"
+ "path/filepath"
+ "sync"
+ "syscall"
+)
+
+var symlinkOnce sync.Once
+var winSymlinkErr error
+
+func initWinHasSymlink() {
+ tmpdir, err := os.MkdirTemp("", "symtest")
+ if err != nil {
+ panic("failed to create temp directory: " + err.Error())
+ }
+ defer os.RemoveAll(tmpdir)
+
+ err = os.Symlink("target", filepath.Join(tmpdir, "symlink"))
+ if err != nil {
+ err = err.(*os.LinkError).Err
+ switch err {
+ case syscall.EWINDOWS, syscall.ERROR_PRIVILEGE_NOT_HELD:
+ winSymlinkErr = err
+ }
+ }
+}
+
+func hasSymlink() (ok bool, reason string) {
+ symlinkOnce.Do(initWinHasSymlink)
+
+ switch winSymlinkErr {
+ case nil:
+ return true, ""
+ case syscall.EWINDOWS:
+ return false, ": symlinks are not supported on your version of Windows"
+ case syscall.ERROR_PRIVILEGE_NOT_HELD:
+ return false, ": you don't have enough privileges to create symlinks"
+ }
+
+ return false, ""
+}