Skip to content

Commit 50426be

Browse files
authored
testscript: create a hidden temp directory by default (#135)
When running a testscript, we currently create a temporary directory in $WORK/tmp. This is problematic because it interacts badly with cmd/go (which is used by go/packages) trying to resolve the ./... pattern from $WORK. Instead establish a temporary directory in workdir, but use a prefix that ensures this directory will not be walked when resolving the ./... pattern from workdir.
1 parent 2630b2f commit 50426be

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

cmd/testscript/testdata/work.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
unquote file.txt dir/file.txt
1010

1111
testscript -v -work file.txt dir/file.txt
12-
stderr '^temporary work directory: \Q'$WORK'\E[/\\]tmp[/\\]'
13-
stderr '^temporary work directory for file.txt: \Q'$WORK'\E[/\\]tmp[/\\]'
14-
stderr '^temporary work directory for dir[/\\]file.txt: \Q'$WORK'\E[/\\]tmp[/\\]'
15-
expandone $WORK/tmp/testscript*/file.txt/script.txt
16-
expandone $WORK/tmp/testscript*/file.txt1/script.txt
12+
stderr '^temporary work directory: \Q'$WORK'\E[/\\]\.tmp[/\\]'
13+
stderr '^temporary work directory for file.txt: \Q'$WORK'\E[/\\]\.tmp[/\\]'
14+
stderr '^temporary work directory for dir[/\\]file.txt: \Q'$WORK'\E[/\\]\.tmp[/\\]'
15+
expandone $WORK/.tmp/testscript*/file.txt/script.txt
16+
expandone $WORK/.tmp/testscript*/file.txt1/script.txt
1717

1818
-- file.txt --
1919
>exec true

testscript/testdata/setupfiles.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# check that the Setup function saw the unarchived files,
22
# including the temp directory that's always created.
3-
setup-filenames a b tmp
3+
setup-filenames .tmp a b
44

55
-- a --
66
-- b/c --

testscript/testscript.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,23 @@ type backgroundCmd struct {
289289
// It returns the comment section of the txtar archive.
290290
func (ts *TestScript) setup() string {
291291
ts.workdir = filepath.Join(ts.testTempDir, "script-"+ts.name)
292-
ts.Check(os.MkdirAll(filepath.Join(ts.workdir, "tmp"), 0777))
292+
293+
// Establish a temporary directory in workdir, but use a prefix that ensures
294+
// this directory will not be walked when resolving the ./... pattern from
295+
// workdir. This is important because when resolving a ./... pattern, cmd/go
296+
// (which is used by go/packages) creates temporary build files and
297+
// directories. This can, and does, therefore interfere with the ./...
298+
// pattern when used from workdir and can lead to race conditions within
299+
// cmd/go as it walks directories to match the ./... pattern.
300+
tmpDir := filepath.Join(ts.workdir, ".tmp")
301+
302+
ts.Check(os.MkdirAll(tmpDir, 0777))
293303
env := &Env{
294304
Vars: []string{
295305
"WORK=" + ts.workdir, // must be first for ts.abbrev
296306
"PATH=" + os.Getenv("PATH"),
297307
homeEnvName() + "=/no-home",
298-
tempEnvName() + "=" + filepath.Join(ts.workdir, "tmp"),
308+
tempEnvName() + "=" + tmpDir,
299309
"devnull=" + os.DevNull,
300310
"/=" + string(os.PathSeparator),
301311
":=" + string(os.PathListSeparator),

0 commit comments

Comments
 (0)