comparison tests/run-tests.py @ 33420:e80041832eec

run-tests: add color to output if pygments is available The output of run-tests has no formatting by default, which hampers readability. This patch colors the diff output when pygments is available. To avoid coloring even when pygments is available, use --color never.
author Matthieu Laneuville <matthieu.laneuville@octobus.net>
date Thu, 29 Jun 2017 20:45:12 +0900
parents cf826b9e9ea4
children 9c6e64911de0
comparison
equal deleted inserted replaced
33419:7c33adc823e0 33420:e80041832eec
86 86
87 origenviron = os.environ.copy() 87 origenviron = os.environ.copy()
88 osenvironb = getattr(os, 'environb', os.environ) 88 osenvironb = getattr(os, 'environb', os.environ)
89 processlock = threading.Lock() 89 processlock = threading.Lock()
90 90
91 with_color = False
92 try: # is pygments installed
93 import pygments
94 import pygments.lexers as lexers
95 import pygments.formatters as formatters
96 with_color = True
97 except ImportError:
98 pass
99
100 if not sys.stderr.isatty(): # check if the terminal is capable
101 with_color = False
102
91 if sys.version_info > (3, 5, 0): 103 if sys.version_info > (3, 5, 0):
92 PYTHON3 = True 104 PYTHON3 = True
93 xrange = range # we use xrange in one place, and we'd rather not use range 105 xrange = range # we use xrange in one place, and we'd rather not use range
94 def _bytespath(p): 106 def _bytespath(p):
95 return p.encode('utf-8') 107 return p.encode('utf-8')
253 help="run tests that are changed in parent rev or working directory") 265 help="run tests that are changed in parent rev or working directory")
254 parser.add_option("-C", "--annotate", action="store_true", 266 parser.add_option("-C", "--annotate", action="store_true",
255 help="output files annotated with coverage") 267 help="output files annotated with coverage")
256 parser.add_option("-c", "--cover", action="store_true", 268 parser.add_option("-c", "--cover", action="store_true",
257 help="print a test coverage report") 269 help="print a test coverage report")
270 parser.add_option("--color", choices=["always", "auto", "never"],
271 default="auto",
272 help="colorisation: always|auto|never (default: auto)")
258 parser.add_option("-d", "--debug", action="store_true", 273 parser.add_option("-d", "--debug", action="store_true",
259 help="debug mode: write output of test scripts to console" 274 help="debug mode: write output of test scripts to console"
260 " rather than capturing and diffing it (disables timeout)") 275 " rather than capturing and diffing it (disables timeout)")
261 parser.add_option("-f", "--first", action="store_true", 276 parser.add_option("-f", "--first", action="store_true",
262 help="exit on the first test failure") 277 help="exit on the first test failure")
395 if options.chg and options.with_hg: 410 if options.chg and options.with_hg:
396 # chg shares installation location with hg 411 # chg shares installation location with hg
397 parser.error('--chg does not work when --with-hg is specified ' 412 parser.error('--chg does not work when --with-hg is specified '
398 '(use --with-chg instead)') 413 '(use --with-chg instead)')
399 414
415 global with_color
416 if options.color != 'auto':
417 if options.color == 'never':
418 with_color = False
419 else: # 'always', for testing purposes
420 with_color = True
421
400 global useipv6 422 global useipv6
401 if options.ipv6: 423 if options.ipv6:
402 useipv6 = checksocketfamily('AF_INET6') 424 useipv6 = checksocketfamily('AF_INET6')
403 else: 425 else:
404 # only use IPv6 if IPv4 is unavailable and IPv6 is available 426 # only use IPv6 if IPv4 is unavailable and IPv6 is available
1623 raise test.failureException( 1645 raise test.failureException(
1624 'server failed to start (HGPORT=%s)' % test._startport) 1646 'server failed to start (HGPORT=%s)' % test._startport)
1625 else: 1647 else:
1626 self.stream.write('\n') 1648 self.stream.write('\n')
1627 for line in lines: 1649 for line in lines:
1650 if with_color:
1651 line = pygments.highlight(
1652 line,
1653 lexers.DiffLexer(),
1654 formatters.Terminal256Formatter())
1628 if PYTHON3: 1655 if PYTHON3:
1629 self.stream.flush() 1656 self.stream.flush()
1630 self.stream.buffer.write(line) 1657 self.stream.buffer.write(line)
1631 self.stream.buffer.flush() 1658 self.stream.buffer.flush()
1632 else: 1659 else: