[PATCH 2 of 4] run-tests: factor out highlight functions

Yuya Nishihara yuya at tcha.org
Thu Aug 24 11:20:41 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1503580540 -32400
#      Thu Aug 24 22:15:40 2017 +0900
# Node ID 7b0eeb08d182eec00c79fe4617c09c1ec12bcbc5
# Parent  f22153763155ce72938f11beed25db352b923ff2
run-tests: factor out highlight functions

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -611,6 +611,18 @@ def log(*msg):
         print()
         sys.stdout.flush()
 
+def highlightdiff(line, color):
+    if not color:
+        return line
+    assert pygmentspresent
+    return pygments.highlight(line, difflexer, terminal256formatter)
+
+def highlightmsg(msg, color):
+    if not color:
+        return msg
+    assert pygmentspresent
+    return pygments.highlight(msg, runnerlexer, runnerformatter)
+
 def terminate(proc):
     """Terminate subprocess"""
     vlog('# Terminating process %d' % proc.pid)
@@ -1636,12 +1648,7 @@ class TestResult(unittest._TextTestResul
                 else:
                     if not self._options.nodiff:
                         formatted = '\nERROR: %s output changed\n' % test
-                        if self.color:
-                            formatted = pygments.highlight(
-                                formatted,
-                                runnerlexer,
-                                runnerformatter)
-                        self.stream.write(formatted)
+                        self.stream.write(highlightmsg(formatted, self.color))
                     self.stream.write('!')
 
                 self.stream.flush()
@@ -1707,10 +1714,7 @@ class TestResult(unittest._TextTestResul
                 else:
                     self.stream.write('\n')
                     for line in lines:
-                        if self.color:
-                            line = pygments.highlight(line,
-                                                      difflexer,
-                                                      terminal256formatter)
+                        line = highlightdiff(line, self.color)
                         if PYTHON3:
                             self.stream.flush()
                             self.stream.buffer.write(line)
@@ -2044,20 +2048,10 @@ class TextTestRunner(unittest.TextTestRu
             if not self._runner.options.noskips:
                 for test, msg in result.skipped:
                     formatted = 'Skipped %s: %s\n' % (test.name, msg)
-                    if result.color:
-                        formatted = pygments.highlight(
-                            formatted,
-                            runnerlexer,
-                            runnerformatter)
-                    self.stream.write(formatted)
+                    self.stream.write(highlightmsg(formatted, result.color))
             for test, msg in result.failures:
                 formatted = 'Failed %s: %s\n' % (test.name, msg)
-                if result.color:
-                    formatted = pygments.highlight(
-                        formatted,
-                        runnerlexer,
-                        runnerformatter)
-                self.stream.write(formatted)
+                self.stream.write(highlightmsg(formatted, result.color))
             for test, msg in result.errors:
                 self.stream.writeln('Errored %s: %s' % (test.name, msg))
 


More information about the Mercurial-devel mailing list