diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2023-06-27 08:03:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-27 08:03:52 +0800 |
commit | 4ce3a68e794fe3d448928ccbbc34191ce24c4fbd (patch) | |
tree | cc7e84158516c424ea6f80fa773f891f15078a78 | |
parent | 4546f5dfe539ff4f37a5100bdfedaf4867293f3a (diff) | |
download | Nim-4ce3a68e794fe3d448928ccbbc34191ce24c4fbd.tar.gz |
fixes #22163; use `{.push warning[BareExcept]:off.}` to override settings temporarily (#21390)
* use `{.push warning[BareExcept]:off.}` to override settings temporarily * likewise, suppress expect
-rw-r--r-- | lib/pure/unittest.nim | 12 | ||||
-rw-r--r-- | lib/std/assertions.nim | 4 |
2 files changed, 10 insertions, 6 deletions
diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index 964fba0e4..afe98ca4e 100644 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -550,14 +550,14 @@ template test*(name, body) {.dirty.} = for formatter in formatters: formatter.testStarted(name) - {.warning[BareExcept]:off.} + {.push warning[BareExcept]:off.} try: when declared(testSetupIMPLFlag): testSetupIMPL() when declared(testTeardownIMPLFlag): defer: testTeardownIMPL() - {.warning[BareExcept]:on.} + {.push warning[BareExcept]:on.} body - {.warning[BareExcept]:off.} + {.pop.} except: let e = getCurrentException() @@ -579,7 +579,7 @@ template test*(name, body) {.dirty.} = ) testEnded(testResult) checkpoints = @[] - {.warning[BareExcept]:on.} + {.pop.} proc checkpoint*(msg: string) = ## Set a checkpoint identified by `msg`. Upon test failure all @@ -767,8 +767,11 @@ macro expect*(exceptions: varargs[typed], body: untyped): untyped = defectiveRobot() template expectBody(errorTypes, lineInfoLit, body): NimNode {.dirty.} = + {.push warning[BareExcept]:off.} try: + {.push warning[BareExcept]:on.} body + {.pop.} checkpoint(lineInfoLit & ": Expect Failed, no exception was thrown.") fail() except errorTypes: @@ -776,6 +779,7 @@ macro expect*(exceptions: varargs[typed], body: untyped): untyped = except: checkpoint(lineInfoLit & ": Expect Failed, unexpected exception was thrown.") fail() + {.pop.} var errorTypes = newNimNode(nnkBracket) for exp in exceptions: diff --git a/lib/std/assertions.nim b/lib/std/assertions.nim index 3dca644ad..56c37d205 100644 --- a/lib/std/assertions.nim +++ b/lib/std/assertions.nim @@ -98,7 +98,7 @@ template doAssertRaises*(exception: typedesc, code: untyped) = const begin = "expected raising '" & astToStr(exception) & "', instead" const msgEnd = " by: " & astToStr(code) template raisedForeign {.gensym.} = raiseAssert(begin & " raised foreign exception" & msgEnd) - {.warning[BareExcept]:off.} + {.push warning[BareExcept]:off.} when Exception is exception: try: if true: @@ -117,6 +117,6 @@ template doAssertRaises*(exception: typedesc, code: untyped) = mixin `$` # alternatively, we could define $cstring in this module raiseAssert(begin & " raised '" & $e.name & "'" & msgEnd) except: raisedForeign() - {.warning[BareExcept]:on.} + {.pop.} if wrong: raiseAssert(begin & " nothing was raised" & msgEnd) |