[PATCH] run-tests: make sure to check if pygments is installed before using it

mlaneuville at gmail.com mlaneuville at gmail.com
Sun Jul 16 01:07:03 EDT 2017


# HG changeset patch
# User Matthieu Laneuville <matthieu.laneuville at octobus.net>
# Date 1500181438 -32400
#      Sun Jul 16 14:03:58 2017 +0900
# Node ID 97ec8637d261a97c30944d9242ee1ccdbf536cb3
# Parent  389536aff376d32d38f13305021c127245d4126a
run-tests: make sure to check if pygments is installed before using it

Makes sure color is never used if pygments is not available by conditioning
test-run-tests.t and checking if self.stream is a tty instead of stderr only.

Built upon:
https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-July/101785.html

diff -r 389536aff376 -r 97ec8637d261 tests/run-tests.py
--- a/tests/run-tests.py	Sat Jul 15 15:01:29 2017 +0900
+++ b/tests/run-tests.py	Sun Jul 16 14:03:58 2017 +0900
@@ -89,6 +89,7 @@
 processlock = threading.Lock()
 
 with_color = False
+with_pygments = False
 
 # ANSI color is unsupported prior to Windows 10
 if os.name != 'nt':
@@ -97,12 +98,10 @@
         import pygments.lexers as lexers
         import pygments.formatters as formatters
         with_color = True
+        with_pygments = True
     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
     xrange = range # we use xrange in one place, and we'd rather not use range
@@ -415,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')
@@ -1573,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 with_pygments:
+                    with_color = True
+
     def addFailure(self, test, reason):
         self.failures.append((test, reason))
 
@@ -1650,7 +1653,7 @@
                 else:
                     self.stream.write('\n')
                     for line in lines:
-                        if with_color:
+                        if with_color and with_pygments:
                             line = pygments.highlight(
                                     line,
                                     lexers.DiffLexer(),
diff -r 389536aff376 -r 97ec8637d261 tests/test-run-tests.t
--- a/tests/test-run-tests.t	Sat Jul 15 15:01:29 2017 +0900
+++ b/tests/test-run-tests.t	Sun Jul 16 14:03:58 2017 +0900
@@ -122,6 +122,7 @@
 test diff colorisation
 
 #if no-windows
+#require pygments
   $ rt test-failure.t --color always
   
   \x1b[38;5;124m--- $TESTTMP/test-failure.t\x1b[39m (esc)


More information about the Mercurial-devel mailing list