Skip to content

Commit 1fc10ab

Browse files
committed
testscript: remove temp dirs when finishing once again
The recent transition from RunMain to Main meant that we started calling os.Exit directly when Main finished. However, we failed to spot that os.Exit would make our earlier deferred cleanup of the temporary directory not run at all. Spotted because my /tmp started getting filled up after a few hours of development, and I found a suspiciously large number of testscript-looking directories.
1 parent eb18234 commit 1fc10ab

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

testscript/exe.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ func Main(m TestingM, commands map[string]func()) {
5656
if err != nil {
5757
log.Fatalf("could not set up temporary directory: %v", err)
5858
}
59-
defer func() {
59+
cleanup := func() {
6060
if err := os.RemoveAll(tmpdir); err != nil {
6161
log.Fatalf("cannot delete temporary directory: %v", err)
6262
}
63-
}()
63+
}
64+
defer cleanup()
6465
bindir := filepath.Join(tmpdir, "bin")
6566
if err := os.MkdirAll(bindir, 0o777); err != nil {
6667
log.Fatalf("could not set up PATH binary directory: %v", err)
@@ -88,7 +89,11 @@ func Main(m TestingM, commands map[string]func()) {
8889
ts.cmdExec(neg, append([]string{name}, args...))
8990
}
9091
}
91-
os.Exit(m.Run())
92+
exit := m.Run()
93+
// The [os.Exit] below prevents defers from running, so we must clean up here.
94+
// The defer above is still useful in case we fail before we get to this point.
95+
cleanup()
96+
os.Exit(exit)
9297
}
9398
// The command being registered is being invoked, so run it, then exit.
9499
os.Args[0] = cmdName

0 commit comments

Comments
 (0)