Patchwork [v3] run-tests: make sure to check if pygments is installed before using it

login
register
mail settings
Submitter Matthieu Laneuville
Date July 17, 2017, 10:08 p.m.
Message ID <6254e8515425dd54f620.1500329329@carbon>
Download mbox | patch
Permalink /patch/22457/
State New, archived
Headers show

Comments

Matthieu Laneuville - July 17, 2017, 10:08 p.m.
# HG changeset patch
# User Matthieu Laneuville <matthieu.laneuville@octobus.net>
# Date 1500181438 -32400
#      Sun Jul 16 14:03:58 2017 +0900
# Node ID 6254e8515425dd54f6200a6231f2d45f86d5a060
# Parent  389536aff376d32d38f13305021c127245d4126a
run-tests: make sure to check if pygments is installed before using it

Makes sure color is never used if pygments is not available by conditioning
test-run-tests.t and checking if self.stream is a tty instead of stderr only.

Built upon:
https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-July/101785.html
Jun Wu - July 17, 2017, 10:27 p.m.
Pulkit's fix was just queued [1]. You might want to follow the hg-committed
repo to get the latest before sending patches.

[1]: https://www.mercurial-scm.org/repo/hg-committed/rev/754569f5e999

Excerpts from mlaneuville's message of 2017-07-18 07:08:49 +0900:
> # HG changeset patch
> # User Matthieu Laneuville <matthieu.laneuville@octobus.net>
> # Date 1500181438 -32400
> #      Sun Jul 16 14:03:58 2017 +0900
> # Node ID 6254e8515425dd54f6200a6231f2d45f86d5a060
> # Parent  389536aff376d32d38f13305021c127245d4126a
> run-tests: make sure to check if pygments is installed before using it
> 
> Makes sure color is never used if pygments is not available by conditioning
> test-run-tests.t and checking if self.stream is a tty instead of stderr only.
> 
> Built upon:
> https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-July/101785.html 
> 
> diff -r 389536aff376 -r 6254e8515425 tests/run-tests.py
> --- a/tests/run-tests.py    Sat Jul 15 15:01:29 2017 +0900
> +++ b/tests/run-tests.py    Sun Jul 16 14:03:58 2017 +0900
> @@ -89,6 +89,7 @@
>  processlock = threading.Lock()
>  
>  with_color = False
> +with_pygments = False
>  
>  # ANSI color is unsupported prior to Windows 10
>  if os.name != 'nt':
> @@ -97,12 +98,10 @@
>          import pygments.lexers as lexers
>          import pygments.formatters as formatters
>          with_color = True
> +        with_pygments = True
>      except ImportError:
>          pass
>  
> -if not sys.stderr.isatty(): # check if the terminal is capable
> -    with_color = False
> -
>  if sys.version_info > (3, 5, 0):
>      PYTHON3 = True
>      xrange = range # we use xrange in one place, and we'd rather not use range
> @@ -415,13 +414,6 @@
>          parser.error('--chg does not work when --with-hg is specified '
>                       '(use --with-chg instead)')
>  
> -    global with_color
> -    if options.color != 'auto':
> -        if options.color == 'never':
> -            with_color = False
> -        else: # 'always', for testing purposes
> -            with_color = True
> -
>      global useipv6
>      if options.ipv6:
>          useipv6 = checksocketfamily('AF_INET6')
> @@ -1573,6 +1565,17 @@
>          self.successes = []
>          self.faildata = {}
>  
> +        global with_color
> +        if not self.stream.isatty(): # check if the terminal is capable
> +            with_color = False
> +
> +        if options.color != 'auto':
> +            if options.color == 'never':
> +                with_color = False
> +            else: # 'always', for testing purposes
> +                if with_pygments:
> +                    with_color = True
> +
>      def addFailure(self, test, reason):
>          self.failures.append((test, reason))
>  
> @@ -1650,7 +1653,7 @@
>                  else:
>                      self.stream.write('\n')
>                      for line in lines:
> -                        if with_color:
> +                        if with_color and with_pygments:
>                              line = pygments.highlight(
>                                      line,
>                                      lexers.DiffLexer(),
> diff -r 389536aff376 -r 6254e8515425 tests/test-run-tests.t
> --- a/tests/test-run-tests.t    Sat Jul 15 15:01:29 2017 +0900
> +++ b/tests/test-run-tests.t    Sun Jul 16 14:03:58 2017 +0900
> @@ -121,7 +121,7 @@
>  
>  test diff colorisation
>  
> -#if no-windows
> +#if no-windows pygments
>    $ rt test-failure.t --color always
>    
>    \x1b[38;5;124m--- $TESTTMP/test-failure.t\x1b[39m (esc)

Patch

diff -r 389536aff376 -r 6254e8515425 tests/run-tests.py
--- a/tests/run-tests.py	Sat Jul 15 15:01:29 2017 +0900
+++ b/tests/run-tests.py	Sun Jul 16 14:03:58 2017 +0900
@@ -89,6 +89,7 @@ 
 processlock = threading.Lock()
 
 with_color = False
+with_pygments = False
 
 # ANSI color is unsupported prior to Windows 10
 if os.name != 'nt':
@@ -97,12 +98,10 @@ 
         import pygments.lexers as lexers
         import pygments.formatters as formatters
         with_color = True
+        with_pygments = True
     except ImportError:
         pass
 
-if not sys.stderr.isatty(): # check if the terminal is capable
-    with_color = False
-
 if sys.version_info > (3, 5, 0):
     PYTHON3 = True
     xrange = range # we use xrange in one place, and we'd rather not use range
@@ -415,13 +414,6 @@ 
         parser.error('--chg does not work when --with-hg is specified '
                      '(use --with-chg instead)')
 
-    global with_color
-    if options.color != 'auto':
-        if options.color == 'never':
-            with_color = False
-        else: # 'always', for testing purposes
-            with_color = True
-
     global useipv6
     if options.ipv6:
         useipv6 = checksocketfamily('AF_INET6')
@@ -1573,6 +1565,17 @@ 
         self.successes = []
         self.faildata = {}
 
+        global with_color
+        if not self.stream.isatty(): # check if the terminal is capable
+            with_color = False
+
+        if options.color != 'auto':
+            if options.color == 'never':
+                with_color = False
+            else: # 'always', for testing purposes
+                if with_pygments:
+                    with_color = True
+
     def addFailure(self, test, reason):
         self.failures.append((test, reason))
 
@@ -1650,7 +1653,7 @@ 
                 else:
                     self.stream.write('\n')
                     for line in lines:
-                        if with_color:
+                        if with_color and with_pygments:
                             line = pygments.highlight(
                                     line,
                                     lexers.DiffLexer(),
diff -r 389536aff376 -r 6254e8515425 tests/test-run-tests.t
--- a/tests/test-run-tests.t	Sat Jul 15 15:01:29 2017 +0900
+++ b/tests/test-run-tests.t	Sun Jul 16 14:03:58 2017 +0900
@@ -121,7 +121,7 @@ 
 
 test diff colorisation
 
-#if no-windows
+#if no-windows pygments
   $ rt test-failure.t --color always
   
   \x1b[38;5;124m--- $TESTTMP/test-failure.t\x1b[39m (esc)