| // 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. |
| |
| // The helper functions in this package are copied from the original |
| // versions from the file of the same name in $GOROOT/src/internal. |
| |
| package testenv |
| |
| import ( |
| "errors" |
| "fmt" |
| "go/build" |
| "os" |
| "os/exec" |
| "path/filepath" |
| "runtime" |
| "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 { |
| 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 |
| } |
| switch runtime.GOOS { |
| case "android", "js", "ios": |
| 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.TB) { |
| if os.Getenv("GO_GCFLAGS") != "" { |
| t.Skipf("skipping test: 'go build' not compatible with setting $GO_GCFLAGS") |
| } |
| if !HasGoBuild() { |
| t.Skipf("skipping test: 'go build' not 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) |
| } |
| return path |
| } |
| |
| // GoTool reports the path to the Go tool. |
| func GoTool() (string, error) { |
| if !HasGoBuild() { |
| return "", errors.New("platform cannot run go tool") |
| } |
| var exeSuffix string |
| if runtime.GOOS == "windows" { |
| exeSuffix = ".exe" |
| } |
| path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix) |
| if _, err := os.Stat(path); err == nil { |
| return path, nil |
| } |
| goBin, err := exec.LookPath("go" + exeSuffix) |
| if err != nil { |
| return "", errors.New("cannot find go tool: " + err.Error()) |
| } |
| return goBin, nil |
| } |
| |
| // HasExternalNetwork reports whether the current system can use |
| // external (non-localhost) networks. |
| func HasExternalNetwork() bool { |
| return !testing.Short() && runtime.GOOS != "js" |
| } |
| |
| // 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" { |
| t.Skipf("skipping test: no external network on %s", runtime.GOOS) |
| } |
| if testing.Short() { |
| t.Skipf("skipping test: no external network in -short mode") |
| } |
| } |
| |
| // Go1Point returns the x in Go 1.x. |
| func Go1Point() int { |
| for i := len(build.Default.ReleaseTags) - 1; i >= 0; i-- { |
| var version int |
| if _, err := fmt.Sscanf(build.Default.ReleaseTags[i], "go1.%d", &version); err != nil { |
| continue |
| } |
| return version |
| } |
| panic("bad release tags") |
| } |
| |
| // Testing is an abstraction of a *testing.T. |
| type Testing interface { |
| Skipf(format string, args ...interface{}) |
| Fatalf(format string, args ...interface{}) |
| } |
| |
| type helperer interface { |
| Helper() |
| } |
| |
| // NeedsGo1Point skips t if the Go version used to run the test is |
| // older than 1.x. |
| func NeedsGo1Point(t Testing, x int) { |
| if t, ok := t.(helperer); ok { |
| t.Helper() |
| } |
| if Go1Point() < x { |
| t.Skipf("running Go version %q is version 1.%d, older than required 1.%d", runtime.Version(), Go1Point(), x) |
| } |
| } |