summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoralaviss <alaviss@users.noreply.github.com>2019-07-01 11:19:07 +0000
committerAndreas Rumpf <rumpf_a@web.de>2019-07-01 13:19:07 +0200
commit2c10b246ec8319a9f065d5ef50fc891eeffc7ad9 (patch)
treeb941e8c95bdf86d12c54a20cd80156235ff500c3
parentbd55c862a6ed8ee33caab2e93e1ad7f9e5bd85a5 (diff)
downloadNim-2c10b246ec8319a9f065d5ef50fc891eeffc7ad9.tar.gz
assertions: fixes #11545 (#11605)
* assertions: properly fix #11545

* tests/assert: enable excessiveStackTrace

* tests/assert: add test case for #11545

* tfailedassert_stacktrace: disable excessiveStackTrace

* assertions: weird workaround for failing tests

This fixes megatest on *nix, but have no idea why
-rw-r--r--lib/system/assertions.nim8
-rw-r--r--tests/assert/config.nims1
-rw-r--r--tests/assert/testhelper.nim4
-rw-r--r--tests/assert/tfailedassert_stacktrace.nim5
-rw-r--r--tests/assert/trelativeassert.nim11
5 files changed, 22 insertions, 7 deletions
diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim
index 67f94aa1f..0a1ec5950 100644
--- a/lib/system/assertions.nim
+++ b/lib/system/assertions.nim
@@ -27,13 +27,15 @@ proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} =
   Hide(raiseAssert)(msg)
 
 template assertImpl(cond: bool, msg: string, expr: string, enabled: static[bool]) =
-  const loc = $instantiationInfo(-1, true)
+  const
+    loc = instantiationInfo(fullPaths = compileOption("excessiveStackTrace"))
+    ploc = $loc
   bind instantiationInfo
   mixin failedAssertImpl
   when enabled:
-    {.line: instantiationInfo(fullPaths = compileOption("excessiveStackTrace")).}:
+    {.line: loc.}:
       if not cond:
-        failedAssertImpl(loc & " `" & expr & "` " & msg)
+        failedAssertImpl(ploc & " `" & expr & "` " & msg)
 
 template assert*(cond: untyped, msg = "") =
   ## Raises ``AssertionError`` with `msg` if `cond` is false. Note
diff --git a/tests/assert/config.nims b/tests/assert/config.nims
new file mode 100644
index 000000000..f1cffeb6c
--- /dev/null
+++ b/tests/assert/config.nims
@@ -0,0 +1 @@
+--excessiveStackTrace:on
diff --git a/tests/assert/testhelper.nim b/tests/assert/testhelper.nim
index 2e5ede990..03bdd2468 100644
--- a/tests/assert/testhelper.nim
+++ b/tests/assert/testhelper.nim
@@ -1,9 +1,9 @@
 from strutils import endsWith, split
 from os import isAbsolute
 
-proc checkMsg*(msg, expectedEnd, name: string)=
+proc checkMsg*(msg, expectedEnd, name: string, absolute = true)=
   let filePrefix = msg.split(' ', maxSplit = 1)[0]
-  if not filePrefix.isAbsolute:
+  if absolute and not filePrefix.isAbsolute:
     echo name, ":not absolute: `", msg & "`"
   elif not msg.endsWith expectedEnd:
     echo name, ":expected suffix:\n`" & expectedEnd & "`\ngot:\n`" & msg & "`"
diff --git a/tests/assert/tfailedassert_stacktrace.nim b/tests/assert/tfailedassert_stacktrace.nim
index ad35ad5bc..002d65c53 100644
--- a/tests/assert/tfailedassert_stacktrace.nim
+++ b/tests/assert/tfailedassert_stacktrace.nim
@@ -1,10 +1,11 @@
 discard """
+  cmd: "nim $target $options --excessiveStackTrace:off $file"
   output: '''true'''
 """
 
 const expected = """
-tfailedassert_stacktrace.nim(34) tfailedassert_stacktrace
-tfailedassert_stacktrace.nim(33) foo
+tfailedassert_stacktrace.nim(35) tfailedassert_stacktrace
+tfailedassert_stacktrace.nim(34) foo
 assertions.nim(*)       failedAssertImpl
 assertions.nim(*)       raiseAssert
 fatal.nim(*)            sysFatal"""
diff --git a/tests/assert/trelativeassert.nim b/tests/assert/trelativeassert.nim
new file mode 100644
index 000000000..62ab2c421
--- /dev/null
+++ b/tests/assert/trelativeassert.nim
@@ -0,0 +1,11 @@
+discard """
+  cmd: "nim $target $options --excessiveStackTrace:off $file"
+  output: '''
+test:ok
+'''
+"""
+import testhelper
+try:
+  doAssert(false, "msg")
+except AssertionError as e:
+  checkMsg(e.msg, "trelativeassert.nim(9, 11) `false` msg", "test", false)