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