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