...
1
2
3
4
5
6
7
8
9
10
11 package testenv
12
13 import (
14 "errors"
15 "flag"
16 "os"
17 "os/exec"
18 "path/filepath"
19 "runtime"
20 "strconv"
21 "strings"
22 "testing"
23 )
24
25
26
27
28
29 func Builder() string {
30 return os.Getenv("GO_BUILDER_NAME")
31 }
32
33
34
35 func HasGoBuild() bool {
36 if os.Getenv("GO_GCFLAGS") != "" {
37
38
39
40
41 return false
42 }
43 switch runtime.GOOS {
44 case "android", "nacl", "js":
45 return false
46 case "darwin":
47 if strings.HasPrefix(runtime.GOARCH, "arm") {
48 return false
49 }
50 }
51 return true
52 }
53
54
55
56
57 func MustHaveGoBuild(t testing.TB) {
58 if os.Getenv("GO_GCFLAGS") != "" {
59 t.Skipf("skipping test: 'go build' not compatible with setting $GO_GCFLAGS")
60 }
61 if !HasGoBuild() {
62 t.Skipf("skipping test: 'go build' not available on %s/%s", runtime.GOOS, runtime.GOARCH)
63 }
64 }
65
66
67 func HasGoRun() bool {
68
69 return HasGoBuild()
70 }
71
72
73
74 func MustHaveGoRun(t testing.TB) {
75 if !HasGoRun() {
76 t.Skipf("skipping test: 'go run' not available on %s/%s", runtime.GOOS, runtime.GOARCH)
77 }
78 }
79
80
81
82
83
84 func GoToolPath(t testing.TB) string {
85 MustHaveGoBuild(t)
86 path, err := GoTool()
87 if err != nil {
88 t.Fatal(err)
89 }
90 return path
91 }
92
93
94 func GoTool() (string, error) {
95 if !HasGoBuild() {
96 return "", errors.New("platform cannot run go tool")
97 }
98 var exeSuffix string
99 if runtime.GOOS == "windows" {
100 exeSuffix = ".exe"
101 }
102 path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
103 if _, err := os.Stat(path); err == nil {
104 return path, nil
105 }
106 goBin, err := exec.LookPath("go" + exeSuffix)
107 if err != nil {
108 return "", errors.New("cannot find go tool: " + err.Error())
109 }
110 return goBin, nil
111 }
112
113
114
115 func HasExec() bool {
116 switch runtime.GOOS {
117 case "nacl", "js":
118 return false
119 case "darwin":
120 if strings.HasPrefix(runtime.GOARCH, "arm") {
121 return false
122 }
123 }
124 return true
125 }
126
127
128 func HasSrc() bool {
129 switch runtime.GOOS {
130 case "nacl":
131 return false
132 case "darwin":
133 if strings.HasPrefix(runtime.GOARCH, "arm") {
134 return false
135 }
136 }
137 return true
138 }
139
140
141
142
143 func MustHaveExec(t testing.TB) {
144 if !HasExec() {
145 t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH)
146 }
147 }
148
149
150
151 func HasExternalNetwork() bool {
152 return !testing.Short() && runtime.GOOS != "nacl" && runtime.GOOS != "js"
153 }
154
155
156
157
158 func MustHaveExternalNetwork(t testing.TB) {
159 if runtime.GOOS == "nacl" || runtime.GOOS == "js" {
160 t.Skipf("skipping test: no external network on %s", runtime.GOOS)
161 }
162 if testing.Short() {
163 t.Skipf("skipping test: no external network in -short mode")
164 }
165 }
166
167 var haveCGO bool
168
169
170 func HasCGO() bool {
171 return haveCGO
172 }
173
174
175 func MustHaveCGO(t testing.TB) {
176 if !haveCGO {
177 t.Skipf("skipping test: no cgo")
178 }
179 }
180
181
182 func HasSymlink() bool {
183 ok, _ := hasSymlink()
184 return ok
185 }
186
187
188
189 func MustHaveSymlink(t testing.TB) {
190 ok, reason := hasSymlink()
191 if !ok {
192 t.Skipf("skipping test: cannot make symlinks on %s/%s%s", runtime.GOOS, runtime.GOARCH, reason)
193 }
194 }
195
196
197 func HasLink() bool {
198
199
200
201 return runtime.GOOS != "plan9" && runtime.GOOS != "android"
202 }
203
204
205
206 func MustHaveLink(t testing.TB) {
207 if !HasLink() {
208 t.Skipf("skipping test: hardlinks are not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
209 }
210 }
211
212 var flaky = flag.Bool("flaky", false, "run known-flaky tests too")
213
214 func SkipFlaky(t testing.TB, issue int) {
215 t.Helper()
216 if !*flaky {
217 t.Skipf("skipping known flaky test without the -flaky flag; see golang.org/issue/%d", issue)
218 }
219 }
220
221 func SkipFlakyNet(t testing.TB) {
222 t.Helper()
223 if v, _ := strconv.ParseBool(os.Getenv("GO_BUILDER_FLAKY_NET")); v {
224 t.Skip("skipping test on builder known to have frequent network failures")
225 }
226 }
227
228
229
230
231 func CleanCmdEnv(cmd *exec.Cmd) *exec.Cmd {
232 if cmd.Env != nil {
233 panic("environment already set")
234 }
235 for _, env := range os.Environ() {
236
237
238 if strings.HasPrefix(env, "GODEBUG=") {
239 continue
240 }
241
242 if strings.HasPrefix(env, "GOTRACEBACK=") {
243 continue
244 }
245 cmd.Env = append(cmd.Env, env)
246 }
247 return cmd
248 }
249
View as plain text