From db67e2ad76840993ff82015987e3089a7d76f55f Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:17:30 +0100 Subject: [PATCH] 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) --- unittest2.nim | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/unittest2.nim b/unittest2.nim index 1ed0fc4..73dbad6 100644 --- a/unittest2.nim +++ b/unittest2.nim @@ -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 = @[]