...
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
26# GODEBUG is always read
27env GODEBUG=asdf=1
28go test testcache -run=TestLookupEnv
29! stdout '\(cached\)'
30go test testcache -run=TestLookupEnv
31stdout '\(cached\)'
32env GODEBUG=
33
34env TESTKEY=y
35go test testcache -run=TestLookupEnv
36! stdout '\(cached\)'
37go test testcache -run=TestLookupEnv
38stdout '\(cached\)'
39
40# Changes in arguments forwarded to the test should invalidate cached test
41# results.
42go test testcache -run=TestOSArgs -v hello
43! stdout '\(cached\)'
44stdout 'hello'
45go test testcache -run=TestOSArgs -v goodbye
46! stdout '\(cached\)'
47stdout 'goodbye'
48
49# golang.org/issue/36134: that includes the `-timeout` argument.
50go test testcache -run=TestOSArgs -timeout=20m -v
51! stdout '\(cached\)'
52stdout '-test\.timeout[= ]20m'
53go test testcache -run=TestOSArgs -timeout=5s -v
54! stdout '\(cached\)'
55stdout '-test\.timeout[= ]5s'
56
57# If the test stats a file, changes to the file should invalidate the cache.
58go test testcache -run=FileSize
59go test testcache -run=FileSize
60stdout '\(cached\)'
61
62cp 4x.txt testcache/file.txt
63go test testcache -run=FileSize
64! stdout '\(cached\)'
65go test testcache -run=FileSize
66stdout '\(cached\)'
67
68# Files should be tracked even if the test changes its working directory.
69go test testcache -run=Chdir
70go test testcache -run=Chdir
71stdout '\(cached\)'
72cp 6x.txt testcache/file.txt
73go test testcache -run=Chdir
74! stdout '\(cached\)'
75go test testcache -run=Chdir
76stdout '\(cached\)'
77
78# The content of files should affect caching, provided that the mtime also changes.
79exec ./mkold$GOEXE 1m testcache/file.txt
80go test testcache -run=FileContent
81go test testcache -run=FileContent
82stdout '\(cached\)'
83cp 2y.txt testcache/file.txt
84exec ./mkold$GOEXE 50s testcache/file.txt
85go test testcache -run=FileContent
86! stdout '\(cached\)'
87go test testcache -run=FileContent
88stdout '\(cached\)'
89
90# Directory contents read via os.ReadDirNames should affect caching.
91go test testcache -run=DirList
92go test testcache -run=DirList
93stdout '\(cached\)'
94rm testcache/file.txt
95go test testcache -run=DirList
96! stdout '\(cached\)'
97go test testcache -run=DirList
98stdout '\(cached\)'
99
100# Files outside GOROOT and GOPATH should not affect caching.
101env TEST_EXTERNAL_FILE=$WORK/external.txt
102go test testcache -run=ExternalFile
103go test testcache -run=ExternalFile
104stdout '\(cached\)'
105
106rm $WORK/external.txt
107go test testcache -run=ExternalFile
108stdout '\(cached\)'
109
110# The -benchtime flag without -bench should not affect caching.
111go test testcache -run=Benchtime -benchtime=1x
112go test testcache -run=Benchtime -benchtime=1x
113stdout '\(cached\)'
114
115go test testcache -run=Benchtime -bench=Benchtime -benchtime=1x
116go test testcache -run=Benchtime -bench=Benchtime -benchtime=1x
117! stdout '\(cached\)'
118
119# golang.org/issue/47355: that includes the `-failfast` argument.
120go test testcache -run=TestOSArgs -failfast
121! stdout '\(cached\)'
122go test testcache -run=TestOSArgs -failfast
123stdout '\(cached\)'
124
125# golang.org/issue/64638: that includes the `-fullpath` argument.
126go test testcache -run=TestOSArgs -fullpath
127! stdout '\(cached\)'
128go test testcache -run=TestOSArgs -fullpath
129stdout '\(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
136chmod 0755 ./testcache/script.sh
137
138exec ./mkold$GOEXEC 1m testcache/script.sh
139go test testcache -run=Exec
140go test testcache -run=Exec
141stdout '\(cached\)'
142
143exec ./mkold$GOEXE 50s testcache/script.sh
144go test testcache -run=Exec
145! stdout '\(cached\)'
146go test testcache -run=Exec
147stdout '\(cached\)'
148
149-- testcache/file.txt --
150xx
151-- 4x.txt --
152xxxx
153-- 6x.txt --
154xxxxxx
155-- 2y.txt --
156yy
157-- $WORK/external.txt --
158This file is outside of GOPATH.
159-- testcache/script.sh --
160#!/bin/sh
161exit 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
167package testcache
168
169import (
170 "io"
171 "os"
172 "testing"
173)
174
175func 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
187func 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
202func 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
212func TestOddGetenv(t *testing.T) {
213 val := os.Getenv("TESTKEY")
214 if len(val)%2 != 1 {
215 t.Fatal("even env value")
216 }
217}
218
219func TestLookupEnv(t *testing.T) {
220 _, ok := os.LookupEnv("TESTKEY")
221 if !ok {
222 t.Fatal("env missing")
223 }
224}
225
226func 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
235func 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
250func 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
258func TestOSArgs(t *testing.T) {
259 t.Log(os.Args)
260}
261
262func TestBenchtime(t *testing.T) {
263}
264
265-- mkold.go --
266package main
267
268import (
269 "log"
270 "os"
271 "time"
272)
273
274func 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}
View as plain text