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