unittest: default no color if stdout is not a tty (#7424)
We accept a new environment variable, NIMTEST_COLOR, which override the effect of NIMTEST_NO_COLOR. The environment variable, NIMTEST_COLOR, can be 'never' or 'always', which set the color output to false or true, respectively.
This commit is contained in:
parent
f3db632b1d
commit
f543388959
2 changed files with 20 additions and 5 deletions
|
|
@ -121,9 +121,16 @@ 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 true for the non-js target,
|
||||||
## unless, the environment variable
|
## for which ``stdout`` is a tty.
|
||||||
## ``NIMTEST_NO_COLOR`` is set.
|
## Setting the environment variable
|
||||||
|
## ``NIMTEST_COLOR`` to ``always`` or
|
||||||
|
## ``never`` changes the default for the
|
||||||
|
## non-js target to true or false respectively.
|
||||||
|
## The deprecated environment variable
|
||||||
|
## ``NIMTEST_NO_COLOR``, when set,
|
||||||
|
## changes the defualt 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``, unless
|
||||||
|
|
@ -186,7 +193,15 @@ proc defaultConsoleFormatter*(): ConsoleOutputFormatter =
|
||||||
# Reading settings
|
# Reading settings
|
||||||
# On a terminal this branch is executed
|
# On a terminal this branch is executed
|
||||||
var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string
|
var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string
|
||||||
var colorOutput = not existsEnv("NIMTEST_NO_COLOR")
|
var colorOutput = isatty(stdout)
|
||||||
|
if existsEnv("NIMTEST_COLOR"):
|
||||||
|
let colorEnv = getenv("NIMTEST_COLOR")
|
||||||
|
if colorEnv == "never":
|
||||||
|
colorOutput = false
|
||||||
|
elif colorEnv == "always":
|
||||||
|
colorOutput = true
|
||||||
|
elif existsEnv("NIMTEST_NO_COLOR"):
|
||||||
|
colorOutput = false
|
||||||
var outputLevel = PRINT_ALL
|
var outputLevel = PRINT_ALL
|
||||||
if envOutLvl.len > 0:
|
if envOutLvl.len > 0:
|
||||||
for opt in countup(low(OutputLevel), high(OutputLevel)):
|
for opt in countup(low(OutputLevel), high(OutputLevel)):
|
||||||
|
|
|
||||||
|
|
@ -427,7 +427,7 @@ include categories
|
||||||
# if status: reSuccess else: reOutputsDiffer)
|
# if status: reSuccess else: reOutputsDiffer)
|
||||||
|
|
||||||
proc main() =
|
proc main() =
|
||||||
os.putenv "NIMTEST_NO_COLOR", "1"
|
os.putenv "NIMTEST_COLOR", "never"
|
||||||
os.putenv "NIMTEST_OUTPUT_LVL", "PRINT_FAILURES"
|
os.putenv "NIMTEST_OUTPUT_LVL", "PRINT_FAILURES"
|
||||||
|
|
||||||
backend.open()
|
backend.open()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue