Skip to content

Commit 104f07c

Browse files
author
Bryan C. Mills
committed
cmd/go: convert TestTestCacheInputs to a script test
Updates #28387 Updates #30316 Change-Id: I48c6dd8619ea9602e9617ce11dfa05f1c70a485d Reviewed-on: https://go-review.googlesource.com/c/go/+/207958 Reviewed-by: Jay Conrod <[email protected]>
1 parent 96c9f3a commit 104f07c

File tree

4 files changed

+231
-208
lines changed

4 files changed

+231
-208
lines changed

src/cmd/go/go_test.go

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4870,122 +4870,6 @@ func TestTestCache(t *testing.T) {
48704870
}
48714871
}
48724872

4873-
func TestTestCacheInputs(t *testing.T) {
4874-
tooSlow(t)
4875-
4876-
if strings.Contains(os.Getenv("GODEBUG"), "gocacheverify") {
4877-
t.Skip("GODEBUG gocacheverify")
4878-
}
4879-
tg := testgo(t)
4880-
defer tg.cleanup()
4881-
tg.parallel()
4882-
tg.makeTempdir()
4883-
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
4884-
tg.setenv("GOCACHE", tg.path("cache"))
4885-
4886-
defer os.Remove(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt"))
4887-
defer os.Remove(filepath.Join(tg.pwd(), "testdata/src/testcache/script.sh"))
4888-
tg.must(ioutil.WriteFile(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt"), []byte("x"), 0644))
4889-
old := time.Now().Add(-1 * time.Minute)
4890-
tg.must(os.Chtimes(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt"), old, old))
4891-
info, err := os.Stat(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt"))
4892-
if err != nil {
4893-
t.Fatal(err)
4894-
}
4895-
t.Logf("file.txt: old=%v, info.ModTime=%v", old, info.ModTime()) // help debug when Chtimes lies about succeeding
4896-
tg.setenv("TESTKEY", "x")
4897-
4898-
tg.must(ioutil.WriteFile(filepath.Join(tg.pwd(), "testdata/src/testcache/script.sh"), []byte("#!/bin/sh\nexit 0\n"), 0755))
4899-
tg.must(os.Chtimes(filepath.Join(tg.pwd(), "testdata/src/testcache/script.sh"), old, old))
4900-
4901-
tg.run("test", "testcache")
4902-
tg.run("test", "testcache")
4903-
tg.grepStdout(`\(cached\)`, "did not cache")
4904-
4905-
tg.setenv("TESTKEY", "y")
4906-
tg.run("test", "testcache")
4907-
tg.grepStdoutNot(`\(cached\)`, "did not notice env var change")
4908-
tg.run("test", "testcache")
4909-
tg.grepStdout(`\(cached\)`, "did not cache")
4910-
4911-
tg.run("test", "testcache", "-run=FileSize")
4912-
tg.run("test", "testcache", "-run=FileSize")
4913-
tg.grepStdout(`\(cached\)`, "did not cache")
4914-
tg.must(ioutil.WriteFile(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt"), []byte("xxx"), 0644))
4915-
tg.run("test", "testcache", "-run=FileSize")
4916-
tg.grepStdoutNot(`\(cached\)`, "did not notice file size change")
4917-
tg.run("test", "testcache", "-run=FileSize")
4918-
tg.grepStdout(`\(cached\)`, "did not cache")
4919-
4920-
tg.run("test", "testcache", "-run=Chdir")
4921-
tg.run("test", "testcache", "-run=Chdir")
4922-
tg.grepStdout(`\(cached\)`, "did not cache")
4923-
tg.must(ioutil.WriteFile(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt"), []byte("xxxxx"), 0644))
4924-
tg.run("test", "testcache", "-run=Chdir")
4925-
tg.grepStdoutNot(`\(cached\)`, "did not notice file size change")
4926-
tg.run("test", "testcache", "-run=Chdir")
4927-
tg.grepStdout(`\(cached\)`, "did not cache")
4928-
4929-
tg.must(os.Chtimes(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt"), old, old))
4930-
tg.run("test", "testcache", "-run=FileContent")
4931-
tg.run("test", "testcache", "-run=FileContent")
4932-
tg.grepStdout(`\(cached\)`, "did not cache")
4933-
tg.must(ioutil.WriteFile(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt"), []byte("yyy"), 0644))
4934-
old2 := old.Add(10 * time.Second)
4935-
tg.must(os.Chtimes(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt"), old2, old2))
4936-
tg.run("test", "testcache", "-run=FileContent")
4937-
tg.grepStdoutNot(`\(cached\)`, "did not notice file content change")
4938-
tg.run("test", "testcache", "-run=FileContent")
4939-
tg.grepStdout(`\(cached\)`, "did not cache")
4940-
4941-
tg.run("test", "testcache", "-run=DirList")
4942-
tg.run("test", "testcache", "-run=DirList")
4943-
tg.grepStdout(`\(cached\)`, "did not cache")
4944-
tg.must(os.Remove(filepath.Join(tg.pwd(), "testdata/src/testcache/file.txt")))
4945-
tg.run("test", "testcache", "-run=DirList")
4946-
tg.grepStdoutNot(`\(cached\)`, "did not notice directory change")
4947-
tg.run("test", "testcache", "-run=DirList")
4948-
tg.grepStdout(`\(cached\)`, "did not cache")
4949-
4950-
tg.tempFile("file.txt", "")
4951-
tg.must(ioutil.WriteFile(filepath.Join(tg.pwd(), "testdata/src/testcache/testcachetmp_test.go"), []byte(`package testcache
4952-
4953-
import (
4954-
"os"
4955-
"testing"
4956-
)
4957-
4958-
func TestExternalFile(t *testing.T) {
4959-
os.Open(`+fmt.Sprintf("%q", tg.path("file.txt"))+`)
4960-
_, err := os.Stat(`+fmt.Sprintf("%q", tg.path("file.txt"))+`)
4961-
if err != nil {
4962-
t.Fatal(err)
4963-
}
4964-
}
4965-
`), 0666))
4966-
defer os.Remove(filepath.Join(tg.pwd(), "testdata/src/testcache/testcachetmp_test.go"))
4967-
tg.run("test", "testcache", "-run=ExternalFile")
4968-
tg.run("test", "testcache", "-run=ExternalFile")
4969-
tg.grepStdout(`\(cached\)`, "did not cache")
4970-
tg.must(os.Remove(filepath.Join(tg.tempdir, "file.txt")))
4971-
tg.run("test", "testcache", "-run=ExternalFile")
4972-
tg.grepStdout(`\(cached\)`, "did not cache")
4973-
4974-
switch runtime.GOOS {
4975-
case "plan9", "windows":
4976-
// no shell scripts
4977-
default:
4978-
tg.run("test", "testcache", "-run=Exec")
4979-
tg.run("test", "testcache", "-run=Exec")
4980-
tg.grepStdout(`\(cached\)`, "did not cache")
4981-
tg.must(os.Chtimes(filepath.Join(tg.pwd(), "testdata/src/testcache/script.sh"), old2, old2))
4982-
tg.run("test", "testcache", "-run=Exec")
4983-
tg.grepStdoutNot(`\(cached\)`, "did not notice script change")
4984-
tg.run("test", "testcache", "-run=Exec")
4985-
tg.grepStdout(`\(cached\)`, "did not cache")
4986-
}
4987-
}
4988-
49894873
func TestTestVet(t *testing.T) {
49904874
tooSlow(t)
49914875
tg := testgo(t)

src/cmd/go/testdata/script/cache_vet.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
env GO111MODULE=off
22

33
[short] skip
4-
[GODEBUG:gocacheverify] skip
4+
[GODEBUG:gocacheverify=1] skip
55
[gccgo] skip # gccgo has no standard packages
66

77
# Start with a clean build cache:
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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+
env TESTKEY=y
27+
go test testcache -run=TestLookupEnv
28+
! stdout '\(cached\)'
29+
go test testcache -run=TestLookupEnv
30+
stdout '\(cached\)'
31+
32+
# If the test stats a file, changes to the file should invalidate the cache.
33+
go test testcache -run=FileSize
34+
go test testcache -run=FileSize
35+
stdout '\(cached\)'
36+
37+
cp 4x.txt testcache/file.txt
38+
go test testcache -run=FileSize
39+
! stdout '\(cached\)'
40+
go test testcache -run=FileSize
41+
stdout '\(cached\)'
42+
43+
# Files should be tracked even if the test changes its working directory.
44+
go test testcache -run=Chdir
45+
go test testcache -run=Chdir
46+
stdout '\(cached\)'
47+
cp 6x.txt testcache/file.txt
48+
go test testcache -run=Chdir
49+
! stdout '\(cached\)'
50+
go test testcache -run=Chdir
51+
stdout '\(cached\)'
52+
53+
# The content of files should affect caching, provided that the mtime also changes.
54+
exec ./mkold$GOEXE 1m testcache/file.txt
55+
go test testcache -run=FileContent
56+
go test testcache -run=FileContent
57+
stdout '\(cached\)'
58+
cp 2y.txt testcache/file.txt
59+
exec ./mkold$GOEXE 50s testcache/file.txt
60+
go test testcache -run=FileContent
61+
! stdout '\(cached\)'
62+
go test testcache -run=FileContent
63+
stdout '\(cached\)'
64+
65+
# Directory contents read via os.ReadDirNames should affect caching.
66+
go test testcache -run=DirList
67+
go test testcache -run=DirList
68+
stdout '\(cached\)'
69+
rm testcache/file.txt
70+
go test testcache -run=DirList
71+
! stdout '\(cached\)'
72+
go test testcache -run=DirList
73+
stdout '\(cached\)'
74+
75+
# Files outside GOROOT and GOPATH should not affect caching.
76+
env TEST_EXTERNAL_FILE=$WORK/external.txt
77+
go test testcache -run=ExternalFile
78+
go test testcache -run=ExternalFile
79+
stdout '\(cached\)'
80+
81+
rm $WORK/external.txt
82+
go test testcache -run=ExternalFile
83+
stdout '\(cached\)'
84+
85+
# Executables within GOROOT and GOPATH should affect caching,
86+
# even if the test does not stat them explicitly.
87+
88+
[!exec:/bin/sh] skip
89+
chmod 0755 ./testcache/script.sh
90+
91+
exec ./mkold$GOEXEC 1m testcache/script.sh
92+
go test testcache -run=Exec
93+
go test testcache -run=Exec
94+
stdout '\(cached\)'
95+
96+
exec ./mkold$GOEXE 50s testcache/script.sh
97+
go test testcache -run=Exec
98+
! stdout '\(cached\)'
99+
go test testcache -run=Exec
100+
stdout '\(cached\)'
101+
102+
-- testcache/file.txt --
103+
xx
104+
-- 4x.txt --
105+
xxxx
106+
-- 6x.txt --
107+
xxxxxx
108+
-- 2y.txt --
109+
yy
110+
-- $WORK/external.txt --
111+
This file is outside of GOPATH.
112+
-- testcache/script.sh --
113+
#!/bin/sh
114+
exit 0
115+
-- testcache/testcache_test.go --
116+
// Copyright 2017 The Go Authors. All rights reserved.
117+
// Use of this source code is governed by a BSD-style
118+
// license that can be found in the LICENSE file.
119+
120+
package testcache
121+
122+
import (
123+
"io/ioutil"
124+
"os"
125+
"testing"
126+
)
127+
128+
func TestChdir(t *testing.T) {
129+
os.Chdir("..")
130+
defer os.Chdir("testcache")
131+
info, err := os.Stat("testcache/file.txt")
132+
if err != nil {
133+
t.Fatal(err)
134+
}
135+
if info.Size()%2 != 1 {
136+
t.Fatal("even file")
137+
}
138+
}
139+
140+
func TestOddFileContent(t *testing.T) {
141+
f, err := os.Open("file.txt")
142+
if err != nil {
143+
t.Fatal(err)
144+
}
145+
data, err := ioutil.ReadAll(f)
146+
f.Close()
147+
if err != nil {
148+
t.Fatal(err)
149+
}
150+
if len(data)%2 != 1 {
151+
t.Fatal("even file")
152+
}
153+
}
154+
155+
func TestOddFileSize(t *testing.T) {
156+
info, err := os.Stat("file.txt")
157+
if err != nil {
158+
t.Fatal(err)
159+
}
160+
if info.Size()%2 != 1 {
161+
t.Fatal("even file")
162+
}
163+
}
164+
165+
func TestOddGetenv(t *testing.T) {
166+
val := os.Getenv("TESTKEY")
167+
if len(val)%2 != 1 {
168+
t.Fatal("even env value")
169+
}
170+
}
171+
172+
func TestLookupEnv(t *testing.T) {
173+
_, ok := os.LookupEnv("TESTKEY")
174+
if !ok {
175+
t.Fatal("env missing")
176+
}
177+
}
178+
179+
func TestDirList(t *testing.T) {
180+
f, err := os.Open(".")
181+
if err != nil {
182+
t.Fatal(err)
183+
}
184+
f.Readdirnames(-1)
185+
f.Close()
186+
}
187+
188+
func TestExec(t *testing.T) {
189+
// Note: not using os/exec to make sure there is no unexpected stat.
190+
p, err := os.StartProcess("./script.sh", []string{"script"}, new(os.ProcAttr))
191+
if err != nil {
192+
t.Fatal(err)
193+
}
194+
ps, err := p.Wait()
195+
if err != nil {
196+
t.Fatal(err)
197+
}
198+
if !ps.Success() {
199+
t.Fatalf("script failed: %v", err)
200+
}
201+
}
202+
203+
func TestExternalFile(t *testing.T) {
204+
os.Open(os.Getenv("TEST_EXTERNAL_FILE"))
205+
_, err := os.Stat(os.Getenv("TEST_EXTERNAL_FILE"))
206+
if err != nil {
207+
t.Fatal(err)
208+
}
209+
}
210+
-- mkold.go --
211+
package main
212+
213+
import (
214+
"log"
215+
"os"
216+
"time"
217+
)
218+
219+
func main() {
220+
d, err := time.ParseDuration(os.Args[1])
221+
if err != nil {
222+
log.Fatal(err)
223+
}
224+
path := os.Args[2]
225+
old := time.Now().Add(-d)
226+
err = os.Chtimes(path, old, old)
227+
if err != nil {
228+
log.Fatal(err)
229+
}
230+
}

0 commit comments

Comments
 (0)