##// END OF EJS Templates
run-tests: write bytes to the binary buffer on sys.{stdout,stderr}
Augie Fackler -
r25053:4f2c74ef default
parent child Browse files
Show More
@@ -1,2165 +1,2170
1 1 #!/usr/bin/env python
2 2 #
3 3 # run-tests.py - Run a set of tests on Mercurial
4 4 #
5 5 # Copyright 2006 Matt Mackall <mpm@selenic.com>
6 6 #
7 7 # This software may be used and distributed according to the terms of the
8 8 # GNU General Public License version 2 or any later version.
9 9
10 10 # Modifying this script is tricky because it has many modes:
11 11 # - serial (default) vs parallel (-jN, N > 1)
12 12 # - no coverage (default) vs coverage (-c, -C, -s)
13 13 # - temp install (default) vs specific hg script (--with-hg, --local)
14 14 # - tests are a mix of shell scripts and Python scripts
15 15 #
16 16 # If you change this script, it is recommended that you ensure you
17 17 # haven't broken it by running it in various modes with a representative
18 18 # sample of test scripts. For example:
19 19 #
20 20 # 1) serial, no coverage, temp install:
21 21 # ./run-tests.py test-s*
22 22 # 2) serial, no coverage, local hg:
23 23 # ./run-tests.py --local test-s*
24 24 # 3) serial, coverage, temp install:
25 25 # ./run-tests.py -c test-s*
26 26 # 4) serial, coverage, local hg:
27 27 # ./run-tests.py -c --local test-s* # unsupported
28 28 # 5) parallel, no coverage, temp install:
29 29 # ./run-tests.py -j2 test-s*
30 30 # 6) parallel, no coverage, local hg:
31 31 # ./run-tests.py -j2 --local test-s*
32 32 # 7) parallel, coverage, temp install:
33 33 # ./run-tests.py -j2 -c test-s* # currently broken
34 34 # 8) parallel, coverage, local install:
35 35 # ./run-tests.py -j2 -c --local test-s* # unsupported (and broken)
36 36 # 9) parallel, custom tmp dir:
37 37 # ./run-tests.py -j2 --tmpdir /tmp/myhgtests
38 38 #
39 39 # (You could use any subset of the tests: test-s* happens to match
40 40 # enough that it's worth doing parallel runs, few enough that it
41 41 # completes fairly quickly, includes both shell and Python scripts, and
42 42 # includes some scripts that run daemon processes.)
43 43
44 44 from __future__ import print_function
45 45
46 46 from distutils import version
47 47 import difflib
48 48 import errno
49 49 import optparse
50 50 import os
51 51 import shutil
52 52 import subprocess
53 53 import signal
54 54 import socket
55 55 import sys
56 56 import tempfile
57 57 import time
58 58 import random
59 59 import re
60 60 import threading
61 61 import killdaemons as killmod
62 62 try:
63 63 import Queue as queue
64 64 except ImportError:
65 65 import queue
66 66 from xml.dom import minidom
67 67 import unittest
68 68
69 69 osenvironb = getattr(os, 'environb', os.environ)
70 70
71 71 try:
72 72 import json
73 73 except ImportError:
74 74 try:
75 75 import simplejson as json
76 76 except ImportError:
77 77 json = None
78 78
79 79 processlock = threading.Lock()
80 80
81 81 if sys.version_info > (3, 0, 0):
82 82 xrange = range # we use xrange in one place, and we'd rather not use range
83 83
84 84 # subprocess._cleanup can race with any Popen.wait or Popen.poll on py24
85 85 # http://bugs.python.org/issue1731717 for details. We shouldn't be producing
86 86 # zombies but it's pretty harmless even if we do.
87 87 if sys.version_info < (2, 5):
88 88 subprocess._cleanup = lambda: None
89 89
90 90 wifexited = getattr(os, "WIFEXITED", lambda x: False)
91 91
92 92 def checkportisavailable(port):
93 93 """return true if a port seems free to bind on localhost"""
94 94 try:
95 95 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
96 96 s.bind(('localhost', port))
97 97 s.close()
98 98 return True
99 99 except socket.error as exc:
100 100 if not exc.errno == errno.EADDRINUSE:
101 101 raise
102 102 return False
103 103
104 104 closefds = os.name == 'posix'
105 105 def Popen4(cmd, wd, timeout, env=None):
106 106 processlock.acquire()
107 107 p = subprocess.Popen(cmd, shell=True, bufsize=-1, cwd=wd, env=env,
108 108 close_fds=closefds,
109 109 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
110 110 stderr=subprocess.STDOUT)
111 111 processlock.release()
112 112
113 113 p.fromchild = p.stdout
114 114 p.tochild = p.stdin
115 115 p.childerr = p.stderr
116 116
117 117 p.timeout = False
118 118 if timeout:
119 119 def t():
120 120 start = time.time()
121 121 while time.time() - start < timeout and p.returncode is None:
122 122 time.sleep(.1)
123 123 p.timeout = True
124 124 if p.returncode is None:
125 125 terminate(p)
126 126 threading.Thread(target=t).start()
127 127
128 128 return p
129 129
130 130 PYTHON = sys.executable.replace('\\', '/')
131 131 IMPL_PATH = b'PYTHONPATH'
132 132 if 'java' in sys.platform:
133 133 IMPL_PATH = b'JYTHONPATH'
134 134
135 135 defaults = {
136 136 'jobs': ('HGTEST_JOBS', 1),
137 137 'timeout': ('HGTEST_TIMEOUT', 180),
138 138 'port': ('HGTEST_PORT', 20059),
139 139 'shell': ('HGTEST_SHELL', 'sh'),
140 140 }
141 141
142 142 def parselistfiles(files, listtype, warn=True):
143 143 entries = dict()
144 144 for filename in files:
145 145 try:
146 146 path = os.path.expanduser(os.path.expandvars(filename))
147 147 f = open(path, "rb")
148 148 except IOError as err:
149 149 if err.errno != errno.ENOENT:
150 150 raise
151 151 if warn:
152 152 print("warning: no such %s file: %s" % (listtype, filename))
153 153 continue
154 154
155 155 for line in f.readlines():
156 156 line = line.split(b'#', 1)[0].strip()
157 157 if line:
158 158 entries[line] = filename
159 159
160 160 f.close()
161 161 return entries
162 162
163 163 def getparser():
164 164 """Obtain the OptionParser used by the CLI."""
165 165 parser = optparse.OptionParser("%prog [options] [tests]")
166 166
167 167 # keep these sorted
168 168 parser.add_option("--blacklist", action="append",
169 169 help="skip tests listed in the specified blacklist file")
170 170 parser.add_option("--whitelist", action="append",
171 171 help="always run tests listed in the specified whitelist file")
172 172 parser.add_option("--changed", type="string",
173 173 help="run tests that are changed in parent rev or working directory")
174 174 parser.add_option("-C", "--annotate", action="store_true",
175 175 help="output files annotated with coverage")
176 176 parser.add_option("-c", "--cover", action="store_true",
177 177 help="print a test coverage report")
178 178 parser.add_option("-d", "--debug", action="store_true",
179 179 help="debug mode: write output of test scripts to console"
180 180 " rather than capturing and diffing it (disables timeout)")
181 181 parser.add_option("-f", "--first", action="store_true",
182 182 help="exit on the first test failure")
183 183 parser.add_option("-H", "--htmlcov", action="store_true",
184 184 help="create an HTML report of the coverage of the files")
185 185 parser.add_option("-i", "--interactive", action="store_true",
186 186 help="prompt to accept changed output")
187 187 parser.add_option("-j", "--jobs", type="int",
188 188 help="number of jobs to run in parallel"
189 189 " (default: $%s or %d)" % defaults['jobs'])
190 190 parser.add_option("--keep-tmpdir", action="store_true",
191 191 help="keep temporary directory after running tests")
192 192 parser.add_option("-k", "--keywords",
193 193 help="run tests matching keywords")
194 194 parser.add_option("-l", "--local", action="store_true",
195 195 help="shortcut for --with-hg=<testdir>/../hg")
196 196 parser.add_option("--loop", action="store_true",
197 197 help="loop tests repeatedly")
198 198 parser.add_option("--runs-per-test", type="int", dest="runs_per_test",
199 199 help="run each test N times (default=1)", default=1)
200 200 parser.add_option("-n", "--nodiff", action="store_true",
201 201 help="skip showing test changes")
202 202 parser.add_option("-p", "--port", type="int",
203 203 help="port on which servers should listen"
204 204 " (default: $%s or %d)" % defaults['port'])
205 205 parser.add_option("--compiler", type="string",
206 206 help="compiler to build with")
207 207 parser.add_option("--pure", action="store_true",
208 208 help="use pure Python code instead of C extensions")
209 209 parser.add_option("-R", "--restart", action="store_true",
210 210 help="restart at last error")
211 211 parser.add_option("-r", "--retest", action="store_true",
212 212 help="retest failed tests")
213 213 parser.add_option("-S", "--noskips", action="store_true",
214 214 help="don't report skip tests verbosely")
215 215 parser.add_option("--shell", type="string",
216 216 help="shell to use (default: $%s or %s)" % defaults['shell'])
217 217 parser.add_option("-t", "--timeout", type="int",
218 218 help="kill errant tests after TIMEOUT seconds"
219 219 " (default: $%s or %d)" % defaults['timeout'])
220 220 parser.add_option("--time", action="store_true",
221 221 help="time how long each test takes")
222 222 parser.add_option("--json", action="store_true",
223 223 help="store test result data in 'report.json' file")
224 224 parser.add_option("--tmpdir", type="string",
225 225 help="run tests in the given temporary directory"
226 226 " (implies --keep-tmpdir)")
227 227 parser.add_option("-v", "--verbose", action="store_true",
228 228 help="output verbose messages")
229 229 parser.add_option("--xunit", type="string",
230 230 help="record xunit results at specified path")
231 231 parser.add_option("--view", type="string",
232 232 help="external diff viewer")
233 233 parser.add_option("--with-hg", type="string",
234 234 metavar="HG",
235 235 help="test using specified hg script rather than a "
236 236 "temporary installation")
237 237 parser.add_option("-3", "--py3k-warnings", action="store_true",
238 238 help="enable Py3k warnings on Python 2.6+")
239 239 parser.add_option('--extra-config-opt', action="append",
240 240 help='set the given config opt in the test hgrc')
241 241 parser.add_option('--random', action="store_true",
242 242 help='run tests in random order')
243 243
244 244 for option, (envvar, default) in defaults.items():
245 245 defaults[option] = type(default)(os.environ.get(envvar, default))
246 246 parser.set_defaults(**defaults)
247 247
248 248 return parser
249 249
250 250 def parseargs(args, parser):
251 251 """Parse arguments with our OptionParser and validate results."""
252 252 (options, args) = parser.parse_args(args)
253 253
254 254 # jython is always pure
255 255 if 'java' in sys.platform or '__pypy__' in sys.modules:
256 256 options.pure = True
257 257
258 258 if options.with_hg:
259 259 options.with_hg = os.path.expanduser(options.with_hg)
260 260 if not (os.path.isfile(options.with_hg) and
261 261 os.access(options.with_hg, os.X_OK)):
262 262 parser.error('--with-hg must specify an executable hg script')
263 263 if not os.path.basename(options.with_hg) == 'hg':
264 264 sys.stderr.write('warning: --with-hg should specify an hg script\n')
265 265 if options.local:
266 266 testdir = os.path.dirname(os.path.realpath(sys.argv[0]).encode('utf-8'))
267 267 hgbin = os.path.join(os.path.dirname(testdir), b'hg')
268 268 if os.name != 'nt' and not os.access(hgbin, os.X_OK):
269 269 parser.error('--local specified, but %r not found or not executable'
270 270 % hgbin)
271 271 options.with_hg = hgbin
272 272
273 273 options.anycoverage = options.cover or options.annotate or options.htmlcov
274 274 if options.anycoverage:
275 275 try:
276 276 import coverage
277 277 covver = version.StrictVersion(coverage.__version__).version
278 278 if covver < (3, 3):
279 279 parser.error('coverage options require coverage 3.3 or later')
280 280 except ImportError:
281 281 parser.error('coverage options now require the coverage package')
282 282
283 283 if options.anycoverage and options.local:
284 284 # this needs some path mangling somewhere, I guess
285 285 parser.error("sorry, coverage options do not work when --local "
286 286 "is specified")
287 287
288 288 if options.anycoverage and options.with_hg:
289 289 parser.error("sorry, coverage options do not work when --with-hg "
290 290 "is specified")
291 291
292 292 global verbose
293 293 if options.verbose:
294 294 verbose = ''
295 295
296 296 if options.tmpdir:
297 297 options.tmpdir = os.path.expanduser(options.tmpdir)
298 298
299 299 if options.jobs < 1:
300 300 parser.error('--jobs must be positive')
301 301 if options.interactive and options.debug:
302 302 parser.error("-i/--interactive and -d/--debug are incompatible")
303 303 if options.debug:
304 304 if options.timeout != defaults['timeout']:
305 305 sys.stderr.write(
306 306 'warning: --timeout option ignored with --debug\n')
307 307 options.timeout = 0
308 308 if options.py3k_warnings:
309 309 if sys.version_info[:2] < (2, 6) or sys.version_info[:2] >= (3, 0):
310 310 parser.error('--py3k-warnings can only be used on Python 2.6+')
311 311 if options.blacklist:
312 312 options.blacklist = parselistfiles(options.blacklist, 'blacklist')
313 313 if options.whitelist:
314 314 options.whitelisted = parselistfiles(options.whitelist, 'whitelist')
315 315 else:
316 316 options.whitelisted = {}
317 317
318 318 return (options, args)
319 319
320 320 def rename(src, dst):
321 321 """Like os.rename(), trade atomicity and opened files friendliness
322 322 for existing destination support.
323 323 """
324 324 shutil.copy(src, dst)
325 325 os.remove(src)
326 326
327 327 _unified_diff = difflib.unified_diff
328 328 if sys.version_info[0] > 2:
329 329 import functools
330 330 _unified_diff = functools.partial(difflib.diff_bytes, difflib.unified_diff)
331 331
332 332 def getdiff(expected, output, ref, err):
333 333 servefail = False
334 334 lines = []
335 335 for line in _unified_diff(expected, output, ref, err):
336 336 if line.startswith(b'+++') or line.startswith(b'---'):
337 337 line = line.replace(b'\\', b'/')
338 338 if line.endswith(b' \n'):
339 339 line = line[:-2] + b'\n'
340 340 lines.append(line)
341 341 if not servefail and line.startswith(
342 342 b'+ abort: child process failed to start'):
343 343 servefail = True
344 344
345 345 return servefail, lines
346 346
347 347 verbose = False
348 348 def vlog(*msg):
349 349 """Log only when in verbose mode."""
350 350 if verbose is False:
351 351 return
352 352
353 353 return log(*msg)
354 354
355 355 # Bytes that break XML even in a CDATA block: control characters 0-31
356 356 # sans \t, \n and \r
357 357 CDATA_EVIL = re.compile(br"[\000-\010\013\014\016-\037]")
358 358
359 359 def cdatasafe(data):
360 360 """Make a string safe to include in a CDATA block.
361 361
362 362 Certain control characters are illegal in a CDATA block, and
363 363 there's no way to include a ]]> in a CDATA either. This function
364 364 replaces illegal bytes with ? and adds a space between the ]] so
365 365 that it won't break the CDATA block.
366 366 """
367 367 return CDATA_EVIL.sub(b'?', data).replace(b']]>', b'] ]>')
368 368
369 369 def log(*msg):
370 370 """Log something to stdout.
371 371
372 372 Arguments are strings to print.
373 373 """
374 374 with iolock:
375 375 if verbose:
376 376 print(verbose, end=' ')
377 377 for m in msg:
378 378 print(m, end=' ')
379 379 print()
380 380 sys.stdout.flush()
381 381
382 382 def terminate(proc):
383 383 """Terminate subprocess (with fallback for Python versions < 2.6)"""
384 384 vlog('# Terminating process %d' % proc.pid)
385 385 try:
386 386 getattr(proc, 'terminate', lambda : os.kill(proc.pid, signal.SIGTERM))()
387 387 except OSError:
388 388 pass
389 389
390 390 def killdaemons(pidfile):
391 391 return killmod.killdaemons(pidfile, tryhard=False, remove=True,
392 392 logfn=vlog)
393 393
394 394 class Test(unittest.TestCase):
395 395 """Encapsulates a single, runnable test.
396 396
397 397 While this class conforms to the unittest.TestCase API, it differs in that
398 398 instances need to be instantiated manually. (Typically, unittest.TestCase
399 399 classes are instantiated automatically by scanning modules.)
400 400 """
401 401
402 402 # Status code reserved for skipped tests (used by hghave).
403 403 SKIPPED_STATUS = 80
404 404
405 405 def __init__(self, path, tmpdir, keeptmpdir=False,
406 406 debug=False,
407 407 timeout=defaults['timeout'],
408 408 startport=defaults['port'], extraconfigopts=None,
409 409 py3kwarnings=False, shell=None):
410 410 """Create a test from parameters.
411 411
412 412 path is the full path to the file defining the test.
413 413
414 414 tmpdir is the main temporary directory to use for this test.
415 415
416 416 keeptmpdir determines whether to keep the test's temporary directory
417 417 after execution. It defaults to removal (False).
418 418
419 419 debug mode will make the test execute verbosely, with unfiltered
420 420 output.
421 421
422 422 timeout controls the maximum run time of the test. It is ignored when
423 423 debug is True.
424 424
425 425 startport controls the starting port number to use for this test. Each
426 426 test will reserve 3 port numbers for execution. It is the caller's
427 427 responsibility to allocate a non-overlapping port range to Test
428 428 instances.
429 429
430 430 extraconfigopts is an iterable of extra hgrc config options. Values
431 431 must have the form "key=value" (something understood by hgrc). Values
432 432 of the form "foo.key=value" will result in "[foo] key=value".
433 433
434 434 py3kwarnings enables Py3k warnings.
435 435
436 436 shell is the shell to execute tests in.
437 437 """
438 438 self.path = path
439 439 self.bname = os.path.basename(path)
440 440 self.name = self.bname.decode('utf-8')
441 441 self._testdir = os.path.dirname(path)
442 442 self.errpath = os.path.join(self._testdir, b'%s.err' % self.bname)
443 443
444 444 self._threadtmp = tmpdir
445 445 self._keeptmpdir = keeptmpdir
446 446 self._debug = debug
447 447 self._timeout = timeout
448 448 self._startport = startport
449 449 self._extraconfigopts = extraconfigopts or []
450 450 self._py3kwarnings = py3kwarnings
451 451 self._shell = shell.encode('utf-8')
452 452
453 453 self._aborted = False
454 454 self._daemonpids = []
455 455 self._finished = None
456 456 self._ret = None
457 457 self._out = None
458 458 self._skipped = None
459 459 self._testtmp = None
460 460
461 461 # If we're not in --debug mode and reference output file exists,
462 462 # check test output against it.
463 463 if debug:
464 464 self._refout = None # to match "out is None"
465 465 elif os.path.exists(self.refpath):
466 466 f = open(self.refpath, 'rb')
467 467 self._refout = f.read().splitlines(True)
468 468 f.close()
469 469 else:
470 470 self._refout = []
471 471
472 472 # needed to get base class __repr__ running
473 473 @property
474 474 def _testMethodName(self):
475 475 return self.name
476 476
477 477 def __str__(self):
478 478 return self.name
479 479
480 480 def shortDescription(self):
481 481 return self.name
482 482
483 483 def setUp(self):
484 484 """Tasks to perform before run()."""
485 485 self._finished = False
486 486 self._ret = None
487 487 self._out = None
488 488 self._skipped = None
489 489
490 490 try:
491 491 os.mkdir(self._threadtmp)
492 492 except OSError as e:
493 493 if e.errno != errno.EEXIST:
494 494 raise
495 495
496 496 self._testtmp = os.path.join(self._threadtmp,
497 497 os.path.basename(self.path))
498 498 os.mkdir(self._testtmp)
499 499
500 500 # Remove any previous output files.
501 501 if os.path.exists(self.errpath):
502 502 try:
503 503 os.remove(self.errpath)
504 504 except OSError as e:
505 505 # We might have raced another test to clean up a .err
506 506 # file, so ignore ENOENT when removing a previous .err
507 507 # file.
508 508 if e.errno != errno.ENOENT:
509 509 raise
510 510
511 511 def run(self, result):
512 512 """Run this test and report results against a TestResult instance."""
513 513 # This function is extremely similar to unittest.TestCase.run(). Once
514 514 # we require Python 2.7 (or at least its version of unittest), this
515 515 # function can largely go away.
516 516 self._result = result
517 517 result.startTest(self)
518 518 try:
519 519 try:
520 520 self.setUp()
521 521 except (KeyboardInterrupt, SystemExit):
522 522 self._aborted = True
523 523 raise
524 524 except Exception:
525 525 result.addError(self, sys.exc_info())
526 526 return
527 527
528 528 success = False
529 529 try:
530 530 self.runTest()
531 531 except KeyboardInterrupt:
532 532 self._aborted = True
533 533 raise
534 534 except SkipTest as e:
535 535 result.addSkip(self, str(e))
536 536 # The base class will have already counted this as a
537 537 # test we "ran", but we want to exclude skipped tests
538 538 # from those we count towards those run.
539 539 result.testsRun -= 1
540 540 except IgnoreTest as e:
541 541 result.addIgnore(self, str(e))
542 542 # As with skips, ignores also should be excluded from
543 543 # the number of tests executed.
544 544 result.testsRun -= 1
545 545 except WarnTest as e:
546 546 result.addWarn(self, str(e))
547 547 except self.failureException as e:
548 548 # This differs from unittest in that we don't capture
549 549 # the stack trace. This is for historical reasons and
550 550 # this decision could be revisited in the future,
551 551 # especially for PythonTest instances.
552 552 if result.addFailure(self, str(e)):
553 553 success = True
554 554 except Exception:
555 555 result.addError(self, sys.exc_info())
556 556 else:
557 557 success = True
558 558
559 559 try:
560 560 self.tearDown()
561 561 except (KeyboardInterrupt, SystemExit):
562 562 self._aborted = True
563 563 raise
564 564 except Exception:
565 565 result.addError(self, sys.exc_info())
566 566 success = False
567 567
568 568 if success:
569 569 result.addSuccess(self)
570 570 finally:
571 571 result.stopTest(self, interrupted=self._aborted)
572 572
573 573 def runTest(self):
574 574 """Run this test instance.
575 575
576 576 This will return a tuple describing the result of the test.
577 577 """
578 578 env = self._getenv()
579 579 self._daemonpids.append(env['DAEMON_PIDS'])
580 580 self._createhgrc(env['HGRCPATH'])
581 581
582 582 vlog('# Test', self.name)
583 583
584 584 ret, out = self._run(env)
585 585 self._finished = True
586 586 self._ret = ret
587 587 self._out = out
588 588
589 589 def describe(ret):
590 590 if ret < 0:
591 591 return 'killed by signal: %d' % -ret
592 592 return 'returned error code %d' % ret
593 593
594 594 self._skipped = False
595 595
596 596 if ret == self.SKIPPED_STATUS:
597 597 if out is None: # Debug mode, nothing to parse.
598 598 missing = ['unknown']
599 599 failed = None
600 600 else:
601 601 missing, failed = TTest.parsehghaveoutput(out)
602 602
603 603 if not missing:
604 604 missing = ['skipped']
605 605
606 606 if failed:
607 607 self.fail('hg have failed checking for %s' % failed[-1])
608 608 else:
609 609 self._skipped = True
610 610 raise SkipTest(missing[-1])
611 611 elif ret == 'timeout':
612 612 self.fail('timed out')
613 613 elif ret is False:
614 614 raise WarnTest('no result code from test')
615 615 elif out != self._refout:
616 616 # Diff generation may rely on written .err file.
617 617 if (ret != 0 or out != self._refout) and not self._skipped \
618 618 and not self._debug:
619 619 f = open(self.errpath, 'wb')
620 620 for line in out:
621 621 f.write(line)
622 622 f.close()
623 623
624 624 # The result object handles diff calculation for us.
625 625 if self._result.addOutputMismatch(self, ret, out, self._refout):
626 626 # change was accepted, skip failing
627 627 return
628 628
629 629 if ret:
630 630 msg = 'output changed and ' + describe(ret)
631 631 else:
632 632 msg = 'output changed'
633 633
634 634 self.fail(msg)
635 635 elif ret:
636 636 self.fail(describe(ret))
637 637
638 638 def tearDown(self):
639 639 """Tasks to perform after run()."""
640 640 for entry in self._daemonpids:
641 641 killdaemons(entry)
642 642 self._daemonpids = []
643 643
644 644 if not self._keeptmpdir:
645 645 shutil.rmtree(self._testtmp, True)
646 646 shutil.rmtree(self._threadtmp, True)
647 647
648 648 if (self._ret != 0 or self._out != self._refout) and not self._skipped \
649 649 and not self._debug and self._out:
650 650 f = open(self.errpath, 'wb')
651 651 for line in self._out:
652 652 f.write(line)
653 653 f.close()
654 654
655 655 vlog("# Ret was:", self._ret, '(%s)' % self.name)
656 656
657 657 def _run(self, env):
658 658 # This should be implemented in child classes to run tests.
659 659 raise SkipTest('unknown test type')
660 660
661 661 def abort(self):
662 662 """Terminate execution of this test."""
663 663 self._aborted = True
664 664
665 665 def _getreplacements(self):
666 666 """Obtain a mapping of text replacements to apply to test output.
667 667
668 668 Test output needs to be normalized so it can be compared to expected
669 669 output. This function defines how some of that normalization will
670 670 occur.
671 671 """
672 672 r = [
673 673 (br':%d\b' % self._startport, b':$HGPORT'),
674 674 (br':%d\b' % (self._startport + 1), b':$HGPORT1'),
675 675 (br':%d\b' % (self._startport + 2), b':$HGPORT2'),
676 676 (br'(?m)^(saved backup bundle to .*\.hg)( \(glob\))?$',
677 677 br'\1 (glob)'),
678 678 ]
679 679
680 680 if os.name == 'nt':
681 681 r.append(
682 682 (b''.join(c.isalpha() and b'[%s%s]' % (c.lower(), c.upper()) or
683 683 c in b'/\\' and br'[/\\]' or c.isdigit() and c or b'\\' + c
684 684 for c in self._testtmp), b'$TESTTMP'))
685 685 else:
686 686 r.append((re.escape(self._testtmp), b'$TESTTMP'))
687 687
688 688 return r
689 689
690 690 def _getenv(self):
691 691 """Obtain environment variables to use during test execution."""
692 692 env = os.environ.copy()
693 693 env['TESTTMP'] = self._testtmp
694 694 env['HOME'] = self._testtmp
695 695 env["HGPORT"] = str(self._startport)
696 696 env["HGPORT1"] = str(self._startport + 1)
697 697 env["HGPORT2"] = str(self._startport + 2)
698 698 env["HGRCPATH"] = os.path.join(self._threadtmp, b'.hgrc')
699 699 env["DAEMON_PIDS"] = os.path.join(self._threadtmp, b'daemon.pids')
700 700 env["HGEDITOR"] = ('"' + sys.executable + '"'
701 701 + ' -c "import sys; sys.exit(0)"')
702 702 env["HGMERGE"] = "internal:merge"
703 703 env["HGUSER"] = "test"
704 704 env["HGENCODING"] = "ascii"
705 705 env["HGENCODINGMODE"] = "strict"
706 706
707 707 # Reset some environment variables to well-known values so that
708 708 # the tests produce repeatable output.
709 709 env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C'
710 710 env['TZ'] = 'GMT'
711 711 env["EMAIL"] = "Foo Bar <foo.bar@example.com>"
712 712 env['COLUMNS'] = '80'
713 713 env['TERM'] = 'xterm'
714 714
715 715 for k in ('HG HGPROF CDPATH GREP_OPTIONS http_proxy no_proxy ' +
716 716 'NO_PROXY').split():
717 717 if k in env:
718 718 del env[k]
719 719
720 720 # unset env related to hooks
721 721 for k in env.keys():
722 722 if k.startswith('HG_'):
723 723 del env[k]
724 724
725 725 return env
726 726
727 727 def _createhgrc(self, path):
728 728 """Create an hgrc file for this test."""
729 729 hgrc = open(path, 'wb')
730 730 hgrc.write(b'[ui]\n')
731 731 hgrc.write(b'slash = True\n')
732 732 hgrc.write(b'interactive = False\n')
733 733 hgrc.write(b'mergemarkers = detailed\n')
734 734 hgrc.write(b'promptecho = True\n')
735 735 hgrc.write(b'[defaults]\n')
736 736 hgrc.write(b'backout = -d "0 0"\n')
737 737 hgrc.write(b'commit = -d "0 0"\n')
738 738 hgrc.write(b'shelve = --date "0 0"\n')
739 739 hgrc.write(b'tag = -d "0 0"\n')
740 740 hgrc.write(b'[devel]\n')
741 741 hgrc.write(b'all = true\n')
742 742 hgrc.write(b'[largefiles]\n')
743 743 hgrc.write(b'usercache = %s\n' %
744 744 (os.path.join(self._testtmp, b'.cache/largefiles')))
745 745
746 746 for opt in self._extraconfigopts:
747 747 section, key = opt.split('.', 1)
748 748 assert '=' in key, ('extra config opt %s must '
749 749 'have an = for assignment' % opt)
750 750 hgrc.write(b'[%s]\n%s\n' % (section, key))
751 751 hgrc.close()
752 752
753 753 def fail(self, msg):
754 754 # unittest differentiates between errored and failed.
755 755 # Failed is denoted by AssertionError (by default at least).
756 756 raise AssertionError(msg)
757 757
758 758 def _runcommand(self, cmd, env, normalizenewlines=False):
759 759 """Run command in a sub-process, capturing the output (stdout and
760 760 stderr).
761 761
762 762 Return a tuple (exitcode, output). output is None in debug mode.
763 763 """
764 764 if self._debug:
765 765 proc = subprocess.Popen(cmd, shell=True, cwd=self._testtmp,
766 766 env=env)
767 767 ret = proc.wait()
768 768 return (ret, None)
769 769
770 770 proc = Popen4(cmd, self._testtmp, self._timeout, env)
771 771 def cleanup():
772 772 terminate(proc)
773 773 ret = proc.wait()
774 774 if ret == 0:
775 775 ret = signal.SIGTERM << 8
776 776 killdaemons(env['DAEMON_PIDS'])
777 777 return ret
778 778
779 779 output = ''
780 780 proc.tochild.close()
781 781
782 782 try:
783 783 output = proc.fromchild.read()
784 784 except KeyboardInterrupt:
785 785 vlog('# Handling keyboard interrupt')
786 786 cleanup()
787 787 raise
788 788
789 789 ret = proc.wait()
790 790 if wifexited(ret):
791 791 ret = os.WEXITSTATUS(ret)
792 792
793 793 if proc.timeout:
794 794 ret = 'timeout'
795 795
796 796 if ret:
797 797 killdaemons(env['DAEMON_PIDS'])
798 798
799 799 for s, r in self._getreplacements():
800 800 output = re.sub(s, r, output)
801 801
802 802 if normalizenewlines:
803 803 output = output.replace('\r\n', '\n')
804 804
805 805 return ret, output.splitlines(True)
806 806
807 807 class PythonTest(Test):
808 808 """A Python-based test."""
809 809
810 810 @property
811 811 def refpath(self):
812 812 return os.path.join(self._testdir, '%s.out' % self.name)
813 813
814 814 def _run(self, env):
815 815 py3kswitch = self._py3kwarnings and ' -3' or ''
816 816 cmd = '%s%s "%s"' % (PYTHON, py3kswitch, self.path)
817 817 vlog("# Running", cmd)
818 818 normalizenewlines = os.name == 'nt'
819 819 result = self._runcommand(cmd, env,
820 820 normalizenewlines=normalizenewlines)
821 821 if self._aborted:
822 822 raise KeyboardInterrupt()
823 823
824 824 return result
825 825
826 826 # This script may want to drop globs from lines matching these patterns on
827 827 # Windows, but check-code.py wants a glob on these lines unconditionally. Don't
828 828 # warn if that is the case for anything matching these lines.
829 829 checkcodeglobpats = [
830 830 re.compile(r'^pushing to \$TESTTMP/.*[^)]$'),
831 831 re.compile(r'^moving \S+/.*[^)]$'),
832 832 re.compile(r'^pulling from \$TESTTMP/.*[^)]$')
833 833 ]
834 834
835 835 bchr = chr
836 836 if sys.version_info[0] == 3:
837 837 bchr = lambda x: bytes([x])
838 838
839 839 class TTest(Test):
840 840 """A "t test" is a test backed by a .t file."""
841 841
842 842 SKIPPED_PREFIX = 'skipped: '
843 843 FAILED_PREFIX = 'hghave check failed: '
844 844 NEEDESCAPE = re.compile(br'[\x00-\x08\x0b-\x1f\x7f-\xff]').search
845 845
846 846 ESCAPESUB = re.compile(br'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub
847 847 ESCAPEMAP = dict((bchr(i), br'\x%02x' % i) for i in range(256))
848 848 ESCAPEMAP.update({b'\\': b'\\\\', b'\r': br'\r'})
849 849
850 850 @property
851 851 def refpath(self):
852 852 return os.path.join(self._testdir, self.bname)
853 853
854 854 def _run(self, env):
855 855 f = open(self.path, 'rb')
856 856 lines = f.readlines()
857 857 f.close()
858 858
859 859 salt, script, after, expected = self._parsetest(lines)
860 860
861 861 # Write out the generated script.
862 862 fname = b'%s.sh' % self._testtmp
863 863 f = open(fname, 'wb')
864 864 for l in script:
865 865 f.write(l)
866 866 f.close()
867 867
868 868 cmd = b'%s "%s"' % (self._shell, fname)
869 869 vlog("# Running", cmd)
870 870
871 871 exitcode, output = self._runcommand(cmd, env)
872 872
873 873 if self._aborted:
874 874 raise KeyboardInterrupt()
875 875
876 876 # Do not merge output if skipped. Return hghave message instead.
877 877 # Similarly, with --debug, output is None.
878 878 if exitcode == self.SKIPPED_STATUS or output is None:
879 879 return exitcode, output
880 880
881 881 return self._processoutput(exitcode, output, salt, after, expected)
882 882
883 883 def _hghave(self, reqs):
884 884 # TODO do something smarter when all other uses of hghave are gone.
885 885 tdir = self._testdir.replace(b'\\', b'/')
886 886 proc = Popen4(b'%s -c "%s/hghave %s"' %
887 887 (self._shell, tdir, b' '.join(reqs)),
888 888 self._testtmp, 0, self._getenv())
889 889 stdout, stderr = proc.communicate()
890 890 ret = proc.wait()
891 891 if wifexited(ret):
892 892 ret = os.WEXITSTATUS(ret)
893 893 if ret == 2:
894 894 print(stdout)
895 895 sys.exit(1)
896 896
897 897 return ret == 0
898 898
899 899 def _parsetest(self, lines):
900 900 # We generate a shell script which outputs unique markers to line
901 901 # up script results with our source. These markers include input
902 902 # line number and the last return code.
903 903 salt = b"SALT%d" % time.time()
904 904 def addsalt(line, inpython):
905 905 if inpython:
906 906 script.append(b'%s %d 0\n' % (salt, line))
907 907 else:
908 908 script.append(b'echo %s %d $?\n' % (salt, line))
909 909
910 910 script = []
911 911
912 912 # After we run the shell script, we re-unify the script output
913 913 # with non-active parts of the source, with synchronization by our
914 914 # SALT line number markers. The after table contains the non-active
915 915 # components, ordered by line number.
916 916 after = {}
917 917
918 918 # Expected shell script output.
919 919 expected = {}
920 920
921 921 pos = prepos = -1
922 922
923 923 # True or False when in a true or false conditional section
924 924 skipping = None
925 925
926 926 # We keep track of whether or not we're in a Python block so we
927 927 # can generate the surrounding doctest magic.
928 928 inpython = False
929 929
930 930 if self._debug:
931 931 script.append('set -x\n')
932 932 if os.getenv('MSYSTEM'):
933 933 script.append('alias pwd="pwd -W"\n')
934 934
935 935 for n, l in enumerate(lines):
936 936 if not l.endswith(b'\n'):
937 937 l += b'\n'
938 938 if l.startswith(b'#require'):
939 939 lsplit = l.split()
940 940 if len(lsplit) < 2 or lsplit[0] != b'#require':
941 941 after.setdefault(pos, []).append(' !!! invalid #require\n')
942 942 if not self._hghave(lsplit[1:]):
943 943 script = ["exit 80\n"]
944 944 break
945 945 after.setdefault(pos, []).append(l)
946 946 elif l.startswith(b'#if'):
947 947 lsplit = l.split()
948 948 if len(lsplit) < 2 or lsplit[0] != b'#if':
949 949 after.setdefault(pos, []).append(' !!! invalid #if\n')
950 950 if skipping is not None:
951 951 after.setdefault(pos, []).append(' !!! nested #if\n')
952 952 skipping = not self._hghave(lsplit[1:])
953 953 after.setdefault(pos, []).append(l)
954 954 elif l.startswith(b'#else'):
955 955 if skipping is None:
956 956 after.setdefault(pos, []).append(' !!! missing #if\n')
957 957 skipping = not skipping
958 958 after.setdefault(pos, []).append(l)
959 959 elif l.startswith(b'#endif'):
960 960 if skipping is None:
961 961 after.setdefault(pos, []).append(' !!! missing #if\n')
962 962 skipping = None
963 963 after.setdefault(pos, []).append(l)
964 964 elif skipping:
965 965 after.setdefault(pos, []).append(l)
966 966 elif l.startswith(b' >>> '): # python inlines
967 967 after.setdefault(pos, []).append(l)
968 968 prepos = pos
969 969 pos = n
970 970 if not inpython:
971 971 # We've just entered a Python block. Add the header.
972 972 inpython = True
973 973 addsalt(prepos, False) # Make sure we report the exit code.
974 974 script.append('%s -m heredoctest <<EOF\n' % PYTHON)
975 975 addsalt(n, True)
976 976 script.append(l[2:])
977 977 elif l.startswith(b' ... '): # python inlines
978 978 after.setdefault(prepos, []).append(l)
979 979 script.append(l[2:])
980 980 elif l.startswith(b' $ '): # commands
981 981 if inpython:
982 982 script.append('EOF\n')
983 983 inpython = False
984 984 after.setdefault(pos, []).append(l)
985 985 prepos = pos
986 986 pos = n
987 987 addsalt(n, False)
988 988 cmd = l[4:].split()
989 989 if len(cmd) == 2 and cmd[0] == 'cd':
990 990 l = ' $ cd %s || exit 1\n' % cmd[1]
991 991 script.append(l[4:])
992 992 elif l.startswith(b' > '): # continuations
993 993 after.setdefault(prepos, []).append(l)
994 994 script.append(l[4:])
995 995 elif l.startswith(b' '): # results
996 996 # Queue up a list of expected results.
997 997 expected.setdefault(pos, []).append(l[2:])
998 998 else:
999 999 if inpython:
1000 1000 script.append('EOF\n')
1001 1001 inpython = False
1002 1002 # Non-command/result. Queue up for merged output.
1003 1003 after.setdefault(pos, []).append(l)
1004 1004
1005 1005 if inpython:
1006 1006 script.append('EOF\n')
1007 1007 if skipping is not None:
1008 1008 after.setdefault(pos, []).append(' !!! missing #endif\n')
1009 1009 addsalt(n + 1, False)
1010 1010
1011 1011 return salt, script, after, expected
1012 1012
1013 1013 def _processoutput(self, exitcode, output, salt, after, expected):
1014 1014 # Merge the script output back into a unified test.
1015 1015 warnonly = 1 # 1: not yet; 2: yes; 3: for sure not
1016 1016 if exitcode != 0:
1017 1017 warnonly = 3
1018 1018
1019 1019 pos = -1
1020 1020 postout = []
1021 1021 for l in output:
1022 1022 lout, lcmd = l, None
1023 1023 if salt in l:
1024 1024 lout, lcmd = l.split(salt, 1)
1025 1025
1026 1026 if lout:
1027 1027 if not lout.endswith(b'\n'):
1028 1028 lout += b' (no-eol)\n'
1029 1029
1030 1030 # Find the expected output at the current position.
1031 1031 el = None
1032 1032 if expected.get(pos, None):
1033 1033 el = expected[pos].pop(0)
1034 1034
1035 1035 r = TTest.linematch(el, lout)
1036 1036 if isinstance(r, str):
1037 1037 if r == '+glob':
1038 1038 lout = el[:-1] + ' (glob)\n'
1039 1039 r = '' # Warn only this line.
1040 1040 elif r == '-glob':
1041 1041 lout = ''.join(el.rsplit(' (glob)', 1))
1042 1042 r = '' # Warn only this line.
1043 1043 else:
1044 1044 log('\ninfo, unknown linematch result: %r\n' % r)
1045 1045 r = False
1046 1046 if r:
1047 1047 postout.append(b' ' + el)
1048 1048 else:
1049 1049 if self.NEEDESCAPE(lout):
1050 1050 lout = TTest._stringescape(b'%s (esc)\n' %
1051 1051 lout.rstrip(b'\n'))
1052 1052 postout.append(b' ' + lout) # Let diff deal with it.
1053 1053 if r != '': # If line failed.
1054 1054 warnonly = 3 # for sure not
1055 1055 elif warnonly == 1: # Is "not yet" and line is warn only.
1056 1056 warnonly = 2 # Yes do warn.
1057 1057
1058 1058 if lcmd:
1059 1059 # Add on last return code.
1060 1060 ret = int(lcmd.split()[1])
1061 1061 if ret != 0:
1062 1062 postout.append(b' [%d]\n' % ret)
1063 1063 if pos in after:
1064 1064 # Merge in non-active test bits.
1065 1065 postout += after.pop(pos)
1066 1066 pos = int(lcmd.split()[0])
1067 1067
1068 1068 if pos in after:
1069 1069 postout += after.pop(pos)
1070 1070
1071 1071 if warnonly == 2:
1072 1072 exitcode = False # Set exitcode to warned.
1073 1073
1074 1074 return exitcode, postout
1075 1075
1076 1076 @staticmethod
1077 1077 def rematch(el, l):
1078 1078 try:
1079 1079 # use \Z to ensure that the regex matches to the end of the string
1080 1080 if os.name == 'nt':
1081 1081 return re.match(el + br'\r?\n\Z', l)
1082 1082 return re.match(el + br'\n\Z', l)
1083 1083 except re.error:
1084 1084 # el is an invalid regex
1085 1085 return False
1086 1086
1087 1087 @staticmethod
1088 1088 def globmatch(el, l):
1089 1089 # The only supported special characters are * and ? plus / which also
1090 1090 # matches \ on windows. Escaping of these characters is supported.
1091 1091 if el + b'\n' == l:
1092 1092 if os.altsep:
1093 1093 # matching on "/" is not needed for this line
1094 1094 for pat in checkcodeglobpats:
1095 1095 if pat.match(el):
1096 1096 return True
1097 1097 return b'-glob'
1098 1098 return True
1099 1099 i, n = 0, len(el)
1100 1100 res = b''
1101 1101 while i < n:
1102 1102 c = el[i:i + 1]
1103 1103 i += 1
1104 1104 if c == b'\\' and i < n and el[i:i + 1] in b'*?\\/':
1105 1105 res += el[i - 1:i + 1]
1106 1106 i += 1
1107 1107 elif c == b'*':
1108 1108 res += b'.*'
1109 1109 elif c == b'?':
1110 1110 res += b'.'
1111 1111 elif c == b'/' and os.altsep:
1112 1112 res += b'[/\\\\]'
1113 1113 else:
1114 1114 res += re.escape(c)
1115 1115 return TTest.rematch(res, l)
1116 1116
1117 1117 @staticmethod
1118 1118 def linematch(el, l):
1119 1119 if el == l: # perfect match (fast)
1120 1120 return True
1121 1121 if el:
1122 1122 if el.endswith(b" (esc)\n"):
1123 1123 if sys.version_info[0] == 3:
1124 1124 el = el[:-7].decode('unicode_escape') + '\n'
1125 1125 el = el.encode('utf-8')
1126 1126 else:
1127 1127 el = el[:-7].decode('string-escape') + '\n'
1128 1128 if el == l or os.name == 'nt' and el[:-1] + b'\r\n' == l:
1129 1129 return True
1130 1130 if el.endswith(b" (re)\n"):
1131 1131 return TTest.rematch(el[:-6], l)
1132 1132 if el.endswith(b" (glob)\n"):
1133 1133 # ignore '(glob)' added to l by 'replacements'
1134 1134 if l.endswith(b" (glob)\n"):
1135 1135 l = l[:-8] + b"\n"
1136 1136 return TTest.globmatch(el[:-8], l)
1137 1137 if os.altsep and l.replace(b'\\', b'/') == el:
1138 1138 return b'+glob'
1139 1139 return False
1140 1140
1141 1141 @staticmethod
1142 1142 def parsehghaveoutput(lines):
1143 1143 '''Parse hghave log lines.
1144 1144
1145 1145 Return tuple of lists (missing, failed):
1146 1146 * the missing/unknown features
1147 1147 * the features for which existence check failed'''
1148 1148 missing = []
1149 1149 failed = []
1150 1150 for line in lines:
1151 1151 if line.startswith(TTest.SKIPPED_PREFIX):
1152 1152 line = line.splitlines()[0]
1153 1153 missing.append(line[len(TTest.SKIPPED_PREFIX):])
1154 1154 elif line.startswith(TTest.FAILED_PREFIX):
1155 1155 line = line.splitlines()[0]
1156 1156 failed.append(line[len(TTest.FAILED_PREFIX):])
1157 1157
1158 1158 return missing, failed
1159 1159
1160 1160 @staticmethod
1161 1161 def _escapef(m):
1162 1162 return TTest.ESCAPEMAP[m.group(0)]
1163 1163
1164 1164 @staticmethod
1165 1165 def _stringescape(s):
1166 1166 return TTest.ESCAPESUB(TTest._escapef, s)
1167 1167
1168 1168 iolock = threading.RLock()
1169 1169
1170 1170 class SkipTest(Exception):
1171 1171 """Raised to indicate that a test is to be skipped."""
1172 1172
1173 1173 class IgnoreTest(Exception):
1174 1174 """Raised to indicate that a test is to be ignored."""
1175 1175
1176 1176 class WarnTest(Exception):
1177 1177 """Raised to indicate that a test warned."""
1178 1178
1179 1179 class TestResult(unittest._TextTestResult):
1180 1180 """Holds results when executing via unittest."""
1181 1181 # Don't worry too much about accessing the non-public _TextTestResult.
1182 1182 # It is relatively common in Python testing tools.
1183 1183 def __init__(self, options, *args, **kwargs):
1184 1184 super(TestResult, self).__init__(*args, **kwargs)
1185 1185
1186 1186 self._options = options
1187 1187
1188 1188 # unittest.TestResult didn't have skipped until 2.7. We need to
1189 1189 # polyfill it.
1190 1190 self.skipped = []
1191 1191
1192 1192 # We have a custom "ignored" result that isn't present in any Python
1193 1193 # unittest implementation. It is very similar to skipped. It may make
1194 1194 # sense to map it into skip some day.
1195 1195 self.ignored = []
1196 1196
1197 1197 # We have a custom "warned" result that isn't present in any Python
1198 1198 # unittest implementation. It is very similar to failed. It may make
1199 1199 # sense to map it into fail some day.
1200 1200 self.warned = []
1201 1201
1202 1202 self.times = []
1203 1203 # Data stored for the benefit of generating xunit reports.
1204 1204 self.successes = []
1205 1205 self.faildata = {}
1206 1206
1207 1207 def addFailure(self, test, reason):
1208 1208 self.failures.append((test, reason))
1209 1209
1210 1210 if self._options.first:
1211 1211 self.stop()
1212 1212 else:
1213 1213 with iolock:
1214 1214 if not self._options.nodiff:
1215 1215 self.stream.write('\nERROR: %s output changed\n' % test)
1216 1216
1217 1217 self.stream.write('!')
1218 1218 self.stream.flush()
1219 1219
1220 1220 def addSuccess(self, test):
1221 1221 with iolock:
1222 1222 super(TestResult, self).addSuccess(test)
1223 1223 self.successes.append(test)
1224 1224
1225 1225 def addError(self, test, err):
1226 1226 super(TestResult, self).addError(test, err)
1227 1227 if self._options.first:
1228 1228 self.stop()
1229 1229
1230 1230 # Polyfill.
1231 1231 def addSkip(self, test, reason):
1232 1232 self.skipped.append((test, reason))
1233 1233 with iolock:
1234 1234 if self.showAll:
1235 1235 self.stream.writeln('skipped %s' % reason)
1236 1236 else:
1237 1237 self.stream.write('s')
1238 1238 self.stream.flush()
1239 1239
1240 1240 def addIgnore(self, test, reason):
1241 1241 self.ignored.append((test, reason))
1242 1242 with iolock:
1243 1243 if self.showAll:
1244 1244 self.stream.writeln('ignored %s' % reason)
1245 1245 else:
1246 1246 if reason not in ('not retesting', "doesn't match keyword"):
1247 1247 self.stream.write('i')
1248 1248 else:
1249 1249 self.testsRun += 1
1250 1250 self.stream.flush()
1251 1251
1252 1252 def addWarn(self, test, reason):
1253 1253 self.warned.append((test, reason))
1254 1254
1255 1255 if self._options.first:
1256 1256 self.stop()
1257 1257
1258 1258 with iolock:
1259 1259 if self.showAll:
1260 1260 self.stream.writeln('warned %s' % reason)
1261 1261 else:
1262 1262 self.stream.write('~')
1263 1263 self.stream.flush()
1264 1264
1265 1265 def addOutputMismatch(self, test, ret, got, expected):
1266 1266 """Record a mismatch in test output for a particular test."""
1267 1267 if self.shouldStop:
1268 1268 # don't print, some other test case already failed and
1269 1269 # printed, we're just stale and probably failed due to our
1270 1270 # temp dir getting cleaned up.
1271 1271 return
1272 1272
1273 1273 accepted = False
1274 1274 failed = False
1275 1275 lines = []
1276 1276
1277 1277 with iolock:
1278 1278 if self._options.nodiff:
1279 1279 pass
1280 1280 elif self._options.view:
1281 1281 os.system("%s %s %s" %
1282 1282 (self._options.view, test.refpath, test.errpath))
1283 1283 else:
1284 1284 servefail, lines = getdiff(expected, got,
1285 1285 test.refpath, test.errpath)
1286 1286 if servefail:
1287 1287 self.addFailure(
1288 1288 test,
1289 1289 'server failed to start (HGPORT=%s)' % test._startport)
1290 1290 else:
1291 1291 self.stream.write('\n')
1292 1292 for line in lines:
1293 if sys.version_info[0] > 2:
1294 self.stream.flush()
1295 self.stream.buffer.write(line)
1296 self.stream.buffer.flush()
1297 else:
1293 1298 self.stream.write(line)
1294 1299 self.stream.flush()
1295 1300
1296 1301 # handle interactive prompt without releasing iolock
1297 1302 if self._options.interactive:
1298 1303 self.stream.write('Accept this change? [n] ')
1299 1304 answer = sys.stdin.readline().strip()
1300 1305 if answer.lower() in ('y', 'yes'):
1301 1306 if test.name.endswith('.t'):
1302 1307 rename(test.errpath, test.path)
1303 1308 else:
1304 1309 rename(test.errpath, '%s.out' % test.path)
1305 1310 accepted = True
1306 1311 if not accepted and not failed:
1307 1312 self.faildata[test.name] = b''.join(lines)
1308 1313
1309 1314 return accepted
1310 1315
1311 1316 def startTest(self, test):
1312 1317 super(TestResult, self).startTest(test)
1313 1318
1314 1319 # os.times module computes the user time and system time spent by
1315 1320 # child's processes along with real elapsed time taken by a process.
1316 1321 # This module has one limitation. It can only work for Linux user
1317 1322 # and not for Windows.
1318 1323 test.started = os.times()
1319 1324
1320 1325 def stopTest(self, test, interrupted=False):
1321 1326 super(TestResult, self).stopTest(test)
1322 1327
1323 1328 test.stopped = os.times()
1324 1329
1325 1330 starttime = test.started
1326 1331 endtime = test.stopped
1327 1332 self.times.append((test.name,
1328 1333 endtime[2] - starttime[2], # user space CPU time
1329 1334 endtime[3] - starttime[3], # sys space CPU time
1330 1335 endtime[4] - starttime[4], # real time
1331 1336 ))
1332 1337
1333 1338 if interrupted:
1334 1339 with iolock:
1335 1340 self.stream.writeln('INTERRUPTED: %s (after %d seconds)' % (
1336 1341 test.name, self.times[-1][3]))
1337 1342
1338 1343 class TestSuite(unittest.TestSuite):
1339 1344 """Custom unittest TestSuite that knows how to execute Mercurial tests."""
1340 1345
1341 1346 def __init__(self, testdir, jobs=1, whitelist=None, blacklist=None,
1342 1347 retest=False, keywords=None, loop=False, runs_per_test=1,
1343 1348 loadtest=None,
1344 1349 *args, **kwargs):
1345 1350 """Create a new instance that can run tests with a configuration.
1346 1351
1347 1352 testdir specifies the directory where tests are executed from. This
1348 1353 is typically the ``tests`` directory from Mercurial's source
1349 1354 repository.
1350 1355
1351 1356 jobs specifies the number of jobs to run concurrently. Each test
1352 1357 executes on its own thread. Tests actually spawn new processes, so
1353 1358 state mutation should not be an issue.
1354 1359
1355 1360 whitelist and blacklist denote tests that have been whitelisted and
1356 1361 blacklisted, respectively. These arguments don't belong in TestSuite.
1357 1362 Instead, whitelist and blacklist should be handled by the thing that
1358 1363 populates the TestSuite with tests. They are present to preserve
1359 1364 backwards compatible behavior which reports skipped tests as part
1360 1365 of the results.
1361 1366
1362 1367 retest denotes whether to retest failed tests. This arguably belongs
1363 1368 outside of TestSuite.
1364 1369
1365 1370 keywords denotes key words that will be used to filter which tests
1366 1371 to execute. This arguably belongs outside of TestSuite.
1367 1372
1368 1373 loop denotes whether to loop over tests forever.
1369 1374 """
1370 1375 super(TestSuite, self).__init__(*args, **kwargs)
1371 1376
1372 1377 self._jobs = jobs
1373 1378 self._whitelist = whitelist
1374 1379 self._blacklist = blacklist
1375 1380 self._retest = retest
1376 1381 self._keywords = keywords
1377 1382 self._loop = loop
1378 1383 self._runs_per_test = runs_per_test
1379 1384 self._loadtest = loadtest
1380 1385
1381 1386 def run(self, result):
1382 1387 # We have a number of filters that need to be applied. We do this
1383 1388 # here instead of inside Test because it makes the running logic for
1384 1389 # Test simpler.
1385 1390 tests = []
1386 1391 num_tests = [0]
1387 1392 for test in self._tests:
1388 1393 def get():
1389 1394 num_tests[0] += 1
1390 1395 if getattr(test, 'should_reload', False):
1391 1396 return self._loadtest(test.bname, num_tests[0])
1392 1397 return test
1393 1398 if not os.path.exists(test.path):
1394 1399 result.addSkip(test, "Doesn't exist")
1395 1400 continue
1396 1401
1397 1402 if not (self._whitelist and test.name in self._whitelist):
1398 1403 if self._blacklist and test.name in self._blacklist:
1399 1404 result.addSkip(test, 'blacklisted')
1400 1405 continue
1401 1406
1402 1407 if self._retest and not os.path.exists(test.errpath):
1403 1408 result.addIgnore(test, 'not retesting')
1404 1409 continue
1405 1410
1406 1411 if self._keywords:
1407 1412 f = open(test.path, 'rb')
1408 1413 t = f.read().lower() + test.bname.lower()
1409 1414 f.close()
1410 1415 ignored = False
1411 1416 for k in self._keywords.lower().split():
1412 1417 if k not in t:
1413 1418 result.addIgnore(test, "doesn't match keyword")
1414 1419 ignored = True
1415 1420 break
1416 1421
1417 1422 if ignored:
1418 1423 continue
1419 1424 for _ in xrange(self._runs_per_test):
1420 1425 tests.append(get())
1421 1426
1422 1427 runtests = list(tests)
1423 1428 done = queue.Queue()
1424 1429 running = 0
1425 1430
1426 1431 def job(test, result):
1427 1432 try:
1428 1433 test(result)
1429 1434 done.put(None)
1430 1435 except KeyboardInterrupt:
1431 1436 pass
1432 1437 except: # re-raises
1433 1438 done.put(('!', test, 'run-test raised an error, see traceback'))
1434 1439 raise
1435 1440
1436 1441 stoppedearly = False
1437 1442
1438 1443 try:
1439 1444 while tests or running:
1440 1445 if not done.empty() or running == self._jobs or not tests:
1441 1446 try:
1442 1447 done.get(True, 1)
1443 1448 running -= 1
1444 1449 if result and result.shouldStop:
1445 1450 stoppedearly = True
1446 1451 break
1447 1452 except queue.Empty:
1448 1453 continue
1449 1454 if tests and not running == self._jobs:
1450 1455 test = tests.pop(0)
1451 1456 if self._loop:
1452 1457 if getattr(test, 'should_reload', False):
1453 1458 num_tests[0] += 1
1454 1459 tests.append(
1455 1460 self._loadtest(test.name, num_tests[0]))
1456 1461 else:
1457 1462 tests.append(test)
1458 1463 t = threading.Thread(target=job, name=test.name,
1459 1464 args=(test, result))
1460 1465 t.start()
1461 1466 running += 1
1462 1467
1463 1468 # If we stop early we still need to wait on started tests to
1464 1469 # finish. Otherwise, there is a race between the test completing
1465 1470 # and the test's cleanup code running. This could result in the
1466 1471 # test reporting incorrect.
1467 1472 if stoppedearly:
1468 1473 while running:
1469 1474 try:
1470 1475 done.get(True, 1)
1471 1476 running -= 1
1472 1477 except queue.Empty:
1473 1478 continue
1474 1479 except KeyboardInterrupt:
1475 1480 for test in runtests:
1476 1481 test.abort()
1477 1482
1478 1483 return result
1479 1484
1480 1485 class TextTestRunner(unittest.TextTestRunner):
1481 1486 """Custom unittest test runner that uses appropriate settings."""
1482 1487
1483 1488 def __init__(self, runner, *args, **kwargs):
1484 1489 super(TextTestRunner, self).__init__(*args, **kwargs)
1485 1490
1486 1491 self._runner = runner
1487 1492
1488 1493 def run(self, test):
1489 1494 result = TestResult(self._runner.options, self.stream,
1490 1495 self.descriptions, self.verbosity)
1491 1496
1492 1497 test(result)
1493 1498
1494 1499 failed = len(result.failures)
1495 1500 warned = len(result.warned)
1496 1501 skipped = len(result.skipped)
1497 1502 ignored = len(result.ignored)
1498 1503
1499 1504 with iolock:
1500 1505 self.stream.writeln('')
1501 1506
1502 1507 if not self._runner.options.noskips:
1503 1508 for test, msg in result.skipped:
1504 1509 self.stream.writeln('Skipped %s: %s' % (test.name, msg))
1505 1510 for test, msg in result.warned:
1506 1511 self.stream.writeln('Warned %s: %s' % (test.name, msg))
1507 1512 for test, msg in result.failures:
1508 1513 self.stream.writeln('Failed %s: %s' % (test.name, msg))
1509 1514 for test, msg in result.errors:
1510 1515 self.stream.writeln('Errored %s: %s' % (test.name, msg))
1511 1516
1512 1517 if self._runner.options.xunit:
1513 1518 xuf = open(self._runner.options.xunit, 'wb')
1514 1519 try:
1515 1520 timesd = dict((t[0], t[3]) for t in result.times)
1516 1521 doc = minidom.Document()
1517 1522 s = doc.createElement('testsuite')
1518 1523 s.setAttribute('name', 'run-tests')
1519 1524 s.setAttribute('tests', str(result.testsRun))
1520 1525 s.setAttribute('errors', "0") # TODO
1521 1526 s.setAttribute('failures', str(failed))
1522 1527 s.setAttribute('skipped', str(skipped + ignored))
1523 1528 doc.appendChild(s)
1524 1529 for tc in result.successes:
1525 1530 t = doc.createElement('testcase')
1526 1531 t.setAttribute('name', tc.name)
1527 1532 t.setAttribute('time', '%.3f' % timesd[tc.name])
1528 1533 s.appendChild(t)
1529 1534 for tc, err in sorted(result.faildata.items()):
1530 1535 t = doc.createElement('testcase')
1531 1536 t.setAttribute('name', tc)
1532 1537 t.setAttribute('time', '%.3f' % timesd[tc])
1533 1538 # createCDATASection expects a unicode or it will
1534 1539 # convert using default conversion rules, which will
1535 1540 # fail if string isn't ASCII.
1536 1541 err = cdatasafe(err).decode('utf-8', 'replace')
1537 1542 cd = doc.createCDATASection(err)
1538 1543 t.appendChild(cd)
1539 1544 s.appendChild(t)
1540 1545 xuf.write(doc.toprettyxml(indent=' ', encoding='utf-8'))
1541 1546 finally:
1542 1547 xuf.close()
1543 1548
1544 1549 if self._runner.options.json:
1545 1550 if json is None:
1546 1551 raise ImportError("json module not installed")
1547 1552 jsonpath = os.path.join(self._runner._testdir, 'report.json')
1548 1553 fp = open(jsonpath, 'w')
1549 1554 try:
1550 1555 timesd = {}
1551 1556 for tdata in result.times:
1552 1557 test = tdata[0]
1553 1558 timesd[test] = tdata[1:]
1554 1559
1555 1560 outcome = {}
1556 1561 groups = [('success', ((tc, None)
1557 1562 for tc in result.successes)),
1558 1563 ('failure', result.failures),
1559 1564 ('skip', result.skipped)]
1560 1565 for res, testcases in groups:
1561 1566 for tc, __ in testcases:
1562 1567 tres = {'result': res,
1563 1568 'time': ('%0.3f' % timesd[tc.name][2]),
1564 1569 'cuser': ('%0.3f' % timesd[tc.name][0]),
1565 1570 'csys': ('%0.3f' % timesd[tc.name][1])}
1566 1571 outcome[tc.name] = tres
1567 1572
1568 1573 jsonout = json.dumps(outcome, sort_keys=True, indent=4)
1569 1574 fp.writelines(("testreport =", jsonout))
1570 1575 finally:
1571 1576 fp.close()
1572 1577
1573 1578 self._runner._checkhglib('Tested')
1574 1579
1575 1580 self.stream.writeln(
1576 1581 '# Ran %d tests, %d skipped, %d warned, %d failed.'
1577 1582 % (result.testsRun,
1578 1583 skipped + ignored, warned, failed))
1579 1584 if failed:
1580 1585 self.stream.writeln('python hash seed: %s' %
1581 1586 os.environ['PYTHONHASHSEED'])
1582 1587 if self._runner.options.time:
1583 1588 self.printtimes(result.times)
1584 1589
1585 1590 return result
1586 1591
1587 1592 def printtimes(self, times):
1588 1593 # iolock held by run
1589 1594 self.stream.writeln('# Producing time report')
1590 1595 times.sort(key=lambda t: (t[3]))
1591 1596 cols = '%7.3f %7.3f %7.3f %s'
1592 1597 self.stream.writeln('%-7s %-7s %-7s %s' % ('cuser', 'csys', 'real',
1593 1598 'Test'))
1594 1599 for tdata in times:
1595 1600 test = tdata[0]
1596 1601 cuser, csys, real = tdata[1:4]
1597 1602 self.stream.writeln(cols % (cuser, csys, real, test))
1598 1603
1599 1604 class TestRunner(object):
1600 1605 """Holds context for executing tests.
1601 1606
1602 1607 Tests rely on a lot of state. This object holds it for them.
1603 1608 """
1604 1609
1605 1610 # Programs required to run tests.
1606 1611 REQUIREDTOOLS = [
1607 1612 os.path.basename(sys.executable).encode('utf-8'),
1608 1613 b'diff',
1609 1614 b'grep',
1610 1615 b'unzip',
1611 1616 b'gunzip',
1612 1617 b'bunzip2',
1613 1618 b'sed',
1614 1619 ]
1615 1620
1616 1621 # Maps file extensions to test class.
1617 1622 TESTTYPES = [
1618 1623 (b'.py', PythonTest),
1619 1624 (b'.t', TTest),
1620 1625 ]
1621 1626
1622 1627 def __init__(self):
1623 1628 self.options = None
1624 1629 self._hgroot = None
1625 1630 self._testdir = None
1626 1631 self._hgtmp = None
1627 1632 self._installdir = None
1628 1633 self._bindir = None
1629 1634 self._tmpbinddir = None
1630 1635 self._pythondir = None
1631 1636 self._coveragefile = None
1632 1637 self._createdfiles = []
1633 1638 self._hgpath = None
1634 1639 self._portoffset = 0
1635 1640 self._ports = {}
1636 1641
1637 1642 def run(self, args, parser=None):
1638 1643 """Run the test suite."""
1639 1644 oldmask = os.umask(0o22)
1640 1645 try:
1641 1646 parser = parser or getparser()
1642 1647 options, args = parseargs(args, parser)
1643 1648 args = [a.encode('utf-8') for a in args]
1644 1649 self.options = options
1645 1650
1646 1651 self._checktools()
1647 1652 tests = self.findtests(args)
1648 1653 return self._run(tests)
1649 1654 finally:
1650 1655 os.umask(oldmask)
1651 1656
1652 1657 def _run(self, tests):
1653 1658 if self.options.random:
1654 1659 random.shuffle(tests)
1655 1660 else:
1656 1661 # keywords for slow tests
1657 1662 slow = b'svn gendoc check-code-hg'.split()
1658 1663 def sortkey(f):
1659 1664 # run largest tests first, as they tend to take the longest
1660 1665 try:
1661 1666 val = -os.stat(f).st_size
1662 1667 except OSError as e:
1663 1668 if e.errno != errno.ENOENT:
1664 1669 raise
1665 1670 return -1e9 # file does not exist, tell early
1666 1671 for kw in slow:
1667 1672 if kw in f:
1668 1673 val *= 10
1669 1674 return val
1670 1675 tests.sort(key=sortkey)
1671 1676
1672 1677 self._testdir = osenvironb[b'TESTDIR'] = getattr(
1673 1678 os, 'getcwdb', os.getcwd)()
1674 1679
1675 1680 if 'PYTHONHASHSEED' not in os.environ:
1676 1681 # use a random python hash seed all the time
1677 1682 # we do the randomness ourself to know what seed is used
1678 1683 os.environ['PYTHONHASHSEED'] = str(random.getrandbits(32))
1679 1684
1680 1685 if self.options.tmpdir:
1681 1686 self.options.keep_tmpdir = True
1682 1687 tmpdir = self.options.tmpdir.encode('utf-8')
1683 1688 if os.path.exists(tmpdir):
1684 1689 # Meaning of tmpdir has changed since 1.3: we used to create
1685 1690 # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if
1686 1691 # tmpdir already exists.
1687 1692 print("error: temp dir %r already exists" % tmpdir)
1688 1693 return 1
1689 1694
1690 1695 # Automatically removing tmpdir sounds convenient, but could
1691 1696 # really annoy anyone in the habit of using "--tmpdir=/tmp"
1692 1697 # or "--tmpdir=$HOME".
1693 1698 #vlog("# Removing temp dir", tmpdir)
1694 1699 #shutil.rmtree(tmpdir)
1695 1700 os.makedirs(tmpdir)
1696 1701 else:
1697 1702 d = None
1698 1703 if os.name == 'nt':
1699 1704 # without this, we get the default temp dir location, but
1700 1705 # in all lowercase, which causes troubles with paths (issue3490)
1701 1706 d = osenvironb.get(b'TMP', None)
1702 1707 # FILE BUG: mkdtemp works only on unicode in Python 3
1703 1708 tmpdir = tempfile.mkdtemp('', 'hgtests.',
1704 1709 d and d.decode('utf-8')).encode('utf-8')
1705 1710
1706 1711 self._hgtmp = osenvironb[b'HGTMP'] = (
1707 1712 os.path.realpath(tmpdir))
1708 1713
1709 1714 if self.options.with_hg:
1710 1715 self._installdir = None
1711 1716 whg = self.options.with_hg
1712 1717 # If --with-hg is not specified, we have bytes already,
1713 1718 # but if it was specified in python3 we get a str, so we
1714 1719 # have to encode it back into a bytes.
1715 1720 if sys.version_info[0] == 3:
1716 1721 if not isinstance(whg, bytes):
1717 1722 whg = whg.encode('utf-8')
1718 1723 self._bindir = os.path.dirname(os.path.realpath(whg))
1719 1724 assert isinstance(self._bindir, bytes)
1720 1725 self._tmpbindir = os.path.join(self._hgtmp, b'install', b'bin')
1721 1726 os.makedirs(self._tmpbindir)
1722 1727
1723 1728 # This looks redundant with how Python initializes sys.path from
1724 1729 # the location of the script being executed. Needed because the
1725 1730 # "hg" specified by --with-hg is not the only Python script
1726 1731 # executed in the test suite that needs to import 'mercurial'
1727 1732 # ... which means it's not really redundant at all.
1728 1733 self._pythondir = self._bindir
1729 1734 else:
1730 1735 self._installdir = os.path.join(self._hgtmp, b"install")
1731 1736 self._bindir = osenvironb[b"BINDIR"] = \
1732 1737 os.path.join(self._installdir, b"bin")
1733 1738 self._tmpbindir = self._bindir
1734 1739 self._pythondir = os.path.join(self._installdir, b"lib", b"python")
1735 1740
1736 1741 osenvironb[b"BINDIR"] = self._bindir
1737 1742 os.environ["PYTHON"] = PYTHON
1738 1743
1739 1744 fileb = __file__.encode('utf-8')
1740 1745 runtestdir = os.path.abspath(os.path.dirname(fileb))
1741 1746 if sys.version_info[0] == 3:
1742 1747 sepb = os.pathsep.encode('utf-8')
1743 1748 else:
1744 1749 sepb = os.pathsep
1745 1750 path = [self._bindir, runtestdir] + osenvironb[b"PATH"].split(sepb)
1746 1751 if os.path.islink(__file__):
1747 1752 # test helper will likely be at the end of the symlink
1748 1753 realfile = os.path.realpath(fileb)
1749 1754 realdir = os.path.abspath(os.path.dirname(realfile))
1750 1755 path.insert(2, realdir)
1751 1756 if self._tmpbindir != self._bindir:
1752 1757 path = [self._tmpbindir] + path
1753 1758 osenvironb[b"PATH"] = sepb.join(path)
1754 1759
1755 1760 # Include TESTDIR in PYTHONPATH so that out-of-tree extensions
1756 1761 # can run .../tests/run-tests.py test-foo where test-foo
1757 1762 # adds an extension to HGRC. Also include run-test.py directory to
1758 1763 # import modules like heredoctest.
1759 1764 pypath = [self._pythondir, self._testdir, runtestdir]
1760 1765 # We have to augment PYTHONPATH, rather than simply replacing
1761 1766 # it, in case external libraries are only available via current
1762 1767 # PYTHONPATH. (In particular, the Subversion bindings on OS X
1763 1768 # are in /opt/subversion.)
1764 1769 oldpypath = osenvironb.get(IMPL_PATH)
1765 1770 if oldpypath:
1766 1771 pypath.append(oldpypath)
1767 1772 osenvironb[IMPL_PATH] = sepb.join(pypath)
1768 1773
1769 1774 if self.options.pure:
1770 1775 os.environ["HGTEST_RUN_TESTS_PURE"] = "--pure"
1771 1776
1772 1777 self._coveragefile = os.path.join(self._testdir, b'.coverage')
1773 1778
1774 1779 vlog("# Using TESTDIR", self._testdir)
1775 1780 vlog("# Using HGTMP", self._hgtmp)
1776 1781 vlog("# Using PATH", os.environ["PATH"])
1777 1782 vlog("# Using", IMPL_PATH, osenvironb[IMPL_PATH])
1778 1783
1779 1784 try:
1780 1785 return self._runtests(tests) or 0
1781 1786 finally:
1782 1787 time.sleep(.1)
1783 1788 self._cleanup()
1784 1789
1785 1790 def findtests(self, args):
1786 1791 """Finds possible test files from arguments.
1787 1792
1788 1793 If you wish to inject custom tests into the test harness, this would
1789 1794 be a good function to monkeypatch or override in a derived class.
1790 1795 """
1791 1796 if not args:
1792 1797 if self.options.changed:
1793 1798 proc = Popen4('hg st --rev "%s" -man0 .' %
1794 1799 self.options.changed, None, 0)
1795 1800 stdout, stderr = proc.communicate()
1796 1801 args = stdout.strip(b'\0').split(b'\0')
1797 1802 else:
1798 1803 args = os.listdir(b'.')
1799 1804
1800 1805 return [t for t in args
1801 1806 if os.path.basename(t).startswith(b'test-')
1802 1807 and (t.endswith(b'.py') or t.endswith(b'.t'))]
1803 1808
1804 1809 def _runtests(self, tests):
1805 1810 try:
1806 1811 if self._installdir:
1807 1812 self._installhg()
1808 1813 self._checkhglib("Testing")
1809 1814 else:
1810 1815 self._usecorrectpython()
1811 1816
1812 1817 if self.options.restart:
1813 1818 orig = list(tests)
1814 1819 while tests:
1815 1820 if os.path.exists(tests[0] + ".err"):
1816 1821 break
1817 1822 tests.pop(0)
1818 1823 if not tests:
1819 1824 print("running all tests")
1820 1825 tests = orig
1821 1826
1822 1827 tests = [self._gettest(t, i) for i, t in enumerate(tests)]
1823 1828
1824 1829 failed = False
1825 1830 warned = False
1826 1831 kws = self.options.keywords
1827 1832 if kws is not None and sys.version_info[0] == 3:
1828 1833 kws = kws.encode('utf-8')
1829 1834
1830 1835 suite = TestSuite(self._testdir,
1831 1836 jobs=self.options.jobs,
1832 1837 whitelist=self.options.whitelisted,
1833 1838 blacklist=self.options.blacklist,
1834 1839 retest=self.options.retest,
1835 1840 keywords=kws,
1836 1841 loop=self.options.loop,
1837 1842 runs_per_test=self.options.runs_per_test,
1838 1843 tests=tests, loadtest=self._gettest)
1839 1844 verbosity = 1
1840 1845 if self.options.verbose:
1841 1846 verbosity = 2
1842 1847 runner = TextTestRunner(self, verbosity=verbosity)
1843 1848 result = runner.run(suite)
1844 1849
1845 1850 if result.failures:
1846 1851 failed = True
1847 1852 if result.warned:
1848 1853 warned = True
1849 1854
1850 1855 if self.options.anycoverage:
1851 1856 self._outputcoverage()
1852 1857 except KeyboardInterrupt:
1853 1858 failed = True
1854 1859 print("\ninterrupted!")
1855 1860
1856 1861 if failed:
1857 1862 return 1
1858 1863 if warned:
1859 1864 return 80
1860 1865
1861 1866 def _getport(self, count):
1862 1867 port = self._ports.get(count) # do we have a cached entry?
1863 1868 if port is None:
1864 1869 port = self.options.port + self._portoffset
1865 1870 portneeded = 3
1866 1871 # above 100 tries we just give up and let test reports failure
1867 1872 for tries in xrange(100):
1868 1873 allfree = True
1869 1874 for idx in xrange(portneeded):
1870 1875 if not checkportisavailable(port + idx):
1871 1876 allfree = False
1872 1877 break
1873 1878 self._portoffset += portneeded
1874 1879 if allfree:
1875 1880 break
1876 1881 self._ports[count] = port
1877 1882 return port
1878 1883
1879 1884 def _gettest(self, test, count):
1880 1885 """Obtain a Test by looking at its filename.
1881 1886
1882 1887 Returns a Test instance. The Test may not be runnable if it doesn't
1883 1888 map to a known type.
1884 1889 """
1885 1890 lctest = test.lower()
1886 1891 testcls = Test
1887 1892
1888 1893 for ext, cls in self.TESTTYPES:
1889 1894 if lctest.endswith(ext):
1890 1895 testcls = cls
1891 1896 break
1892 1897
1893 1898 refpath = os.path.join(self._testdir, test)
1894 1899 tmpdir = os.path.join(self._hgtmp, b'child%d' % count)
1895 1900
1896 1901 t = testcls(refpath, tmpdir,
1897 1902 keeptmpdir=self.options.keep_tmpdir,
1898 1903 debug=self.options.debug,
1899 1904 timeout=self.options.timeout,
1900 1905 startport=self._getport(count),
1901 1906 extraconfigopts=self.options.extra_config_opt,
1902 1907 py3kwarnings=self.options.py3k_warnings,
1903 1908 shell=self.options.shell)
1904 1909 t.should_reload = True
1905 1910 return t
1906 1911
1907 1912 def _cleanup(self):
1908 1913 """Clean up state from this test invocation."""
1909 1914
1910 1915 if self.options.keep_tmpdir:
1911 1916 return
1912 1917
1913 1918 vlog("# Cleaning up HGTMP", self._hgtmp)
1914 1919 shutil.rmtree(self._hgtmp, True)
1915 1920 for f in self._createdfiles:
1916 1921 try:
1917 1922 os.remove(f)
1918 1923 except OSError:
1919 1924 pass
1920 1925
1921 1926 def _usecorrectpython(self):
1922 1927 """Configure the environment to use the appropriate Python in tests."""
1923 1928 # Tests must use the same interpreter as us or bad things will happen.
1924 1929 pyexename = sys.platform == 'win32' and b'python.exe' or b'python'
1925 1930 if getattr(os, 'symlink', None):
1926 1931 vlog("# Making python executable in test path a symlink to '%s'" %
1927 1932 sys.executable)
1928 1933 mypython = os.path.join(self._tmpbindir, pyexename)
1929 1934 try:
1930 1935 if os.readlink(mypython) == sys.executable:
1931 1936 return
1932 1937 os.unlink(mypython)
1933 1938 except OSError as err:
1934 1939 if err.errno != errno.ENOENT:
1935 1940 raise
1936 1941 if self._findprogram(pyexename) != sys.executable:
1937 1942 try:
1938 1943 os.symlink(sys.executable, mypython)
1939 1944 self._createdfiles.append(mypython)
1940 1945 except OSError as err:
1941 1946 # child processes may race, which is harmless
1942 1947 if err.errno != errno.EEXIST:
1943 1948 raise
1944 1949 else:
1945 1950 exedir, exename = os.path.split(sys.executable)
1946 1951 vlog("# Modifying search path to find %s as %s in '%s'" %
1947 1952 (exename, pyexename, exedir))
1948 1953 path = os.environ['PATH'].split(os.pathsep)
1949 1954 while exedir in path:
1950 1955 path.remove(exedir)
1951 1956 os.environ['PATH'] = os.pathsep.join([exedir] + path)
1952 1957 if not self._findprogram(pyexename):
1953 1958 print("WARNING: Cannot find %s in search path" % pyexename)
1954 1959
1955 1960 def _installhg(self):
1956 1961 """Install hg into the test environment.
1957 1962
1958 1963 This will also configure hg with the appropriate testing settings.
1959 1964 """
1960 1965 vlog("# Performing temporary installation of HG")
1961 1966 installerrs = os.path.join(b"tests", b"install.err")
1962 1967 compiler = ''
1963 1968 if self.options.compiler:
1964 1969 compiler = '--compiler ' + self.options.compiler
1965 1970 if self.options.pure:
1966 1971 pure = b"--pure"
1967 1972 else:
1968 1973 pure = b""
1969 1974 py3 = ''
1970 1975
1971 1976 # Run installer in hg root
1972 1977 script = os.path.realpath(sys.argv[0])
1973 1978 exe = sys.executable
1974 1979 if sys.version_info[0] == 3:
1975 1980 py3 = b'--c2to3'
1976 1981 compiler = compiler.encode('utf-8')
1977 1982 script = script.encode('utf-8')
1978 1983 exe = exe.encode('utf-8')
1979 1984 hgroot = os.path.dirname(os.path.dirname(script))
1980 1985 self._hgroot = hgroot
1981 1986 os.chdir(hgroot)
1982 1987 nohome = b'--home=""'
1983 1988 if os.name == 'nt':
1984 1989 # The --home="" trick works only on OS where os.sep == '/'
1985 1990 # because of a distutils convert_path() fast-path. Avoid it at
1986 1991 # least on Windows for now, deal with .pydistutils.cfg bugs
1987 1992 # when they happen.
1988 1993 nohome = b''
1989 1994 cmd = (b'%(exe)s setup.py %(py3)s %(pure)s clean --all'
1990 1995 b' build %(compiler)s --build-base="%(base)s"'
1991 1996 b' install --force --prefix="%(prefix)s"'
1992 1997 b' --install-lib="%(libdir)s"'
1993 1998 b' --install-scripts="%(bindir)s" %(nohome)s >%(logfile)s 2>&1'
1994 1999 % {b'exe': exe, b'py3': py3, b'pure': pure,
1995 2000 b'compiler': compiler,
1996 2001 b'base': os.path.join(self._hgtmp, b"build"),
1997 2002 b'prefix': self._installdir, b'libdir': self._pythondir,
1998 2003 b'bindir': self._bindir,
1999 2004 b'nohome': nohome, b'logfile': installerrs})
2000 2005
2001 2006 # setuptools requires install directories to exist.
2002 2007 def makedirs(p):
2003 2008 try:
2004 2009 os.makedirs(p)
2005 2010 except OSError as e:
2006 2011 if e.errno != errno.EEXIST:
2007 2012 raise
2008 2013 makedirs(self._pythondir)
2009 2014 makedirs(self._bindir)
2010 2015
2011 2016 vlog("# Running", cmd)
2012 2017 if os.system(cmd) == 0:
2013 2018 if not self.options.verbose:
2014 2019 os.remove(installerrs)
2015 2020 else:
2016 2021 f = open(installerrs, 'rb')
2017 2022 for line in f:
2018 2023 if sys.version_info[0] > 2:
2019 2024 sys.stdout.buffer.write(line)
2020 2025 else:
2021 2026 sys.stdout.write(line)
2022 2027 f.close()
2023 2028 sys.exit(1)
2024 2029 os.chdir(self._testdir)
2025 2030
2026 2031 self._usecorrectpython()
2027 2032
2028 2033 if self.options.py3k_warnings and not self.options.anycoverage:
2029 2034 vlog("# Updating hg command to enable Py3k Warnings switch")
2030 2035 f = open(os.path.join(self._bindir, 'hg'), 'rb')
2031 2036 lines = [line.rstrip() for line in f]
2032 2037 lines[0] += ' -3'
2033 2038 f.close()
2034 2039 f = open(os.path.join(self._bindir, 'hg'), 'wb')
2035 2040 for line in lines:
2036 2041 f.write(line + '\n')
2037 2042 f.close()
2038 2043
2039 2044 hgbat = os.path.join(self._bindir, b'hg.bat')
2040 2045 if os.path.isfile(hgbat):
2041 2046 # hg.bat expects to be put in bin/scripts while run-tests.py
2042 2047 # installation layout put it in bin/ directly. Fix it
2043 2048 f = open(hgbat, 'rb')
2044 2049 data = f.read()
2045 2050 f.close()
2046 2051 if b'"%~dp0..\python" "%~dp0hg" %*' in data:
2047 2052 data = data.replace(b'"%~dp0..\python" "%~dp0hg" %*',
2048 2053 b'"%~dp0python" "%~dp0hg" %*')
2049 2054 f = open(hgbat, 'wb')
2050 2055 f.write(data)
2051 2056 f.close()
2052 2057 else:
2053 2058 print('WARNING: cannot fix hg.bat reference to python.exe')
2054 2059
2055 2060 if self.options.anycoverage:
2056 2061 custom = os.path.join(self._testdir, 'sitecustomize.py')
2057 2062 target = os.path.join(self._pythondir, 'sitecustomize.py')
2058 2063 vlog('# Installing coverage trigger to %s' % target)
2059 2064 shutil.copyfile(custom, target)
2060 2065 rc = os.path.join(self._testdir, '.coveragerc')
2061 2066 vlog('# Installing coverage rc to %s' % rc)
2062 2067 os.environ['COVERAGE_PROCESS_START'] = rc
2063 2068 covdir = os.path.join(self._installdir, '..', 'coverage')
2064 2069 try:
2065 2070 os.mkdir(covdir)
2066 2071 except OSError as e:
2067 2072 if e.errno != errno.EEXIST:
2068 2073 raise
2069 2074
2070 2075 os.environ['COVERAGE_DIR'] = covdir
2071 2076
2072 2077 def _checkhglib(self, verb):
2073 2078 """Ensure that the 'mercurial' package imported by python is
2074 2079 the one we expect it to be. If not, print a warning to stderr."""
2075 2080 if ((self._bindir == self._pythondir) and
2076 2081 (self._bindir != self._tmpbindir)):
2077 2082 # The pythondir has been inferred from --with-hg flag.
2078 2083 # We cannot expect anything sensible here.
2079 2084 return
2080 2085 expecthg = os.path.join(self._pythondir, b'mercurial')
2081 2086 actualhg = self._gethgpath()
2082 2087 if os.path.abspath(actualhg) != os.path.abspath(expecthg):
2083 2088 sys.stderr.write('warning: %s with unexpected mercurial lib: %s\n'
2084 2089 ' (expected %s)\n'
2085 2090 % (verb, actualhg, expecthg))
2086 2091 def _gethgpath(self):
2087 2092 """Return the path to the mercurial package that is actually found by
2088 2093 the current Python interpreter."""
2089 2094 if self._hgpath is not None:
2090 2095 return self._hgpath
2091 2096
2092 2097 cmd = '%s -c "import mercurial; print (mercurial.__path__[0])"'
2093 2098 pipe = os.popen(cmd % PYTHON)
2094 2099 try:
2095 2100 self._hgpath = pipe.read().strip()
2096 2101 finally:
2097 2102 pipe.close()
2098 2103
2099 2104 return self._hgpath
2100 2105
2101 2106 def _outputcoverage(self):
2102 2107 """Produce code coverage output."""
2103 2108 from coverage import coverage
2104 2109
2105 2110 vlog('# Producing coverage report')
2106 2111 # chdir is the easiest way to get short, relative paths in the
2107 2112 # output.
2108 2113 os.chdir(self._hgroot)
2109 2114 covdir = os.path.join(self._installdir, '..', 'coverage')
2110 2115 cov = coverage(data_file=os.path.join(covdir, 'cov'))
2111 2116
2112 2117 # Map install directory paths back to source directory.
2113 2118 cov.config.paths['srcdir'] = ['.', self._pythondir]
2114 2119
2115 2120 cov.combine()
2116 2121
2117 2122 omit = [os.path.join(x, '*') for x in [self._bindir, self._testdir]]
2118 2123 cov.report(ignore_errors=True, omit=omit)
2119 2124
2120 2125 if self.options.htmlcov:
2121 2126 htmldir = os.path.join(self._testdir, 'htmlcov')
2122 2127 cov.html_report(directory=htmldir, omit=omit)
2123 2128 if self.options.annotate:
2124 2129 adir = os.path.join(self._testdir, 'annotated')
2125 2130 if not os.path.isdir(adir):
2126 2131 os.mkdir(adir)
2127 2132 cov.annotate(directory=adir, omit=omit)
2128 2133
2129 2134 def _findprogram(self, program):
2130 2135 """Search PATH for a executable program"""
2131 2136 if sys.version_info[0] > 2:
2132 2137 dpb = os.defpath.encode('utf-8')
2133 2138 sepb = os.pathsep.encode('utf-8')
2134 2139 else:
2135 2140 dpb = os.defpath
2136 2141 sepb = os.pathsep
2137 2142 for p in osenvironb.get(b'PATH', dpb).split(sepb):
2138 2143 name = os.path.join(p, program)
2139 2144 if os.name == 'nt' or os.access(name, os.X_OK):
2140 2145 return name
2141 2146 return None
2142 2147
2143 2148 def _checktools(self):
2144 2149 """Ensure tools required to run tests are present."""
2145 2150 for p in self.REQUIREDTOOLS:
2146 2151 if os.name == 'nt' and not p.endswith('.exe'):
2147 2152 p += '.exe'
2148 2153 found = self._findprogram(p)
2149 2154 if found:
2150 2155 vlog("# Found prerequisite", p, "at", found)
2151 2156 else:
2152 2157 print("WARNING: Did not find prerequisite tool: %s " % p)
2153 2158
2154 2159 if __name__ == '__main__':
2155 2160 runner = TestRunner()
2156 2161
2157 2162 try:
2158 2163 import msvcrt
2159 2164 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
2160 2165 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
2161 2166 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
2162 2167 except ImportError:
2163 2168 pass
2164 2169
2165 2170 sys.exit(runner.run(sys.argv[1:]))
General Comments 0
You need to be logged in to leave comments. Login now