1 env GO111MODULE=off
2 env GOPATH=$WORK/gopath
3 cd $WORK/gopath/src
4
5 # Test that cached test results are invalidated in response to
6 # changes to the external inputs to the test.
7
8 [short] skip
9 [GODEBUG:gocacheverify=1] skip
10
11 # We're testing cache behavior, so start with a clean GOCACHE.
12 env GOCACHE=$WORK/cache
13
14 # Build a helper binary to invoke os.Chtimes.
15 go build -o mkold$GOEXE mkold.go
16
17 # Make test input files appear to be a minute old.
18 exec ./mkold$GOEXE 1m testcache/file.txt
19 exec ./mkold$GOEXE 1m testcache/script.sh
20
21 # If the test reads an environment variable, changes to that variable
22 # should invalidate cached test results.
23 env TESTKEY=x
24 go test testcache -run=TestLookupEnv
25 go test testcache -run=TestLookupEnv
26 stdout '\(cached\)'
27
28 # GODEBUG is always read
29 env GODEBUG=asdf=1
30 go test testcache -run=TestLookupEnv
31 ! stdout '\(cached\)'
32 go test testcache -run=TestLookupEnv
33 stdout '\(cached\)'
34 env GODEBUG=
35
36 env TESTKEY=y
37 go test testcache -run=TestLookupEnv
38 ! stdout '\(cached\)'
39 go test testcache -run=TestLookupEnv
40 stdout '\(cached\)'
41
42 # Changes in arguments forwarded to the test should invalidate cached test
43 # results.
44 go test testcache -run=TestOSArgs -v hello
45 ! stdout '\(cached\)'
46 stdout 'hello'
47 go test testcache -run=TestOSArgs -v goodbye
48 ! stdout '\(cached\)'
49 stdout 'goodbye'
50
51 # golang.org/issue/36134: that includes the `-timeout` argument.
52 go test testcache -run=TestOSArgs -timeout=20m -v
53 ! stdout '\(cached\)'
54 stdout '-test\.timeout[= ]20m'
55 go test testcache -run=TestOSArgs -timeout=5s -v
56 ! stdout '\(cached\)'
57 stdout '-test\.timeout[= ]5s'
58
59 # If the test stats a file, changes to the file should invalidate the cache.
60 go test testcache -run=FileSize
61 go test testcache -run=FileSize
62 stdout '\(cached\)'
63
64 cp 4x.txt testcache/file.txt
65 go test testcache -run=FileSize
66 ! stdout '\(cached\)'
67 go test testcache -run=FileSize
68 stdout '\(cached\)'
69
70 # Files should be tracked even if the test changes its working directory.
71 go test testcache -run=Chdir
72 go test testcache -run=Chdir
73 stdout '\(cached\)'
74 cp 6x.txt testcache/file.txt
75 go test testcache -run=Chdir
76 ! stdout '\(cached\)'
77 go test testcache -run=Chdir
78 stdout '\(cached\)'
79
80 # The content of files should affect caching, provided that the mtime also changes.
81 exec ./mkold$GOEXE 1m testcache/file.txt
82 go test testcache -run=FileContent
83 go test testcache -run=FileContent
84 stdout '\(cached\)'
85 cp 2y.txt testcache/file.txt
86 exec ./mkold$GOEXE 50s testcache/file.txt
87 go test testcache -run=FileContent
88 ! stdout '\(cached\)'
89 go test testcache -run=FileContent
90 stdout '\(cached\)'
91
92 # Directory contents read via os.ReadDirNames should affect caching.
93 go test testcache -run=DirList
94 go test testcache -run=DirList
95 stdout '\(cached\)'
96 rm testcache/file.txt
97 go test testcache -run=DirList
98 ! stdout '\(cached\)'
99 go test testcache -run=DirList
100 stdout '\(cached\)'
101
102 # Files outside GOROOT and GOPATH should not affect caching.
103 env TEST_EXTERNAL_FILE=$WORK/external.txt
104 go test testcache -run=ExternalFile
105 go test testcache -run=ExternalFile
106 stdout '\(cached\)'
107
108 rm external.txt
109 go test testcache -run=ExternalFile
110 stdout '\(cached\)'
111
112 # The -benchtime flag without -bench should not affect caching.
113 go test testcache -run=Benchtime -benchtime=1x
114 go test testcache -run=Benchtime -benchtime=1x
115 stdout '\(cached\)'
116
117 go test testcache -run=Benchtime -bench=Benchtime -benchtime=1x
118 go test testcache -run=Benchtime -bench=Benchtime -benchtime=1x
119 ! stdout '\(cached\)'
120
121 # golang.org/issue/47355: that includes the `-failfast` argument.
122 go test testcache -run=TestOSArgs -failfast
123 ! stdout '\(cached\)'
124 go test testcache -run=TestOSArgs -failfast
125 stdout '\(cached\)'
126
127 # golang.org/issue/64638: that includes the `-fullpath` argument.
128 go test testcache -run=TestOSArgs -fullpath
129 ! stdout '\(cached\)'
130 go test testcache -run=TestOSArgs -fullpath
131 stdout '\(cached\)'
132
133 # golang.org/issue/70692: that includes the `-skip` flag
134 go test testcache -run=TestOdd -skip=TestOddFile
135 ! stdout '\(cached\)'
136 go test testcache -run=TestOdd -skip=TestOddFile
137 stdout '\(cached\)'
138
139 # Ensure that coverage profiles are being cached.
140 go test testcache -run=TestCoverageCache -coverprofile=coverage.out
141 go test testcache -run=TestCoverageCache -coverprofile=coverage.out
142 stdout '\(cached\)'
143 exists coverage.out
144 grep -q 'mode: set' coverage.out
145 grep -q 'testcache/hello.go:' coverage.out
146
147 # A new -coverprofile file should use the cached coverage profile contents.
148 go test testcache -run=TestCoverageCache -coverprofile=coverage2.out
149 stdout '\(cached\)'
150 cmp coverage.out coverage2.out
151
152 # Explicitly setting the default covermode should still use cache.
153 go test testcache -run=TestCoverageCache -coverprofile=coverage_set.out -covermode=set
154 stdout '\(cached\)'
155 cmp coverage.out coverage_set.out
156
157 # A new -covermode should not use the cached coverage profile.
158 go test testcache -run=TestCoverageCache -coverprofile=coverage_atomic.out -covermode=atomic
159 ! stdout '\(cached\)'
160 ! cmp coverage.out coverage_atomic.out
161 grep -q 'mode: atomic' coverage_atomic.out
162 grep -q 'testcache/hello.go:' coverage_atomic.out
163
164 # A new -coverpkg should not use the cached coverage profile.
165 go test testcache -run=TestCoverageCache -coverprofile=coverage_pkg.out -coverpkg=all
166 ! stdout '\(cached\)'
167 ! cmp coverage.out coverage_pkg.out
168
169 # Test that -v doesn't prevent caching.
170 go test testcache -v -run=TestCoverageCache -coverprofile=coverage_v.out
171 go test testcache -v -run=TestCoverageCache -coverprofile=coverage_v2.out
172 stdout '\(cached\)'
173 cmp coverage_v.out coverage_v2.out
174
175 # Test that -count affects caching.
176 go test testcache -run=TestCoverageCache -coverprofile=coverage_count.out -count=2
177 ! stdout '\(cached\)'
178
179 # Executables within GOROOT and GOPATH should affect caching,
180 # even if the test does not stat them explicitly.
181
182 [!exec:/bin/sh] skip
183 chmod 0755 ./testcache/script.sh
184
185 exec ./mkold$GOEXEC 1m testcache/script.sh
186 go test testcache -run=Exec
187 go test testcache -run=Exec
188 stdout '\(cached\)'
189
190 exec ./mkold$GOEXE 50s testcache/script.sh
191 go test testcache -run=Exec
192 ! stdout '\(cached\)'
193 go test testcache -run=Exec
194 stdout '\(cached\)'
195
196 -- gopath/src/testcache/file.txt --
197 xx
198 -- gopath/src/4x.txt --
199 xxxx
200 -- gopath/src/6x.txt --
201 xxxxxx
202 -- gopath/src/2y.txt --
203 yy
204 -- external.txt --
205 This file is outside of GOPATH.
206 -- gopath/src/testcache/script.sh --
207 #!/bin/sh
208 exit 0
209 -- gopath/src/testcache/hello.go --
210 package testcache
211
212 import "fmt"
213
214 func HelloWorld(name string) string {
215 if name == "" {
216 return "Hello, World!"
217 }
218 return fmt.Sprintf("Hello, %s!", name)
219 }
220
221 -- gopath/src/testcache/testcache_test.go --
222 // Copyright 2017 The Go Authors. All rights reserved.
223 // Use of this source code is governed by a BSD-style
224 // license that can be found in the LICENSE file.
225
226 package testcache
227
228 import (
229 "io"
230 "os"
231 "testing"
232 )
233
234 func TestChdir(t *testing.T) {
235 os.Chdir("..")
236 defer os.Chdir("testcache")
237 info, err := os.Stat("testcache/file.txt")
238 if err != nil {
239 t.Fatal(err)
240 }
241 if info.Size()%2 != 1 {
242 t.Fatal("even file")
243 }
244 }
245
246 func TestOddFileContent(t *testing.T) {
247 f, err := os.Open("file.txt")
248 if err != nil {
249 t.Fatal(err)
250 }
251 data, err := io.ReadAll(f)
252 f.Close()
253 if err != nil {
254 t.Fatal(err)
255 }
256 if len(data)%2 != 1 {
257 t.Fatal("even file")
258 }
259 }
260
261 func TestOddFileSize(t *testing.T) {
262 info, err := os.Stat("file.txt")
263 if err != nil {
264 t.Fatal(err)
265 }
266 if info.Size()%2 != 1 {
267 t.Fatal("even file")
268 }
269 }
270
271 func TestOddGetenv(t *testing.T) {
272 val := os.Getenv("TESTKEY")
273 if len(val)%2 != 1 {
274 t.Fatal("even env value")
275 }
276 }
277
278 func TestLookupEnv(t *testing.T) {
279 _, ok := os.LookupEnv("TESTKEY")
280 if !ok {
281 t.Fatal("env missing")
282 }
283 }
284
285 func TestDirList(t *testing.T) {
286 f, err := os.Open(".")
287 if err != nil {
288 t.Fatal(err)
289 }
290 f.Readdirnames(-1)
291 f.Close()
292 }
293
294 func TestExec(t *testing.T) {
295 // Note: not using os/exec to make sure there is no unexpected stat.
296 p, err := os.StartProcess("./script.sh", []string{"script"}, new(os.ProcAttr))
297 if err != nil {
298 t.Fatal(err)
299 }
300 ps, err := p.Wait()
301 if err != nil {
302 t.Fatal(err)
303 }
304 if !ps.Success() {
305 t.Fatalf("script failed: %v", err)
306 }
307 }
308
309 func TestExternalFile(t *testing.T) {
310 os.Open(os.Getenv("TEST_EXTERNAL_FILE"))
311 _, err := os.Stat(os.Getenv("TEST_EXTERNAL_FILE"))
312 if err != nil {
313 t.Fatal(err)
314 }
315 }
316
317 func TestOSArgs(t *testing.T) {
318 t.Log(os.Args)
319 }
320
321 func TestBenchtime(t *testing.T) {
322 }
323
324 func TestCoverageCache(t *testing.T) {
325 result := HelloWorld("")
326 if result != "Hello, World!" {
327 t.Errorf("Expected 'Hello, World!', got '%s'", result)
328 }
329
330 result = HelloWorld("Go")
331 if result != "Hello, Go!" {
332 t.Errorf("Expected 'Hello, Go!', got '%s'", result)
333 }
334 }
335
336 -- gopath/src/mkold.go --
337 package main
338
339 import (
340 "log"
341 "os"
342 "time"
343 )
344
345 func main() {
346 d, err := time.ParseDuration(os.Args[1])
347 if err != nil {
348 log.Fatal(err)
349 }
350 path := os.Args[2]
351 old := time.Now().Add(-d)
352 err = os.Chtimes(path, old, old)
353 if err != nil {
354 log.Fatal(err)
355 }
356 }
357
View as plain text