[PATCH] run-tests: check if stream is a tty before using color

mlaneuville at gmail.com mlaneuville at gmail.com
Mon Jul 17 22:33:13 UTC 2017


# HG changeset patch
# User Matthieu Laneuville <matthieu.laneuville at octobus.net>
# Date 1500329966 -32400
#      Tue Jul 18 07:19:26 2017 +0900
# Node ID 7f5f97697162331b5acf78aef091ae3fd341e308
# Parent  9b2647a7a70a270e85457ea55583b6eba0551258
run-tests: check if stream is a tty before using color

Previous implementation (e80041832eec) checked only if sys.stderr was a tty
which was less general. Also makes sure that colors is never used if pygments is
not available, irrespective of --color flag value.

diff -r 9b2647a7a70a -r 7f5f97697162 tests/run-tests.py
--- a/tests/run-tests.py	Mon Jul 17 11:45:38 2017 -0700
+++ b/tests/run-tests.py	Tue Jul 18 07:19:26 2017 +0900
@@ -101,8 +101,6 @@
     except ImportError:
         pass
 
-if not sys.stderr.isatty(): # check if the terminal is capable
-    with_color = False
 
 if sys.version_info > (3, 5, 0):
     PYTHON3 = True
@@ -416,13 +414,6 @@
         parser.error('--chg does not work when --with-hg is specified '
                      '(use --with-chg instead)')
 
-    global with_color
-    if options.color != 'auto':
-        if options.color == 'never':
-            with_color = False
-        else: # 'always', for testing purposes
-            with_color = True
-
     global useipv6
     if options.ipv6:
         useipv6 = checksocketfamily('AF_INET6')
@@ -1574,6 +1565,17 @@
         self.successes = []
         self.faildata = {}
 
+        global with_color
+        if not self.stream.isatty(): # check if the terminal is capable
+            with_color = False
+
+        if options.color != 'auto':
+            if options.color == 'never':
+                with_color = False
+            else: # 'always', for testing purposes
+                if pygmentspresent:
+                    with_color = True
+
     def addFailure(self, test, reason):
         self.failures.append((test, reason))
 


More information about the Mercurial-devel mailing list