unittest2.nim: ensure the testTeardownIMPL is performed at the end (#35)

* unittest2.nim: ensure the testTeardownIMPL is performed at the end

This is important to prevent failing tests
to fail in an uncontrolled way and generating a SIGSEGV
(segmentation fault)

Particularly, when the test body raises an exception,
e.g. "assert false", what happened was that the "testTeardownIMPL"
was invoked before the exception handlging and that caused
errors like:

Traceback (most recent call last, using override)
/home/ivansete/workspace/status/nwaku/vendor/nim-unittest2/unittest2.nim(1154)
unittest2
/home/ivansete/workspace/status/nwaku/vendor/nim-unittest2/unittest2.nim(1086)
runDirect
/home/ivansete/workspace/status/nwaku/vendor/nim-unittest2/unittest2.nim(1111)
runTestX60gensym398
/home/ivansete/workspace/status/nwaku/vendor/nimbus-build-system/vendor/Nim/lib/system/excpt.nim(631)
signalHandler
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Segmentation fault (core dumped)
This commit is contained in:
Ivan FB 2024-01-12 17:17:30 +01:00 committed by GitHub
parent 262b697f38
commit db67e2ad76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 2 deletions

View File

@ -1105,8 +1105,6 @@ template runtimeTest*(nameParam: string, body: untyped) =
try:
when declared(testSetupIMPLFlag): testSetupIMPL()
when declared(testTeardownIMPLFlag):
defer: testTeardownIMPL()
block:
body
@ -1126,6 +1124,15 @@ template runtimeTest*(nameParam: string, body: untyped) =
checkpoint("Unhandled exception that may cause undefined behavior: " & e.msg & " " & eTypeDesc)
var stackTrace {.inject.} = e.getStackTrace()
fail()
finally:
try:
when declared(testTeardownIMPLFlag):
testTeardownIMPL()
except Exception as e:
let eTypeDesc = "[" & $e.name & "]"
checkpoint("Exception when calling teardown: " & e.msg & " " & eTypeDesc)
var stackTrace {.inject.} = e.getStackTrace()
fail()
checkpoints = @[]