##// END OF EJS Templates
run-tests: use a context manager when looking for available ports...
Matt Harbison -
r46569:064449f9 default
parent child Browse files
Show More
@@ -1,3786 +1,3786 b''
1 1 #!/usr/bin/env python3
2 2 #
3 3 # run-tests.py - Run a set of tests on Mercurial
4 4 #
5 5 # Copyright 2006 Matt Mackall <mpm@selenic.com>
6 6 #
7 7 # This software may be used and distributed according to the terms of the
8 8 # GNU General Public License version 2 or any later version.
9 9
10 10 # Modifying this script is tricky because it has many modes:
11 11 # - serial (default) vs parallel (-jN, N > 1)
12 12 # - no coverage (default) vs coverage (-c, -C, -s)
13 13 # - temp install (default) vs specific hg script (--with-hg, --local)
14 14 # - tests are a mix of shell scripts and Python scripts
15 15 #
16 16 # If you change this script, it is recommended that you ensure you
17 17 # haven't broken it by running it in various modes with a representative
18 18 # sample of test scripts. For example:
19 19 #
20 20 # 1) serial, no coverage, temp install:
21 21 # ./run-tests.py test-s*
22 22 # 2) serial, no coverage, local hg:
23 23 # ./run-tests.py --local test-s*
24 24 # 3) serial, coverage, temp install:
25 25 # ./run-tests.py -c test-s*
26 26 # 4) serial, coverage, local hg:
27 27 # ./run-tests.py -c --local test-s* # unsupported
28 28 # 5) parallel, no coverage, temp install:
29 29 # ./run-tests.py -j2 test-s*
30 30 # 6) parallel, no coverage, local hg:
31 31 # ./run-tests.py -j2 --local test-s*
32 32 # 7) parallel, coverage, temp install:
33 33 # ./run-tests.py -j2 -c test-s* # currently broken
34 34 # 8) parallel, coverage, local install:
35 35 # ./run-tests.py -j2 -c --local test-s* # unsupported (and broken)
36 36 # 9) parallel, custom tmp dir:
37 37 # ./run-tests.py -j2 --tmpdir /tmp/myhgtests
38 38 # 10) parallel, pure, tests that call run-tests:
39 39 # ./run-tests.py --pure `grep -l run-tests.py *.t`
40 40 #
41 41 # (You could use any subset of the tests: test-s* happens to match
42 42 # enough that it's worth doing parallel runs, few enough that it
43 43 # completes fairly quickly, includes both shell and Python scripts, and
44 44 # includes some scripts that run daemon processes.)
45 45
46 46 from __future__ import absolute_import, print_function
47 47
48 48 import argparse
49 49 import collections
50 import contextlib
50 51 import difflib
51 52 import distutils.version as version
52 53 import errno
53 54 import json
54 55 import multiprocessing
55 56 import os
56 57 import platform
57 58 import random
58 59 import re
59 60 import shutil
60 61 import signal
61 62 import socket
62 63 import subprocess
63 64 import sys
64 65 import sysconfig
65 66 import tempfile
66 67 import threading
67 68 import time
68 69 import unittest
69 70 import uuid
70 71 import xml.dom.minidom as minidom
71 72
72 73 try:
73 74 import Queue as queue
74 75 except ImportError:
75 76 import queue
76 77
77 78 try:
78 79 import shlex
79 80
80 81 shellquote = shlex.quote
81 82 except (ImportError, AttributeError):
82 83 import pipes
83 84
84 85 shellquote = pipes.quote
85 86
86 87 processlock = threading.Lock()
87 88
88 89 pygmentspresent = False
89 90 # ANSI color is unsupported prior to Windows 10
90 91 if os.name != 'nt':
91 92 try: # is pygments installed
92 93 import pygments
93 94 import pygments.lexers as lexers
94 95 import pygments.lexer as lexer
95 96 import pygments.formatters as formatters
96 97 import pygments.token as token
97 98 import pygments.style as style
98 99
99 100 pygmentspresent = True
100 101 difflexer = lexers.DiffLexer()
101 102 terminal256formatter = formatters.Terminal256Formatter()
102 103 except ImportError:
103 104 pass
104 105
105 106 if pygmentspresent:
106 107
107 108 class TestRunnerStyle(style.Style):
108 109 default_style = ""
109 110 skipped = token.string_to_tokentype("Token.Generic.Skipped")
110 111 failed = token.string_to_tokentype("Token.Generic.Failed")
111 112 skippedname = token.string_to_tokentype("Token.Generic.SName")
112 113 failedname = token.string_to_tokentype("Token.Generic.FName")
113 114 styles = {
114 115 skipped: '#e5e5e5',
115 116 skippedname: '#00ffff',
116 117 failed: '#7f0000',
117 118 failedname: '#ff0000',
118 119 }
119 120
120 121 class TestRunnerLexer(lexer.RegexLexer):
121 122 testpattern = r'[\w-]+\.(t|py)(#[a-zA-Z0-9_\-\.]+)?'
122 123 tokens = {
123 124 'root': [
124 125 (r'^Skipped', token.Generic.Skipped, 'skipped'),
125 126 (r'^Failed ', token.Generic.Failed, 'failed'),
126 127 (r'^ERROR: ', token.Generic.Failed, 'failed'),
127 128 ],
128 129 'skipped': [
129 130 (testpattern, token.Generic.SName),
130 131 (r':.*', token.Generic.Skipped),
131 132 ],
132 133 'failed': [
133 134 (testpattern, token.Generic.FName),
134 135 (r'(:| ).*', token.Generic.Failed),
135 136 ],
136 137 }
137 138
138 139 runnerformatter = formatters.Terminal256Formatter(style=TestRunnerStyle)
139 140 runnerlexer = TestRunnerLexer()
140 141
141 142 origenviron = os.environ.copy()
142 143
143 144 if sys.version_info > (3, 5, 0):
144 145 PYTHON3 = True
145 146 xrange = range # we use xrange in one place, and we'd rather not use range
146 147
147 148 def _sys2bytes(p):
148 149 if p is None:
149 150 return p
150 151 return p.encode('utf-8')
151 152
152 153 def _bytes2sys(p):
153 154 if p is None:
154 155 return p
155 156 return p.decode('utf-8')
156 157
157 158 osenvironb = getattr(os, 'environb', None)
158 159 if osenvironb is None:
159 160 # Windows lacks os.environb, for instance. A proxy over the real thing
160 161 # instead of a copy allows the environment to be updated via bytes on
161 162 # all platforms.
162 163 class environbytes(object):
163 164 def __init__(self, strenv):
164 165 self.__len__ = strenv.__len__
165 166 self.clear = strenv.clear
166 167 self._strenv = strenv
167 168
168 169 def __getitem__(self, k):
169 170 v = self._strenv.__getitem__(_bytes2sys(k))
170 171 return _sys2bytes(v)
171 172
172 173 def __setitem__(self, k, v):
173 174 self._strenv.__setitem__(_bytes2sys(k), _bytes2sys(v))
174 175
175 176 def __delitem__(self, k):
176 177 self._strenv.__delitem__(_bytes2sys(k))
177 178
178 179 def __contains__(self, k):
179 180 return self._strenv.__contains__(_bytes2sys(k))
180 181
181 182 def __iter__(self):
182 183 return iter([_sys2bytes(k) for k in iter(self._strenv)])
183 184
184 185 def get(self, k, default=None):
185 186 v = self._strenv.get(_bytes2sys(k), _bytes2sys(default))
186 187 return _sys2bytes(v)
187 188
188 189 def pop(self, k, default=None):
189 190 v = self._strenv.pop(_bytes2sys(k), _bytes2sys(default))
190 191 return _sys2bytes(v)
191 192
192 193 osenvironb = environbytes(os.environ)
193 194
194 195 getcwdb = getattr(os, 'getcwdb')
195 196 if not getcwdb or os.name == 'nt':
196 197 getcwdb = lambda: _sys2bytes(os.getcwd())
197 198
198 199 elif sys.version_info >= (3, 0, 0):
199 200 print(
200 201 '%s is only supported on Python 3.5+ and 2.7, not %s'
201 202 % (sys.argv[0], '.'.join(str(v) for v in sys.version_info[:3]))
202 203 )
203 204 sys.exit(70) # EX_SOFTWARE from `man 3 sysexit`
204 205 else:
205 206 PYTHON3 = False
206 207
207 208 # In python 2.x, path operations are generally done using
208 209 # bytestrings by default, so we don't have to do any extra
209 210 # fiddling there. We define the wrapper functions anyway just to
210 211 # help keep code consistent between platforms.
211 212 def _sys2bytes(p):
212 213 return p
213 214
214 215 _bytes2sys = _sys2bytes
215 216 osenvironb = os.environ
216 217 getcwdb = os.getcwd
217 218
218 219 # For Windows support
219 220 wifexited = getattr(os, "WIFEXITED", lambda x: False)
220 221
221 222 # Whether to use IPv6
222 223 def checksocketfamily(name, port=20058):
223 224 """return true if we can listen on localhost using family=name
224 225
225 226 name should be either 'AF_INET', or 'AF_INET6'.
226 227 port being used is okay - EADDRINUSE is considered as successful.
227 228 """
228 229 family = getattr(socket, name, None)
229 230 if family is None:
230 231 return False
231 232 try:
232 233 s = socket.socket(family, socket.SOCK_STREAM)
233 234 s.bind(('localhost', port))
234 235 s.close()
235 236 return True
236 237 except socket.error as exc:
237 238 if exc.errno == errno.EADDRINUSE:
238 239 return True
239 240 elif exc.errno in (errno.EADDRNOTAVAIL, errno.EPROTONOSUPPORT):
240 241 return False
241 242 else:
242 243 raise
243 244 else:
244 245 return False
245 246
246 247
247 248 # useipv6 will be set by parseargs
248 249 useipv6 = None
249 250
250 251
251 252 def checkportisavailable(port):
252 253 """return true if a port seems free to bind on localhost"""
253 254 if useipv6:
254 255 family = socket.AF_INET6
255 256 else:
256 257 family = socket.AF_INET
257 258 try:
258 s = socket.socket(family, socket.SOCK_STREAM)
259 s.bind(('localhost', port))
260 s.close()
259 with contextlib.closing(socket.socket(family, socket.SOCK_STREAM)) as s:
260 s.bind(('localhost', port))
261 261 return True
262 262 except socket.error as exc:
263 263 if exc.errno not in (
264 264 errno.EADDRINUSE,
265 265 errno.EADDRNOTAVAIL,
266 266 errno.EPROTONOSUPPORT,
267 267 ):
268 268 raise
269 269 return False
270 270
271 271
272 272 closefds = os.name == 'posix'
273 273
274 274
275 275 def Popen4(cmd, wd, timeout, env=None):
276 276 processlock.acquire()
277 277 p = subprocess.Popen(
278 278 _bytes2sys(cmd),
279 279 shell=True,
280 280 bufsize=-1,
281 281 cwd=_bytes2sys(wd),
282 282 env=env,
283 283 close_fds=closefds,
284 284 stdin=subprocess.PIPE,
285 285 stdout=subprocess.PIPE,
286 286 stderr=subprocess.STDOUT,
287 287 )
288 288 processlock.release()
289 289
290 290 p.fromchild = p.stdout
291 291 p.tochild = p.stdin
292 292 p.childerr = p.stderr
293 293
294 294 p.timeout = False
295 295 if timeout:
296 296
297 297 def t():
298 298 start = time.time()
299 299 while time.time() - start < timeout and p.returncode is None:
300 300 time.sleep(0.1)
301 301 p.timeout = True
302 302 if p.returncode is None:
303 303 terminate(p)
304 304
305 305 threading.Thread(target=t).start()
306 306
307 307 return p
308 308
309 309
310 310 if sys.executable:
311 311 sysexecutable = sys.executable
312 312 elif os.environ.get('PYTHONEXECUTABLE'):
313 313 sysexecutable = os.environ['PYTHONEXECUTABLE']
314 314 elif os.environ.get('PYTHON'):
315 315 sysexecutable = os.environ['PYTHON']
316 316 else:
317 317 raise AssertionError('Could not find Python interpreter')
318 318
319 319 PYTHON = _sys2bytes(sysexecutable.replace('\\', '/'))
320 320 IMPL_PATH = b'PYTHONPATH'
321 321 if 'java' in sys.platform:
322 322 IMPL_PATH = b'JYTHONPATH'
323 323
324 324 default_defaults = {
325 325 'jobs': ('HGTEST_JOBS', multiprocessing.cpu_count()),
326 326 'timeout': ('HGTEST_TIMEOUT', 180),
327 327 'slowtimeout': ('HGTEST_SLOWTIMEOUT', 1500),
328 328 'port': ('HGTEST_PORT', 20059),
329 329 'shell': ('HGTEST_SHELL', 'sh'),
330 330 }
331 331
332 332 defaults = default_defaults.copy()
333 333
334 334
335 335 def canonpath(path):
336 336 return os.path.realpath(os.path.expanduser(path))
337 337
338 338
339 339 def parselistfiles(files, listtype, warn=True):
340 340 entries = dict()
341 341 for filename in files:
342 342 try:
343 343 path = os.path.expanduser(os.path.expandvars(filename))
344 344 f = open(path, "rb")
345 345 except IOError as err:
346 346 if err.errno != errno.ENOENT:
347 347 raise
348 348 if warn:
349 349 print("warning: no such %s file: %s" % (listtype, filename))
350 350 continue
351 351
352 352 for line in f.readlines():
353 353 line = line.split(b'#', 1)[0].strip()
354 354 if line:
355 355 entries[line] = filename
356 356
357 357 f.close()
358 358 return entries
359 359
360 360
361 361 def parsettestcases(path):
362 362 """read a .t test file, return a set of test case names
363 363
364 364 If path does not exist, return an empty set.
365 365 """
366 366 cases = []
367 367 try:
368 368 with open(path, 'rb') as f:
369 369 for l in f:
370 370 if l.startswith(b'#testcases '):
371 371 cases.append(sorted(l[11:].split()))
372 372 except IOError as ex:
373 373 if ex.errno != errno.ENOENT:
374 374 raise
375 375 return cases
376 376
377 377
378 378 def getparser():
379 379 """Obtain the OptionParser used by the CLI."""
380 380 parser = argparse.ArgumentParser(usage='%(prog)s [options] [tests]')
381 381
382 382 selection = parser.add_argument_group('Test Selection')
383 383 selection.add_argument(
384 384 '--allow-slow-tests',
385 385 action='store_true',
386 386 help='allow extremely slow tests',
387 387 )
388 388 selection.add_argument(
389 389 "--blacklist",
390 390 action="append",
391 391 help="skip tests listed in the specified blacklist file",
392 392 )
393 393 selection.add_argument(
394 394 "--changed",
395 395 help="run tests that are changed in parent rev or working directory",
396 396 )
397 397 selection.add_argument(
398 398 "-k", "--keywords", help="run tests matching keywords"
399 399 )
400 400 selection.add_argument(
401 401 "-r", "--retest", action="store_true", help="retest failed tests"
402 402 )
403 403 selection.add_argument(
404 404 "--test-list",
405 405 action="append",
406 406 help="read tests to run from the specified file",
407 407 )
408 408 selection.add_argument(
409 409 "--whitelist",
410 410 action="append",
411 411 help="always run tests listed in the specified whitelist file",
412 412 )
413 413 selection.add_argument(
414 414 'tests', metavar='TESTS', nargs='*', help='Tests to run'
415 415 )
416 416
417 417 harness = parser.add_argument_group('Test Harness Behavior')
418 418 harness.add_argument(
419 419 '--bisect-repo',
420 420 metavar='bisect_repo',
421 421 help=(
422 422 "Path of a repo to bisect. Use together with " "--known-good-rev"
423 423 ),
424 424 )
425 425 harness.add_argument(
426 426 "-d",
427 427 "--debug",
428 428 action="store_true",
429 429 help="debug mode: write output of test scripts to console"
430 430 " rather than capturing and diffing it (disables timeout)",
431 431 )
432 432 harness.add_argument(
433 433 "-f",
434 434 "--first",
435 435 action="store_true",
436 436 help="exit on the first test failure",
437 437 )
438 438 harness.add_argument(
439 439 "-i",
440 440 "--interactive",
441 441 action="store_true",
442 442 help="prompt to accept changed output",
443 443 )
444 444 harness.add_argument(
445 445 "-j",
446 446 "--jobs",
447 447 type=int,
448 448 help="number of jobs to run in parallel"
449 449 " (default: $%s or %d)" % defaults['jobs'],
450 450 )
451 451 harness.add_argument(
452 452 "--keep-tmpdir",
453 453 action="store_true",
454 454 help="keep temporary directory after running tests",
455 455 )
456 456 harness.add_argument(
457 457 '--known-good-rev',
458 458 metavar="known_good_rev",
459 459 help=(
460 460 "Automatically bisect any failures using this "
461 461 "revision as a known-good revision."
462 462 ),
463 463 )
464 464 harness.add_argument(
465 465 "--list-tests",
466 466 action="store_true",
467 467 help="list tests instead of running them",
468 468 )
469 469 harness.add_argument(
470 470 "--loop", action="store_true", help="loop tests repeatedly"
471 471 )
472 472 harness.add_argument(
473 473 '--random', action="store_true", help='run tests in random order'
474 474 )
475 475 harness.add_argument(
476 476 '--order-by-runtime',
477 477 action="store_true",
478 478 help='run slowest tests first, according to .testtimes',
479 479 )
480 480 harness.add_argument(
481 481 "-p",
482 482 "--port",
483 483 type=int,
484 484 help="port on which servers should listen"
485 485 " (default: $%s or %d)" % defaults['port'],
486 486 )
487 487 harness.add_argument(
488 488 '--profile-runner',
489 489 action='store_true',
490 490 help='run statprof on run-tests',
491 491 )
492 492 harness.add_argument(
493 493 "-R", "--restart", action="store_true", help="restart at last error"
494 494 )
495 495 harness.add_argument(
496 496 "--runs-per-test",
497 497 type=int,
498 498 dest="runs_per_test",
499 499 help="run each test N times (default=1)",
500 500 default=1,
501 501 )
502 502 harness.add_argument(
503 503 "--shell", help="shell to use (default: $%s or %s)" % defaults['shell']
504 504 )
505 505 harness.add_argument(
506 506 '--showchannels', action='store_true', help='show scheduling channels'
507 507 )
508 508 harness.add_argument(
509 509 "--slowtimeout",
510 510 type=int,
511 511 help="kill errant slow tests after SLOWTIMEOUT seconds"
512 512 " (default: $%s or %d)" % defaults['slowtimeout'],
513 513 )
514 514 harness.add_argument(
515 515 "-t",
516 516 "--timeout",
517 517 type=int,
518 518 help="kill errant tests after TIMEOUT seconds"
519 519 " (default: $%s or %d)" % defaults['timeout'],
520 520 )
521 521 harness.add_argument(
522 522 "--tmpdir",
523 523 help="run tests in the given temporary directory"
524 524 " (implies --keep-tmpdir)",
525 525 )
526 526 harness.add_argument(
527 527 "-v", "--verbose", action="store_true", help="output verbose messages"
528 528 )
529 529
530 530 hgconf = parser.add_argument_group('Mercurial Configuration')
531 531 hgconf.add_argument(
532 532 "--chg",
533 533 action="store_true",
534 534 help="install and use chg wrapper in place of hg",
535 535 )
536 536 hgconf.add_argument(
537 537 "--chg-debug",
538 538 action="store_true",
539 539 help="show chg debug logs",
540 540 )
541 541 hgconf.add_argument("--compiler", help="compiler to build with")
542 542 hgconf.add_argument(
543 543 '--extra-config-opt',
544 544 action="append",
545 545 default=[],
546 546 help='set the given config opt in the test hgrc',
547 547 )
548 548 hgconf.add_argument(
549 549 "-l",
550 550 "--local",
551 551 action="store_true",
552 552 help="shortcut for --with-hg=<testdir>/../hg, "
553 553 "and --with-chg=<testdir>/../contrib/chg/chg if --chg is set",
554 554 )
555 555 hgconf.add_argument(
556 556 "--ipv6",
557 557 action="store_true",
558 558 help="prefer IPv6 to IPv4 for network related tests",
559 559 )
560 560 hgconf.add_argument(
561 561 "--pure",
562 562 action="store_true",
563 563 help="use pure Python code instead of C extensions",
564 564 )
565 565 hgconf.add_argument(
566 566 "--rust",
567 567 action="store_true",
568 568 help="use Rust code alongside C extensions",
569 569 )
570 570 hgconf.add_argument(
571 571 "--no-rust",
572 572 action="store_true",
573 573 help="do not use Rust code even if compiled",
574 574 )
575 575 hgconf.add_argument(
576 576 "--with-chg",
577 577 metavar="CHG",
578 578 help="use specified chg wrapper in place of hg",
579 579 )
580 580 hgconf.add_argument(
581 581 "--with-hg",
582 582 metavar="HG",
583 583 help="test using specified hg script rather than a "
584 584 "temporary installation",
585 585 )
586 586
587 587 reporting = parser.add_argument_group('Results Reporting')
588 588 reporting.add_argument(
589 589 "-C",
590 590 "--annotate",
591 591 action="store_true",
592 592 help="output files annotated with coverage",
593 593 )
594 594 reporting.add_argument(
595 595 "--color",
596 596 choices=["always", "auto", "never"],
597 597 default=os.environ.get('HGRUNTESTSCOLOR', 'auto'),
598 598 help="colorisation: always|auto|never (default: auto)",
599 599 )
600 600 reporting.add_argument(
601 601 "-c",
602 602 "--cover",
603 603 action="store_true",
604 604 help="print a test coverage report",
605 605 )
606 606 reporting.add_argument(
607 607 '--exceptions',
608 608 action='store_true',
609 609 help='log all exceptions and generate an exception report',
610 610 )
611 611 reporting.add_argument(
612 612 "-H",
613 613 "--htmlcov",
614 614 action="store_true",
615 615 help="create an HTML report of the coverage of the files",
616 616 )
617 617 reporting.add_argument(
618 618 "--json",
619 619 action="store_true",
620 620 help="store test result data in 'report.json' file",
621 621 )
622 622 reporting.add_argument(
623 623 "--outputdir",
624 624 help="directory to write error logs to (default=test directory)",
625 625 )
626 626 reporting.add_argument(
627 627 "-n", "--nodiff", action="store_true", help="skip showing test changes"
628 628 )
629 629 reporting.add_argument(
630 630 "-S",
631 631 "--noskips",
632 632 action="store_true",
633 633 help="don't report skip tests verbosely",
634 634 )
635 635 reporting.add_argument(
636 636 "--time", action="store_true", help="time how long each test takes"
637 637 )
638 638 reporting.add_argument("--view", help="external diff viewer")
639 639 reporting.add_argument(
640 640 "--xunit", help="record xunit results at specified path"
641 641 )
642 642
643 643 for option, (envvar, default) in defaults.items():
644 644 defaults[option] = type(default)(os.environ.get(envvar, default))
645 645 parser.set_defaults(**defaults)
646 646
647 647 return parser
648 648
649 649
650 650 def parseargs(args, parser):
651 651 """Parse arguments with our OptionParser and validate results."""
652 652 options = parser.parse_args(args)
653 653
654 654 # jython is always pure
655 655 if 'java' in sys.platform or '__pypy__' in sys.modules:
656 656 options.pure = True
657 657
658 658 if platform.python_implementation() != 'CPython' and options.rust:
659 659 parser.error('Rust extensions are only available with CPython')
660 660
661 661 if options.pure and options.rust:
662 662 parser.error('--rust cannot be used with --pure')
663 663
664 664 if options.rust and options.no_rust:
665 665 parser.error('--rust cannot be used with --no-rust')
666 666
667 667 if options.local:
668 668 if options.with_hg or options.with_chg:
669 669 parser.error('--local cannot be used with --with-hg or --with-chg')
670 670 testdir = os.path.dirname(_sys2bytes(canonpath(sys.argv[0])))
671 671 reporootdir = os.path.dirname(testdir)
672 672 pathandattrs = [(b'hg', 'with_hg')]
673 673 if options.chg:
674 674 pathandattrs.append((b'contrib/chg/chg', 'with_chg'))
675 675 for relpath, attr in pathandattrs:
676 676 binpath = os.path.join(reporootdir, relpath)
677 677 if os.name != 'nt' and not os.access(binpath, os.X_OK):
678 678 parser.error(
679 679 '--local specified, but %r not found or '
680 680 'not executable' % binpath
681 681 )
682 682 setattr(options, attr, _bytes2sys(binpath))
683 683
684 684 if options.with_hg:
685 685 options.with_hg = canonpath(_sys2bytes(options.with_hg))
686 686 if not (
687 687 os.path.isfile(options.with_hg)
688 688 and os.access(options.with_hg, os.X_OK)
689 689 ):
690 690 parser.error('--with-hg must specify an executable hg script')
691 691 if os.path.basename(options.with_hg) not in [b'hg', b'hg.exe']:
692 692 sys.stderr.write('warning: --with-hg should specify an hg script\n')
693 693 sys.stderr.flush()
694 694
695 695 if (options.chg or options.with_chg) and os.name == 'nt':
696 696 parser.error('chg does not work on %s' % os.name)
697 697 if options.with_chg:
698 698 options.chg = False # no installation to temporary location
699 699 options.with_chg = canonpath(_sys2bytes(options.with_chg))
700 700 if not (
701 701 os.path.isfile(options.with_chg)
702 702 and os.access(options.with_chg, os.X_OK)
703 703 ):
704 704 parser.error('--with-chg must specify a chg executable')
705 705 if options.chg and options.with_hg:
706 706 # chg shares installation location with hg
707 707 parser.error(
708 708 '--chg does not work when --with-hg is specified '
709 709 '(use --with-chg instead)'
710 710 )
711 711
712 712 if options.color == 'always' and not pygmentspresent:
713 713 sys.stderr.write(
714 714 'warning: --color=always ignored because '
715 715 'pygments is not installed\n'
716 716 )
717 717
718 718 if options.bisect_repo and not options.known_good_rev:
719 719 parser.error("--bisect-repo cannot be used without --known-good-rev")
720 720
721 721 global useipv6
722 722 if options.ipv6:
723 723 useipv6 = checksocketfamily('AF_INET6')
724 724 else:
725 725 # only use IPv6 if IPv4 is unavailable and IPv6 is available
726 726 useipv6 = (not checksocketfamily('AF_INET')) and checksocketfamily(
727 727 'AF_INET6'
728 728 )
729 729
730 730 options.anycoverage = options.cover or options.annotate or options.htmlcov
731 731 if options.anycoverage:
732 732 try:
733 733 import coverage
734 734
735 735 covver = version.StrictVersion(coverage.__version__).version
736 736 if covver < (3, 3):
737 737 parser.error('coverage options require coverage 3.3 or later')
738 738 except ImportError:
739 739 parser.error('coverage options now require the coverage package')
740 740
741 741 if options.anycoverage and options.local:
742 742 # this needs some path mangling somewhere, I guess
743 743 parser.error(
744 744 "sorry, coverage options do not work when --local " "is specified"
745 745 )
746 746
747 747 if options.anycoverage and options.with_hg:
748 748 parser.error(
749 749 "sorry, coverage options do not work when --with-hg " "is specified"
750 750 )
751 751
752 752 global verbose
753 753 if options.verbose:
754 754 verbose = ''
755 755
756 756 if options.tmpdir:
757 757 options.tmpdir = canonpath(options.tmpdir)
758 758
759 759 if options.jobs < 1:
760 760 parser.error('--jobs must be positive')
761 761 if options.interactive and options.debug:
762 762 parser.error("-i/--interactive and -d/--debug are incompatible")
763 763 if options.debug:
764 764 if options.timeout != defaults['timeout']:
765 765 sys.stderr.write('warning: --timeout option ignored with --debug\n')
766 766 if options.slowtimeout != defaults['slowtimeout']:
767 767 sys.stderr.write(
768 768 'warning: --slowtimeout option ignored with --debug\n'
769 769 )
770 770 options.timeout = 0
771 771 options.slowtimeout = 0
772 772
773 773 if options.blacklist:
774 774 options.blacklist = parselistfiles(options.blacklist, 'blacklist')
775 775 if options.whitelist:
776 776 options.whitelisted = parselistfiles(options.whitelist, 'whitelist')
777 777 else:
778 778 options.whitelisted = {}
779 779
780 780 if options.showchannels:
781 781 options.nodiff = True
782 782
783 783 return options
784 784
785 785
786 786 def rename(src, dst):
787 787 """Like os.rename(), trade atomicity and opened files friendliness
788 788 for existing destination support.
789 789 """
790 790 shutil.copy(src, dst)
791 791 os.remove(src)
792 792
793 793
794 794 def makecleanable(path):
795 795 """Try to fix directory permission recursively so that the entire tree
796 796 can be deleted"""
797 797 for dirpath, dirnames, _filenames in os.walk(path, topdown=True):
798 798 for d in dirnames:
799 799 p = os.path.join(dirpath, d)
800 800 try:
801 801 os.chmod(p, os.stat(p).st_mode & 0o777 | 0o700) # chmod u+rwx
802 802 except OSError:
803 803 pass
804 804
805 805
806 806 _unified_diff = difflib.unified_diff
807 807 if PYTHON3:
808 808 import functools
809 809
810 810 _unified_diff = functools.partial(difflib.diff_bytes, difflib.unified_diff)
811 811
812 812
813 813 def getdiff(expected, output, ref, err):
814 814 servefail = False
815 815 lines = []
816 816 for line in _unified_diff(expected, output, ref, err):
817 817 if line.startswith(b'+++') or line.startswith(b'---'):
818 818 line = line.replace(b'\\', b'/')
819 819 if line.endswith(b' \n'):
820 820 line = line[:-2] + b'\n'
821 821 lines.append(line)
822 822 if not servefail and line.startswith(
823 823 b'+ abort: child process failed to start'
824 824 ):
825 825 servefail = True
826 826
827 827 return servefail, lines
828 828
829 829
830 830 verbose = False
831 831
832 832
833 833 def vlog(*msg):
834 834 """Log only when in verbose mode."""
835 835 if verbose is False:
836 836 return
837 837
838 838 return log(*msg)
839 839
840 840
841 841 # Bytes that break XML even in a CDATA block: control characters 0-31
842 842 # sans \t, \n and \r
843 843 CDATA_EVIL = re.compile(br"[\000-\010\013\014\016-\037]")
844 844
845 845 # Match feature conditionalized output lines in the form, capturing the feature
846 846 # list in group 2, and the preceeding line output in group 1:
847 847 #
848 848 # output..output (feature !)\n
849 849 optline = re.compile(br'(.*) \((.+?) !\)\n$')
850 850
851 851
852 852 def cdatasafe(data):
853 853 """Make a string safe to include in a CDATA block.
854 854
855 855 Certain control characters are illegal in a CDATA block, and
856 856 there's no way to include a ]]> in a CDATA either. This function
857 857 replaces illegal bytes with ? and adds a space between the ]] so
858 858 that it won't break the CDATA block.
859 859 """
860 860 return CDATA_EVIL.sub(b'?', data).replace(b']]>', b'] ]>')
861 861
862 862
863 863 def log(*msg):
864 864 """Log something to stdout.
865 865
866 866 Arguments are strings to print.
867 867 """
868 868 with iolock:
869 869 if verbose:
870 870 print(verbose, end=' ')
871 871 for m in msg:
872 872 print(m, end=' ')
873 873 print()
874 874 sys.stdout.flush()
875 875
876 876
877 877 def highlightdiff(line, color):
878 878 if not color:
879 879 return line
880 880 assert pygmentspresent
881 881 return pygments.highlight(
882 882 line.decode('latin1'), difflexer, terminal256formatter
883 883 ).encode('latin1')
884 884
885 885
886 886 def highlightmsg(msg, color):
887 887 if not color:
888 888 return msg
889 889 assert pygmentspresent
890 890 return pygments.highlight(msg, runnerlexer, runnerformatter)
891 891
892 892
893 893 def terminate(proc):
894 894 """Terminate subprocess"""
895 895 vlog('# Terminating process %d' % proc.pid)
896 896 try:
897 897 proc.terminate()
898 898 except OSError:
899 899 pass
900 900
901 901
902 902 def killdaemons(pidfile):
903 903 import killdaemons as killmod
904 904
905 905 return killmod.killdaemons(pidfile, tryhard=False, remove=True, logfn=vlog)
906 906
907 907
908 908 class Test(unittest.TestCase):
909 909 """Encapsulates a single, runnable test.
910 910
911 911 While this class conforms to the unittest.TestCase API, it differs in that
912 912 instances need to be instantiated manually. (Typically, unittest.TestCase
913 913 classes are instantiated automatically by scanning modules.)
914 914 """
915 915
916 916 # Status code reserved for skipped tests (used by hghave).
917 917 SKIPPED_STATUS = 80
918 918
919 919 def __init__(
920 920 self,
921 921 path,
922 922 outputdir,
923 923 tmpdir,
924 924 keeptmpdir=False,
925 925 debug=False,
926 926 first=False,
927 927 timeout=None,
928 928 startport=None,
929 929 extraconfigopts=None,
930 930 shell=None,
931 931 hgcommand=None,
932 932 slowtimeout=None,
933 933 usechg=False,
934 934 chgdebug=False,
935 935 useipv6=False,
936 936 ):
937 937 """Create a test from parameters.
938 938
939 939 path is the full path to the file defining the test.
940 940
941 941 tmpdir is the main temporary directory to use for this test.
942 942
943 943 keeptmpdir determines whether to keep the test's temporary directory
944 944 after execution. It defaults to removal (False).
945 945
946 946 debug mode will make the test execute verbosely, with unfiltered
947 947 output.
948 948
949 949 timeout controls the maximum run time of the test. It is ignored when
950 950 debug is True. See slowtimeout for tests with #require slow.
951 951
952 952 slowtimeout overrides timeout if the test has #require slow.
953 953
954 954 startport controls the starting port number to use for this test. Each
955 955 test will reserve 3 port numbers for execution. It is the caller's
956 956 responsibility to allocate a non-overlapping port range to Test
957 957 instances.
958 958
959 959 extraconfigopts is an iterable of extra hgrc config options. Values
960 960 must have the form "key=value" (something understood by hgrc). Values
961 961 of the form "foo.key=value" will result in "[foo] key=value".
962 962
963 963 shell is the shell to execute tests in.
964 964 """
965 965 if timeout is None:
966 966 timeout = defaults['timeout']
967 967 if startport is None:
968 968 startport = defaults['port']
969 969 if slowtimeout is None:
970 970 slowtimeout = defaults['slowtimeout']
971 971 self.path = path
972 972 self.relpath = os.path.relpath(path)
973 973 self.bname = os.path.basename(path)
974 974 self.name = _bytes2sys(self.bname)
975 975 self._testdir = os.path.dirname(path)
976 976 self._outputdir = outputdir
977 977 self._tmpname = os.path.basename(path)
978 978 self.errpath = os.path.join(self._outputdir, b'%s.err' % self.bname)
979 979
980 980 self._threadtmp = tmpdir
981 981 self._keeptmpdir = keeptmpdir
982 982 self._debug = debug
983 983 self._first = first
984 984 self._timeout = timeout
985 985 self._slowtimeout = slowtimeout
986 986 self._startport = startport
987 987 self._extraconfigopts = extraconfigopts or []
988 988 self._shell = _sys2bytes(shell)
989 989 self._hgcommand = hgcommand or b'hg'
990 990 self._usechg = usechg
991 991 self._chgdebug = chgdebug
992 992 self._useipv6 = useipv6
993 993
994 994 self._aborted = False
995 995 self._daemonpids = []
996 996 self._finished = None
997 997 self._ret = None
998 998 self._out = None
999 999 self._skipped = None
1000 1000 self._testtmp = None
1001 1001 self._chgsockdir = None
1002 1002
1003 1003 self._refout = self.readrefout()
1004 1004
1005 1005 def readrefout(self):
1006 1006 """read reference output"""
1007 1007 # If we're not in --debug mode and reference output file exists,
1008 1008 # check test output against it.
1009 1009 if self._debug:
1010 1010 return None # to match "out is None"
1011 1011 elif os.path.exists(self.refpath):
1012 1012 with open(self.refpath, 'rb') as f:
1013 1013 return f.read().splitlines(True)
1014 1014 else:
1015 1015 return []
1016 1016
1017 1017 # needed to get base class __repr__ running
1018 1018 @property
1019 1019 def _testMethodName(self):
1020 1020 return self.name
1021 1021
1022 1022 def __str__(self):
1023 1023 return self.name
1024 1024
1025 1025 def shortDescription(self):
1026 1026 return self.name
1027 1027
1028 1028 def setUp(self):
1029 1029 """Tasks to perform before run()."""
1030 1030 self._finished = False
1031 1031 self._ret = None
1032 1032 self._out = None
1033 1033 self._skipped = None
1034 1034
1035 1035 try:
1036 1036 os.mkdir(self._threadtmp)
1037 1037 except OSError as e:
1038 1038 if e.errno != errno.EEXIST:
1039 1039 raise
1040 1040
1041 1041 name = self._tmpname
1042 1042 self._testtmp = os.path.join(self._threadtmp, name)
1043 1043 os.mkdir(self._testtmp)
1044 1044
1045 1045 # Remove any previous output files.
1046 1046 if os.path.exists(self.errpath):
1047 1047 try:
1048 1048 os.remove(self.errpath)
1049 1049 except OSError as e:
1050 1050 # We might have raced another test to clean up a .err
1051 1051 # file, so ignore ENOENT when removing a previous .err
1052 1052 # file.
1053 1053 if e.errno != errno.ENOENT:
1054 1054 raise
1055 1055
1056 1056 if self._usechg:
1057 1057 self._chgsockdir = os.path.join(
1058 1058 self._threadtmp, b'%s.chgsock' % name
1059 1059 )
1060 1060 os.mkdir(self._chgsockdir)
1061 1061
1062 1062 def run(self, result):
1063 1063 """Run this test and report results against a TestResult instance."""
1064 1064 # This function is extremely similar to unittest.TestCase.run(). Once
1065 1065 # we require Python 2.7 (or at least its version of unittest), this
1066 1066 # function can largely go away.
1067 1067 self._result = result
1068 1068 result.startTest(self)
1069 1069 try:
1070 1070 try:
1071 1071 self.setUp()
1072 1072 except (KeyboardInterrupt, SystemExit):
1073 1073 self._aborted = True
1074 1074 raise
1075 1075 except Exception:
1076 1076 result.addError(self, sys.exc_info())
1077 1077 return
1078 1078
1079 1079 success = False
1080 1080 try:
1081 1081 self.runTest()
1082 1082 except KeyboardInterrupt:
1083 1083 self._aborted = True
1084 1084 raise
1085 1085 except unittest.SkipTest as e:
1086 1086 result.addSkip(self, str(e))
1087 1087 # The base class will have already counted this as a
1088 1088 # test we "ran", but we want to exclude skipped tests
1089 1089 # from those we count towards those run.
1090 1090 result.testsRun -= 1
1091 1091 except self.failureException as e:
1092 1092 # This differs from unittest in that we don't capture
1093 1093 # the stack trace. This is for historical reasons and
1094 1094 # this decision could be revisited in the future,
1095 1095 # especially for PythonTest instances.
1096 1096 if result.addFailure(self, str(e)):
1097 1097 success = True
1098 1098 except Exception:
1099 1099 result.addError(self, sys.exc_info())
1100 1100 else:
1101 1101 success = True
1102 1102
1103 1103 try:
1104 1104 self.tearDown()
1105 1105 except (KeyboardInterrupt, SystemExit):
1106 1106 self._aborted = True
1107 1107 raise
1108 1108 except Exception:
1109 1109 result.addError(self, sys.exc_info())
1110 1110 success = False
1111 1111
1112 1112 if success:
1113 1113 result.addSuccess(self)
1114 1114 finally:
1115 1115 result.stopTest(self, interrupted=self._aborted)
1116 1116
1117 1117 def runTest(self):
1118 1118 """Run this test instance.
1119 1119
1120 1120 This will return a tuple describing the result of the test.
1121 1121 """
1122 1122 env = self._getenv()
1123 1123 self._genrestoreenv(env)
1124 1124 self._daemonpids.append(env['DAEMON_PIDS'])
1125 1125 self._createhgrc(env['HGRCPATH'])
1126 1126
1127 1127 vlog('# Test', self.name)
1128 1128
1129 1129 ret, out = self._run(env)
1130 1130 self._finished = True
1131 1131 self._ret = ret
1132 1132 self._out = out
1133 1133
1134 1134 def describe(ret):
1135 1135 if ret < 0:
1136 1136 return 'killed by signal: %d' % -ret
1137 1137 return 'returned error code %d' % ret
1138 1138
1139 1139 self._skipped = False
1140 1140
1141 1141 if ret == self.SKIPPED_STATUS:
1142 1142 if out is None: # Debug mode, nothing to parse.
1143 1143 missing = ['unknown']
1144 1144 failed = None
1145 1145 else:
1146 1146 missing, failed = TTest.parsehghaveoutput(out)
1147 1147
1148 1148 if not missing:
1149 1149 missing = ['skipped']
1150 1150
1151 1151 if failed:
1152 1152 self.fail('hg have failed checking for %s' % failed[-1])
1153 1153 else:
1154 1154 self._skipped = True
1155 1155 raise unittest.SkipTest(missing[-1])
1156 1156 elif ret == 'timeout':
1157 1157 self.fail('timed out')
1158 1158 elif ret is False:
1159 1159 self.fail('no result code from test')
1160 1160 elif out != self._refout:
1161 1161 # Diff generation may rely on written .err file.
1162 1162 if (
1163 1163 (ret != 0 or out != self._refout)
1164 1164 and not self._skipped
1165 1165 and not self._debug
1166 1166 ):
1167 1167 with open(self.errpath, 'wb') as f:
1168 1168 for line in out:
1169 1169 f.write(line)
1170 1170
1171 1171 # The result object handles diff calculation for us.
1172 1172 with firstlock:
1173 1173 if self._result.addOutputMismatch(self, ret, out, self._refout):
1174 1174 # change was accepted, skip failing
1175 1175 return
1176 1176 if self._first:
1177 1177 global firsterror
1178 1178 firsterror = True
1179 1179
1180 1180 if ret:
1181 1181 msg = 'output changed and ' + describe(ret)
1182 1182 else:
1183 1183 msg = 'output changed'
1184 1184
1185 1185 self.fail(msg)
1186 1186 elif ret:
1187 1187 self.fail(describe(ret))
1188 1188
1189 1189 def tearDown(self):
1190 1190 """Tasks to perform after run()."""
1191 1191 for entry in self._daemonpids:
1192 1192 killdaemons(entry)
1193 1193 self._daemonpids = []
1194 1194
1195 1195 if self._keeptmpdir:
1196 1196 log(
1197 1197 '\nKeeping testtmp dir: %s\nKeeping threadtmp dir: %s'
1198 1198 % (
1199 1199 _bytes2sys(self._testtmp),
1200 1200 _bytes2sys(self._threadtmp),
1201 1201 )
1202 1202 )
1203 1203 else:
1204 1204 try:
1205 1205 shutil.rmtree(self._testtmp)
1206 1206 except OSError:
1207 1207 # unreadable directory may be left in $TESTTMP; fix permission
1208 1208 # and try again
1209 1209 makecleanable(self._testtmp)
1210 1210 shutil.rmtree(self._testtmp, True)
1211 1211 shutil.rmtree(self._threadtmp, True)
1212 1212
1213 1213 if self._usechg:
1214 1214 # chgservers will stop automatically after they find the socket
1215 1215 # files are deleted
1216 1216 shutil.rmtree(self._chgsockdir, True)
1217 1217
1218 1218 if (
1219 1219 (self._ret != 0 or self._out != self._refout)
1220 1220 and not self._skipped
1221 1221 and not self._debug
1222 1222 and self._out
1223 1223 ):
1224 1224 with open(self.errpath, 'wb') as f:
1225 1225 for line in self._out:
1226 1226 f.write(line)
1227 1227
1228 1228 vlog("# Ret was:", self._ret, '(%s)' % self.name)
1229 1229
1230 1230 def _run(self, env):
1231 1231 # This should be implemented in child classes to run tests.
1232 1232 raise unittest.SkipTest('unknown test type')
1233 1233
1234 1234 def abort(self):
1235 1235 """Terminate execution of this test."""
1236 1236 self._aborted = True
1237 1237
1238 1238 def _portmap(self, i):
1239 1239 offset = b'' if i == 0 else b'%d' % i
1240 1240 return (br':%d\b' % (self._startport + i), b':$HGPORT%s' % offset)
1241 1241
1242 1242 def _getreplacements(self):
1243 1243 """Obtain a mapping of text replacements to apply to test output.
1244 1244
1245 1245 Test output needs to be normalized so it can be compared to expected
1246 1246 output. This function defines how some of that normalization will
1247 1247 occur.
1248 1248 """
1249 1249 r = [
1250 1250 # This list should be parallel to defineport in _getenv
1251 1251 self._portmap(0),
1252 1252 self._portmap(1),
1253 1253 self._portmap(2),
1254 1254 (br'([^0-9])%s' % re.escape(self._localip()), br'\1$LOCALIP'),
1255 1255 (br'\bHG_TXNID=TXN:[a-f0-9]{40}\b', br'HG_TXNID=TXN:$ID$'),
1256 1256 ]
1257 1257 r.append((self._escapepath(self._testtmp), b'$TESTTMP'))
1258 1258
1259 1259 replacementfile = os.path.join(self._testdir, b'common-pattern.py')
1260 1260
1261 1261 if os.path.exists(replacementfile):
1262 1262 data = {}
1263 1263 with open(replacementfile, mode='rb') as source:
1264 1264 # the intermediate 'compile' step help with debugging
1265 1265 code = compile(source.read(), replacementfile, 'exec')
1266 1266 exec(code, data)
1267 1267 for value in data.get('substitutions', ()):
1268 1268 if len(value) != 2:
1269 1269 msg = 'malformatted substitution in %s: %r'
1270 1270 msg %= (replacementfile, value)
1271 1271 raise ValueError(msg)
1272 1272 r.append(value)
1273 1273 return r
1274 1274
1275 1275 def _escapepath(self, p):
1276 1276 if os.name == 'nt':
1277 1277 return b''.join(
1278 1278 c.isalpha()
1279 1279 and b'[%s%s]' % (c.lower(), c.upper())
1280 1280 or c in b'/\\'
1281 1281 and br'[/\\]'
1282 1282 or c.isdigit()
1283 1283 and c
1284 1284 or b'\\' + c
1285 1285 for c in [p[i : i + 1] for i in range(len(p))]
1286 1286 )
1287 1287 else:
1288 1288 return re.escape(p)
1289 1289
1290 1290 def _localip(self):
1291 1291 if self._useipv6:
1292 1292 return b'::1'
1293 1293 else:
1294 1294 return b'127.0.0.1'
1295 1295
1296 1296 def _genrestoreenv(self, testenv):
1297 1297 """Generate a script that can be used by tests to restore the original
1298 1298 environment."""
1299 1299 # Put the restoreenv script inside self._threadtmp
1300 1300 scriptpath = os.path.join(self._threadtmp, b'restoreenv.sh')
1301 1301 testenv['HGTEST_RESTOREENV'] = _bytes2sys(scriptpath)
1302 1302
1303 1303 # Only restore environment variable names that the shell allows
1304 1304 # us to export.
1305 1305 name_regex = re.compile('^[a-zA-Z][a-zA-Z0-9_]*$')
1306 1306
1307 1307 # Do not restore these variables; otherwise tests would fail.
1308 1308 reqnames = {'PYTHON', 'TESTDIR', 'TESTTMP'}
1309 1309
1310 1310 with open(scriptpath, 'w') as envf:
1311 1311 for name, value in origenviron.items():
1312 1312 if not name_regex.match(name):
1313 1313 # Skip environment variables with unusual names not
1314 1314 # allowed by most shells.
1315 1315 continue
1316 1316 if name in reqnames:
1317 1317 continue
1318 1318 envf.write('%s=%s\n' % (name, shellquote(value)))
1319 1319
1320 1320 for name in testenv:
1321 1321 if name in origenviron or name in reqnames:
1322 1322 continue
1323 1323 envf.write('unset %s\n' % (name,))
1324 1324
1325 1325 def _getenv(self):
1326 1326 """Obtain environment variables to use during test execution."""
1327 1327
1328 1328 def defineport(i):
1329 1329 offset = '' if i == 0 else '%s' % i
1330 1330 env["HGPORT%s" % offset] = '%s' % (self._startport + i)
1331 1331
1332 1332 env = os.environ.copy()
1333 1333 env['PYTHONUSERBASE'] = sysconfig.get_config_var('userbase') or ''
1334 1334 env['HGEMITWARNINGS'] = '1'
1335 1335 env['TESTTMP'] = _bytes2sys(self._testtmp)
1336 1336 env['TESTNAME'] = self.name
1337 1337 env['HOME'] = _bytes2sys(self._testtmp)
1338 1338 formated_timeout = _bytes2sys(b"%d" % default_defaults['timeout'][1])
1339 1339 env['HGTEST_TIMEOUT_DEFAULT'] = formated_timeout
1340 1340 env['HGTEST_TIMEOUT'] = _bytes2sys(b"%d" % self._timeout)
1341 1341 # This number should match portneeded in _getport
1342 1342 for port in xrange(3):
1343 1343 # This list should be parallel to _portmap in _getreplacements
1344 1344 defineport(port)
1345 1345 env["HGRCPATH"] = _bytes2sys(os.path.join(self._threadtmp, b'.hgrc'))
1346 1346 env["DAEMON_PIDS"] = _bytes2sys(
1347 1347 os.path.join(self._threadtmp, b'daemon.pids')
1348 1348 )
1349 1349 env["HGEDITOR"] = (
1350 1350 '"' + sysexecutable + '"' + ' -c "import sys; sys.exit(0)"'
1351 1351 )
1352 1352 env["HGUSER"] = "test"
1353 1353 env["HGENCODING"] = "ascii"
1354 1354 env["HGENCODINGMODE"] = "strict"
1355 1355 env["HGHOSTNAME"] = "test-hostname"
1356 1356 env['HGIPV6'] = str(int(self._useipv6))
1357 1357 # See contrib/catapipe.py for how to use this functionality.
1358 1358 if 'HGTESTCATAPULTSERVERPIPE' not in env:
1359 1359 # If we don't have HGTESTCATAPULTSERVERPIPE explicitly set, pull the
1360 1360 # non-test one in as a default, otherwise set to devnull
1361 1361 env['HGTESTCATAPULTSERVERPIPE'] = env.get(
1362 1362 'HGCATAPULTSERVERPIPE', os.devnull
1363 1363 )
1364 1364
1365 1365 extraextensions = []
1366 1366 for opt in self._extraconfigopts:
1367 1367 section, key = _sys2bytes(opt).split(b'.', 1)
1368 1368 if section != 'extensions':
1369 1369 continue
1370 1370 name = key.split(b'=', 1)[0]
1371 1371 extraextensions.append(name)
1372 1372
1373 1373 if extraextensions:
1374 1374 env['HGTESTEXTRAEXTENSIONS'] = b' '.join(extraextensions)
1375 1375
1376 1376 # LOCALIP could be ::1 or 127.0.0.1. Useful for tests that require raw
1377 1377 # IP addresses.
1378 1378 env['LOCALIP'] = _bytes2sys(self._localip())
1379 1379
1380 1380 # This has the same effect as Py_LegacyWindowsStdioFlag in exewrapper.c,
1381 1381 # but this is needed for testing python instances like dummyssh,
1382 1382 # dummysmtpd.py, and dumbhttp.py.
1383 1383 if PYTHON3 and os.name == 'nt':
1384 1384 env['PYTHONLEGACYWINDOWSSTDIO'] = '1'
1385 1385
1386 1386 # Modified HOME in test environment can confuse Rust tools. So set
1387 1387 # CARGO_HOME and RUSTUP_HOME automatically if a Rust toolchain is
1388 1388 # present and these variables aren't already defined.
1389 1389 cargo_home_path = os.path.expanduser('~/.cargo')
1390 1390 rustup_home_path = os.path.expanduser('~/.rustup')
1391 1391
1392 1392 if os.path.exists(cargo_home_path) and b'CARGO_HOME' not in osenvironb:
1393 1393 env['CARGO_HOME'] = cargo_home_path
1394 1394 if (
1395 1395 os.path.exists(rustup_home_path)
1396 1396 and b'RUSTUP_HOME' not in osenvironb
1397 1397 ):
1398 1398 env['RUSTUP_HOME'] = rustup_home_path
1399 1399
1400 1400 # Reset some environment variables to well-known values so that
1401 1401 # the tests produce repeatable output.
1402 1402 env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C'
1403 1403 env['TZ'] = 'GMT'
1404 1404 env["EMAIL"] = "Foo Bar <foo.bar@example.com>"
1405 1405 env['COLUMNS'] = '80'
1406 1406 env['TERM'] = 'xterm'
1407 1407
1408 1408 dropped = [
1409 1409 'CDPATH',
1410 1410 'CHGDEBUG',
1411 1411 'EDITOR',
1412 1412 'GREP_OPTIONS',
1413 1413 'HG',
1414 1414 'HGMERGE',
1415 1415 'HGPLAIN',
1416 1416 'HGPLAINEXCEPT',
1417 1417 'HGPROF',
1418 1418 'http_proxy',
1419 1419 'no_proxy',
1420 1420 'NO_PROXY',
1421 1421 'PAGER',
1422 1422 'VISUAL',
1423 1423 ]
1424 1424
1425 1425 for k in dropped:
1426 1426 if k in env:
1427 1427 del env[k]
1428 1428
1429 1429 # unset env related to hooks
1430 1430 for k in list(env):
1431 1431 if k.startswith('HG_'):
1432 1432 del env[k]
1433 1433
1434 1434 if self._usechg:
1435 1435 env['CHGSOCKNAME'] = os.path.join(self._chgsockdir, b'server')
1436 1436 if self._chgdebug:
1437 1437 env['CHGDEBUG'] = 'true'
1438 1438
1439 1439 return env
1440 1440
1441 1441 def _createhgrc(self, path):
1442 1442 """Create an hgrc file for this test."""
1443 1443 with open(path, 'wb') as hgrc:
1444 1444 hgrc.write(b'[ui]\n')
1445 1445 hgrc.write(b'slash = True\n')
1446 1446 hgrc.write(b'interactive = False\n')
1447 1447 hgrc.write(b'detailed-exit-code = True\n')
1448 1448 hgrc.write(b'merge = internal:merge\n')
1449 1449 hgrc.write(b'mergemarkers = detailed\n')
1450 1450 hgrc.write(b'promptecho = True\n')
1451 1451 hgrc.write(b'[defaults]\n')
1452 1452 hgrc.write(b'[devel]\n')
1453 1453 hgrc.write(b'all-warnings = true\n')
1454 1454 hgrc.write(b'default-date = 0 0\n')
1455 1455 hgrc.write(b'[largefiles]\n')
1456 1456 hgrc.write(
1457 1457 b'usercache = %s\n'
1458 1458 % (os.path.join(self._testtmp, b'.cache/largefiles'))
1459 1459 )
1460 1460 hgrc.write(b'[lfs]\n')
1461 1461 hgrc.write(
1462 1462 b'usercache = %s\n'
1463 1463 % (os.path.join(self._testtmp, b'.cache/lfs'))
1464 1464 )
1465 1465 hgrc.write(b'[web]\n')
1466 1466 hgrc.write(b'address = localhost\n')
1467 1467 hgrc.write(b'ipv6 = %r\n' % self._useipv6)
1468 1468 hgrc.write(b'server-header = testing stub value\n')
1469 1469
1470 1470 for opt in self._extraconfigopts:
1471 1471 section, key = _sys2bytes(opt).split(b'.', 1)
1472 1472 assert b'=' in key, (
1473 1473 'extra config opt %s must ' 'have an = for assignment' % opt
1474 1474 )
1475 1475 hgrc.write(b'[%s]\n%s\n' % (section, key))
1476 1476
1477 1477 def fail(self, msg):
1478 1478 # unittest differentiates between errored and failed.
1479 1479 # Failed is denoted by AssertionError (by default at least).
1480 1480 raise AssertionError(msg)
1481 1481
1482 1482 def _runcommand(self, cmd, env, normalizenewlines=False):
1483 1483 """Run command in a sub-process, capturing the output (stdout and
1484 1484 stderr).
1485 1485
1486 1486 Return a tuple (exitcode, output). output is None in debug mode.
1487 1487 """
1488 1488 if self._debug:
1489 1489 proc = subprocess.Popen(
1490 1490 _bytes2sys(cmd),
1491 1491 shell=True,
1492 1492 cwd=_bytes2sys(self._testtmp),
1493 1493 env=env,
1494 1494 )
1495 1495 ret = proc.wait()
1496 1496 return (ret, None)
1497 1497
1498 1498 proc = Popen4(cmd, self._testtmp, self._timeout, env)
1499 1499
1500 1500 def cleanup():
1501 1501 terminate(proc)
1502 1502 ret = proc.wait()
1503 1503 if ret == 0:
1504 1504 ret = signal.SIGTERM << 8
1505 1505 killdaemons(env['DAEMON_PIDS'])
1506 1506 return ret
1507 1507
1508 1508 proc.tochild.close()
1509 1509
1510 1510 try:
1511 1511 output = proc.fromchild.read()
1512 1512 except KeyboardInterrupt:
1513 1513 vlog('# Handling keyboard interrupt')
1514 1514 cleanup()
1515 1515 raise
1516 1516
1517 1517 ret = proc.wait()
1518 1518 if wifexited(ret):
1519 1519 ret = os.WEXITSTATUS(ret)
1520 1520
1521 1521 if proc.timeout:
1522 1522 ret = 'timeout'
1523 1523
1524 1524 if ret:
1525 1525 killdaemons(env['DAEMON_PIDS'])
1526 1526
1527 1527 for s, r in self._getreplacements():
1528 1528 output = re.sub(s, r, output)
1529 1529
1530 1530 if normalizenewlines:
1531 1531 output = output.replace(b'\r\n', b'\n')
1532 1532
1533 1533 return ret, output.splitlines(True)
1534 1534
1535 1535
1536 1536 class PythonTest(Test):
1537 1537 """A Python-based test."""
1538 1538
1539 1539 @property
1540 1540 def refpath(self):
1541 1541 return os.path.join(self._testdir, b'%s.out' % self.bname)
1542 1542
1543 1543 def _run(self, env):
1544 1544 # Quote the python(3) executable for Windows
1545 1545 cmd = b'"%s" "%s"' % (PYTHON, self.path)
1546 1546 vlog("# Running", cmd.decode("utf-8"))
1547 1547 normalizenewlines = os.name == 'nt'
1548 1548 result = self._runcommand(cmd, env, normalizenewlines=normalizenewlines)
1549 1549 if self._aborted:
1550 1550 raise KeyboardInterrupt()
1551 1551
1552 1552 return result
1553 1553
1554 1554
1555 1555 # Some glob patterns apply only in some circumstances, so the script
1556 1556 # might want to remove (glob) annotations that otherwise should be
1557 1557 # retained.
1558 1558 checkcodeglobpats = [
1559 1559 # On Windows it looks like \ doesn't require a (glob), but we know
1560 1560 # better.
1561 1561 re.compile(br'^pushing to \$TESTTMP/.*[^)]$'),
1562 1562 re.compile(br'^moving \S+/.*[^)]$'),
1563 1563 re.compile(br'^pulling from \$TESTTMP/.*[^)]$'),
1564 1564 # Not all platforms have 127.0.0.1 as loopback (though most do),
1565 1565 # so we always glob that too.
1566 1566 re.compile(br'.*\$LOCALIP.*$'),
1567 1567 ]
1568 1568
1569 1569 bchr = chr
1570 1570 if PYTHON3:
1571 1571 bchr = lambda x: bytes([x])
1572 1572
1573 1573 WARN_UNDEFINED = 1
1574 1574 WARN_YES = 2
1575 1575 WARN_NO = 3
1576 1576
1577 1577 MARK_OPTIONAL = b" (?)\n"
1578 1578
1579 1579
1580 1580 def isoptional(line):
1581 1581 return line.endswith(MARK_OPTIONAL)
1582 1582
1583 1583
1584 1584 class TTest(Test):
1585 1585 """A "t test" is a test backed by a .t file."""
1586 1586
1587 1587 SKIPPED_PREFIX = b'skipped: '
1588 1588 FAILED_PREFIX = b'hghave check failed: '
1589 1589 NEEDESCAPE = re.compile(br'[\x00-\x08\x0b-\x1f\x7f-\xff]').search
1590 1590
1591 1591 ESCAPESUB = re.compile(br'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub
1592 1592 ESCAPEMAP = {bchr(i): br'\x%02x' % i for i in range(256)}
1593 1593 ESCAPEMAP.update({b'\\': b'\\\\', b'\r': br'\r'})
1594 1594
1595 1595 def __init__(self, path, *args, **kwds):
1596 1596 # accept an extra "case" parameter
1597 1597 case = kwds.pop('case', [])
1598 1598 self._case = case
1599 1599 self._allcases = {x for y in parsettestcases(path) for x in y}
1600 1600 super(TTest, self).__init__(path, *args, **kwds)
1601 1601 if case:
1602 1602 casepath = b'#'.join(case)
1603 1603 self.name = '%s#%s' % (self.name, _bytes2sys(casepath))
1604 1604 self.errpath = b'%s#%s.err' % (self.errpath[:-4], casepath)
1605 1605 self._tmpname += b'-%s' % casepath.replace(b'#', b'-')
1606 1606 self._have = {}
1607 1607
1608 1608 @property
1609 1609 def refpath(self):
1610 1610 return os.path.join(self._testdir, self.bname)
1611 1611
1612 1612 def _run(self, env):
1613 1613 with open(self.path, 'rb') as f:
1614 1614 lines = f.readlines()
1615 1615
1616 1616 # .t file is both reference output and the test input, keep reference
1617 1617 # output updated with the the test input. This avoids some race
1618 1618 # conditions where the reference output does not match the actual test.
1619 1619 if self._refout is not None:
1620 1620 self._refout = lines
1621 1621
1622 1622 salt, script, after, expected = self._parsetest(lines)
1623 1623
1624 1624 # Write out the generated script.
1625 1625 fname = b'%s.sh' % self._testtmp
1626 1626 with open(fname, 'wb') as f:
1627 1627 for l in script:
1628 1628 f.write(l)
1629 1629
1630 1630 cmd = b'%s "%s"' % (self._shell, fname)
1631 1631 vlog("# Running", cmd.decode("utf-8"))
1632 1632
1633 1633 exitcode, output = self._runcommand(cmd, env)
1634 1634
1635 1635 if self._aborted:
1636 1636 raise KeyboardInterrupt()
1637 1637
1638 1638 # Do not merge output if skipped. Return hghave message instead.
1639 1639 # Similarly, with --debug, output is None.
1640 1640 if exitcode == self.SKIPPED_STATUS or output is None:
1641 1641 return exitcode, output
1642 1642
1643 1643 return self._processoutput(exitcode, output, salt, after, expected)
1644 1644
1645 1645 def _hghave(self, reqs):
1646 1646 allreqs = b' '.join(reqs)
1647 1647
1648 1648 self._detectslow(reqs)
1649 1649
1650 1650 if allreqs in self._have:
1651 1651 return self._have.get(allreqs)
1652 1652
1653 1653 # TODO do something smarter when all other uses of hghave are gone.
1654 1654 runtestdir = osenvironb[b'RUNTESTDIR']
1655 1655 tdir = runtestdir.replace(b'\\', b'/')
1656 1656 proc = Popen4(
1657 1657 b'%s -c "%s/hghave %s"' % (self._shell, tdir, allreqs),
1658 1658 self._testtmp,
1659 1659 0,
1660 1660 self._getenv(),
1661 1661 )
1662 1662 stdout, stderr = proc.communicate()
1663 1663 ret = proc.wait()
1664 1664 if wifexited(ret):
1665 1665 ret = os.WEXITSTATUS(ret)
1666 1666 if ret == 2:
1667 1667 print(stdout.decode('utf-8'))
1668 1668 sys.exit(1)
1669 1669
1670 1670 if ret != 0:
1671 1671 self._have[allreqs] = (False, stdout)
1672 1672 return False, stdout
1673 1673
1674 1674 self._have[allreqs] = (True, None)
1675 1675 return True, None
1676 1676
1677 1677 def _detectslow(self, reqs):
1678 1678 """update the timeout of slow test when appropriate"""
1679 1679 if b'slow' in reqs:
1680 1680 self._timeout = self._slowtimeout
1681 1681
1682 1682 def _iftest(self, args):
1683 1683 # implements "#if"
1684 1684 reqs = []
1685 1685 for arg in args:
1686 1686 if arg.startswith(b'no-') and arg[3:] in self._allcases:
1687 1687 if arg[3:] in self._case:
1688 1688 return False
1689 1689 elif arg in self._allcases:
1690 1690 if arg not in self._case:
1691 1691 return False
1692 1692 else:
1693 1693 reqs.append(arg)
1694 1694 self._detectslow(reqs)
1695 1695 return self._hghave(reqs)[0]
1696 1696
1697 1697 def _parsetest(self, lines):
1698 1698 # We generate a shell script which outputs unique markers to line
1699 1699 # up script results with our source. These markers include input
1700 1700 # line number and the last return code.
1701 1701 salt = b"SALT%d" % time.time()
1702 1702
1703 1703 def addsalt(line, inpython):
1704 1704 if inpython:
1705 1705 script.append(b'%s %d 0\n' % (salt, line))
1706 1706 else:
1707 1707 script.append(b'echo %s %d $?\n' % (salt, line))
1708 1708
1709 1709 activetrace = []
1710 1710 session = str(uuid.uuid4())
1711 1711 if PYTHON3:
1712 1712 session = session.encode('ascii')
1713 1713 hgcatapult = os.getenv('HGTESTCATAPULTSERVERPIPE') or os.getenv(
1714 1714 'HGCATAPULTSERVERPIPE'
1715 1715 )
1716 1716
1717 1717 def toggletrace(cmd=None):
1718 1718 if not hgcatapult or hgcatapult == os.devnull:
1719 1719 return
1720 1720
1721 1721 if activetrace:
1722 1722 script.append(
1723 1723 b'echo END %s %s >> "$HGTESTCATAPULTSERVERPIPE"\n'
1724 1724 % (session, activetrace[0])
1725 1725 )
1726 1726 if cmd is None:
1727 1727 return
1728 1728
1729 1729 if isinstance(cmd, str):
1730 1730 quoted = shellquote(cmd.strip())
1731 1731 else:
1732 1732 quoted = shellquote(cmd.strip().decode('utf8')).encode('utf8')
1733 1733 quoted = quoted.replace(b'\\', b'\\\\')
1734 1734 script.append(
1735 1735 b'echo START %s %s >> "$HGTESTCATAPULTSERVERPIPE"\n'
1736 1736 % (session, quoted)
1737 1737 )
1738 1738 activetrace[0:] = [quoted]
1739 1739
1740 1740 script = []
1741 1741
1742 1742 # After we run the shell script, we re-unify the script output
1743 1743 # with non-active parts of the source, with synchronization by our
1744 1744 # SALT line number markers. The after table contains the non-active
1745 1745 # components, ordered by line number.
1746 1746 after = {}
1747 1747
1748 1748 # Expected shell script output.
1749 1749 expected = {}
1750 1750
1751 1751 pos = prepos = -1
1752 1752
1753 1753 # True or False when in a true or false conditional section
1754 1754 skipping = None
1755 1755
1756 1756 # We keep track of whether or not we're in a Python block so we
1757 1757 # can generate the surrounding doctest magic.
1758 1758 inpython = False
1759 1759
1760 1760 if self._debug:
1761 1761 script.append(b'set -x\n')
1762 1762 if self._hgcommand != b'hg':
1763 1763 script.append(b'alias hg="%s"\n' % self._hgcommand)
1764 1764 if os.getenv('MSYSTEM'):
1765 1765 script.append(b'alias pwd="pwd -W"\n')
1766 1766
1767 1767 if hgcatapult and hgcatapult != os.devnull:
1768 1768 if PYTHON3:
1769 1769 hgcatapult = hgcatapult.encode('utf8')
1770 1770 cataname = self.name.encode('utf8')
1771 1771 else:
1772 1772 cataname = self.name
1773 1773
1774 1774 # Kludge: use a while loop to keep the pipe from getting
1775 1775 # closed by our echo commands. The still-running file gets
1776 1776 # reaped at the end of the script, which causes the while
1777 1777 # loop to exit and closes the pipe. Sigh.
1778 1778 script.append(
1779 1779 b'rtendtracing() {\n'
1780 1780 b' echo END %(session)s %(name)s >> %(catapult)s\n'
1781 1781 b' rm -f "$TESTTMP/.still-running"\n'
1782 1782 b'}\n'
1783 1783 b'trap "rtendtracing" 0\n'
1784 1784 b'touch "$TESTTMP/.still-running"\n'
1785 1785 b'while [ -f "$TESTTMP/.still-running" ]; do sleep 1; done '
1786 1786 b'> %(catapult)s &\n'
1787 1787 b'HGCATAPULTSESSION=%(session)s ; export HGCATAPULTSESSION\n'
1788 1788 b'echo START %(session)s %(name)s >> %(catapult)s\n'
1789 1789 % {
1790 1790 b'name': cataname,
1791 1791 b'session': session,
1792 1792 b'catapult': hgcatapult,
1793 1793 }
1794 1794 )
1795 1795
1796 1796 if self._case:
1797 1797 casestr = b'#'.join(self._case)
1798 1798 if isinstance(casestr, str):
1799 1799 quoted = shellquote(casestr)
1800 1800 else:
1801 1801 quoted = shellquote(casestr.decode('utf8')).encode('utf8')
1802 1802 script.append(b'TESTCASE=%s\n' % quoted)
1803 1803 script.append(b'export TESTCASE\n')
1804 1804
1805 1805 n = 0
1806 1806 for n, l in enumerate(lines):
1807 1807 if not l.endswith(b'\n'):
1808 1808 l += b'\n'
1809 1809 if l.startswith(b'#require'):
1810 1810 lsplit = l.split()
1811 1811 if len(lsplit) < 2 or lsplit[0] != b'#require':
1812 1812 after.setdefault(pos, []).append(
1813 1813 b' !!! invalid #require\n'
1814 1814 )
1815 1815 if not skipping:
1816 1816 haveresult, message = self._hghave(lsplit[1:])
1817 1817 if not haveresult:
1818 1818 script = [b'echo "%s"\nexit 80\n' % message]
1819 1819 break
1820 1820 after.setdefault(pos, []).append(l)
1821 1821 elif l.startswith(b'#if'):
1822 1822 lsplit = l.split()
1823 1823 if len(lsplit) < 2 or lsplit[0] != b'#if':
1824 1824 after.setdefault(pos, []).append(b' !!! invalid #if\n')
1825 1825 if skipping is not None:
1826 1826 after.setdefault(pos, []).append(b' !!! nested #if\n')
1827 1827 skipping = not self._iftest(lsplit[1:])
1828 1828 after.setdefault(pos, []).append(l)
1829 1829 elif l.startswith(b'#else'):
1830 1830 if skipping is None:
1831 1831 after.setdefault(pos, []).append(b' !!! missing #if\n')
1832 1832 skipping = not skipping
1833 1833 after.setdefault(pos, []).append(l)
1834 1834 elif l.startswith(b'#endif'):
1835 1835 if skipping is None:
1836 1836 after.setdefault(pos, []).append(b' !!! missing #if\n')
1837 1837 skipping = None
1838 1838 after.setdefault(pos, []).append(l)
1839 1839 elif skipping:
1840 1840 after.setdefault(pos, []).append(l)
1841 1841 elif l.startswith(b' >>> '): # python inlines
1842 1842 after.setdefault(pos, []).append(l)
1843 1843 prepos = pos
1844 1844 pos = n
1845 1845 if not inpython:
1846 1846 # We've just entered a Python block. Add the header.
1847 1847 inpython = True
1848 1848 addsalt(prepos, False) # Make sure we report the exit code.
1849 1849 script.append(b'"%s" -m heredoctest <<EOF\n' % PYTHON)
1850 1850 addsalt(n, True)
1851 1851 script.append(l[2:])
1852 1852 elif l.startswith(b' ... '): # python inlines
1853 1853 after.setdefault(prepos, []).append(l)
1854 1854 script.append(l[2:])
1855 1855 elif l.startswith(b' $ '): # commands
1856 1856 if inpython:
1857 1857 script.append(b'EOF\n')
1858 1858 inpython = False
1859 1859 after.setdefault(pos, []).append(l)
1860 1860 prepos = pos
1861 1861 pos = n
1862 1862 addsalt(n, False)
1863 1863 rawcmd = l[4:]
1864 1864 cmd = rawcmd.split()
1865 1865 toggletrace(rawcmd)
1866 1866 if len(cmd) == 2 and cmd[0] == b'cd':
1867 1867 rawcmd = b'cd %s || exit 1\n' % cmd[1]
1868 1868 script.append(rawcmd)
1869 1869 elif l.startswith(b' > '): # continuations
1870 1870 after.setdefault(prepos, []).append(l)
1871 1871 script.append(l[4:])
1872 1872 elif l.startswith(b' '): # results
1873 1873 # Queue up a list of expected results.
1874 1874 expected.setdefault(pos, []).append(l[2:])
1875 1875 else:
1876 1876 if inpython:
1877 1877 script.append(b'EOF\n')
1878 1878 inpython = False
1879 1879 # Non-command/result. Queue up for merged output.
1880 1880 after.setdefault(pos, []).append(l)
1881 1881
1882 1882 if inpython:
1883 1883 script.append(b'EOF\n')
1884 1884 if skipping is not None:
1885 1885 after.setdefault(pos, []).append(b' !!! missing #endif\n')
1886 1886 addsalt(n + 1, False)
1887 1887 # Need to end any current per-command trace
1888 1888 if activetrace:
1889 1889 toggletrace()
1890 1890 return salt, script, after, expected
1891 1891
1892 1892 def _processoutput(self, exitcode, output, salt, after, expected):
1893 1893 # Merge the script output back into a unified test.
1894 1894 warnonly = WARN_UNDEFINED # 1: not yet; 2: yes; 3: for sure not
1895 1895 if exitcode != 0:
1896 1896 warnonly = WARN_NO
1897 1897
1898 1898 pos = -1
1899 1899 postout = []
1900 1900 for out_rawline in output:
1901 1901 out_line, cmd_line = out_rawline, None
1902 1902 if salt in out_rawline:
1903 1903 out_line, cmd_line = out_rawline.split(salt, 1)
1904 1904
1905 1905 pos, postout, warnonly = self._process_out_line(
1906 1906 out_line, pos, postout, expected, warnonly
1907 1907 )
1908 1908 pos, postout = self._process_cmd_line(cmd_line, pos, postout, after)
1909 1909
1910 1910 if pos in after:
1911 1911 postout += after.pop(pos)
1912 1912
1913 1913 if warnonly == WARN_YES:
1914 1914 exitcode = False # Set exitcode to warned.
1915 1915
1916 1916 return exitcode, postout
1917 1917
1918 1918 def _process_out_line(self, out_line, pos, postout, expected, warnonly):
1919 1919 while out_line:
1920 1920 if not out_line.endswith(b'\n'):
1921 1921 out_line += b' (no-eol)\n'
1922 1922
1923 1923 # Find the expected output at the current position.
1924 1924 els = [None]
1925 1925 if expected.get(pos, None):
1926 1926 els = expected[pos]
1927 1927
1928 1928 optional = []
1929 1929 for i, el in enumerate(els):
1930 1930 r = False
1931 1931 if el:
1932 1932 r, exact = self.linematch(el, out_line)
1933 1933 if isinstance(r, str):
1934 1934 if r == '-glob':
1935 1935 out_line = ''.join(el.rsplit(' (glob)', 1))
1936 1936 r = '' # Warn only this line.
1937 1937 elif r == "retry":
1938 1938 postout.append(b' ' + el)
1939 1939 else:
1940 1940 log('\ninfo, unknown linematch result: %r\n' % r)
1941 1941 r = False
1942 1942 if r:
1943 1943 els.pop(i)
1944 1944 break
1945 1945 if el:
1946 1946 if isoptional(el):
1947 1947 optional.append(i)
1948 1948 else:
1949 1949 m = optline.match(el)
1950 1950 if m:
1951 1951 conditions = [c for c in m.group(2).split(b' ')]
1952 1952
1953 1953 if not self._iftest(conditions):
1954 1954 optional.append(i)
1955 1955 if exact:
1956 1956 # Don't allow line to be matches against a later
1957 1957 # line in the output
1958 1958 els.pop(i)
1959 1959 break
1960 1960
1961 1961 if r:
1962 1962 if r == "retry":
1963 1963 continue
1964 1964 # clean up any optional leftovers
1965 1965 for i in optional:
1966 1966 postout.append(b' ' + els[i])
1967 1967 for i in reversed(optional):
1968 1968 del els[i]
1969 1969 postout.append(b' ' + el)
1970 1970 else:
1971 1971 if self.NEEDESCAPE(out_line):
1972 1972 out_line = TTest._stringescape(
1973 1973 b'%s (esc)\n' % out_line.rstrip(b'\n')
1974 1974 )
1975 1975 postout.append(b' ' + out_line) # Let diff deal with it.
1976 1976 if r != '': # If line failed.
1977 1977 warnonly = WARN_NO
1978 1978 elif warnonly == WARN_UNDEFINED:
1979 1979 warnonly = WARN_YES
1980 1980 break
1981 1981 else:
1982 1982 # clean up any optional leftovers
1983 1983 while expected.get(pos, None):
1984 1984 el = expected[pos].pop(0)
1985 1985 if el:
1986 1986 if not isoptional(el):
1987 1987 m = optline.match(el)
1988 1988 if m:
1989 1989 conditions = [c for c in m.group(2).split(b' ')]
1990 1990
1991 1991 if self._iftest(conditions):
1992 1992 # Don't append as optional line
1993 1993 continue
1994 1994 else:
1995 1995 continue
1996 1996 postout.append(b' ' + el)
1997 1997 return pos, postout, warnonly
1998 1998
1999 1999 def _process_cmd_line(self, cmd_line, pos, postout, after):
2000 2000 """process a "command" part of a line from unified test output"""
2001 2001 if cmd_line:
2002 2002 # Add on last return code.
2003 2003 ret = int(cmd_line.split()[1])
2004 2004 if ret != 0:
2005 2005 postout.append(b' [%d]\n' % ret)
2006 2006 if pos in after:
2007 2007 # Merge in non-active test bits.
2008 2008 postout += after.pop(pos)
2009 2009 pos = int(cmd_line.split()[0])
2010 2010 return pos, postout
2011 2011
2012 2012 @staticmethod
2013 2013 def rematch(el, l):
2014 2014 try:
2015 2015 # parse any flags at the beginning of the regex. Only 'i' is
2016 2016 # supported right now, but this should be easy to extend.
2017 2017 flags, el = re.match(br'^(\(\?i\))?(.*)', el).groups()[0:2]
2018 2018 flags = flags or b''
2019 2019 el = flags + b'(?:' + el + b')'
2020 2020 # use \Z to ensure that the regex matches to the end of the string
2021 2021 if os.name == 'nt':
2022 2022 return re.match(el + br'\r?\n\Z', l)
2023 2023 return re.match(el + br'\n\Z', l)
2024 2024 except re.error:
2025 2025 # el is an invalid regex
2026 2026 return False
2027 2027
2028 2028 @staticmethod
2029 2029 def globmatch(el, l):
2030 2030 # The only supported special characters are * and ? plus / which also
2031 2031 # matches \ on windows. Escaping of these characters is supported.
2032 2032 if el + b'\n' == l:
2033 2033 if os.altsep:
2034 2034 # matching on "/" is not needed for this line
2035 2035 for pat in checkcodeglobpats:
2036 2036 if pat.match(el):
2037 2037 return True
2038 2038 return b'-glob'
2039 2039 return True
2040 2040 el = el.replace(b'$LOCALIP', b'*')
2041 2041 i, n = 0, len(el)
2042 2042 res = b''
2043 2043 while i < n:
2044 2044 c = el[i : i + 1]
2045 2045 i += 1
2046 2046 if c == b'\\' and i < n and el[i : i + 1] in b'*?\\/':
2047 2047 res += el[i - 1 : i + 1]
2048 2048 i += 1
2049 2049 elif c == b'*':
2050 2050 res += b'.*'
2051 2051 elif c == b'?':
2052 2052 res += b'.'
2053 2053 elif c == b'/' and os.altsep:
2054 2054 res += b'[/\\\\]'
2055 2055 else:
2056 2056 res += re.escape(c)
2057 2057 return TTest.rematch(res, l)
2058 2058
2059 2059 def linematch(self, el, l):
2060 2060 if el == l: # perfect match (fast)
2061 2061 return True, True
2062 2062 retry = False
2063 2063 if isoptional(el):
2064 2064 retry = "retry"
2065 2065 el = el[: -len(MARK_OPTIONAL)] + b"\n"
2066 2066 else:
2067 2067 m = optline.match(el)
2068 2068 if m:
2069 2069 conditions = [c for c in m.group(2).split(b' ')]
2070 2070
2071 2071 el = m.group(1) + b"\n"
2072 2072 if not self._iftest(conditions):
2073 2073 # listed feature missing, should not match
2074 2074 return "retry", False
2075 2075
2076 2076 if el.endswith(b" (esc)\n"):
2077 2077 if PYTHON3:
2078 2078 el = el[:-7].decode('unicode_escape') + '\n'
2079 2079 el = el.encode('latin-1')
2080 2080 else:
2081 2081 el = el[:-7].decode('string-escape') + '\n'
2082 2082 if el == l or os.name == 'nt' and el[:-1] + b'\r\n' == l:
2083 2083 return True, True
2084 2084 if el.endswith(b" (re)\n"):
2085 2085 return (TTest.rematch(el[:-6], l) or retry), False
2086 2086 if el.endswith(b" (glob)\n"):
2087 2087 # ignore '(glob)' added to l by 'replacements'
2088 2088 if l.endswith(b" (glob)\n"):
2089 2089 l = l[:-8] + b"\n"
2090 2090 return (TTest.globmatch(el[:-8], l) or retry), False
2091 2091 if os.altsep:
2092 2092 _l = l.replace(b'\\', b'/')
2093 2093 if el == _l or os.name == 'nt' and el[:-1] + b'\r\n' == _l:
2094 2094 return True, True
2095 2095 return retry, True
2096 2096
2097 2097 @staticmethod
2098 2098 def parsehghaveoutput(lines):
2099 2099 """Parse hghave log lines.
2100 2100
2101 2101 Return tuple of lists (missing, failed):
2102 2102 * the missing/unknown features
2103 2103 * the features for which existence check failed"""
2104 2104 missing = []
2105 2105 failed = []
2106 2106 for line in lines:
2107 2107 if line.startswith(TTest.SKIPPED_PREFIX):
2108 2108 line = line.splitlines()[0]
2109 2109 missing.append(_bytes2sys(line[len(TTest.SKIPPED_PREFIX) :]))
2110 2110 elif line.startswith(TTest.FAILED_PREFIX):
2111 2111 line = line.splitlines()[0]
2112 2112 failed.append(_bytes2sys(line[len(TTest.FAILED_PREFIX) :]))
2113 2113
2114 2114 return missing, failed
2115 2115
2116 2116 @staticmethod
2117 2117 def _escapef(m):
2118 2118 return TTest.ESCAPEMAP[m.group(0)]
2119 2119
2120 2120 @staticmethod
2121 2121 def _stringescape(s):
2122 2122 return TTest.ESCAPESUB(TTest._escapef, s)
2123 2123
2124 2124
2125 2125 iolock = threading.RLock()
2126 2126 firstlock = threading.RLock()
2127 2127 firsterror = False
2128 2128
2129 2129
2130 2130 class TestResult(unittest._TextTestResult):
2131 2131 """Holds results when executing via unittest."""
2132 2132
2133 2133 # Don't worry too much about accessing the non-public _TextTestResult.
2134 2134 # It is relatively common in Python testing tools.
2135 2135 def __init__(self, options, *args, **kwargs):
2136 2136 super(TestResult, self).__init__(*args, **kwargs)
2137 2137
2138 2138 self._options = options
2139 2139
2140 2140 # unittest.TestResult didn't have skipped until 2.7. We need to
2141 2141 # polyfill it.
2142 2142 self.skipped = []
2143 2143
2144 2144 # We have a custom "ignored" result that isn't present in any Python
2145 2145 # unittest implementation. It is very similar to skipped. It may make
2146 2146 # sense to map it into skip some day.
2147 2147 self.ignored = []
2148 2148
2149 2149 self.times = []
2150 2150 self._firststarttime = None
2151 2151 # Data stored for the benefit of generating xunit reports.
2152 2152 self.successes = []
2153 2153 self.faildata = {}
2154 2154
2155 2155 if options.color == 'auto':
2156 2156 self.color = pygmentspresent and self.stream.isatty()
2157 2157 elif options.color == 'never':
2158 2158 self.color = False
2159 2159 else: # 'always', for testing purposes
2160 2160 self.color = pygmentspresent
2161 2161
2162 2162 def onStart(self, test):
2163 2163 """Can be overriden by custom TestResult"""
2164 2164
2165 2165 def onEnd(self):
2166 2166 """Can be overriden by custom TestResult"""
2167 2167
2168 2168 def addFailure(self, test, reason):
2169 2169 self.failures.append((test, reason))
2170 2170
2171 2171 if self._options.first:
2172 2172 self.stop()
2173 2173 else:
2174 2174 with iolock:
2175 2175 if reason == "timed out":
2176 2176 self.stream.write('t')
2177 2177 else:
2178 2178 if not self._options.nodiff:
2179 2179 self.stream.write('\n')
2180 2180 # Exclude the '\n' from highlighting to lex correctly
2181 2181 formatted = 'ERROR: %s output changed\n' % test
2182 2182 self.stream.write(highlightmsg(formatted, self.color))
2183 2183 self.stream.write('!')
2184 2184
2185 2185 self.stream.flush()
2186 2186
2187 2187 def addSuccess(self, test):
2188 2188 with iolock:
2189 2189 super(TestResult, self).addSuccess(test)
2190 2190 self.successes.append(test)
2191 2191
2192 2192 def addError(self, test, err):
2193 2193 super(TestResult, self).addError(test, err)
2194 2194 if self._options.first:
2195 2195 self.stop()
2196 2196
2197 2197 # Polyfill.
2198 2198 def addSkip(self, test, reason):
2199 2199 self.skipped.append((test, reason))
2200 2200 with iolock:
2201 2201 if self.showAll:
2202 2202 self.stream.writeln('skipped %s' % reason)
2203 2203 else:
2204 2204 self.stream.write('s')
2205 2205 self.stream.flush()
2206 2206
2207 2207 def addIgnore(self, test, reason):
2208 2208 self.ignored.append((test, reason))
2209 2209 with iolock:
2210 2210 if self.showAll:
2211 2211 self.stream.writeln('ignored %s' % reason)
2212 2212 else:
2213 2213 if reason not in ('not retesting', "doesn't match keyword"):
2214 2214 self.stream.write('i')
2215 2215 else:
2216 2216 self.testsRun += 1
2217 2217 self.stream.flush()
2218 2218
2219 2219 def addOutputMismatch(self, test, ret, got, expected):
2220 2220 """Record a mismatch in test output for a particular test."""
2221 2221 if self.shouldStop or firsterror:
2222 2222 # don't print, some other test case already failed and
2223 2223 # printed, we're just stale and probably failed due to our
2224 2224 # temp dir getting cleaned up.
2225 2225 return
2226 2226
2227 2227 accepted = False
2228 2228 lines = []
2229 2229
2230 2230 with iolock:
2231 2231 if self._options.nodiff:
2232 2232 pass
2233 2233 elif self._options.view:
2234 2234 v = self._options.view
2235 2235 subprocess.call(
2236 2236 r'"%s" "%s" "%s"'
2237 2237 % (v, _bytes2sys(test.refpath), _bytes2sys(test.errpath)),
2238 2238 shell=True,
2239 2239 )
2240 2240 else:
2241 2241 servefail, lines = getdiff(
2242 2242 expected, got, test.refpath, test.errpath
2243 2243 )
2244 2244 self.stream.write('\n')
2245 2245 for line in lines:
2246 2246 line = highlightdiff(line, self.color)
2247 2247 if PYTHON3:
2248 2248 self.stream.flush()
2249 2249 self.stream.buffer.write(line)
2250 2250 self.stream.buffer.flush()
2251 2251 else:
2252 2252 self.stream.write(line)
2253 2253 self.stream.flush()
2254 2254
2255 2255 if servefail:
2256 2256 raise test.failureException(
2257 2257 'server failed to start (HGPORT=%s)' % test._startport
2258 2258 )
2259 2259
2260 2260 # handle interactive prompt without releasing iolock
2261 2261 if self._options.interactive:
2262 2262 if test.readrefout() != expected:
2263 2263 self.stream.write(
2264 2264 'Reference output has changed (run again to prompt '
2265 2265 'changes)'
2266 2266 )
2267 2267 else:
2268 2268 self.stream.write('Accept this change? [y/N] ')
2269 2269 self.stream.flush()
2270 2270 answer = sys.stdin.readline().strip()
2271 2271 if answer.lower() in ('y', 'yes'):
2272 2272 if test.path.endswith(b'.t'):
2273 2273 rename(test.errpath, test.path)
2274 2274 else:
2275 2275 rename(test.errpath, '%s.out' % test.path)
2276 2276 accepted = True
2277 2277 if not accepted:
2278 2278 self.faildata[test.name] = b''.join(lines)
2279 2279
2280 2280 return accepted
2281 2281
2282 2282 def startTest(self, test):
2283 2283 super(TestResult, self).startTest(test)
2284 2284
2285 2285 # os.times module computes the user time and system time spent by
2286 2286 # child's processes along with real elapsed time taken by a process.
2287 2287 # This module has one limitation. It can only work for Linux user
2288 2288 # and not for Windows. Hence why we fall back to another function
2289 2289 # for wall time calculations.
2290 2290 test.started_times = os.times()
2291 2291 # TODO use a monotonic clock once support for Python 2.7 is dropped.
2292 2292 test.started_time = time.time()
2293 2293 if self._firststarttime is None: # thread racy but irrelevant
2294 2294 self._firststarttime = test.started_time
2295 2295
2296 2296 def stopTest(self, test, interrupted=False):
2297 2297 super(TestResult, self).stopTest(test)
2298 2298
2299 2299 test.stopped_times = os.times()
2300 2300 stopped_time = time.time()
2301 2301
2302 2302 starttime = test.started_times
2303 2303 endtime = test.stopped_times
2304 2304 origin = self._firststarttime
2305 2305 self.times.append(
2306 2306 (
2307 2307 test.name,
2308 2308 endtime[2] - starttime[2], # user space CPU time
2309 2309 endtime[3] - starttime[3], # sys space CPU time
2310 2310 stopped_time - test.started_time, # real time
2311 2311 test.started_time - origin, # start date in run context
2312 2312 stopped_time - origin, # end date in run context
2313 2313 )
2314 2314 )
2315 2315
2316 2316 if interrupted:
2317 2317 with iolock:
2318 2318 self.stream.writeln(
2319 2319 'INTERRUPTED: %s (after %d seconds)'
2320 2320 % (test.name, self.times[-1][3])
2321 2321 )
2322 2322
2323 2323
2324 2324 def getTestResult():
2325 2325 """
2326 2326 Returns the relevant test result
2327 2327 """
2328 2328 if "CUSTOM_TEST_RESULT" in os.environ:
2329 2329 testresultmodule = __import__(os.environ["CUSTOM_TEST_RESULT"])
2330 2330 return testresultmodule.TestResult
2331 2331 else:
2332 2332 return TestResult
2333 2333
2334 2334
2335 2335 class TestSuite(unittest.TestSuite):
2336 2336 """Custom unittest TestSuite that knows how to execute Mercurial tests."""
2337 2337
2338 2338 def __init__(
2339 2339 self,
2340 2340 testdir,
2341 2341 jobs=1,
2342 2342 whitelist=None,
2343 2343 blacklist=None,
2344 2344 keywords=None,
2345 2345 loop=False,
2346 2346 runs_per_test=1,
2347 2347 loadtest=None,
2348 2348 showchannels=False,
2349 2349 *args,
2350 2350 **kwargs
2351 2351 ):
2352 2352 """Create a new instance that can run tests with a configuration.
2353 2353
2354 2354 testdir specifies the directory where tests are executed from. This
2355 2355 is typically the ``tests`` directory from Mercurial's source
2356 2356 repository.
2357 2357
2358 2358 jobs specifies the number of jobs to run concurrently. Each test
2359 2359 executes on its own thread. Tests actually spawn new processes, so
2360 2360 state mutation should not be an issue.
2361 2361
2362 2362 If there is only one job, it will use the main thread.
2363 2363
2364 2364 whitelist and blacklist denote tests that have been whitelisted and
2365 2365 blacklisted, respectively. These arguments don't belong in TestSuite.
2366 2366 Instead, whitelist and blacklist should be handled by the thing that
2367 2367 populates the TestSuite with tests. They are present to preserve
2368 2368 backwards compatible behavior which reports skipped tests as part
2369 2369 of the results.
2370 2370
2371 2371 keywords denotes key words that will be used to filter which tests
2372 2372 to execute. This arguably belongs outside of TestSuite.
2373 2373
2374 2374 loop denotes whether to loop over tests forever.
2375 2375 """
2376 2376 super(TestSuite, self).__init__(*args, **kwargs)
2377 2377
2378 2378 self._jobs = jobs
2379 2379 self._whitelist = whitelist
2380 2380 self._blacklist = blacklist
2381 2381 self._keywords = keywords
2382 2382 self._loop = loop
2383 2383 self._runs_per_test = runs_per_test
2384 2384 self._loadtest = loadtest
2385 2385 self._showchannels = showchannels
2386 2386
2387 2387 def run(self, result):
2388 2388 # We have a number of filters that need to be applied. We do this
2389 2389 # here instead of inside Test because it makes the running logic for
2390 2390 # Test simpler.
2391 2391 tests = []
2392 2392 num_tests = [0]
2393 2393 for test in self._tests:
2394 2394
2395 2395 def get():
2396 2396 num_tests[0] += 1
2397 2397 if getattr(test, 'should_reload', False):
2398 2398 return self._loadtest(test, num_tests[0])
2399 2399 return test
2400 2400
2401 2401 if not os.path.exists(test.path):
2402 2402 result.addSkip(test, "Doesn't exist")
2403 2403 continue
2404 2404
2405 2405 is_whitelisted = self._whitelist and (
2406 2406 test.relpath in self._whitelist or test.bname in self._whitelist
2407 2407 )
2408 2408 if not is_whitelisted:
2409 2409 is_blacklisted = self._blacklist and (
2410 2410 test.relpath in self._blacklist
2411 2411 or test.bname in self._blacklist
2412 2412 )
2413 2413 if is_blacklisted:
2414 2414 result.addSkip(test, 'blacklisted')
2415 2415 continue
2416 2416 if self._keywords:
2417 2417 with open(test.path, 'rb') as f:
2418 2418 t = f.read().lower() + test.bname.lower()
2419 2419 ignored = False
2420 2420 for k in self._keywords.lower().split():
2421 2421 if k not in t:
2422 2422 result.addIgnore(test, "doesn't match keyword")
2423 2423 ignored = True
2424 2424 break
2425 2425
2426 2426 if ignored:
2427 2427 continue
2428 2428 for _ in xrange(self._runs_per_test):
2429 2429 tests.append(get())
2430 2430
2431 2431 runtests = list(tests)
2432 2432 done = queue.Queue()
2433 2433 running = 0
2434 2434
2435 2435 channels = [""] * self._jobs
2436 2436
2437 2437 def job(test, result):
2438 2438 for n, v in enumerate(channels):
2439 2439 if not v:
2440 2440 channel = n
2441 2441 break
2442 2442 else:
2443 2443 raise ValueError('Could not find output channel')
2444 2444 channels[channel] = "=" + test.name[5:].split(".")[0]
2445 2445 try:
2446 2446 test(result)
2447 2447 done.put(None)
2448 2448 except KeyboardInterrupt:
2449 2449 pass
2450 2450 except: # re-raises
2451 2451 done.put(('!', test, 'run-test raised an error, see traceback'))
2452 2452 raise
2453 2453 finally:
2454 2454 try:
2455 2455 channels[channel] = ''
2456 2456 except IndexError:
2457 2457 pass
2458 2458
2459 2459 def stat():
2460 2460 count = 0
2461 2461 while channels:
2462 2462 d = '\n%03s ' % count
2463 2463 for n, v in enumerate(channels):
2464 2464 if v:
2465 2465 d += v[0]
2466 2466 channels[n] = v[1:] or '.'
2467 2467 else:
2468 2468 d += ' '
2469 2469 d += ' '
2470 2470 with iolock:
2471 2471 sys.stdout.write(d + ' ')
2472 2472 sys.stdout.flush()
2473 2473 for x in xrange(10):
2474 2474 if channels:
2475 2475 time.sleep(0.1)
2476 2476 count += 1
2477 2477
2478 2478 stoppedearly = False
2479 2479
2480 2480 if self._showchannels:
2481 2481 statthread = threading.Thread(target=stat, name="stat")
2482 2482 statthread.start()
2483 2483
2484 2484 try:
2485 2485 while tests or running:
2486 2486 if not done.empty() or running == self._jobs or not tests:
2487 2487 try:
2488 2488 done.get(True, 1)
2489 2489 running -= 1
2490 2490 if result and result.shouldStop:
2491 2491 stoppedearly = True
2492 2492 break
2493 2493 except queue.Empty:
2494 2494 continue
2495 2495 if tests and not running == self._jobs:
2496 2496 test = tests.pop(0)
2497 2497 if self._loop:
2498 2498 if getattr(test, 'should_reload', False):
2499 2499 num_tests[0] += 1
2500 2500 tests.append(self._loadtest(test, num_tests[0]))
2501 2501 else:
2502 2502 tests.append(test)
2503 2503 if self._jobs == 1:
2504 2504 job(test, result)
2505 2505 else:
2506 2506 t = threading.Thread(
2507 2507 target=job, name=test.name, args=(test, result)
2508 2508 )
2509 2509 t.start()
2510 2510 running += 1
2511 2511
2512 2512 # If we stop early we still need to wait on started tests to
2513 2513 # finish. Otherwise, there is a race between the test completing
2514 2514 # and the test's cleanup code running. This could result in the
2515 2515 # test reporting incorrect.
2516 2516 if stoppedearly:
2517 2517 while running:
2518 2518 try:
2519 2519 done.get(True, 1)
2520 2520 running -= 1
2521 2521 except queue.Empty:
2522 2522 continue
2523 2523 except KeyboardInterrupt:
2524 2524 for test in runtests:
2525 2525 test.abort()
2526 2526
2527 2527 channels = []
2528 2528
2529 2529 return result
2530 2530
2531 2531
2532 2532 # Save the most recent 5 wall-clock runtimes of each test to a
2533 2533 # human-readable text file named .testtimes. Tests are sorted
2534 2534 # alphabetically, while times for each test are listed from oldest to
2535 2535 # newest.
2536 2536
2537 2537
2538 2538 def loadtimes(outputdir):
2539 2539 times = []
2540 2540 try:
2541 2541 with open(os.path.join(outputdir, b'.testtimes')) as fp:
2542 2542 for line in fp:
2543 2543 m = re.match('(.*?) ([0-9. ]+)', line)
2544 2544 times.append(
2545 2545 (m.group(1), [float(t) for t in m.group(2).split()])
2546 2546 )
2547 2547 except IOError as err:
2548 2548 if err.errno != errno.ENOENT:
2549 2549 raise
2550 2550 return times
2551 2551
2552 2552
2553 2553 def savetimes(outputdir, result):
2554 2554 saved = dict(loadtimes(outputdir))
2555 2555 maxruns = 5
2556 2556 skipped = {str(t[0]) for t in result.skipped}
2557 2557 for tdata in result.times:
2558 2558 test, real = tdata[0], tdata[3]
2559 2559 if test not in skipped:
2560 2560 ts = saved.setdefault(test, [])
2561 2561 ts.append(real)
2562 2562 ts[:] = ts[-maxruns:]
2563 2563
2564 2564 fd, tmpname = tempfile.mkstemp(
2565 2565 prefix=b'.testtimes', dir=outputdir, text=True
2566 2566 )
2567 2567 with os.fdopen(fd, 'w') as fp:
2568 2568 for name, ts in sorted(saved.items()):
2569 2569 fp.write('%s %s\n' % (name, ' '.join(['%.3f' % (t,) for t in ts])))
2570 2570 timepath = os.path.join(outputdir, b'.testtimes')
2571 2571 try:
2572 2572 os.unlink(timepath)
2573 2573 except OSError:
2574 2574 pass
2575 2575 try:
2576 2576 os.rename(tmpname, timepath)
2577 2577 except OSError:
2578 2578 pass
2579 2579
2580 2580
2581 2581 class TextTestRunner(unittest.TextTestRunner):
2582 2582 """Custom unittest test runner that uses appropriate settings."""
2583 2583
2584 2584 def __init__(self, runner, *args, **kwargs):
2585 2585 super(TextTestRunner, self).__init__(*args, **kwargs)
2586 2586
2587 2587 self._runner = runner
2588 2588
2589 2589 self._result = getTestResult()(
2590 2590 self._runner.options, self.stream, self.descriptions, self.verbosity
2591 2591 )
2592 2592
2593 2593 def listtests(self, test):
2594 2594 test = sorted(test, key=lambda t: t.name)
2595 2595
2596 2596 self._result.onStart(test)
2597 2597
2598 2598 for t in test:
2599 2599 print(t.name)
2600 2600 self._result.addSuccess(t)
2601 2601
2602 2602 if self._runner.options.xunit:
2603 2603 with open(self._runner.options.xunit, "wb") as xuf:
2604 2604 self._writexunit(self._result, xuf)
2605 2605
2606 2606 if self._runner.options.json:
2607 2607 jsonpath = os.path.join(self._runner._outputdir, b'report.json')
2608 2608 with open(jsonpath, 'w') as fp:
2609 2609 self._writejson(self._result, fp)
2610 2610
2611 2611 return self._result
2612 2612
2613 2613 def run(self, test):
2614 2614 self._result.onStart(test)
2615 2615 test(self._result)
2616 2616
2617 2617 failed = len(self._result.failures)
2618 2618 skipped = len(self._result.skipped)
2619 2619 ignored = len(self._result.ignored)
2620 2620
2621 2621 with iolock:
2622 2622 self.stream.writeln('')
2623 2623
2624 2624 if not self._runner.options.noskips:
2625 2625 for test, msg in sorted(
2626 2626 self._result.skipped, key=lambda s: s[0].name
2627 2627 ):
2628 2628 formatted = 'Skipped %s: %s\n' % (test.name, msg)
2629 2629 msg = highlightmsg(formatted, self._result.color)
2630 2630 self.stream.write(msg)
2631 2631 for test, msg in sorted(
2632 2632 self._result.failures, key=lambda f: f[0].name
2633 2633 ):
2634 2634 formatted = 'Failed %s: %s\n' % (test.name, msg)
2635 2635 self.stream.write(highlightmsg(formatted, self._result.color))
2636 2636 for test, msg in sorted(
2637 2637 self._result.errors, key=lambda e: e[0].name
2638 2638 ):
2639 2639 self.stream.writeln('Errored %s: %s' % (test.name, msg))
2640 2640
2641 2641 if self._runner.options.xunit:
2642 2642 with open(self._runner.options.xunit, "wb") as xuf:
2643 2643 self._writexunit(self._result, xuf)
2644 2644
2645 2645 if self._runner.options.json:
2646 2646 jsonpath = os.path.join(self._runner._outputdir, b'report.json')
2647 2647 with open(jsonpath, 'w') as fp:
2648 2648 self._writejson(self._result, fp)
2649 2649
2650 2650 self._runner._checkhglib('Tested')
2651 2651
2652 2652 savetimes(self._runner._outputdir, self._result)
2653 2653
2654 2654 if failed and self._runner.options.known_good_rev:
2655 2655 self._bisecttests(t for t, m in self._result.failures)
2656 2656 self.stream.writeln(
2657 2657 '# Ran %d tests, %d skipped, %d failed.'
2658 2658 % (self._result.testsRun, skipped + ignored, failed)
2659 2659 )
2660 2660 if failed:
2661 2661 self.stream.writeln(
2662 2662 'python hash seed: %s' % os.environ['PYTHONHASHSEED']
2663 2663 )
2664 2664 if self._runner.options.time:
2665 2665 self.printtimes(self._result.times)
2666 2666
2667 2667 if self._runner.options.exceptions:
2668 2668 exceptions = aggregateexceptions(
2669 2669 os.path.join(self._runner._outputdir, b'exceptions')
2670 2670 )
2671 2671
2672 2672 self.stream.writeln('Exceptions Report:')
2673 2673 self.stream.writeln(
2674 2674 '%d total from %d frames'
2675 2675 % (exceptions['total'], len(exceptions['exceptioncounts']))
2676 2676 )
2677 2677 combined = exceptions['combined']
2678 2678 for key in sorted(combined, key=combined.get, reverse=True):
2679 2679 frame, line, exc = key
2680 2680 totalcount, testcount, leastcount, leasttest = combined[key]
2681 2681
2682 2682 self.stream.writeln(
2683 2683 '%d (%d tests)\t%s: %s (%s - %d total)'
2684 2684 % (
2685 2685 totalcount,
2686 2686 testcount,
2687 2687 frame,
2688 2688 exc,
2689 2689 leasttest,
2690 2690 leastcount,
2691 2691 )
2692 2692 )
2693 2693
2694 2694 self.stream.flush()
2695 2695
2696 2696 return self._result
2697 2697
2698 2698 def _bisecttests(self, tests):
2699 2699 bisectcmd = ['hg', 'bisect']
2700 2700 bisectrepo = self._runner.options.bisect_repo
2701 2701 if bisectrepo:
2702 2702 bisectcmd.extend(['-R', os.path.abspath(bisectrepo)])
2703 2703
2704 2704 def pread(args):
2705 2705 env = os.environ.copy()
2706 2706 env['HGPLAIN'] = '1'
2707 2707 p = subprocess.Popen(
2708 2708 args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, env=env
2709 2709 )
2710 2710 data = p.stdout.read()
2711 2711 p.wait()
2712 2712 return data
2713 2713
2714 2714 for test in tests:
2715 2715 pread(bisectcmd + ['--reset']),
2716 2716 pread(bisectcmd + ['--bad', '.'])
2717 2717 pread(bisectcmd + ['--good', self._runner.options.known_good_rev])
2718 2718 # TODO: we probably need to forward more options
2719 2719 # that alter hg's behavior inside the tests.
2720 2720 opts = ''
2721 2721 withhg = self._runner.options.with_hg
2722 2722 if withhg:
2723 2723 opts += ' --with-hg=%s ' % shellquote(_bytes2sys(withhg))
2724 2724 rtc = '%s %s %s %s' % (sysexecutable, sys.argv[0], opts, test)
2725 2725 data = pread(bisectcmd + ['--command', rtc])
2726 2726 m = re.search(
2727 2727 (
2728 2728 br'\nThe first (?P<goodbad>bad|good) revision '
2729 2729 br'is:\nchangeset: +\d+:(?P<node>[a-f0-9]+)\n.*\n'
2730 2730 br'summary: +(?P<summary>[^\n]+)\n'
2731 2731 ),
2732 2732 data,
2733 2733 (re.MULTILINE | re.DOTALL),
2734 2734 )
2735 2735 if m is None:
2736 2736 self.stream.writeln(
2737 2737 'Failed to identify failure point for %s' % test
2738 2738 )
2739 2739 continue
2740 2740 dat = m.groupdict()
2741 2741 verb = 'broken' if dat['goodbad'] == b'bad' else 'fixed'
2742 2742 self.stream.writeln(
2743 2743 '%s %s by %s (%s)'
2744 2744 % (
2745 2745 test,
2746 2746 verb,
2747 2747 dat['node'].decode('ascii'),
2748 2748 dat['summary'].decode('utf8', 'ignore'),
2749 2749 )
2750 2750 )
2751 2751
2752 2752 def printtimes(self, times):
2753 2753 # iolock held by run
2754 2754 self.stream.writeln('# Producing time report')
2755 2755 times.sort(key=lambda t: (t[3]))
2756 2756 cols = '%7.3f %7.3f %7.3f %7.3f %7.3f %s'
2757 2757 self.stream.writeln(
2758 2758 '%-7s %-7s %-7s %-7s %-7s %s'
2759 2759 % ('start', 'end', 'cuser', 'csys', 'real', 'Test')
2760 2760 )
2761 2761 for tdata in times:
2762 2762 test = tdata[0]
2763 2763 cuser, csys, real, start, end = tdata[1:6]
2764 2764 self.stream.writeln(cols % (start, end, cuser, csys, real, test))
2765 2765
2766 2766 @staticmethod
2767 2767 def _writexunit(result, outf):
2768 2768 # See http://llg.cubic.org/docs/junit/ for a reference.
2769 2769 timesd = {t[0]: t[3] for t in result.times}
2770 2770 doc = minidom.Document()
2771 2771 s = doc.createElement('testsuite')
2772 2772 s.setAttribute('errors', "0") # TODO
2773 2773 s.setAttribute('failures', str(len(result.failures)))
2774 2774 s.setAttribute('name', 'run-tests')
2775 2775 s.setAttribute(
2776 2776 'skipped', str(len(result.skipped) + len(result.ignored))
2777 2777 )
2778 2778 s.setAttribute('tests', str(result.testsRun))
2779 2779 doc.appendChild(s)
2780 2780 for tc in result.successes:
2781 2781 t = doc.createElement('testcase')
2782 2782 t.setAttribute('name', tc.name)
2783 2783 tctime = timesd.get(tc.name)
2784 2784 if tctime is not None:
2785 2785 t.setAttribute('time', '%.3f' % tctime)
2786 2786 s.appendChild(t)
2787 2787 for tc, err in sorted(result.faildata.items()):
2788 2788 t = doc.createElement('testcase')
2789 2789 t.setAttribute('name', tc)
2790 2790 tctime = timesd.get(tc)
2791 2791 if tctime is not None:
2792 2792 t.setAttribute('time', '%.3f' % tctime)
2793 2793 # createCDATASection expects a unicode or it will
2794 2794 # convert using default conversion rules, which will
2795 2795 # fail if string isn't ASCII.
2796 2796 err = cdatasafe(err).decode('utf-8', 'replace')
2797 2797 cd = doc.createCDATASection(err)
2798 2798 # Use 'failure' here instead of 'error' to match errors = 0,
2799 2799 # failures = len(result.failures) in the testsuite element.
2800 2800 failelem = doc.createElement('failure')
2801 2801 failelem.setAttribute('message', 'output changed')
2802 2802 failelem.setAttribute('type', 'output-mismatch')
2803 2803 failelem.appendChild(cd)
2804 2804 t.appendChild(failelem)
2805 2805 s.appendChild(t)
2806 2806 for tc, message in result.skipped:
2807 2807 # According to the schema, 'skipped' has no attributes. So store
2808 2808 # the skip message as a text node instead.
2809 2809 t = doc.createElement('testcase')
2810 2810 t.setAttribute('name', tc.name)
2811 2811 binmessage = message.encode('utf-8')
2812 2812 message = cdatasafe(binmessage).decode('utf-8', 'replace')
2813 2813 cd = doc.createCDATASection(message)
2814 2814 skipelem = doc.createElement('skipped')
2815 2815 skipelem.appendChild(cd)
2816 2816 t.appendChild(skipelem)
2817 2817 s.appendChild(t)
2818 2818 outf.write(doc.toprettyxml(indent=' ', encoding='utf-8'))
2819 2819
2820 2820 @staticmethod
2821 2821 def _writejson(result, outf):
2822 2822 timesd = {}
2823 2823 for tdata in result.times:
2824 2824 test = tdata[0]
2825 2825 timesd[test] = tdata[1:]
2826 2826
2827 2827 outcome = {}
2828 2828 groups = [
2829 2829 ('success', ((tc, None) for tc in result.successes)),
2830 2830 ('failure', result.failures),
2831 2831 ('skip', result.skipped),
2832 2832 ]
2833 2833 for res, testcases in groups:
2834 2834 for tc, __ in testcases:
2835 2835 if tc.name in timesd:
2836 2836 diff = result.faildata.get(tc.name, b'')
2837 2837 try:
2838 2838 diff = diff.decode('unicode_escape')
2839 2839 except UnicodeDecodeError as e:
2840 2840 diff = '%r decoding diff, sorry' % e
2841 2841 tres = {
2842 2842 'result': res,
2843 2843 'time': ('%0.3f' % timesd[tc.name][2]),
2844 2844 'cuser': ('%0.3f' % timesd[tc.name][0]),
2845 2845 'csys': ('%0.3f' % timesd[tc.name][1]),
2846 2846 'start': ('%0.3f' % timesd[tc.name][3]),
2847 2847 'end': ('%0.3f' % timesd[tc.name][4]),
2848 2848 'diff': diff,
2849 2849 }
2850 2850 else:
2851 2851 # blacklisted test
2852 2852 tres = {'result': res}
2853 2853
2854 2854 outcome[tc.name] = tres
2855 2855 jsonout = json.dumps(
2856 2856 outcome, sort_keys=True, indent=4, separators=(',', ': ')
2857 2857 )
2858 2858 outf.writelines(("testreport =", jsonout))
2859 2859
2860 2860
2861 2861 def sorttests(testdescs, previoustimes, shuffle=False):
2862 2862 """Do an in-place sort of tests."""
2863 2863 if shuffle:
2864 2864 random.shuffle(testdescs)
2865 2865 return
2866 2866
2867 2867 if previoustimes:
2868 2868
2869 2869 def sortkey(f):
2870 2870 f = f['path']
2871 2871 if f in previoustimes:
2872 2872 # Use most recent time as estimate
2873 2873 return -(previoustimes[f][-1])
2874 2874 else:
2875 2875 # Default to a rather arbitrary value of 1 second for new tests
2876 2876 return -1.0
2877 2877
2878 2878 else:
2879 2879 # keywords for slow tests
2880 2880 slow = {
2881 2881 b'svn': 10,
2882 2882 b'cvs': 10,
2883 2883 b'hghave': 10,
2884 2884 b'largefiles-update': 10,
2885 2885 b'run-tests': 10,
2886 2886 b'corruption': 10,
2887 2887 b'race': 10,
2888 2888 b'i18n': 10,
2889 2889 b'check': 100,
2890 2890 b'gendoc': 100,
2891 2891 b'contrib-perf': 200,
2892 2892 b'merge-combination': 100,
2893 2893 }
2894 2894 perf = {}
2895 2895
2896 2896 def sortkey(f):
2897 2897 # run largest tests first, as they tend to take the longest
2898 2898 f = f['path']
2899 2899 try:
2900 2900 return perf[f]
2901 2901 except KeyError:
2902 2902 try:
2903 2903 val = -os.stat(f).st_size
2904 2904 except OSError as e:
2905 2905 if e.errno != errno.ENOENT:
2906 2906 raise
2907 2907 perf[f] = -1e9 # file does not exist, tell early
2908 2908 return -1e9
2909 2909 for kw, mul in slow.items():
2910 2910 if kw in f:
2911 2911 val *= mul
2912 2912 if f.endswith(b'.py'):
2913 2913 val /= 10.0
2914 2914 perf[f] = val / 1000.0
2915 2915 return perf[f]
2916 2916
2917 2917 testdescs.sort(key=sortkey)
2918 2918
2919 2919
2920 2920 class TestRunner(object):
2921 2921 """Holds context for executing tests.
2922 2922
2923 2923 Tests rely on a lot of state. This object holds it for them.
2924 2924 """
2925 2925
2926 2926 # Programs required to run tests.
2927 2927 REQUIREDTOOLS = [
2928 2928 b'diff',
2929 2929 b'grep',
2930 2930 b'unzip',
2931 2931 b'gunzip',
2932 2932 b'bunzip2',
2933 2933 b'sed',
2934 2934 ]
2935 2935
2936 2936 # Maps file extensions to test class.
2937 2937 TESTTYPES = [
2938 2938 (b'.py', PythonTest),
2939 2939 (b'.t', TTest),
2940 2940 ]
2941 2941
2942 2942 def __init__(self):
2943 2943 self.options = None
2944 2944 self._hgroot = None
2945 2945 self._testdir = None
2946 2946 self._outputdir = None
2947 2947 self._hgtmp = None
2948 2948 self._installdir = None
2949 2949 self._bindir = None
2950 2950 self._tmpbinddir = None
2951 2951 self._pythondir = None
2952 2952 self._coveragefile = None
2953 2953 self._createdfiles = []
2954 2954 self._hgcommand = None
2955 2955 self._hgpath = None
2956 2956 self._portoffset = 0
2957 2957 self._ports = {}
2958 2958
2959 2959 def run(self, args, parser=None):
2960 2960 """Run the test suite."""
2961 2961 oldmask = os.umask(0o22)
2962 2962 try:
2963 2963 parser = parser or getparser()
2964 2964 options = parseargs(args, parser)
2965 2965 tests = [_sys2bytes(a) for a in options.tests]
2966 2966 if options.test_list is not None:
2967 2967 for listfile in options.test_list:
2968 2968 with open(listfile, 'rb') as f:
2969 2969 tests.extend(t for t in f.read().splitlines() if t)
2970 2970 self.options = options
2971 2971
2972 2972 self._checktools()
2973 2973 testdescs = self.findtests(tests)
2974 2974 if options.profile_runner:
2975 2975 import statprof
2976 2976
2977 2977 statprof.start()
2978 2978 result = self._run(testdescs)
2979 2979 if options.profile_runner:
2980 2980 statprof.stop()
2981 2981 statprof.display()
2982 2982 return result
2983 2983
2984 2984 finally:
2985 2985 os.umask(oldmask)
2986 2986
2987 2987 def _run(self, testdescs):
2988 2988 testdir = getcwdb()
2989 2989 self._testdir = osenvironb[b'TESTDIR'] = getcwdb()
2990 2990 # assume all tests in same folder for now
2991 2991 if testdescs:
2992 2992 pathname = os.path.dirname(testdescs[0]['path'])
2993 2993 if pathname:
2994 2994 testdir = os.path.join(testdir, pathname)
2995 2995 self._testdir = osenvironb[b'TESTDIR'] = testdir
2996 2996 if self.options.outputdir:
2997 2997 self._outputdir = canonpath(_sys2bytes(self.options.outputdir))
2998 2998 else:
2999 2999 self._outputdir = getcwdb()
3000 3000 if testdescs and pathname:
3001 3001 self._outputdir = os.path.join(self._outputdir, pathname)
3002 3002 previoustimes = {}
3003 3003 if self.options.order_by_runtime:
3004 3004 previoustimes = dict(loadtimes(self._outputdir))
3005 3005 sorttests(testdescs, previoustimes, shuffle=self.options.random)
3006 3006
3007 3007 if 'PYTHONHASHSEED' not in os.environ:
3008 3008 # use a random python hash seed all the time
3009 3009 # we do the randomness ourself to know what seed is used
3010 3010 os.environ['PYTHONHASHSEED'] = str(random.getrandbits(32))
3011 3011
3012 3012 # Rayon (Rust crate for multi-threading) will use all logical CPU cores
3013 3013 # by default, causing thrashing on high-cpu-count systems.
3014 3014 # Setting its limit to 3 during tests should still let us uncover
3015 3015 # multi-threading bugs while keeping the thrashing reasonable.
3016 3016 os.environ.setdefault("RAYON_NUM_THREADS", "3")
3017 3017
3018 3018 if self.options.tmpdir:
3019 3019 self.options.keep_tmpdir = True
3020 3020 tmpdir = _sys2bytes(self.options.tmpdir)
3021 3021 if os.path.exists(tmpdir):
3022 3022 # Meaning of tmpdir has changed since 1.3: we used to create
3023 3023 # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if
3024 3024 # tmpdir already exists.
3025 3025 print("error: temp dir %r already exists" % tmpdir)
3026 3026 return 1
3027 3027
3028 3028 os.makedirs(tmpdir)
3029 3029 else:
3030 3030 d = None
3031 3031 if os.name == 'nt':
3032 3032 # without this, we get the default temp dir location, but
3033 3033 # in all lowercase, which causes troubles with paths (issue3490)
3034 3034 d = osenvironb.get(b'TMP', None)
3035 3035 tmpdir = tempfile.mkdtemp(b'', b'hgtests.', d)
3036 3036
3037 3037 self._hgtmp = osenvironb[b'HGTMP'] = os.path.realpath(tmpdir)
3038 3038
3039 3039 if self.options.with_hg:
3040 3040 self._installdir = None
3041 3041 whg = self.options.with_hg
3042 3042 self._bindir = os.path.dirname(os.path.realpath(whg))
3043 3043 assert isinstance(self._bindir, bytes)
3044 3044 self._hgcommand = os.path.basename(whg)
3045 3045 self._tmpbindir = os.path.join(self._hgtmp, b'install', b'bin')
3046 3046 os.makedirs(self._tmpbindir)
3047 3047
3048 3048 normbin = os.path.normpath(os.path.abspath(whg))
3049 3049 normbin = normbin.replace(_sys2bytes(os.sep), b'/')
3050 3050
3051 3051 # Other Python scripts in the test harness need to
3052 3052 # `import mercurial`. If `hg` is a Python script, we assume
3053 3053 # the Mercurial modules are relative to its path and tell the tests
3054 3054 # to load Python modules from its directory.
3055 3055 with open(whg, 'rb') as fh:
3056 3056 initial = fh.read(1024)
3057 3057
3058 3058 if re.match(b'#!.*python', initial):
3059 3059 self._pythondir = self._bindir
3060 3060 # If it looks like our in-repo Rust binary, use the source root.
3061 3061 # This is a bit hacky. But rhg is still not supported outside the
3062 3062 # source directory. So until it is, do the simple thing.
3063 3063 elif re.search(b'/rust/target/[^/]+/hg', normbin):
3064 3064 self._pythondir = os.path.dirname(self._testdir)
3065 3065 # Fall back to the legacy behavior.
3066 3066 else:
3067 3067 self._pythondir = self._bindir
3068 3068
3069 3069 else:
3070 3070 self._installdir = os.path.join(self._hgtmp, b"install")
3071 3071 self._bindir = os.path.join(self._installdir, b"bin")
3072 3072 self._hgcommand = b'hg'
3073 3073 self._tmpbindir = self._bindir
3074 3074 self._pythondir = os.path.join(self._installdir, b"lib", b"python")
3075 3075
3076 3076 # Force the use of hg.exe instead of relying on MSYS to recognize hg is
3077 3077 # a python script and feed it to python.exe. Legacy stdio is force
3078 3078 # enabled by hg.exe, and this is a more realistic way to launch hg
3079 3079 # anyway.
3080 3080 if os.name == 'nt' and not self._hgcommand.endswith(b'.exe'):
3081 3081 self._hgcommand += b'.exe'
3082 3082
3083 3083 # set CHGHG, then replace "hg" command by "chg"
3084 3084 chgbindir = self._bindir
3085 3085 if self.options.chg or self.options.with_chg:
3086 3086 osenvironb[b'CHGHG'] = os.path.join(self._bindir, self._hgcommand)
3087 3087 else:
3088 3088 osenvironb.pop(b'CHGHG', None) # drop flag for hghave
3089 3089 if self.options.chg:
3090 3090 self._hgcommand = b'chg'
3091 3091 elif self.options.with_chg:
3092 3092 chgbindir = os.path.dirname(os.path.realpath(self.options.with_chg))
3093 3093 self._hgcommand = os.path.basename(self.options.with_chg)
3094 3094
3095 3095 osenvironb[b"BINDIR"] = self._bindir
3096 3096 osenvironb[b"PYTHON"] = PYTHON
3097 3097
3098 3098 fileb = _sys2bytes(__file__)
3099 3099 runtestdir = os.path.abspath(os.path.dirname(fileb))
3100 3100 osenvironb[b'RUNTESTDIR'] = runtestdir
3101 3101 if PYTHON3:
3102 3102 sepb = _sys2bytes(os.pathsep)
3103 3103 else:
3104 3104 sepb = os.pathsep
3105 3105 path = [self._bindir, runtestdir] + osenvironb[b"PATH"].split(sepb)
3106 3106 if os.path.islink(__file__):
3107 3107 # test helper will likely be at the end of the symlink
3108 3108 realfile = os.path.realpath(fileb)
3109 3109 realdir = os.path.abspath(os.path.dirname(realfile))
3110 3110 path.insert(2, realdir)
3111 3111 if chgbindir != self._bindir:
3112 3112 path.insert(1, chgbindir)
3113 3113 if self._testdir != runtestdir:
3114 3114 path = [self._testdir] + path
3115 3115 if self._tmpbindir != self._bindir:
3116 3116 path = [self._tmpbindir] + path
3117 3117 osenvironb[b"PATH"] = sepb.join(path)
3118 3118
3119 3119 # Include TESTDIR in PYTHONPATH so that out-of-tree extensions
3120 3120 # can run .../tests/run-tests.py test-foo where test-foo
3121 3121 # adds an extension to HGRC. Also include run-test.py directory to
3122 3122 # import modules like heredoctest.
3123 3123 pypath = [self._pythondir, self._testdir, runtestdir]
3124 3124 # We have to augment PYTHONPATH, rather than simply replacing
3125 3125 # it, in case external libraries are only available via current
3126 3126 # PYTHONPATH. (In particular, the Subversion bindings on OS X
3127 3127 # are in /opt/subversion.)
3128 3128 oldpypath = osenvironb.get(IMPL_PATH)
3129 3129 if oldpypath:
3130 3130 pypath.append(oldpypath)
3131 3131 osenvironb[IMPL_PATH] = sepb.join(pypath)
3132 3132
3133 3133 if self.options.pure:
3134 3134 os.environ["HGTEST_RUN_TESTS_PURE"] = "--pure"
3135 3135 os.environ["HGMODULEPOLICY"] = "py"
3136 3136 if self.options.rust:
3137 3137 os.environ["HGMODULEPOLICY"] = "rust+c"
3138 3138 if self.options.no_rust:
3139 3139 current_policy = os.environ.get("HGMODULEPOLICY", "")
3140 3140 if current_policy.startswith("rust+"):
3141 3141 os.environ["HGMODULEPOLICY"] = current_policy[len("rust+") :]
3142 3142 os.environ.pop("HGWITHRUSTEXT", None)
3143 3143
3144 3144 if self.options.allow_slow_tests:
3145 3145 os.environ["HGTEST_SLOW"] = "slow"
3146 3146 elif 'HGTEST_SLOW' in os.environ:
3147 3147 del os.environ['HGTEST_SLOW']
3148 3148
3149 3149 self._coveragefile = os.path.join(self._testdir, b'.coverage')
3150 3150
3151 3151 if self.options.exceptions:
3152 3152 exceptionsdir = os.path.join(self._outputdir, b'exceptions')
3153 3153 try:
3154 3154 os.makedirs(exceptionsdir)
3155 3155 except OSError as e:
3156 3156 if e.errno != errno.EEXIST:
3157 3157 raise
3158 3158
3159 3159 # Remove all existing exception reports.
3160 3160 for f in os.listdir(exceptionsdir):
3161 3161 os.unlink(os.path.join(exceptionsdir, f))
3162 3162
3163 3163 osenvironb[b'HGEXCEPTIONSDIR'] = exceptionsdir
3164 3164 logexceptions = os.path.join(self._testdir, b'logexceptions.py')
3165 3165 self.options.extra_config_opt.append(
3166 3166 'extensions.logexceptions=%s' % logexceptions.decode('utf-8')
3167 3167 )
3168 3168
3169 3169 vlog("# Using TESTDIR", _bytes2sys(self._testdir))
3170 3170 vlog("# Using RUNTESTDIR", _bytes2sys(osenvironb[b'RUNTESTDIR']))
3171 3171 vlog("# Using HGTMP", _bytes2sys(self._hgtmp))
3172 3172 vlog("# Using PATH", os.environ["PATH"])
3173 3173 vlog(
3174 3174 "# Using",
3175 3175 _bytes2sys(IMPL_PATH),
3176 3176 _bytes2sys(osenvironb[IMPL_PATH]),
3177 3177 )
3178 3178 vlog("# Writing to directory", _bytes2sys(self._outputdir))
3179 3179
3180 3180 try:
3181 3181 return self._runtests(testdescs) or 0
3182 3182 finally:
3183 3183 time.sleep(0.1)
3184 3184 self._cleanup()
3185 3185
3186 3186 def findtests(self, args):
3187 3187 """Finds possible test files from arguments.
3188 3188
3189 3189 If you wish to inject custom tests into the test harness, this would
3190 3190 be a good function to monkeypatch or override in a derived class.
3191 3191 """
3192 3192 if not args:
3193 3193 if self.options.changed:
3194 3194 proc = Popen4(
3195 3195 b'hg st --rev "%s" -man0 .'
3196 3196 % _sys2bytes(self.options.changed),
3197 3197 None,
3198 3198 0,
3199 3199 )
3200 3200 stdout, stderr = proc.communicate()
3201 3201 args = stdout.strip(b'\0').split(b'\0')
3202 3202 else:
3203 3203 args = os.listdir(b'.')
3204 3204
3205 3205 expanded_args = []
3206 3206 for arg in args:
3207 3207 if os.path.isdir(arg):
3208 3208 if not arg.endswith(b'/'):
3209 3209 arg += b'/'
3210 3210 expanded_args.extend([arg + a for a in os.listdir(arg)])
3211 3211 else:
3212 3212 expanded_args.append(arg)
3213 3213 args = expanded_args
3214 3214
3215 3215 testcasepattern = re.compile(br'([\w-]+\.t|py)(?:#([a-zA-Z0-9_\-.#]+))')
3216 3216 tests = []
3217 3217 for t in args:
3218 3218 case = []
3219 3219
3220 3220 if not (
3221 3221 os.path.basename(t).startswith(b'test-')
3222 3222 and (t.endswith(b'.py') or t.endswith(b'.t'))
3223 3223 ):
3224 3224
3225 3225 m = testcasepattern.match(os.path.basename(t))
3226 3226 if m is not None:
3227 3227 t_basename, casestr = m.groups()
3228 3228 t = os.path.join(os.path.dirname(t), t_basename)
3229 3229 if casestr:
3230 3230 case = casestr.split(b'#')
3231 3231 else:
3232 3232 continue
3233 3233
3234 3234 if t.endswith(b'.t'):
3235 3235 # .t file may contain multiple test cases
3236 3236 casedimensions = parsettestcases(t)
3237 3237 if casedimensions:
3238 3238 cases = []
3239 3239
3240 3240 def addcases(case, casedimensions):
3241 3241 if not casedimensions:
3242 3242 cases.append(case)
3243 3243 else:
3244 3244 for c in casedimensions[0]:
3245 3245 addcases(case + [c], casedimensions[1:])
3246 3246
3247 3247 addcases([], casedimensions)
3248 3248 if case and case in cases:
3249 3249 cases = [case]
3250 3250 elif case:
3251 3251 # Ignore invalid cases
3252 3252 cases = []
3253 3253 else:
3254 3254 pass
3255 3255 tests += [{'path': t, 'case': c} for c in sorted(cases)]
3256 3256 else:
3257 3257 tests.append({'path': t})
3258 3258 else:
3259 3259 tests.append({'path': t})
3260 3260
3261 3261 if self.options.retest:
3262 3262 retest_args = []
3263 3263 for test in tests:
3264 3264 errpath = self._geterrpath(test)
3265 3265 if os.path.exists(errpath):
3266 3266 retest_args.append(test)
3267 3267 tests = retest_args
3268 3268 return tests
3269 3269
3270 3270 def _runtests(self, testdescs):
3271 3271 def _reloadtest(test, i):
3272 3272 # convert a test back to its description dict
3273 3273 desc = {'path': test.path}
3274 3274 case = getattr(test, '_case', [])
3275 3275 if case:
3276 3276 desc['case'] = case
3277 3277 return self._gettest(desc, i)
3278 3278
3279 3279 try:
3280 3280 if self.options.restart:
3281 3281 orig = list(testdescs)
3282 3282 while testdescs:
3283 3283 desc = testdescs[0]
3284 3284 errpath = self._geterrpath(desc)
3285 3285 if os.path.exists(errpath):
3286 3286 break
3287 3287 testdescs.pop(0)
3288 3288 if not testdescs:
3289 3289 print("running all tests")
3290 3290 testdescs = orig
3291 3291
3292 3292 tests = [self._gettest(d, i) for i, d in enumerate(testdescs)]
3293 3293 num_tests = len(tests) * self.options.runs_per_test
3294 3294
3295 3295 jobs = min(num_tests, self.options.jobs)
3296 3296
3297 3297 failed = False
3298 3298 kws = self.options.keywords
3299 3299 if kws is not None and PYTHON3:
3300 3300 kws = kws.encode('utf-8')
3301 3301
3302 3302 suite = TestSuite(
3303 3303 self._testdir,
3304 3304 jobs=jobs,
3305 3305 whitelist=self.options.whitelisted,
3306 3306 blacklist=self.options.blacklist,
3307 3307 keywords=kws,
3308 3308 loop=self.options.loop,
3309 3309 runs_per_test=self.options.runs_per_test,
3310 3310 showchannels=self.options.showchannels,
3311 3311 tests=tests,
3312 3312 loadtest=_reloadtest,
3313 3313 )
3314 3314 verbosity = 1
3315 3315 if self.options.list_tests:
3316 3316 verbosity = 0
3317 3317 elif self.options.verbose:
3318 3318 verbosity = 2
3319 3319 runner = TextTestRunner(self, verbosity=verbosity)
3320 3320
3321 3321 if self.options.list_tests:
3322 3322 result = runner.listtests(suite)
3323 3323 else:
3324 3324 if self._installdir:
3325 3325 self._installhg()
3326 3326 self._checkhglib("Testing")
3327 3327 else:
3328 3328 self._usecorrectpython()
3329 3329 if self.options.chg:
3330 3330 assert self._installdir
3331 3331 self._installchg()
3332 3332
3333 3333 log(
3334 3334 'running %d tests using %d parallel processes'
3335 3335 % (num_tests, jobs)
3336 3336 )
3337 3337
3338 3338 result = runner.run(suite)
3339 3339
3340 3340 if result.failures or result.errors:
3341 3341 failed = True
3342 3342
3343 3343 result.onEnd()
3344 3344
3345 3345 if self.options.anycoverage:
3346 3346 self._outputcoverage()
3347 3347 except KeyboardInterrupt:
3348 3348 failed = True
3349 3349 print("\ninterrupted!")
3350 3350
3351 3351 if failed:
3352 3352 return 1
3353 3353
3354 3354 def _geterrpath(self, test):
3355 3355 # test['path'] is a relative path
3356 3356 if 'case' in test:
3357 3357 # for multiple dimensions test cases
3358 3358 casestr = b'#'.join(test['case'])
3359 3359 errpath = b'%s#%s.err' % (test['path'], casestr)
3360 3360 else:
3361 3361 errpath = b'%s.err' % test['path']
3362 3362 if self.options.outputdir:
3363 3363 self._outputdir = canonpath(_sys2bytes(self.options.outputdir))
3364 3364 errpath = os.path.join(self._outputdir, errpath)
3365 3365 return errpath
3366 3366
3367 3367 def _getport(self, count):
3368 3368 port = self._ports.get(count) # do we have a cached entry?
3369 3369 if port is None:
3370 3370 portneeded = 3
3371 3371 # above 100 tries we just give up and let test reports failure
3372 3372 for tries in xrange(100):
3373 3373 allfree = True
3374 3374 port = self.options.port + self._portoffset
3375 3375 for idx in xrange(portneeded):
3376 3376 if not checkportisavailable(port + idx):
3377 3377 allfree = False
3378 3378 break
3379 3379 self._portoffset += portneeded
3380 3380 if allfree:
3381 3381 break
3382 3382 self._ports[count] = port
3383 3383 return port
3384 3384
3385 3385 def _gettest(self, testdesc, count):
3386 3386 """Obtain a Test by looking at its filename.
3387 3387
3388 3388 Returns a Test instance. The Test may not be runnable if it doesn't
3389 3389 map to a known type.
3390 3390 """
3391 3391 path = testdesc['path']
3392 3392 lctest = path.lower()
3393 3393 testcls = Test
3394 3394
3395 3395 for ext, cls in self.TESTTYPES:
3396 3396 if lctest.endswith(ext):
3397 3397 testcls = cls
3398 3398 break
3399 3399
3400 3400 refpath = os.path.join(getcwdb(), path)
3401 3401 tmpdir = os.path.join(self._hgtmp, b'child%d' % count)
3402 3402
3403 3403 # extra keyword parameters. 'case' is used by .t tests
3404 3404 kwds = {k: testdesc[k] for k in ['case'] if k in testdesc}
3405 3405
3406 3406 t = testcls(
3407 3407 refpath,
3408 3408 self._outputdir,
3409 3409 tmpdir,
3410 3410 keeptmpdir=self.options.keep_tmpdir,
3411 3411 debug=self.options.debug,
3412 3412 first=self.options.first,
3413 3413 timeout=self.options.timeout,
3414 3414 startport=self._getport(count),
3415 3415 extraconfigopts=self.options.extra_config_opt,
3416 3416 shell=self.options.shell,
3417 3417 hgcommand=self._hgcommand,
3418 3418 usechg=bool(self.options.with_chg or self.options.chg),
3419 3419 chgdebug=self.options.chg_debug,
3420 3420 useipv6=useipv6,
3421 3421 **kwds
3422 3422 )
3423 3423 t.should_reload = True
3424 3424 return t
3425 3425
3426 3426 def _cleanup(self):
3427 3427 """Clean up state from this test invocation."""
3428 3428 if self.options.keep_tmpdir:
3429 3429 return
3430 3430
3431 3431 vlog("# Cleaning up HGTMP", _bytes2sys(self._hgtmp))
3432 3432 shutil.rmtree(self._hgtmp, True)
3433 3433 for f in self._createdfiles:
3434 3434 try:
3435 3435 os.remove(f)
3436 3436 except OSError:
3437 3437 pass
3438 3438
3439 3439 def _usecorrectpython(self):
3440 3440 """Configure the environment to use the appropriate Python in tests."""
3441 3441 # Tests must use the same interpreter as us or bad things will happen.
3442 3442 pyexename = sys.platform == 'win32' and b'python.exe' or b'python3'
3443 3443
3444 3444 # os.symlink() is a thing with py3 on Windows, but it requires
3445 3445 # Administrator rights.
3446 3446 if getattr(os, 'symlink', None) and os.name != 'nt':
3447 3447 vlog(
3448 3448 "# Making python executable in test path a symlink to '%s'"
3449 3449 % sysexecutable
3450 3450 )
3451 3451 mypython = os.path.join(self._tmpbindir, pyexename)
3452 3452 try:
3453 3453 if os.readlink(mypython) == sysexecutable:
3454 3454 return
3455 3455 os.unlink(mypython)
3456 3456 except OSError as err:
3457 3457 if err.errno != errno.ENOENT:
3458 3458 raise
3459 3459 if self._findprogram(pyexename) != sysexecutable:
3460 3460 try:
3461 3461 os.symlink(sysexecutable, mypython)
3462 3462 self._createdfiles.append(mypython)
3463 3463 except OSError as err:
3464 3464 # child processes may race, which is harmless
3465 3465 if err.errno != errno.EEXIST:
3466 3466 raise
3467 3467 else:
3468 3468 exedir, exename = os.path.split(sysexecutable)
3469 3469 vlog(
3470 3470 "# Modifying search path to find %s as %s in '%s'"
3471 3471 % (exename, pyexename, exedir)
3472 3472 )
3473 3473 path = os.environ['PATH'].split(os.pathsep)
3474 3474 while exedir in path:
3475 3475 path.remove(exedir)
3476 3476 os.environ['PATH'] = os.pathsep.join([exedir] + path)
3477 3477 if not self._findprogram(pyexename):
3478 3478 print("WARNING: Cannot find %s in search path" % pyexename)
3479 3479
3480 3480 def _installhg(self):
3481 3481 """Install hg into the test environment.
3482 3482
3483 3483 This will also configure hg with the appropriate testing settings.
3484 3484 """
3485 3485 vlog("# Performing temporary installation of HG")
3486 3486 installerrs = os.path.join(self._hgtmp, b"install.err")
3487 3487 compiler = ''
3488 3488 if self.options.compiler:
3489 3489 compiler = '--compiler ' + self.options.compiler
3490 3490 setup_opts = b""
3491 3491 if self.options.pure:
3492 3492 setup_opts = b"--pure"
3493 3493 elif self.options.rust:
3494 3494 setup_opts = b"--rust"
3495 3495 elif self.options.no_rust:
3496 3496 setup_opts = b"--no-rust"
3497 3497
3498 3498 # Run installer in hg root
3499 3499 script = os.path.realpath(sys.argv[0])
3500 3500 exe = sysexecutable
3501 3501 if PYTHON3:
3502 3502 compiler = _sys2bytes(compiler)
3503 3503 script = _sys2bytes(script)
3504 3504 exe = _sys2bytes(exe)
3505 3505 hgroot = os.path.dirname(os.path.dirname(script))
3506 3506 self._hgroot = hgroot
3507 3507 os.chdir(hgroot)
3508 3508 nohome = b'--home=""'
3509 3509 if os.name == 'nt':
3510 3510 # The --home="" trick works only on OS where os.sep == '/'
3511 3511 # because of a distutils convert_path() fast-path. Avoid it at
3512 3512 # least on Windows for now, deal with .pydistutils.cfg bugs
3513 3513 # when they happen.
3514 3514 nohome = b''
3515 3515 cmd = (
3516 3516 b'"%(exe)s" setup.py %(setup_opts)s clean --all'
3517 3517 b' build %(compiler)s --build-base="%(base)s"'
3518 3518 b' install --force --prefix="%(prefix)s"'
3519 3519 b' --install-lib="%(libdir)s"'
3520 3520 b' --install-scripts="%(bindir)s" %(nohome)s >%(logfile)s 2>&1'
3521 3521 % {
3522 3522 b'exe': exe,
3523 3523 b'setup_opts': setup_opts,
3524 3524 b'compiler': compiler,
3525 3525 b'base': os.path.join(self._hgtmp, b"build"),
3526 3526 b'prefix': self._installdir,
3527 3527 b'libdir': self._pythondir,
3528 3528 b'bindir': self._bindir,
3529 3529 b'nohome': nohome,
3530 3530 b'logfile': installerrs,
3531 3531 }
3532 3532 )
3533 3533
3534 3534 # setuptools requires install directories to exist.
3535 3535 def makedirs(p):
3536 3536 try:
3537 3537 os.makedirs(p)
3538 3538 except OSError as e:
3539 3539 if e.errno != errno.EEXIST:
3540 3540 raise
3541 3541
3542 3542 makedirs(self._pythondir)
3543 3543 makedirs(self._bindir)
3544 3544
3545 3545 vlog("# Running", cmd.decode("utf-8"))
3546 3546 if subprocess.call(_bytes2sys(cmd), shell=True) == 0:
3547 3547 if not self.options.verbose:
3548 3548 try:
3549 3549 os.remove(installerrs)
3550 3550 except OSError as e:
3551 3551 if e.errno != errno.ENOENT:
3552 3552 raise
3553 3553 else:
3554 3554 with open(installerrs, 'rb') as f:
3555 3555 for line in f:
3556 3556 if PYTHON3:
3557 3557 sys.stdout.buffer.write(line)
3558 3558 else:
3559 3559 sys.stdout.write(line)
3560 3560 sys.exit(1)
3561 3561 os.chdir(self._testdir)
3562 3562
3563 3563 self._usecorrectpython()
3564 3564
3565 3565 hgbat = os.path.join(self._bindir, b'hg.bat')
3566 3566 if os.path.isfile(hgbat):
3567 3567 # hg.bat expects to be put in bin/scripts while run-tests.py
3568 3568 # installation layout put it in bin/ directly. Fix it
3569 3569 with open(hgbat, 'rb') as f:
3570 3570 data = f.read()
3571 3571 if br'"%~dp0..\python" "%~dp0hg" %*' in data:
3572 3572 data = data.replace(
3573 3573 br'"%~dp0..\python" "%~dp0hg" %*',
3574 3574 b'"%~dp0python" "%~dp0hg" %*',
3575 3575 )
3576 3576 with open(hgbat, 'wb') as f:
3577 3577 f.write(data)
3578 3578 else:
3579 3579 print('WARNING: cannot fix hg.bat reference to python.exe')
3580 3580
3581 3581 if self.options.anycoverage:
3582 3582 custom = os.path.join(
3583 3583 osenvironb[b'RUNTESTDIR'], b'sitecustomize.py'
3584 3584 )
3585 3585 target = os.path.join(self._pythondir, b'sitecustomize.py')
3586 3586 vlog('# Installing coverage trigger to %s' % target)
3587 3587 shutil.copyfile(custom, target)
3588 3588 rc = os.path.join(self._testdir, b'.coveragerc')
3589 3589 vlog('# Installing coverage rc to %s' % rc)
3590 3590 osenvironb[b'COVERAGE_PROCESS_START'] = rc
3591 3591 covdir = os.path.join(self._installdir, b'..', b'coverage')
3592 3592 try:
3593 3593 os.mkdir(covdir)
3594 3594 except OSError as e:
3595 3595 if e.errno != errno.EEXIST:
3596 3596 raise
3597 3597
3598 3598 osenvironb[b'COVERAGE_DIR'] = covdir
3599 3599
3600 3600 def _checkhglib(self, verb):
3601 3601 """Ensure that the 'mercurial' package imported by python is
3602 3602 the one we expect it to be. If not, print a warning to stderr."""
3603 3603 if (self._bindir == self._pythondir) and (
3604 3604 self._bindir != self._tmpbindir
3605 3605 ):
3606 3606 # The pythondir has been inferred from --with-hg flag.
3607 3607 # We cannot expect anything sensible here.
3608 3608 return
3609 3609 expecthg = os.path.join(self._pythondir, b'mercurial')
3610 3610 actualhg = self._gethgpath()
3611 3611 if os.path.abspath(actualhg) != os.path.abspath(expecthg):
3612 3612 sys.stderr.write(
3613 3613 'warning: %s with unexpected mercurial lib: %s\n'
3614 3614 ' (expected %s)\n' % (verb, actualhg, expecthg)
3615 3615 )
3616 3616
3617 3617 def _gethgpath(self):
3618 3618 """Return the path to the mercurial package that is actually found by
3619 3619 the current Python interpreter."""
3620 3620 if self._hgpath is not None:
3621 3621 return self._hgpath
3622 3622
3623 3623 cmd = b'"%s" -c "import mercurial; print (mercurial.__path__[0])"'
3624 3624 cmd = cmd % PYTHON
3625 3625 if PYTHON3:
3626 3626 cmd = _bytes2sys(cmd)
3627 3627
3628 3628 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
3629 3629 out, err = p.communicate()
3630 3630
3631 3631 self._hgpath = out.strip()
3632 3632
3633 3633 return self._hgpath
3634 3634
3635 3635 def _installchg(self):
3636 3636 """Install chg into the test environment"""
3637 3637 vlog('# Performing temporary installation of CHG')
3638 3638 assert os.path.dirname(self._bindir) == self._installdir
3639 3639 assert self._hgroot, 'must be called after _installhg()'
3640 3640 cmd = b'"%(make)s" clean install PREFIX="%(prefix)s"' % {
3641 3641 b'make': b'make', # TODO: switch by option or environment?
3642 3642 b'prefix': self._installdir,
3643 3643 }
3644 3644 cwd = os.path.join(self._hgroot, b'contrib', b'chg')
3645 3645 vlog("# Running", cmd)
3646 3646 proc = subprocess.Popen(
3647 3647 cmd,
3648 3648 shell=True,
3649 3649 cwd=cwd,
3650 3650 stdin=subprocess.PIPE,
3651 3651 stdout=subprocess.PIPE,
3652 3652 stderr=subprocess.STDOUT,
3653 3653 )
3654 3654 out, _err = proc.communicate()
3655 3655 if proc.returncode != 0:
3656 3656 if PYTHON3:
3657 3657 sys.stdout.buffer.write(out)
3658 3658 else:
3659 3659 sys.stdout.write(out)
3660 3660 sys.exit(1)
3661 3661
3662 3662 def _outputcoverage(self):
3663 3663 """Produce code coverage output."""
3664 3664 import coverage
3665 3665
3666 3666 coverage = coverage.coverage
3667 3667
3668 3668 vlog('# Producing coverage report')
3669 3669 # chdir is the easiest way to get short, relative paths in the
3670 3670 # output.
3671 3671 os.chdir(self._hgroot)
3672 3672 covdir = os.path.join(_bytes2sys(self._installdir), '..', 'coverage')
3673 3673 cov = coverage(data_file=os.path.join(covdir, 'cov'))
3674 3674
3675 3675 # Map install directory paths back to source directory.
3676 3676 cov.config.paths['srcdir'] = ['.', _bytes2sys(self._pythondir)]
3677 3677
3678 3678 cov.combine()
3679 3679
3680 3680 omit = [
3681 3681 _bytes2sys(os.path.join(x, b'*'))
3682 3682 for x in [self._bindir, self._testdir]
3683 3683 ]
3684 3684 cov.report(ignore_errors=True, omit=omit)
3685 3685
3686 3686 if self.options.htmlcov:
3687 3687 htmldir = os.path.join(_bytes2sys(self._outputdir), 'htmlcov')
3688 3688 cov.html_report(directory=htmldir, omit=omit)
3689 3689 if self.options.annotate:
3690 3690 adir = os.path.join(_bytes2sys(self._outputdir), 'annotated')
3691 3691 if not os.path.isdir(adir):
3692 3692 os.mkdir(adir)
3693 3693 cov.annotate(directory=adir, omit=omit)
3694 3694
3695 3695 def _findprogram(self, program):
3696 3696 """Search PATH for a executable program"""
3697 3697 dpb = _sys2bytes(os.defpath)
3698 3698 sepb = _sys2bytes(os.pathsep)
3699 3699 for p in osenvironb.get(b'PATH', dpb).split(sepb):
3700 3700 name = os.path.join(p, program)
3701 3701 if os.name == 'nt' or os.access(name, os.X_OK):
3702 3702 return _bytes2sys(name)
3703 3703 return None
3704 3704
3705 3705 def _checktools(self):
3706 3706 """Ensure tools required to run tests are present."""
3707 3707 for p in self.REQUIREDTOOLS:
3708 3708 if os.name == 'nt' and not p.endswith(b'.exe'):
3709 3709 p += b'.exe'
3710 3710 found = self._findprogram(p)
3711 3711 p = p.decode("utf-8")
3712 3712 if found:
3713 3713 vlog("# Found prerequisite", p, "at", found)
3714 3714 else:
3715 3715 print("WARNING: Did not find prerequisite tool: %s " % p)
3716 3716
3717 3717
3718 3718 def aggregateexceptions(path):
3719 3719 exceptioncounts = collections.Counter()
3720 3720 testsbyfailure = collections.defaultdict(set)
3721 3721 failuresbytest = collections.defaultdict(set)
3722 3722
3723 3723 for f in os.listdir(path):
3724 3724 with open(os.path.join(path, f), 'rb') as fh:
3725 3725 data = fh.read().split(b'\0')
3726 3726 if len(data) != 5:
3727 3727 continue
3728 3728
3729 3729 exc, mainframe, hgframe, hgline, testname = data
3730 3730 exc = exc.decode('utf-8')
3731 3731 mainframe = mainframe.decode('utf-8')
3732 3732 hgframe = hgframe.decode('utf-8')
3733 3733 hgline = hgline.decode('utf-8')
3734 3734 testname = testname.decode('utf-8')
3735 3735
3736 3736 key = (hgframe, hgline, exc)
3737 3737 exceptioncounts[key] += 1
3738 3738 testsbyfailure[key].add(testname)
3739 3739 failuresbytest[testname].add(key)
3740 3740
3741 3741 # Find test having fewest failures for each failure.
3742 3742 leastfailing = {}
3743 3743 for key, tests in testsbyfailure.items():
3744 3744 fewesttest = None
3745 3745 fewestcount = 99999999
3746 3746 for test in sorted(tests):
3747 3747 if len(failuresbytest[test]) < fewestcount:
3748 3748 fewesttest = test
3749 3749 fewestcount = len(failuresbytest[test])
3750 3750
3751 3751 leastfailing[key] = (fewestcount, fewesttest)
3752 3752
3753 3753 # Create a combined counter so we can sort by total occurrences and
3754 3754 # impacted tests.
3755 3755 combined = {}
3756 3756 for key in exceptioncounts:
3757 3757 combined[key] = (
3758 3758 exceptioncounts[key],
3759 3759 len(testsbyfailure[key]),
3760 3760 leastfailing[key][0],
3761 3761 leastfailing[key][1],
3762 3762 )
3763 3763
3764 3764 return {
3765 3765 'exceptioncounts': exceptioncounts,
3766 3766 'total': sum(exceptioncounts.values()),
3767 3767 'combined': combined,
3768 3768 'leastfailing': leastfailing,
3769 3769 'byfailure': testsbyfailure,
3770 3770 'bytest': failuresbytest,
3771 3771 }
3772 3772
3773 3773
3774 3774 if __name__ == '__main__':
3775 3775 runner = TestRunner()
3776 3776
3777 3777 try:
3778 3778 import msvcrt
3779 3779
3780 3780 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
3781 3781 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
3782 3782 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
3783 3783 except ImportError:
3784 3784 pass
3785 3785
3786 3786 sys.exit(runner.run(sys.argv[1:]))
General Comments 0
You need to be logged in to leave comments. Login now