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