summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorXiao-Yong <jinxiaoyong@gmail.com>2018-04-12 10:57:34 -0500
committerAndreas Rumpf <rumpf_a@web.de>2018-04-12 17:57:34 +0200
commitf543388959a4c74e4fa04e56d7e1961b5c44b4c0 (patch)
tree60961e11def3ff5902ea519133cec2fa8b5fd8f8
parentf3db632b1d730892e6770a6034acfb8aec077b34 (diff)
downloadNim-f543388959a4c74e4fa04e56d7e1961b5c44b4c0.tar.gz
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.
-rw-r--r--lib/pure/unittest.nim23
-rw-r--r--tests/testament/tester.nim2
2 files changed, 20 insertions, 5 deletions
diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim
index 0034d0c60..917251a6c 100644
--- a/lib/pure/unittest.nim
+++ b/lib/pure/unittest.nim
@@ -121,9 +121,16 @@ type
   ConsoleOutputFormatter* = ref object of OutputFormatter
     colorOutput: bool
       ## Have test results printed in color.
-      ## Default is true for the non-js target
-      ## unless, the environment variable
-      ## ``NIMTEST_NO_COLOR`` is set.
+      ## Default is true for the non-js target,
+      ## for which ``stdout`` is a tty.
+      ## 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
       ## Set the verbosity of test results.
       ## Default is ``PRINT_ALL``, unless
@@ -186,7 +193,15 @@ proc defaultConsoleFormatter*(): ConsoleOutputFormatter =
     # Reading settings
     # On a terminal this branch is executed
     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
     if envOutLvl.len > 0:
       for opt in countup(low(OutputLevel), high(OutputLevel)):
diff --git a/tests/testament/tester.nim b/tests/testament/tester.nim
index 06f5fec7d..856f95f3b 100644
--- a/tests/testament/tester.nim
+++ b/tests/testament/tester.nim
@@ -427,7 +427,7 @@ include categories
 #                 if status: reSuccess else: reOutputsDiffer)
 
 proc main() =
-  os.putenv "NIMTEST_NO_COLOR", "1"
+  os.putenv "NIMTEST_COLOR", "never"
   os.putenv "NIMTEST_OUTPUT_LVL", "PRINT_FAILURES"
 
   backend.open()