|
| 1 | +// Copyright 2015 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package testenv provides information about what functionality |
| 6 | +// is available in different testing environments run by the Go team. |
| 7 | +// |
| 8 | +// It is an internal package because these details are specific |
| 9 | +// to the Go team's test setup (on build.golang.org) and not |
| 10 | +// fundamental to tests in general. |
| 11 | +package testenv |
| 12 | + |
| 13 | +import ( |
| 14 | + "os" |
| 15 | + "runtime" |
| 16 | + "strings" |
| 17 | + "testing" |
| 18 | +) |
| 19 | + |
| 20 | +// Builder reports the name of the builder running this test |
| 21 | +// (for example, "linux-amd64" or "windows-386-gce"). |
| 22 | +// If the test is not running on the build infrastructure, |
| 23 | +// Builder returns the empty string. |
| 24 | +func Builder() string { |
| 25 | + return os.Getenv("GO_BUILDER_NAME") |
| 26 | +} |
| 27 | + |
| 28 | +// HasGoBuild reports whether the current system can build programs with ``go build'' |
| 29 | +// and then run them with os.StartProcess or exec.Command. |
| 30 | +func HasGoBuild() bool { |
| 31 | + switch runtime.GOOS { |
| 32 | + case "android", "nacl": |
| 33 | + return false |
| 34 | + case "darwin": |
| 35 | + if strings.HasPrefix(runtime.GOARCH, "arm") { |
| 36 | + return false |
| 37 | + } |
| 38 | + } |
| 39 | + return true |
| 40 | +} |
| 41 | + |
| 42 | +// MustHaveGoBuild checks that the current system can build programs with ``go build'' |
| 43 | +// and then run them with os.StartProcess or exec.Command. |
| 44 | +// If not, MustHaveGoBuild calls t.Skip with an explanation. |
| 45 | +func MustHaveGoBuild(t *testing.T) { |
| 46 | + if !HasGoBuild() { |
| 47 | + t.Skipf("skipping test: 'go build' not available on %s/%s", runtime.GOOS, runtime.GOARCH) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// HasGoRun reports whether the current system can run programs with ``go run.'' |
| 52 | +func HasGoRun() bool { |
| 53 | + // For now, having go run and having go build are the same. |
| 54 | + return HasGoBuild() |
| 55 | +} |
| 56 | + |
| 57 | +// MustHaveGoRun checks that the current system can run programs with ``go run.'' |
| 58 | +// If not, MustHaveGoRun calls t.Skip with an explanation. |
| 59 | +func MustHaveGoRun(t *testing.T) { |
| 60 | + if !HasGoRun() { |
| 61 | + t.Skipf("skipping test: 'go run' not available on %s/%s", runtime.GOOS, runtime.GOARCH) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// HasExec reports whether the current system can start new processes |
| 66 | +// using os.StartProcess or (more commonly) exec.Command. |
| 67 | +func HasExec() bool { |
| 68 | + switch runtime.GOOS { |
| 69 | + case "nacl": |
| 70 | + return false |
| 71 | + case "darwin": |
| 72 | + if strings.HasPrefix(runtime.GOARCH, "arm") { |
| 73 | + return false |
| 74 | + } |
| 75 | + } |
| 76 | + return true |
| 77 | +} |
| 78 | + |
| 79 | +// MustHaveExec checks that the current system can start new processes |
| 80 | +// using os.StartProcess or (more commonly) exec.Command. |
| 81 | +// If not, MustHaveExec calls t.Skip with an explanation. |
| 82 | +func MustHaveExec(t *testing.T) { |
| 83 | + if !HasExec() { |
| 84 | + t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// HasExternalNetwork reports whether the current system can use |
| 89 | +// external (non-localhost) networks. |
| 90 | +func HasExternalNetwork() bool { |
| 91 | + return !testing.Short() |
| 92 | +} |
| 93 | + |
| 94 | +// MustHaveExternalNetwork checks that the current system can use |
| 95 | +// external (non-localhost) networks. |
| 96 | +// If not, MustHaveExternalNetwork calls t.Skip with an explanation. |
| 97 | +func MustHaveExternalNetwork(t *testing.T) { |
| 98 | + if testing.Short() { |
| 99 | + t.Skipf("skipping test: no external network in -short mode") |
| 100 | + } |
| 101 | +} |
0 commit comments