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