Skip to content

Commit fd276da

Browse files
authored
Run the cleanup routines even if the main goroutine panics (#882)
We should stop any running containers on panic, and things like complement-crypto expect their cleanup handlers to be run.
1 parent 6ac42a5 commit fd276da

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

test_main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,20 @@ func TestMain(m *testing.M, namespace string, customOpts ...opt) {
7676
fmt.Printf("Error: %s", err)
7777
os.Exit(1)
7878
}
79-
exitCode := m.Run()
80-
if opts.cleanup != nil {
81-
opts.cleanup(testPackage.Config)
79+
80+
// Run the cleanup functions even on panic.
81+
// Note that deferred functions aren't run on `os.Exit`, so we need to put the `defer` calls
82+
// inside a new `func()`.
83+
runAndCleanup := func() int {
84+
defer testPackage.Cleanup()
85+
if opts.cleanup != nil {
86+
defer opts.cleanup(testPackage.Config)
87+
}
88+
89+
return m.Run()
8290
}
83-
testPackage.Cleanup()
84-
os.Exit(exitCode)
91+
92+
os.Exit(runAndCleanup())
8593
}
8694

8795
// Deploy will deploy the given blueprint or terminate the test.

0 commit comments

Comments
 (0)