run-tests: factor out highlight functions
authorYuya Nishihara <yuya@tcha.org>
Thu, 24 Aug 2017 22:15:40 +0900
changeset 33930 f5d4bb8e944d
parent 33929 5d2ce90c71f1
child 33931 7cab956794e4
run-tests: factor out highlight functions
tests/run-tests.py
--- a/tests/run-tests.py	Thu Aug 24 22:09:57 2017 +0900
+++ b/tests/run-tests.py	Thu Aug 24 22:15:40 2017 +0900
@@ -611,6 +611,18 @@
         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 @@
                 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 @@
                 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 @@
             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))