Patchwork [v4] run-tests: also color the summary messages (skipped, failed...)

login
register
mail settings
Submitter Matthieu Laneuville
Date July 19, 2017, 5:03 a.m.
Message ID <23af517bb11ca75e0ce9.1500440603@carbon>
Download mbox | patch
Permalink /patch/22523/
State Deferred
Headers show

Comments

Matthieu Laneuville - July 19, 2017, 5:03 a.m.
# HG changeset patch
# User Matthieu Laneuville <matthieu.laneuville@octobus.net>
# Date 1500440296 -32400
#      Wed Jul 19 13:58:16 2017 +0900
# Node ID 23af517bb11ca75e0ce9187d8dc13c3bc6f9b496
# Parent  9a944e908ecf9ac3aabf30a24406513370eeebe7
run-tests: also color the summary messages (skipped, failed...)
Yuya Nishihara - July 19, 2017, 12:48 p.m.
On Wed, 19 Jul 2017 14:03:23 +0900, mlaneuville@gmail.com wrote:
> # HG changeset patch
> # User Matthieu Laneuville <matthieu.laneuville@octobus.net>
> # Date 1500440296 -32400
> #      Wed Jul 19 13:58:16 2017 +0900
> # Node ID 23af517bb11ca75e0ce9187d8dc13c3bc6f9b496
> # Parent  9a944e908ecf9ac3aabf30a24406513370eeebe7
> run-tests: also color the summary messages (skipped, failed...)

It's effectively in freeze, so let's revisit later.

Patch

diff -r 9a944e908ecf -r 23af517bb11c tests/run-tests.py
--- a/tests/run-tests.py	Tue Jul 18 23:04:08 2017 +0530
+++ b/tests/run-tests.py	Wed Jul 19 13:58:16 2017 +0900
@@ -94,11 +94,46 @@ 
     try: # is pygments installed
         import pygments
         import pygments.lexers as lexers
+        import pygments.lexer as lexer
         import pygments.formatters as formatters
+        import pygments.token as token
+        import pygments.style as style
         pygmentspresent = True
     except ImportError:
         pass
 
+if pygmentspresent:
+    class TestRunnerStyle(style.Style):
+        default_style = ""
+        skipped = token.string_to_tokentype("Token.Generic.Skipped")
+        failed = token.string_to_tokentype("Token.Generic.Failed")
+        error = token.string_to_tokentype("Token.Generic.Error")
+        skippedname = token.string_to_tokentype("Token.Generic.SName")
+        failedname = token.string_to_tokentype("Token.Generic.FName")
+        styles = {
+            skipped:         '#e5e5e5',
+            skippedname:     '#555555',
+            failed:          '#7f0000',
+            failedname:      '#ff0000',
+        }
+
+    class TestRunnerLexer(lexer.RegexLexer):
+        tokens = {
+            'root': [
+                (r'^Skipped', token.Generic.Skipped, 'skipped'),
+                (r'^Failed ', token.Generic.Failed, 'failed'),
+                (r'^ERROR: ', token.Generic.Failed, 'failed'),
+            ],
+            'skipped': [
+                (r'[\w-]+\.t', token.Generic.SName),
+                (r':.*', token.Generic.Skipped),
+            ],
+            'failed': [
+                (r'[\w-]+\.t', token.Generic.FName),
+                (r'(:| ).*', token.Generic.Failed),
+            ]
+        }
+
 if sys.version_info > (3, 5, 0):
     PYTHON3 = True
     xrange = range # we use xrange in one place, and we'd rather not use range
@@ -1584,7 +1619,14 @@ 
                     self.stream.write('t')
                 else:
                     if not self._options.nodiff:
-                        self.stream.write('\nERROR: %s output changed\n' % test)
+                        formatted = '\nERROR: %s output changed\n' % test
+                        if self.color:
+                            formatted = pygments.highlight(
+                                formatted,
+                                TestRunnerLexer(),
+                                formatters.Terminal256Formatter(
+                                            style=TestRunnerStyle))
+                        self.stream.write(formatted)
                     self.stream.write('!')
 
                 self.stream.flush()
@@ -1987,9 +2029,23 @@ 
 
             if not self._runner.options.noskips:
                 for test, msg in result.skipped:
-                    self.stream.writeln('Skipped %s: %s' % (test.name, msg))
+                    formatted = 'Skipped %s: %s' % (test.name, msg)
+                    if result.color:
+                        formatted = pygments.highlight(
+                            formatted,
+                            TestRunnerLexer(),
+                            formatters.Terminal256Formatter(
+                                            style=TestRunnerStyle)).strip("\n")
+                    self.stream.writeln(formatted)
             for test, msg in result.failures:
-                self.stream.writeln('Failed %s: %s' % (test.name, msg))
+                formatted = 'Failed %s: %s' % (test.name, msg)
+                if result.color:
+                    formatted = pygments.highlight(
+                        formatted,
+                        TestRunnerLexer(),
+                        formatters.Terminal256Formatter(
+                                        style=TestRunnerStyle)).strip("\n")
+                self.stream.writeln(formatted)
             for test, msg in result.errors:
                 self.stream.writeln('Errored %s: %s' % (test.name, msg))
 
diff -r 9a944e908ecf -r 23af517bb11c tests/test-run-tests.t
--- a/tests/test-run-tests.t	Tue Jul 18 23:04:08 2017 +0530
+++ b/tests/test-run-tests.t	Wed Jul 19 13:58:16 2017 +0900
@@ -131,10 +131,9 @@ 
   \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
+  \x1b[38;5;88mERROR: \x1b[39m\x1b[38;5;9mtest-failure.t\x1b[39m\x1b[38;5;88m output changed\x1b[39m (esc)
   !
-  Failed test-failure.t: output changed
+  \x1b[38;5;88mFailed \x1b[39m\x1b[38;5;9mtest-failure.t\x1b[39m\x1b[38;5;88m: output changed\x1b[39m (esc)
   # Ran 1 tests, 0 skipped, 1 failed.
   python hash seed: * (glob)
   [1]