Patchwork D10760: run-tests: enable color on Windows

login
register
mail settings
Submitter phabricator
Date May 20, 2021, 8:17 p.m.
Message ID <differential-rev-PHID-DREV-aijqamsluhm357sgno4s-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49080/
State Superseded
Headers show

Comments

phabricator - May 20, 2021, 8:17 p.m.
mharbison72 created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  In setting up the CI for Windows on heptapod, I noticed it was complaining about
  color not being enabled because pygments wasn't installed- even though it was.
  I had initially disabled color on Windows when using Windows 7, because that
  didn't understand ANSI color codes and made a mess of the output.  But now that
  it's been unsupported for over a year, I don't think we should care about it
  either.
  
  It's admittedly a hack to depend on Mercurial proper to enable color support in
  the terminal, but I didn't feel like duplicating that code.  I'm under the
  impression that 3rd party stuff is supposed to use this runner in the Mercurial
  repo instead of using their own copy, so I think it's safe to assume the
  Mercurial code is available.  If it's not, it won't break anything.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10760

AFFECTED FILES
  tests/run-tests.py

CHANGE DETAILS




To: mharbison72, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -87,21 +87,31 @@ 
 processlock = threading.Lock()
 
 pygmentspresent = False
-# ANSI color is unsupported prior to Windows 10
-if os.name != 'nt':
-    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
-        difflexer = lexers.DiffLexer()
-        terminal256formatter = formatters.Terminal256Formatter()
-    except ImportError:
-        pass
+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
+
+    if os.name == 'nt':
+        hgpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+        sys.path.append(hgpath)
+        try:
+            from mercurial import win32  # pytype: disable=import-error
+
+            # Don't check the result code because it fails on heptapod, but
+            # something is able to convert to color anyway.
+            win32.enablevtmode()
+        finally:
+            sys.path = sys.path[:-1]
+
+    pygmentspresent = True
+    difflexer = lexers.DiffLexer()
+    terminal256formatter = formatters.Terminal256Formatter()
+except ImportError:
+    pass
 
 if pygmentspresent: