Skip to content
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
6 changes: 4 additions & 2 deletions testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,10 @@ func RunT(t T, p Params) {
removeAll(ts.workdir)
if atomic.AddInt32(&refCount, -1) == 0 {
// This is the last subtest to finish. Remove the
// parent directory too, and cancel the context.
os.Remove(testTempDir)
// parent directory too, along with anything that Setup
// functions may have created next to the work
// directories, and cancel the context.
removeAll(testTempDir)
if cancel != nil {
cancel()
}
Expand Down
28 changes: 28 additions & 0 deletions testscript/testscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,34 @@ func TestWorkdirRoot(t *testing.T) {
}
}

// TestTempDirRemoved tests that the directory holding the work directories is
// removed once the tests are done, even if Setup created files next to them.
func TestTempDirRemoved(t *testing.T) {
if *testWork {
t.Skip("-testwork keeps the work directories")
}
td := t.TempDir()
t.Setenv("GOTMPDIR", td)
params := Params{
Dir: filepath.Join("testdata", "nothing"),
Setup: func(env *Env) error {
return os.WriteFile(filepath.Join(env.WorkDir, "..", "shared"), nil, 0o666)
},
}
// Run as a sub-test so that this call blocks until the sub-tests created by
// calling Run (which themselves call t.Parallel) complete.
t.Run("run tests", func(t *testing.T) {
Run(t, params)
})
files, err := os.ReadDir(td)
if err != nil {
t.Fatal(err)
}
if len(files) != 0 {
t.Fatalf("unexpected files left in GOTMPDIR: %v", files)
}
}

// TestBadDir verifies that invoking testscript with a directory that either
// does not exist or that contains no *.txt scripts fails the test
func TestBadDir(t *testing.T) {
Expand Down
Loading