From a9604b7d4094132850e5f5b8ecfd188223f71e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 4 Oct 2026 12:40:22 +0100 Subject: [PATCH 1/2] testscript: test that the work directory root is removed testscript removes each script's work directory with all its contents, but the directory holding them only with os.Remove, which fails if it is not empty. A Setup function which creates files next to the work directories, such as a shared configuration file, therefore leaves a go-test-script directory behind in the system temporary directory. --- testscript/testscript_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/testscript/testscript_test.go b/testscript/testscript_test.go index 437c1e2..ddeffa5 100644 --- a/testscript/testscript_test.go +++ b/testscript/testscript_test.go @@ -430,6 +430,35 @@ 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) + } + // TODO: the go-test-script directory should be removed too. + if len(files) != 1 { + 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) { From 03e1badad36a2b900a039b05a5958e06e5e1e286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 4 Oct 2026 12:40:31 +0100 Subject: [PATCH 2/2] testscript: remove the work directory root with all its contents Use removeAll rather than os.Remove, so that files created by Setup functions next to the work directories do not keep it around. The directory is created by testscript itself and is only removed when work directories are not kept, so its contents are never wanted afterwards. --- testscript/testscript.go | 6 ++++-- testscript/testscript_test.go | 3 +-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/testscript/testscript.go b/testscript/testscript.go index 57ef3d5..d69ce5a 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -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() } diff --git a/testscript/testscript_test.go b/testscript/testscript_test.go index ddeffa5..9169cf5 100644 --- a/testscript/testscript_test.go +++ b/testscript/testscript_test.go @@ -453,8 +453,7 @@ func TestTempDirRemoved(t *testing.T) { if err != nil { t.Fatal(err) } - // TODO: the go-test-script directory should be removed too. - if len(files) != 1 { + if len(files) != 0 { t.Fatalf("unexpected files left in GOTMPDIR: %v", files) } }