fix #14475; unittest.require now works with nim c; require and check now works with -d:nodejs (#14676)

* fix #14475; make unittest work with -d:nodejs

* fixup

* fixup

* disable inim, delaunay which failed after unittest.require got fixed

* re-enable tests that have been fixed
This commit is contained in:
Timothee Cour 2020-07-14 04:14:32 -07:00 • committed by GitHub
commit e07d661d16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 13 deletions

View file

@ -94,6 +94,7 @@
## echo "suite teardown: run once after the tests" ## echo "suite teardown: run once after the tests"
import std/private/since import std/private/since
import std/exitprocs
import import
macros, strutils, streams, times, sets, sequtils macros, strutils, streams, times, sets, sequtils
@ -498,7 +499,7 @@ template test*(name, body) {.dirty.} =
## .. code-block:: ## .. code-block::
## ##
## [OK] roses are red ## [OK] roses are red
bind shouldRun, checkpoints, formatters, ensureInitialized, testEnded, exceptionTypeName bind shouldRun, checkpoints, formatters, ensureInitialized, testEnded, exceptionTypeName, setProgramResult
ensureInitialized() ensureInitialized()
@ -524,7 +525,7 @@ template test*(name, body) {.dirty.} =
finally: finally:
if testStatusIMPL == TestStatus.FAILED: if testStatusIMPL == TestStatus.FAILED:
programResult = 1 setProgramResult 1
let testResult = TestResult( let testResult = TestResult(
suiteName: when declared(testSuiteName): testSuiteName else: "", suiteName: when declared(testSuiteName): testSuiteName else: "",
testName: name, testName: name,
@ -560,12 +561,11 @@ template fail* =
## fail() ## fail()
## ##
## outputs "Checkpoint A" before quitting. ## outputs "Checkpoint A" before quitting.
bind ensureInitialized bind ensureInitialized, setProgramResult
when declared(testStatusIMPL): when declared(testStatusIMPL):
testStatusIMPL = TestStatus.FAILED testStatusIMPL = TestStatus.FAILED
else: else:
programResult = 1 setProgramResult 1
ensureInitialized() ensureInitialized()
@ -576,8 +576,7 @@ template fail* =
else: else:
formatter.failureOccurred(checkpoints, "") formatter.failureOccurred(checkpoints, "")
when declared(programResult): if abortOnError: quit(1)
if abortOnError: quit(programResult)
checkpoints = @[] checkpoints = @[]

View file

@ -63,3 +63,25 @@ proc addExitProc*(cl: proc() {.noconv.}) =
withLock gFunsLock: withLock gFunsLock:
fun() fun()
gFuns.add Fun(kind: kNoconv, fun2: cl) gFuns.add Fun(kind: kNoconv, fun2: cl)
when not defined(nimscript):
proc getProgramResult*(): int =
when defined(js) and defined(nodejs):
asm """
`result` = process.exitCode;
"""
elif not defined(js):
result = programResult
else:
doAssert false
proc setProgramResult*(a: int) =
# pending https://github.com/nim-lang/Nim/issues/14674
when defined(js) and defined(nodejs):
asm """
process.exitCode = `a`;
"""
elif not defined(js):
programResult = a
else:
doAssert false

View file

@ -1127,12 +1127,9 @@ const
## is the value that should be passed to `quit <#quit,int>`_ to indicate ## is the value that should be passed to `quit <#quit,int>`_ to indicate
## failure. ## failure.
when defined(js) and defined(nodejs) and not defined(nimscript): when not defined(js) and hostOS != "standalone":
var programResult* {.importc: "process.exitCode".}: int
programResult = 0
elif hostOS != "standalone":
var programResult* {.compilerproc, exportc: "nim_program_result".}: int var programResult* {.compilerproc, exportc: "nim_program_result".}: int
## deprecated, prefer ``quit`` ## deprecated, prefer `quit` or `exitprocs.getProgramResult`, `exitprocs.setProgramResult`.
import std/private/since import std/private/since

View file

@ -3,6 +3,8 @@ discard """
outputsub: "[FAILED] with exception" outputsub: "[FAILED] with exception"
""" """
# see also: `tests/stdlib/tunittest_error.nim`
import unittest import unittest
proc ddd() = proc ddd() =

View file

@ -5,7 +5,7 @@ just exiting...
joinable: false joinable: false
""" """
# Test the new beforeQuit variable: # Test `addQuitProc`
proc myExit() {.noconv.} = proc myExit() {.noconv.} =
write(stdout, "just exiting...\n") write(stdout, "just exiting...\n")

View file

@ -0,0 +1,22 @@
discard """
exitcode: 1
outputsub: "failed: 1 == 3"
matrix: "-d:case1; -d:case2"
targets: "c js"
joinable: false
"""
when defined case1:
import unittest
suite "Test":
test "test require":
check 1==2
check 1==3
when defined case2:
import unittest
suite "Test":
test "test require":
require 1 == 3
if true:
quit 0 # intentional