run-tests: add color to output if pygments is available
authorMatthieu Laneuville <matthieu.laneuville@octobus.net>
Thu, 29 Jun 2017 20:45:12 +0900
changeset 33420 e80041832eec
parent 33419 7c33adc823e0
child 33421 d1aa3fee4ca4
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.
tests/run-tests.py
tests/test-run-tests.t
--- 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)
--- a/tests/test-run-tests.t	Thu Mar 30 00:33:00 2017 -0400
+++ b/tests/test-run-tests.t	Thu Jun 29 20:45:12 2017 +0900
@@ -119,6 +119,43 @@
   python hash seed: * (glob)
   [1]
 
+test diff colorisation
+
+  $ rt test-failure.t --color always
+  
+  \x1b[38;5;124m--- $TESTTMP/test-failure.t\x1b[39m (esc)
+  \x1b[38;5;34m+++ $TESTTMP/test-failure.t.err\x1b[39m (esc)
+  \x1b[38;5;90;01m@@ -1,3 +1,3 @@\x1b[39;00m (esc)
+     $ echo "bar-baz"; echo "bar-bad"
+  \x1b[38;5;34m+  bar*baz (glob)\x1b[39m (esc)
+     bar*bad (glob)
+  \x1b[38;5;124m-  bar*baz (glob)\x1b[39m (esc)
+  
+  ERROR: test-failure.t output changed
+  !
+  Failed test-failure.t: output changed
+  # Ran 1 tests, 0 skipped, 1 failed.
+  python hash seed: * (glob)
+  [1]
+
+  $ rt test-failure.t 2> tmp.log
+  [1]
+  $ cat tmp.log
+  
+  --- $TESTTMP/test-failure.t
+  +++ $TESTTMP/test-failure.t.err
+  @@ -1,3 +1,3 @@
+     $ echo "bar-baz"; echo "bar-bad"
+  +  bar*baz (glob)
+     bar*bad (glob)
+  -  bar*baz (glob)
+  
+  ERROR: test-failure.t output changed
+  !
+  Failed test-failure.t: output changed
+  # Ran 1 tests, 0 skipped, 1 failed.
+  python hash seed: * (glob)
+
 basic failing test
   $ cat > test-failure.t << EOF
   >   $ echo babar