diff tests/run-tests.py @ 33420:e80041832eec

run-tests: add color to output if pygments is available The output of run-tests has no formatting by default, which hampers readability. This patch colors the diff output when pygments is available. To avoid coloring even when pygments is available, use --color never.
author Matthieu Laneuville <matthieu.laneuville@octobus.net>
date Thu, 29 Jun 2017 20:45:12 +0900
parents cf826b9e9ea4
children 9c6e64911de0
line wrap: on
line diff
--- a/tests/run-tests.py	Thu Mar 30 00:33:00 2017 -0400
+++ b/tests/run-tests.py	Thu Jun 29 20:45:12 2017 +0900
@@ -88,6 +88,18 @@
 osenvironb = getattr(os, 'environb', os.environ)
 processlock = threading.Lock()
 
+with_color = False
+try: # is pygments installed
+    import pygments
+    import pygments.lexers as lexers
+    import pygments.formatters as formatters
+    with_color = 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
@@ -255,6 +267,9 @@
         help="output files annotated with coverage")
     parser.add_option("-c", "--cover", action="store_true",
         help="print a test coverage report")
+    parser.add_option("--color", choices=["always", "auto", "never"],
+                      default="auto",
+                      help="colorisation: always|auto|never (default: auto)")
     parser.add_option("-d", "--debug", action="store_true",
         help="debug mode: write output of test scripts to console"
              " rather than capturing and diffing it (disables timeout)")
@@ -397,6 +412,13 @@
         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')
@@ -1625,6 +1647,11 @@
                 else:
                     self.stream.write('\n')
                     for line in lines:
+                        if with_color:
+                            line = pygments.highlight(
+                                    line,
+                                    lexers.DiffLexer(),
+                                    formatters.Terminal256Formatter())
                         if PYTHON3:
                             self.stream.flush()
                             self.stream.buffer.write(line)