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

login
register
mail settings
Submitter Matthieu Laneuville
Date July 16, 2017, 6:12 a.m.
Message ID <9722bfa929b50e7ad87e.1500185541@carbon>
Download mbox | patch
Permalink /patch/22424/
State Changes Requested
Headers show

Comments

Matthieu Laneuville - July 16, 2017, 6:12 a.m.
# HG changeset patch
# User Matthieu Laneuville <matthieu.laneuville@octobus.net>
# Date 1500185462 -32400
#      Sun Jul 16 15:11:02 2017 +0900
# Node ID 9722bfa929b50e7ad87e0561f8d5fdd8e928d755
# Parent  97ec8637d261a97c30944d9242ee1ccdbf536cb3
run-tests: also color the summary messages (skipped, failed...)
Augie Fackler - July 17, 2017, 2:36 p.m.
On Sun, Jul 16, 2017 at 03:12:21PM +0900, mlaneuville@gmail.com wrote:
> # HG changeset patch
> # User Matthieu Laneuville <matthieu.laneuville@octobus.net>
> # Date 1500185462 -32400
> #      Sun Jul 16 15:11:02 2017 +0900
> # Node ID 9722bfa929b50e7ad87e0561f8d5fdd8e928d755
> # Parent  97ec8637d261a97c30944d9242ee1ccdbf536cb3
> run-tests: also color the summary messages (skipped, failed...)

This isn't applying for me, could you rebase and resend?


>
> diff -r 97ec8637d261 -r 9722bfa929b5 tests/run-tests.py
> --- a/tests/run-tests.py	Sun Jul 16 14:03:58 2017 +0900
> +++ b/tests/run-tests.py	Sun Jul 16 15:11:02 2017 +0900
> @@ -96,12 +96,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
>          with_color = True
>          with_pygments = True
>      except ImportError:
>          pass
>
> +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:         '#ansilightgray',
> +        skippedname:     '#ansidarkgray',
> +        failed:          '#ansidarkred',
> +        failedname:      '#ansired',
> +    }
> +
> +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
> @@ -1587,7 +1621,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 with_color and with_pygments:
> +                            formatted = pygments.highlight(
> +                                formatted,
> +                                TestRunnerLexer(),
> +                                formatters.Terminal256Formatter(
> +                                            style=TestRunnerStyle)).strip("\n")
> +                        self.stream.write(formatted)
>                      self.stream.write('!')
>
>                  self.stream.flush()
> @@ -1990,9 +2031,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 and with_pygments:
> +                        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 and with_pygments:
> +                    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 97ec8637d261 -r 9722bfa929b5 tests/test-run-tests.t
> --- a/tests/test-run-tests.t	Sun Jul 16 14:03:58 2017 +0900
> +++ b/tests/test-run-tests.t	Sun Jul 16 15:11:02 2017 +0900
> @@ -132,10 +132,8 @@
>    \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
> +  \x1b[31mERROR: \x1b[39m\x1b[31;01mtest-failure.t\x1b[39;00m\x1b[31m output changed\x1b[39m! (esc)
> +  \x1b[31mFailed \x1b[39m\x1b[31;01mtest-failure.t\x1b[39;00m\x1b[31m: output changed\x1b[39m (esc)
>    # Ran 1 tests, 0 skipped, 1 failed.
>    python hash seed: * (glob)
>    [1]
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Yuya Nishihara - July 17, 2017, 3:04 p.m.
On Sun, 16 Jul 2017 15:12:21 +0900, mlaneuville@gmail.com wrote:
> # HG changeset patch
> # User Matthieu Laneuville <matthieu.laneuville@octobus.net>
> # Date 1500185462 -32400
> #      Sun Jul 16 15:11:02 2017 +0900
> # Node ID 9722bfa929b50e7ad87e0561f8d5fdd8e928d755
> # Parent  97ec8637d261a97c30944d9242ee1ccdbf536cb3
> run-tests: also color the summary messages (skipped, failed...)
> 
> diff -r 97ec8637d261 -r 9722bfa929b5 tests/run-tests.py
> --- a/tests/run-tests.py	Sun Jul 16 14:03:58 2017 +0900
> +++ b/tests/run-tests.py	Sun Jul 16 15:11:02 2017 +0900
> @@ -96,12 +96,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
>          with_color = True
>          with_pygments = True
>      except ImportError:
>          pass
>  
> +class TestRunnerStyle(style.Style):

Still an issue. The error was that the style module was missing if pygments
couldn't be loaded.

Patch

diff -r 97ec8637d261 -r 9722bfa929b5 tests/run-tests.py
--- a/tests/run-tests.py	Sun Jul 16 14:03:58 2017 +0900
+++ b/tests/run-tests.py	Sun Jul 16 15:11:02 2017 +0900
@@ -96,12 +96,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
         with_color = True
         with_pygments = True
     except ImportError:
         pass
 
+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:         '#ansilightgray',
+        skippedname:     '#ansidarkgray',
+        failed:          '#ansidarkred',
+        failedname:      '#ansired',
+    }
+
+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
@@ -1587,7 +1621,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 with_color and with_pygments:
+                            formatted = pygments.highlight(
+                                formatted,
+                                TestRunnerLexer(),
+                                formatters.Terminal256Formatter(
+                                            style=TestRunnerStyle)).strip("\n")
+                        self.stream.write(formatted)
                     self.stream.write('!')
 
                 self.stream.flush()
@@ -1990,9 +2031,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 and with_pygments:
+                        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 and with_pygments:
+                    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 97ec8637d261 -r 9722bfa929b5 tests/test-run-tests.t
--- a/tests/test-run-tests.t	Sun Jul 16 14:03:58 2017 +0900
+++ b/tests/test-run-tests.t	Sun Jul 16 15:11:02 2017 +0900
@@ -132,10 +132,8 @@ 
   \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
+  \x1b[31mERROR: \x1b[39m\x1b[31;01mtest-failure.t\x1b[39;00m\x1b[31m output changed\x1b[39m! (esc)
+  \x1b[31mFailed \x1b[39m\x1b[31;01mtest-failure.t\x1b[39;00m\x1b[31m: output changed\x1b[39m (esc)
   # Ran 1 tests, 0 skipped, 1 failed.
   python hash seed: * (glob)
   [1]