[PATCH 1 of 2] run-tests: add color to output if pygments is available

mlaneuville at gmail.com mlaneuville at gmail.com
Sun Jul 9 01:31:40 UTC 2017


# HG changeset patch
# User Matthieu Laneuville <matthieu.laneuville at octobus.net>
# Date 1498736712 -32400
#      Thu Jun 29 20:45:12 2017 +0900
# Node ID 87d1921ae32f56511ea26e8054796bd7b54daf46
# Parent  4672db164c986da4442bd864cd044512d975c3f2
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.

diff -r 4672db164c98 -r 87d1921ae32f tests/run-tests.py
--- a/tests/run-tests.py	Sat Jun 24 15:29:42 2017 -0700
+++ 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)
diff -r 4672db164c98 -r 87d1921ae32f tests/test-run-tests.t
--- a/tests/test-run-tests.t	Sat Jun 24 15:29:42 2017 -0700
+++ 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
@@ -1287,3 +1324,4 @@
   # Ran 2 tests, 0 skipped, 1 failed.
   python hash seed: * (glob)
   [1]
+


More information about the Mercurial-devel mailing list