...
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# golang.org/issue/70692: that includes the `-skip` flag
132go test testcache -run=TestOdd -skip=TestOddFile
133! stdout '\(cached\)'
134go test testcache -run=TestOdd -skip=TestOddFile
135stdout '\(cached\)'
136
137# Ensure that coverage profiles are being cached.
138go test testcache -run=TestCoverageCache -coverprofile=coverage.out
139go test testcache -run=TestCoverageCache -coverprofile=coverage.out
140stdout '\(cached\)'
141exists coverage.out
142grep -q 'mode: set' coverage.out
143grep -q 'testcache/hello.go:' coverage.out
144
145# A new -coverprofile file should use the cached coverage profile contents.
146go test testcache -run=TestCoverageCache -coverprofile=coverage2.out
147stdout '\(cached\)'
148cmp coverage.out coverage2.out
149
150# Explicitly setting the default covermode should still use cache.
151go test testcache -run=TestCoverageCache -coverprofile=coverage_set.out -covermode=set
152stdout '\(cached\)'
153cmp coverage.out coverage_set.out
154
155# A new -covermode should not use the cached coverage profile.
156go test testcache -run=TestCoverageCache -coverprofile=coverage_atomic.out -covermode=atomic
157! stdout '\(cached\)'
158! cmp coverage.out coverage_atomic.out
159grep -q 'mode: atomic' coverage_atomic.out
160grep -q 'testcache/hello.go:' coverage_atomic.out
161
162# A new -coverpkg should not use the cached coverage profile.
163go test testcache -run=TestCoverageCache -coverprofile=coverage_pkg.out -coverpkg=all
164! stdout '\(cached\)'
165! cmp coverage.out coverage_pkg.out
166
167# Test that -v doesn't prevent caching.
168go test testcache -v -run=TestCoverageCache -coverprofile=coverage_v.out
169go test testcache -v -run=TestCoverageCache -coverprofile=coverage_v2.out
170stdout '\(cached\)'
171cmp coverage_v.out coverage_v2.out
172
173# Test that -count affects caching.
174go test testcache -run=TestCoverageCache -coverprofile=coverage_count.out -count=2
175! stdout '\(cached\)'
176
177# Executables within GOROOT and GOPATH should affect caching,
178# even if the test does not stat them explicitly.
179
180[!exec:/bin/sh] skip
181chmod 0755 ./testcache/script.sh
182
183exec ./mkold$GOEXEC 1m testcache/script.sh
184go test testcache -run=Exec
185go test testcache -run=Exec
186stdout '\(cached\)'
187
188exec ./mkold$GOEXE 50s testcache/script.sh
189go test testcache -run=Exec
190! stdout '\(cached\)'
191go test testcache -run=Exec
192stdout '\(cached\)'
193
194-- testcache/file.txt --
195xx
196-- 4x.txt --
197xxxx
198-- 6x.txt --
199xxxxxx
200-- 2y.txt --
201yy
202-- $WORK/external.txt --
203This file is outside of GOPATH.
204-- testcache/script.sh --
205#!/bin/sh
206exit 0
207-- testcache/hello.go --
208package testcache
209
210import "fmt"
211
212func HelloWorld(name string) string {
213 if name == "" {
214 return "Hello, World!"
215 }
216 return fmt.Sprintf("Hello, %s!", name)
217}
218
219-- testcache/testcache_test.go --
220// Copyright 2017 The Go Authors. All rights reserved.
221// Use of this source code is governed by a BSD-style
222// license that can be found in the LICENSE file.
223
224package testcache
225
226import (
227 "io"
228 "os"
229 "testing"
230)
231
232func TestChdir(t *testing.T) {
233 os.Chdir("..")
234 defer os.Chdir("testcache")
235 info, err := os.Stat("testcache/file.txt")
236 if err != nil {
237 t.Fatal(err)
238 }
239 if info.Size()%2 != 1 {
240 t.Fatal("even file")
241 }
242}
243
244func TestOddFileContent(t *testing.T) {
245 f, err := os.Open("file.txt")
246 if err != nil {
247 t.Fatal(err)
248 }
249 data, err := io.ReadAll(f)
250 f.Close()
251 if err != nil {
252 t.Fatal(err)
253 }
254 if len(data)%2 != 1 {
255 t.Fatal("even file")
256 }
257}
258
259func TestOddFileSize(t *testing.T) {
260 info, err := os.Stat("file.txt")
261 if err != nil {
262 t.Fatal(err)
263 }
264 if info.Size()%2 != 1 {
265 t.Fatal("even file")
266 }
267}
268
269func TestOddGetenv(t *testing.T) {
270 val := os.Getenv("TESTKEY")
271 if len(val)%2 != 1 {
272 t.Fatal("even env value")
273 }
274}
275
276func TestLookupEnv(t *testing.T) {
277 _, ok := os.LookupEnv("TESTKEY")
278 if !ok {
279 t.Fatal("env missing")
280 }
281}
282
283func TestDirList(t *testing.T) {
284 f, err := os.Open(".")
285 if err != nil {
286 t.Fatal(err)
287 }
288 f.Readdirnames(-1)
289 f.Close()
290}
291
292func TestExec(t *testing.T) {
293 // Note: not using os/exec to make sure there is no unexpected stat.
294 p, err := os.StartProcess("./script.sh", []string{"script"}, new(os.ProcAttr))
295 if err != nil {
296 t.Fatal(err)
297 }
298 ps, err := p.Wait()
299 if err != nil {
300 t.Fatal(err)
301 }
302 if !ps.Success() {
303 t.Fatalf("script failed: %v", err)
304 }
305}
306
307func TestExternalFile(t *testing.T) {
308 os.Open(os.Getenv("TEST_EXTERNAL_FILE"))
309 _, err := os.Stat(os.Getenv("TEST_EXTERNAL_FILE"))
310 if err != nil {
311 t.Fatal(err)
312 }
313}
314
315func TestOSArgs(t *testing.T) {
316 t.Log(os.Args)
317}
318
319func TestBenchtime(t *testing.T) {
320}
321
322func TestCoverageCache(t *testing.T) {
323 result := HelloWorld("")
324 if result != "Hello, World!" {
325 t.Errorf("Expected 'Hello, World!', got '%s'", result)
326 }
327
328 result = HelloWorld("Go")
329 if result != "Hello, Go!" {
330 t.Errorf("Expected 'Hello, Go!', got '%s'", result)
331 }
332}
333
334-- mkold.go --
335package main
336
337import (
338 "log"
339 "os"
340 "time"
341)
342
343func main() {
344 d, err := time.ParseDuration(os.Args[1])
345 if err != nil {
346 log.Fatal(err)
347 }
348 path := os.Args[2]
349 old := time.Now().Add(-d)
350 err = os.Chtimes(path, old, old)
351 if err != nil {
352 log.Fatal(err)
353 }
354}
View as plain text