| 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 |
# GODEBUG is always read |
| 27 |
env GODEBUG=asdf=1 |
| 28 |
go test testcache -run=TestLookupEnv |
| 29 |
! stdout '\(cached\)' |
| 30 |
go test testcache -run=TestLookupEnv |
| 31 |
stdout '\(cached\)' |
| 32 |
env GODEBUG= |
| 33 |
|
| 34 |
env TESTKEY=y |
| 35 |
go test testcache -run=TestLookupEnv |
| 36 |
! stdout '\(cached\)' |
| 37 |
go test testcache -run=TestLookupEnv |
| 38 |
stdout '\(cached\)' |
| 39 |
|
| 40 |
# Changes in arguments forwarded to the test should invalidate cached test |
| 41 |
# results. |
| 42 |
go test testcache -run=TestOSArgs -v hello |
| 43 |
! stdout '\(cached\)' |
| 44 |
stdout 'hello' |
| 45 |
go test testcache -run=TestOSArgs -v goodbye |
| 46 |
! stdout '\(cached\)' |
| 47 |
stdout 'goodbye' |
| 48 |
|
| 49 |
# golang.org/issue/36134: that includes the `-timeout` argument. |
| 50 |
go test testcache -run=TestOSArgs -timeout=20m -v |
| 51 |
! stdout '\(cached\)' |
| 52 |
stdout '-test\.timeout[= ]20m' |
| 53 |
go test testcache -run=TestOSArgs -timeout=5s -v |
| 54 |
! stdout '\(cached\)' |
| 55 |
stdout '-test\.timeout[= ]5s' |
| 56 |
|
| 57 |
# If the test stats a file, changes to the file should invalidate the cache. |
| 58 |
go test testcache -run=FileSize |
| 59 |
go test testcache -run=FileSize |
| 60 |
stdout '\(cached\)' |
| 61 |
|
| 62 |
cp 4x.txt testcache/file.txt |
| 63 |
go test testcache -run=FileSize |
| 64 |
! stdout '\(cached\)' |
| 65 |
go test testcache -run=FileSize |
| 66 |
stdout '\(cached\)' |
| 67 |
|
| 68 |
# Files should be tracked even if the test changes its working directory. |
| 69 |
go test testcache -run=Chdir |
| 70 |
go test testcache -run=Chdir |
| 71 |
stdout '\(cached\)' |
| 72 |
cp 6x.txt testcache/file.txt |
| 73 |
go test testcache -run=Chdir |
| 74 |
! stdout '\(cached\)' |
| 75 |
go test testcache -run=Chdir |
| 76 |
stdout '\(cached\)' |
| 77 |
|
| 78 |
# The content of files should affect caching, provided that the mtime also changes. |
| 79 |
exec ./mkold$GOEXE 1m testcache/file.txt |
| 80 |
go test testcache -run=FileContent |
| 81 |
go test testcache -run=FileContent |
| 82 |
stdout '\(cached\)' |
| 83 |
cp 2y.txt testcache/file.txt |
| 84 |
exec ./mkold$GOEXE 50s testcache/file.txt |
| 85 |
go test testcache -run=FileContent |
| 86 |
! stdout '\(cached\)' |
| 87 |
go test testcache -run=FileContent |
| 88 |
stdout '\(cached\)' |
| 89 |
|
| 90 |
# Directory contents read via os.ReadDirNames should affect caching. |
| 91 |
go test testcache -run=DirList |
| 92 |
go test testcache -run=DirList |
| 93 |
stdout '\(cached\)' |
| 94 |
rm testcache/file.txt |
| 95 |
go test testcache -run=DirList |
| 96 |
! stdout '\(cached\)' |
| 97 |
go test testcache -run=DirList |
| 98 |
stdout '\(cached\)' |
| 99 |
|
| 100 |
# Files outside GOROOT and GOPATH should not affect caching. |
| 101 |
env TEST_EXTERNAL_FILE=$WORK/external.txt |
| 102 |
go test testcache -run=ExternalFile |
| 103 |
go test testcache -run=ExternalFile |
| 104 |
stdout '\(cached\)' |
| 105 |
|
| 106 |
rm $WORK/external.txt |
| 107 |
go test testcache -run=ExternalFile |
| 108 |
stdout '\(cached\)' |
| 109 |
|
| 110 |
# The -benchtime flag without -bench should not affect caching. |
| 111 |
go test testcache -run=Benchtime -benchtime=1x |
| 112 |
go test testcache -run=Benchtime -benchtime=1x |
| 113 |
stdout '\(cached\)' |
| 114 |
|
| 115 |
go test testcache -run=Benchtime -bench=Benchtime -benchtime=1x |
| 116 |
go test testcache -run=Benchtime -bench=Benchtime -benchtime=1x |
| 117 |
! stdout '\(cached\)' |
| 118 |
|
| 119 |
# golang.org/issue/47355: that includes the `-failfast` argument. |
| 120 |
go test testcache -run=TestOSArgs -failfast |
| 121 |
! stdout '\(cached\)' |
| 122 |
go test testcache -run=TestOSArgs -failfast |
| 123 |
stdout '\(cached\)' |
| 124 |
|
| 125 |
# golang.org/issue/64638: that includes the `-fullpath` argument. |
| 126 |
go test testcache -run=TestOSArgs -fullpath |
| 127 |
! stdout '\(cached\)' |
| 128 |
go test testcache -run=TestOSArgs -fullpath |
| 129 |
stdout '\(cached\)' |
| 130 |
|
| 131 |
# golang.org/issue/70692: that includes the `-skip` flag |
| 132 |
go test testcache -run=TestOdd -skip=TestOddFile |
| 133 |
! stdout '\(cached\)' |
| 134 |
go test testcache -run=TestOdd -skip=TestOddFile |
| 135 |
stdout '\(cached\)' |
| 136 |
|
| 137 |
# Ensure that coverage profiles are being cached. |
| 138 |
go test testcache -run=TestCoverageCache -coverprofile=coverage.out |
| 139 |
go test testcache -run=TestCoverageCache -coverprofile=coverage.out |
| 140 |
stdout '\(cached\)' |
| 141 |
exists coverage.out |
| 142 |
grep -q 'mode: set' coverage.out |
| 143 |
grep -q 'testcache/hello.go:' coverage.out |
| 144 |
|
| 145 |
# A new -coverprofile file should use the cached coverage profile contents. |
| 146 |
go test testcache -run=TestCoverageCache -coverprofile=coverage2.out |
| 147 |
stdout '\(cached\)' |
| 148 |
cmp coverage.out coverage2.out |
| 149 |
|
| 150 |
# Explicitly setting the default covermode should still use cache. |
| 151 |
go test testcache -run=TestCoverageCache -coverprofile=coverage_set.out -covermode=set |
| 152 |
stdout '\(cached\)' |
| 153 |
cmp coverage.out coverage_set.out |
| 154 |
|
| 155 |
# A new -covermode should not use the cached coverage profile. |
| 156 |
go test testcache -run=TestCoverageCache -coverprofile=coverage_atomic.out -covermode=atomic |
| 157 |
! stdout '\(cached\)' |
| 158 |
! cmp coverage.out coverage_atomic.out |
| 159 |
grep -q 'mode: atomic' coverage_atomic.out |
| 160 |
grep -q 'testcache/hello.go:' coverage_atomic.out |
| 161 |
|
| 162 |
# A new -coverpkg should not use the cached coverage profile. |
| 163 |
go 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. |
| 168 |
go test testcache -v -run=TestCoverageCache -coverprofile=coverage_v.out |
| 169 |
go test testcache -v -run=TestCoverageCache -coverprofile=coverage_v2.out |
| 170 |
stdout '\(cached\)' |
| 171 |
cmp coverage_v.out coverage_v2.out |
| 172 |
|
| 173 |
# Test that -count affects caching. |
| 174 |
go 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 |
| 181 |
chmod 0755 ./testcache/script.sh |
| 182 |
|
| 183 |
exec ./mkold$GOEXEC 1m testcache/script.sh |
| 184 |
go test testcache -run=Exec |
| 185 |
go test testcache -run=Exec |
| 186 |
stdout '\(cached\)' |
| 187 |
|
| 188 |
exec ./mkold$GOEXE 50s testcache/script.sh |
| 189 |
go test testcache -run=Exec |
| 190 |
! stdout '\(cached\)' |
| 191 |
go test testcache -run=Exec |
| 192 |
stdout '\(cached\)' |
| 193 |
|
| 194 |
-- testcache/file.txt -- |
| 195 |
xx |
| 196 |
-- 4x.txt -- |
| 197 |
xxxx |
| 198 |
-- 6x.txt -- |
| 199 |
xxxxxx |
| 200 |
-- 2y.txt -- |
| 201 |
yy |
| 202 |
-- $WORK/external.txt -- |
| 203 |
This file is outside of GOPATH. |
| 204 |
-- testcache/script.sh -- |
| 205 |
#!/bin/sh |
| 206 |
exit 0 |
| 207 |
-- testcache/hello.go -- |
| 208 |
package testcache |
| 209 |
|
| 210 |
import "fmt" |
| 211 |
|
| 212 |
func 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 |
|
| 224 |
package testcache |
| 225 |
|
| 226 |
import ( |
| 227 |
"io" |
| 228 |
"os" |
| 229 |
"testing" |
| 230 |
) |
| 231 |
|
| 232 |
func 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 |
|
| 244 |
func 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 |
|
| 259 |
func 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 |
|
| 269 |
func TestOddGetenv(t *testing.T) { |
| 270 |
val := os.Getenv("TESTKEY") |
| 271 |
if len(val)%2 != 1 { |
| 272 |
t.Fatal("even env value") |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
func TestLookupEnv(t *testing.T) { |
| 277 |
_, ok := os.LookupEnv("TESTKEY") |
| 278 |
if !ok { |
| 279 |
t.Fatal("env missing") |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
func 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 |
|
| 292 |
func 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 |
|
| 307 |
func 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 |
|
| 315 |
func TestOSArgs(t *testing.T) { |
| 316 |
t.Log(os.Args) |
| 317 |
} |
| 318 |
|
| 319 |
func TestBenchtime(t *testing.T) { |
| 320 |
} |
| 321 |
|
| 322 |
func 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 -- |
| 335 |
package main |
| 336 |
|
| 337 |
import ( |
| 338 |
"log" |
| 339 |
"os" |
| 340 |
"time" |
| 341 |
) |
| 342 |
|
| 343 |
func 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 |
} |