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

login
register
mail settings
Submitter Matthieu Laneuville
Date July 9, 2017, 1:31 a.m.
Message ID <b40cf3e9cfae8e0b56c1.1499563901@carbon>
Download mbox | patch
Permalink /patch/22154/
State Accepted
Headers show

Comments

Matthieu Laneuville - July 9, 2017, 1:31 a.m.
# HG changeset patch
# User Matthieu Laneuville <matthieu.laneuville@octobus.net>
# Date 1499297836 -32400
#      Thu Jul 06 08:37:16 2017 +0900
# Node ID b40cf3e9cfae8e0b56c1d252398167ae227e1282
# Parent  87d1921ae32f56511ea26e8054796bd7b54daf46
run-tests: also color the summary messages (skipped, failed...)
Yuya Nishihara - July 14, 2017, 1:55 p.m.
On Sun, 09 Jul 2017 10:31:41 +0900, mlaneuville@gmail.com wrote:
> # HG changeset patch
> # User Matthieu Laneuville <matthieu.laneuville@octobus.net>
> # Date 1499297836 -32400
> #      Thu Jul 06 08:37:16 2017 +0900
> # Node ID b40cf3e9cfae8e0b56c1d252398167ae227e1282
> # Parent  87d1921ae32f56511ea26e8054796bd7b54daf46
> run-tests: also color the summary messages (skipped, failed...)
> 
> diff -r 87d1921ae32f -r b40cf3e9cfae tests/run-tests.py
> --- a/tests/run-tests.py	Thu Jun 29 20:45:12 2017 +0900
> +++ b/tests/run-tests.py	Thu Jul 06 08:37:16 2017 +0900
> @@ -92,7 +92,10 @@
>  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
>      with_color = True
>  except ImportError:
>      pass
> @@ -100,6 +103,23 @@
>  if not sys.stderr.isatty(): # check if the terminal is capable
>      with_color = False
>  
> +class TestRunnerStyle(style.Style):
> +    default_style = ""
> +    skipped = token.string_to_tokentype("Token.Generic.Skipped")
> +    failed = token.string_to_tokentype("Token.Generic.Failed")
> +    styles = {
> +        skipped:         '#ansilightgray',
> +        failed:          '#ansidarkred',
> +    }
> +
> +class TestRunnerLexer(lexer.RegexLexer):
> +    tokens = {
> +        'root': [
> +            (r'^Skipped.*', token.Generic.Skipped),
> +            (r'^Failed.*', token.Generic.Failed),
> +        ]
> +    }

Oops. This requires pygments. I'll drop this patch from hg-committed. Please
make sure your local revision won't be pruned before pulling.

Patch

diff -r 87d1921ae32f -r b40cf3e9cfae tests/run-tests.py
--- a/tests/run-tests.py	Thu Jun 29 20:45:12 2017 +0900
+++ b/tests/run-tests.py	Thu Jul 06 08:37:16 2017 +0900
@@ -92,7 +92,10 @@ 
 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
     with_color = True
 except ImportError:
     pass
@@ -100,6 +103,23 @@ 
 if not sys.stderr.isatty(): # check if the terminal is capable
     with_color = False
 
+class TestRunnerStyle(style.Style):
+    default_style = ""
+    skipped = token.string_to_tokentype("Token.Generic.Skipped")
+    failed = token.string_to_tokentype("Token.Generic.Failed")
+    styles = {
+        skipped:         '#ansilightgray',
+        failed:          '#ansidarkred',
+    }
+
+class TestRunnerLexer(lexer.RegexLexer):
+    tokens = {
+        'root': [
+            (r'^Skipped.*', token.Generic.Skipped),
+            (r'^Failed.*', 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
@@ -1984,9 +2004,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 with_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 with_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 87d1921ae32f -r b40cf3e9cfae tests/test-run-tests.t
--- a/tests/test-run-tests.t	Thu Jun 29 20:45:12 2017 +0900
+++ b/tests/test-run-tests.t	Thu Jul 06 08:37:16 2017 +0900
@@ -133,11 +133,16 @@ 
   
   ERROR: test-failure.t output changed
   !
-  Failed test-failure.t: output changed
+  \x1b[31mFailed test-failure.t: output changed\x1b[39m (esc)
   # Ran 1 tests, 0 skipped, 1 failed.
   python hash seed: * (glob)
   [1]
 
+  $ rt test-non-existing.t --color always
+  s
+  \x1b[37mSkipped test-non-existing.t: Doesn't exist\x1b[39m (esc)
+  # Ran 0 tests, 1 skipped, 0 failed.
+
   $ rt test-failure.t 2> tmp.log
   [1]
   $ cat tmp.log