Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testscript: create a hidden temp directory by default #135

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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