Fix crash when unhandled exception is cleared by teardown

This commit is contained in:
Mark Spanbroek 2022-07-05 16:40:21 +02:00 committed by markspanbroek
parent 32e62b6589
commit 3aaa852b43
5 changed files with 33 additions and 0 deletions

View File

@ -20,8 +20,10 @@ template suite*(name, body) =
template teardown(teardownBody) {.used.} =
teardown:
let exception = getCurrentException()
let asyncproc = proc {.async.} = teardownBody
waitFor asyncproc()
setCurrentException(exception)
let suiteproc = proc = # Avoids GcUnsafe2 warnings with chronos
body

View File

@ -2,3 +2,4 @@ import pkg/asynctest
import pkg/chronos
include ../stdlib/testbody
include ../stdlib/testfail

View File

@ -2,3 +2,4 @@ import std/asyncdispatch
import pkg/asynctest
include ./testbody
include ./testfail

View File

@ -0,0 +1,28 @@
import std/exitprocs
template silent(body) =
let exitcode = getProgramResult()
resetOutputFormatters()
addOutputFormatter(OutputFormatter())
body
resetOutputFormatters()
addOutputFormatter(defaultConsoleFormatter())
setProgramResult(exitcode)
suite "reports unhandled exception when teardown handles exceptions too":
silent:
proc someAsyncProc {.async.} = discard
teardown:
try:
await someAsyncProc()
except:
discard
test "should fail, but not crash":
raise newException(ValueError, "This exception is expected")

View File

@ -2,3 +2,4 @@ import pkg/asynctest/unittest2
import pkg/chronos
include ../stdlib/testbody
include ../stdlib/testfail