3

I'm writing some rather complex unit tests in Go and I have something like:

err = os.Mkdir(tempDir, 0755)
assert.NoError(t, err, "error creating temporary directory")
defer func() {
    log.Info("cleaning up temporary directory '%s'..", tempDir)
    err = os.RemoveAll(tempDir)
    assert.NoError(t, err, "error removing temporary directory")
}()

The problem is that this won't work and temporary dir won't be deleted if I hit Ctrl+C to interrupt the test for whatever the reason (it generates temporary data and it takes a lot of time, it can be beneficial to just SIGINT the process if I realize something is wrong).

Any clean way to do this?

Dean
  • 6,610
  • 6
  • 40
  • 90
  • 4
    If you want the deletion to continue when CTRL+C is pressed, then handle the interrupt and don't let the app to terminate. – icza Mar 04 '22 at 13:09
  • I believe this answers your question: https://stackoverflow.com/questions/11268943/is-it-possible-to-capture-a-ctrlc-signal-and-run-a-cleanup-function-in-a-defe – Vanessa Andre Mar 05 '22 at 06:05
  • 1
    Does this answer your question? [Clean data after testing](https://stackoverflow.com/questions/51239363/clean-data-after-testing) –  Mar 05 '22 at 09:49

0 Answers0