From f9cb40f88b5281e6cd2d5ba0d0a76b777858f598 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 5 Jul 2022 16:40:21 +0200 Subject: [PATCH] Fix crash when unhandled exception is cleared by teardown --- asynctest/templates.nim | 2 ++ testmodules/chronos/test.nim | 1 + testmodules/stdlib/test.nim | 1 + testmodules/stdlib/testfail.nim | 28 ++++++++++++++++++++++++++++ testmodules/unittest2/test.nim | 1 + 5 files changed, 33 insertions(+) create mode 100644 testmodules/stdlib/testfail.nim diff --git a/asynctest/templates.nim b/asynctest/templates.nim index 4bd0f0a..3a338c8 100644 --- a/asynctest/templates.nim +++ b/asynctest/templates.nim @@ -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 diff --git a/testmodules/chronos/test.nim b/testmodules/chronos/test.nim index 9a542b6..22d3773 100644 --- a/testmodules/chronos/test.nim +++ b/testmodules/chronos/test.nim @@ -2,3 +2,4 @@ import pkg/asynctest import pkg/chronos include ../stdlib/testbody +include ../stdlib/testfail diff --git a/testmodules/stdlib/test.nim b/testmodules/stdlib/test.nim index e8d872b..bfa135d 100644 --- a/testmodules/stdlib/test.nim +++ b/testmodules/stdlib/test.nim @@ -2,3 +2,4 @@ import std/asyncdispatch import pkg/asynctest include ./testbody +include ./testfail diff --git a/testmodules/stdlib/testfail.nim b/testmodules/stdlib/testfail.nim new file mode 100644 index 0000000..0c54d68 --- /dev/null +++ b/testmodules/stdlib/testfail.nim @@ -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") diff --git a/testmodules/unittest2/test.nim b/testmodules/unittest2/test.nim index 77bdd7c..b4dfb23 100644 --- a/testmodules/unittest2/test.nim +++ b/testmodules/unittest2/test.nim @@ -2,3 +2,4 @@ import pkg/asynctest/unittest2 import pkg/chronos include ../stdlib/testbody +include ../stdlib/testfail