unittest: use defines instead of env vars (#16165)

* unittest: use defines instead of env vars

* use defines in testament

* fixup

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
Timothee Cour 2020-12-09 15:57:52 -08:00 • committed by GitHub
commit 82bb4db4b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 57 deletions

View file

@ -139,21 +139,19 @@ type
ConsoleOutputFormatter* = ref object of OutputFormatter ConsoleOutputFormatter* = ref object of OutputFormatter
colorOutput: bool colorOutput: bool
## Have test results printed in color. ## Have test results printed in color.
## Default is true for the non-js target, ## Default is `auto` depending on `isatty(stdout)`, or override it with
## for which ``stdout`` is a tty. ## `-d:nimUnittestColor:auto|on|off`.
## Setting the environment variable ##
## ``NIMTEST_COLOR`` to ``always`` or ## Deprecated: Setting the environment variable `NIMTEST_COLOR` to `always`
## ``never`` changes the default for the ## or `never` changes the default for the non-js target to true or false respectively.
## non-js target to true or false respectively. ## Deprecated: the environment variable `NIMTEST_NO_COLOR`, when set, changes the
## The deprecated environment variable ## default to true, if `NIMTEST_COLOR` is undefined.
## ``NIMTEST_NO_COLOR``, when set,
## changes the default to true, if
## ``NIMTEST_COLOR`` is undefined.
outputLevel: OutputLevel outputLevel: OutputLevel
## Set the verbosity of test results. ## Set the verbosity of test results.
## Default is ``PRINT_ALL``, unless ## Default is `PRINT_ALL`, or override with:
## the ``NIMTEST_OUTPUT_LVL`` environment ## `-d:nimUnittestOutputLevel:PRINT_ALL|PRINT_FAILURES|PRINT_NONE`.
## variable is set for the non-js target. ##
## Deprecated: the `NIMTEST_OUTPUT_LVL` environment variable is set for the non-js target.
isInSuite: bool isInSuite: bool
isInTest: bool isInTest: bool
@ -166,17 +164,31 @@ type
var var
abortOnError* {.threadvar.}: bool ## Set to true in order to quit abortOnError* {.threadvar.}: bool ## Set to true in order to quit
## immediately on fail. Default is false, ## immediately on fail. Default is false,
## unless the ``NIMTEST_ABORT_ON_ERROR`` ## or override with `-d:nimUnittestAbortOnError:on|off`.
## environment variable is set for ##
## the non-js target. ## Deprecated: can also override depending on whether
## `NIMTEST_ABORT_ON_ERROR` environment variable is set.
checkpoints {.threadvar.}: seq[string] checkpoints {.threadvar.}: seq[string]
formatters {.threadvar.}: seq[OutputFormatter] formatters {.threadvar.}: seq[OutputFormatter]
testsFilters {.threadvar.}: HashSet[string] testsFilters {.threadvar.}: HashSet[string]
disabledParamFiltering {.threadvar.}: bool disabledParamFiltering {.threadvar.}: bool
const
outputLevelDefault = PRINT_ALL
nimUnittestOutputLevel {.strdefine.} = $outputLevelDefault
nimUnittestColor {.strdefine.} = "auto" ## auto|on|off
nimUnittestAbortOnError {.booldefine.} = false
template deprecateEnvVarHere() =
# xxx issue a runtime warning to deprecate this envvar.
discard
abortOnError = nimUnittestAbortOnError
when declared(stdout): when declared(stdout):
abortOnError = existsEnv("NIMTEST_ABORT_ON_ERROR") if existsEnv("NIMTEST_ABORT_ON_ERROR"):
deprecateEnvVarHere()
abortOnError = true
method suiteStarted*(formatter: OutputFormatter, suiteName: string) {.base, gcsafe.} = method suiteStarted*(formatter: OutputFormatter, suiteName: string) {.base, gcsafe.} =
discard discard
@ -202,36 +214,44 @@ proc delOutputFormatter*(formatter: OutputFormatter) =
proc resetOutputFormatters* {.since: (1, 1).} = proc resetOutputFormatters* {.since: (1, 1).} =
formatters = @[] formatters = @[]
proc newConsoleOutputFormatter*(outputLevel: OutputLevel = OutputLevel.PRINT_ALL, proc newConsoleOutputFormatter*(outputLevel: OutputLevel = outputLevelDefault,
colorOutput = true): <//>ConsoleOutputFormatter = colorOutput = true): <//>ConsoleOutputFormatter =
ConsoleOutputFormatter( ConsoleOutputFormatter(
outputLevel: outputLevel, outputLevel: outputLevel,
colorOutput: colorOutput colorOutput: colorOutput
) )
proc defaultConsoleFormatter*(): <//>ConsoleOutputFormatter = proc colorOutput(): bool =
let color = nimUnittestColor
case color
of "auto":
when declared(stdout): result = isatty(stdout)
else: result = false
of "on": result = true
of "off": result = false
else: doAssert false, $color
when declared(stdout): when declared(stdout):
# Reading settings
# On a terminal this branch is executed
var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string
var colorOutput = isatty(stdout)
if existsEnv("NIMTEST_COLOR"): if existsEnv("NIMTEST_COLOR"):
deprecateEnvVarHere()
let colorEnv = getEnv("NIMTEST_COLOR") let colorEnv = getEnv("NIMTEST_COLOR")
if colorEnv == "never": if colorEnv == "never":
colorOutput = false result = false
elif colorEnv == "always": elif colorEnv == "always":
colorOutput = true result = true
elif existsEnv("NIMTEST_NO_COLOR"): elif existsEnv("NIMTEST_NO_COLOR"):
colorOutput = false deprecateEnvVarHere()
var outputLevel = OutputLevel.PRINT_ALL result = false
if envOutLvl.len > 0:
for opt in countup(low(OutputLevel), high(OutputLevel)): proc defaultConsoleFormatter*(): <//>ConsoleOutputFormatter =
if $opt == envOutLvl: var colorOutput = colorOutput()
outputLevel = opt var outputLevel = nimUnittestOutputLevel.parseEnum[:OutputLevel]
break when declared(stdout):
result = newConsoleOutputFormatter(outputLevel, colorOutput) const a = "NIMTEST_OUTPUT_LVL"
else: if existsEnv(a):
result = newConsoleOutputFormatter() # xxx issue a warning to deprecate this envvar.
outputLevel = getEnv(a).parseEnum[:OutputLevel]
result = newConsoleOutputFormatter(outputLevel, colorOutput)
method suiteStarted*(formatter: ConsoleOutputFormatter, suiteName: string) = method suiteStarted*(formatter: ConsoleOutputFormatter, suiteName: string) =
template rawPrint() = echo("\n[Suite] ", suiteName) template rawPrint() = echo("\n[Suite] ", suiteName)

View file

@ -661,9 +661,6 @@ proc loadSkipFrom(name: string): seq[string] =
result.add sline result.add sline
proc main() = proc main() =
os.putEnv "NIMTEST_COLOR", "never"
os.putEnv "NIMTEST_OUTPUT_LVL", "PRINT_FAILURES"
azure.init() azure.init()
backend.open() backend.open()
var optPrintResults = false var optPrintResults = false

View file

@ -8,4 +8,9 @@ switch("path", "$lib/../testament/lib")
switch("colors", "off") switch("colors", "off")
switch("listFullPaths", "off") switch("listFullPaths", "off")
switch("excessiveStackTrace", "off") switch("excessiveStackTrace", "off")
# for std/unittest
switch("define", "nimUnittestOutputLevel:PRINT_FAILURES")
switch("define", "nimUnittestColor:off")
switch("define", "nimLegacyTypeMismatch") switch("define", "nimLegacyTypeMismatch")

View file

@ -1,13 +0,0 @@
discard """
output: '''
[Suite] Bacon
[OK] >:)
'''
"""
import unittest
suite "Bacon":
test ">:)":
check(true == true)

View file

@ -7,12 +7,6 @@ import times, strutils, unittest
when not defined(js): when not defined(js):
import os import os
# Normally testament configures unittest with environment variables,
# but that doesn't work for the JS target. So instead we must set the correct
# settings here.
addOutputFormatter(
newConsoleOutputFormatter(PRINT_FAILURES, colorOutput = false))
proc staticTz(hours, minutes, seconds: int = 0): Timezone {.noSideEffect.} = proc staticTz(hours, minutes, seconds: int = 0): Timezone {.noSideEffect.} =
let offset = hours * 3600 + minutes * 60 + seconds let offset = hours * 3600 + minutes * 60 + seconds

View file

@ -19,6 +19,7 @@ discard """
[Suite] test name filtering [Suite] test name filtering
''' '''
targets: "c js"
""" """
import unittest, sequtils import unittest, sequtils