// 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 testenvimport ()// 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 () string {returnos.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 () bool {ifos.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.returnfalse }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.) := exec.Command("go", "tool", "-n", "compile") .Env = origEnv , := .Output()if != nil {goBuildErr = fmt.Errorf("%v: %w", , )return } = bytes.TrimSpace()iflen() == 0 {goBuildErr = fmt.Errorf("%v: no tool reported", )return }if , := exec.LookPath(string()); != nil {goBuildErr = return }ifplatform.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.ifos.Getenv("CC") == "" { := exec.Command("go", "env", "CC") .Env = origEnv , := .Output()if != nil {goBuildErr = fmt.Errorf("%v: %w", , )return } = bytes.TrimSpace()iflen() == 0 {goBuildErr = fmt.Errorf("%v: no CC reported", )return } _, goBuildErr = exec.LookPath(string()) } } })returngoBuildErr == 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 ( testing.TB) {ifos.Getenv("GO_GCFLAGS") != "" { .Helper() .Skipf("skipping test: 'go build' not compatible with setting $GO_GCFLAGS") }if !HasGoBuild() { .Helper() .Skipf("skipping test: 'go build' unavailable: %v", goBuildErr) }}// HasGoRun reports whether the current system can run programs with “go run”.func () bool {// For now, having go run and having go build are the same.returnHasGoBuild()}// MustHaveGoRun checks that the current system can run programs with “go run”.// If not, MustHaveGoRun calls t.Skip with an explanation.func ( testing.TB) {if !HasGoRun() { .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 () bool {switchruntime.GOOS {case"js", "wasip1":returnfalse }returntrue}// MustHaveParallelism checks that the current system can execute multiple// threads in parallel. If not, MustHaveParallelism calls t.Skip with an explanation.func ( testing.TB) {if !HasParallelism() { .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 ( testing.TB) string {MustHaveGoBuild() , := GoTool()if != nil { .Fatal() }// 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 , := rangestrings.Fields(cfg.KnownEnv) {os.Getenv() }return}var ( gorootOnce sync.Once gorootPath string gorootErr error)func findGOROOT() (string, error) {gorootOnce.Do(func() {gorootPath = runtime.GOROOT()ifgorootPath != "" {// 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. But this case is // 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). // // 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. , := os.Getwd()if != nil {gorootErr = fmt.Errorf("finding GOROOT: %w", )return } := for { := filepath.Dir()if == {// dir is either "." or only a volume name.gorootErr = fmt.Errorf("failed to locate GOROOT/src in any parent directory")return }if := filepath.Base(); != "src" { = continue// dir cannot be GOROOT/src if it doesn't end in "src". } , := os.ReadFile(filepath.Join(, "go.mod"))if != nil {ifos.IsNotExist() { = continue }gorootErr = fmt.Errorf("finding GOROOT: %w", )return } := string()for != "" {varstring , , _ = strings.Cut(, "\n") := strings.Fields()iflen() >= 2 && [0] == "module" && [1] == "std" {// Found "module std", which is the module declaration in GOROOT/src!gorootPath = return } } } })returngorootPath, 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 ( testing.TB) string { , := findGOROOT()if != nil {if == nil {panic() } .Helper() .Skip() }return}// GoTool reports the path to the Go tool.func () (string, error) {if !HasGoBuild() {return"", errors.New("platform cannot run go tool") }goToolOnce.Do(func() {goToolPath, goToolErr = exec.LookPath("go") })returngoToolPath, goToolErr}var ( goToolOnce sync.Once goToolPath string goToolErr error)// HasSrc reports whether the entire source tree is available under GOROOT.func () bool {switchruntime.GOOS {case"ios":returnfalse }returntrue}// HasExternalNetwork reports whether the current system can use// external (non-localhost) networks.func () 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 ( testing.TB) {ifruntime.GOOS == "js" || runtime.GOOS == "wasip1" { .Helper() .Skipf("skipping test: no external network on %s", runtime.GOOS) }iftesting.Short() { .Helper() .Skipf("skipping test: no external network in -short mode") }}// HasCGO reports whether the current system can use cgo.func () bool {hasCgoOnce.Do(func() { , := GoTool()if != nil {return } := exec.Command(, "env", "CGO_ENABLED") .Env = origEnv , := .Output()if != nil {panic(fmt.Sprintf("%v: %v", , )) }hasCgo, = strconv.ParseBool(string(bytes.TrimSpace()))if != nil {panic(fmt.Sprintf("%v: non-boolean output %q", , )) } })returnhasCgo}var ( hasCgoOnce sync.Once hasCgo bool)// MustHaveCGO calls t.Skip if cgo is not available.func ( testing.TB) {if !HasCGO() { .Skipf("skipping test: no cgo") }}// CanInternalLink reports whether the current system can link programs with// internal linking.func ( bool) bool {return !platform.MustLinkExternal(runtime.GOOS, runtime.GOARCH, )}// MustInternalLink checks that the current system can link programs with internal// linking.// If not, MustInternalLink calls t.Skip with an explanation.func ( testing.TB, bool) {if !CanInternalLink() {if && CanInternalLink(false) { .Skipf("skipping test: internal linking on %s/%s is not supported with cgo", runtime.GOOS, runtime.GOARCH) } .Skipf("skipping test: internal linking on %s/%s is not supported", runtime.GOOS, runtime.GOARCH) }}// MustInternalLinkPIE checks whether the current system can link PIE binary using// internal linking.// If not, MustInternalLinkPIE calls t.Skip with an explanation.func ( testing.TB) {if !platform.InternalLinkPIESupported(runtime.GOOS, runtime.GOARCH) { .Skipf("skipping test: internal linking for buildmode=pie 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 ( testing.TB, string) {if !platform.BuildModeSupported(runtime.Compiler, , runtime.GOOS, runtime.GOARCH) { .Skipf("skipping test: build mode %s on %s/%s is not supported by the %s compiler", , runtime.GOOS, runtime.GOARCH, runtime.Compiler) }}// HasSymlink reports whether the current system can use os.Symlink.func () bool { , := hasSymlink()return}// MustHaveSymlink reports whether the current system can use os.Symlink.// If not, MustHaveSymlink calls t.Skip with an explanation.func ( testing.TB) { , := hasSymlink()if ! { .Skipf("skipping test: cannot make symlinks on %s/%s: %s", runtime.GOOS, runtime.GOARCH, ) }}// HasLink reports whether the current system can use os.Link.func () 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=3150returnruntime.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 ( testing.TB) {if !HasLink() { .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 ( testing.TB, int) { .Helper()if !*flaky { .Skipf("skipping known flaky test without the -flaky flag; see golang.org/issue/%d", ) }}func ( testing.TB) { .Helper()if , := strconv.ParseBool(os.Getenv("GO_BUILDER_FLAKY_NET")); { .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 () bool {switchruntime.GOARCH {case"arm", "mips", "mipsle", "mips64", "mips64le", "wasm":returntrue }returnfalse}// 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 ( testing.TB) {iftesting.Short() && CPUIsSlow() { .Helper() .Skipf("skipping test in -short mode on %s", runtime.GOARCH) }}// SkipIfOptimizationOff skips t if optimization is disabled.func ( testing.TB) {ifOptimizationOff() { .Helper() .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 ( testing.TB, string, map[string]string, ...string) { .Helper() := new(bytes.Buffer) .WriteString("# import config\n")for , := range {fmt.Fprintf(, "packagefile %s=%s\n", , ) }iflen() > 0 {// Use 'go list' to resolve any missing packages and rewrite the import map. := Command(, GoToolPath(), "list", "-export", "-deps", "-f", `{{if ne .ImportPath "command-line-arguments"}}{{if .Export}}{{.ImportPath}}={{.Export}}{{end}}{{end}}`) .Args = append(.Args, ...) .Stderr = new(strings.Builder) , := .Output()if != nil { .Fatalf("%v: %v\n%s", , , .Stderr) }for , := rangestrings.Split(string(), "\n") {if == "" {continue } , , := strings.Cut(, "=")if ! { .Fatalf("invalid line in output from %v:\n%s", , ) }if [] == "" {fmt.Fprintf(, "packagefile %s=%s\n", , ) } } }if := os.WriteFile(, .Bytes(), 0666); != nil { .Fatal() }}// SyscallIsNotSupported reports whether err may indicate that a system call is// not supported by the current platform or execution environment.func ( error) bool {returnsyscallIsNotSupported()}// ParallelOn64Bit calls t.Parallel() unless there is a case that cannot be parallel.// This function should be used when it is necessary to avoid t.Parallel on// 32-bit machines, typically because the test uses lots of memory.func ( *testing.T) {ifgoarch.PtrSize == 4 {return } .Parallel()}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.