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