Skip to content

Commit

Permalink
testscript: create a hidden temp directory by default (#135)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
myitcv committed Mar 12, 2021
1 parent 2630b2f commit 50426be
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 5 additions & 5 deletions cmd/testscript/testdata/work.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
unquote file.txt dir/file.txt

testscript -v -work file.txt dir/file.txt
stderr '^temporary work directory: \Q'$WORK'\E[/\\]tmp[/\\]'
stderr '^temporary work directory for file.txt: \Q'$WORK'\E[/\\]tmp[/\\]'
stderr '^temporary work directory for dir[/\\]file.txt: \Q'$WORK'\E[/\\]tmp[/\\]'
expandone $WORK/tmp/testscript*/file.txt/script.txt
expandone $WORK/tmp/testscript*/file.txt1/script.txt
stderr '^temporary work directory: \Q'$WORK'\E[/\\]\.tmp[/\\]'
stderr '^temporary work directory for file.txt: \Q'$WORK'\E[/\\]\.tmp[/\\]'
stderr '^temporary work directory for dir[/\\]file.txt: \Q'$WORK'\E[/\\]\.tmp[/\\]'
expandone $WORK/.tmp/testscript*/file.txt/script.txt
expandone $WORK/.tmp/testscript*/file.txt1/script.txt

-- file.txt --
>exec true
Expand Down
2 changes: 1 addition & 1 deletion testscript/testdata/setupfiles.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# check that the Setup function saw the unarchived files,
# including the temp directory that's always created.
setup-filenames a b tmp
setup-filenames .tmp a b

-- a --
-- b/c --
14 changes: 12 additions & 2 deletions testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,23 @@ type backgroundCmd struct {
// It returns the comment section of the txtar archive.
func (ts *TestScript) setup() string {
ts.workdir = filepath.Join(ts.testTempDir, "script-"+ts.name)
ts.Check(os.MkdirAll(filepath.Join(ts.workdir, "tmp"), 0777))

// Establish a temporary directory in workdir, but use a prefix that ensures
// this directory will not be walked when resolving the ./... pattern from
// workdir. This is important because when resolving a ./... pattern, cmd/go
// (which is used by go/packages) creates temporary build files and
// directories. This can, and does, therefore interfere with the ./...
// pattern when used from workdir and can lead to race conditions within
// cmd/go as it walks directories to match the ./... pattern.
tmpDir := filepath.Join(ts.workdir, ".tmp")

ts.Check(os.MkdirAll(tmpDir, 0777))
env := &Env{
Vars: []string{
"WORK=" + ts.workdir, // must be first for ts.abbrev
"PATH=" + os.Getenv("PATH"),
homeEnvName() + "=/no-home",
tempEnvName() + "=" + filepath.Join(ts.workdir, "tmp"),
tempEnvName() + "=" + tmpDir,
"devnull=" + os.DevNull,
"/=" + string(os.PathSeparator),
":=" + string(os.PathListSeparator),
Expand Down

0 comments on commit 50426be

Please sign in to comment.