Skip to content
Merged
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
18 changes: 13 additions & 5 deletions test_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,20 @@ func TestMain(m *testing.M, namespace string, customOpts ...opt) {
fmt.Printf("Error: %s", err)
os.Exit(1)
}
exitCode := m.Run()
if opts.cleanup != nil {
opts.cleanup(testPackage.Config)

// Run the cleanup functions even on panic.
Comment thread
richvdh marked this conversation as resolved.
// Note that deferred functions aren't run on `os.Exit`, so we need to put the `defer` calls
// inside a new `func()`.
runAndCleanup := func() int {
defer testPackage.Cleanup()
if opts.cleanup != nil {
defer opts.cleanup(testPackage.Config)
}

return m.Run()
}
testPackage.Cleanup()
os.Exit(exitCode)

os.Exit(runAndCleanup())
Comment on lines +80 to +92

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this also work? (less layers)

Suggested change
// Run the cleanup functions even on panic.
runAndCleanup := func() int {
defer testPackage.Cleanup()
if opts.cleanup != nil {
defer opts.cleanup(testPackage.Config)
}
return m.Run()
}
testPackage.Cleanup()
os.Exit(exitCode)
os.Exit(runAndCleanup())
defer testPackage.Cleanup()
if opts.cleanup != nil {
defer opts.cleanup(testPackage.Config)
}
exitCode := m.Run()
os.Exit(exitCode)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Per https://pkg.go.dev/os#Exit, deferred functions are not run on os.Exit. The extra layer gives the deferred functions a chance to run.

I'll add a comment to clarify this.

}

// Deploy will deploy the given blueprint or terminate the test.
Expand Down
Loading