##// END OF EJS Templates
run-tests: define custom result and runner classes for unittest...
Gregory Szorc -
r21429:203ed3cf default
parent child Browse files
Show More
@@ -1,1537 +1,1550 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 distutils import version
45 45 import difflib
46 46 import errno
47 47 import optparse
48 48 import os
49 49 import shutil
50 50 import subprocess
51 51 import signal
52 52 import sys
53 53 import tempfile
54 54 import time
55 55 import random
56 56 import re
57 57 import threading
58 58 import killdaemons as killmod
59 59 import Queue as queue
60 60 import unittest
61 61
62 62 processlock = threading.Lock()
63 63
64 64 # subprocess._cleanup can race with any Popen.wait or Popen.poll on py24
65 65 # http://bugs.python.org/issue1731717 for details. We shouldn't be producing
66 66 # zombies but it's pretty harmless even if we do.
67 67 if sys.version_info < (2, 5):
68 68 subprocess._cleanup = lambda: None
69 69
70 70 closefds = os.name == 'posix'
71 71 def Popen4(cmd, wd, timeout, env=None):
72 72 processlock.acquire()
73 73 p = subprocess.Popen(cmd, shell=True, bufsize=-1, cwd=wd, env=env,
74 74 close_fds=closefds,
75 75 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
76 76 stderr=subprocess.STDOUT)
77 77 processlock.release()
78 78
79 79 p.fromchild = p.stdout
80 80 p.tochild = p.stdin
81 81 p.childerr = p.stderr
82 82
83 83 p.timeout = False
84 84 if timeout:
85 85 def t():
86 86 start = time.time()
87 87 while time.time() - start < timeout and p.returncode is None:
88 88 time.sleep(.1)
89 89 p.timeout = True
90 90 if p.returncode is None:
91 91 terminate(p)
92 92 threading.Thread(target=t).start()
93 93
94 94 return p
95 95
96 96 PYTHON = sys.executable.replace('\\', '/')
97 97 IMPL_PATH = 'PYTHONPATH'
98 98 if 'java' in sys.platform:
99 99 IMPL_PATH = 'JYTHONPATH'
100 100
101 101 TESTDIR = HGTMP = INST = BINDIR = TMPBINDIR = PYTHONDIR = None
102 102
103 103 defaults = {
104 104 'jobs': ('HGTEST_JOBS', 1),
105 105 'timeout': ('HGTEST_TIMEOUT', 180),
106 106 'port': ('HGTEST_PORT', 20059),
107 107 'shell': ('HGTEST_SHELL', 'sh'),
108 108 }
109 109
110 110 def parselistfiles(files, listtype, warn=True):
111 111 entries = dict()
112 112 for filename in files:
113 113 try:
114 114 path = os.path.expanduser(os.path.expandvars(filename))
115 115 f = open(path, "r")
116 116 except IOError, err:
117 117 if err.errno != errno.ENOENT:
118 118 raise
119 119 if warn:
120 120 print "warning: no such %s file: %s" % (listtype, filename)
121 121 continue
122 122
123 123 for line in f.readlines():
124 124 line = line.split('#', 1)[0].strip()
125 125 if line:
126 126 entries[line] = filename
127 127
128 128 f.close()
129 129 return entries
130 130
131 131 def getparser():
132 132 """Obtain the OptionParser used by the CLI."""
133 133 parser = optparse.OptionParser("%prog [options] [tests]")
134 134
135 135 # keep these sorted
136 136 parser.add_option("--blacklist", action="append",
137 137 help="skip tests listed in the specified blacklist file")
138 138 parser.add_option("--whitelist", action="append",
139 139 help="always run tests listed in the specified whitelist file")
140 140 parser.add_option("--changed", type="string",
141 141 help="run tests that are changed in parent rev or working directory")
142 142 parser.add_option("-C", "--annotate", action="store_true",
143 143 help="output files annotated with coverage")
144 144 parser.add_option("-c", "--cover", action="store_true",
145 145 help="print a test coverage report")
146 146 parser.add_option("-d", "--debug", action="store_true",
147 147 help="debug mode: write output of test scripts to console"
148 148 " rather than capturing and diffing it (disables timeout)")
149 149 parser.add_option("-f", "--first", action="store_true",
150 150 help="exit on the first test failure")
151 151 parser.add_option("-H", "--htmlcov", action="store_true",
152 152 help="create an HTML report of the coverage of the files")
153 153 parser.add_option("-i", "--interactive", action="store_true",
154 154 help="prompt to accept changed output")
155 155 parser.add_option("-j", "--jobs", type="int",
156 156 help="number of jobs to run in parallel"
157 157 " (default: $%s or %d)" % defaults['jobs'])
158 158 parser.add_option("--keep-tmpdir", action="store_true",
159 159 help="keep temporary directory after running tests")
160 160 parser.add_option("-k", "--keywords",
161 161 help="run tests matching keywords")
162 162 parser.add_option("-l", "--local", action="store_true",
163 163 help="shortcut for --with-hg=<testdir>/../hg")
164 164 parser.add_option("--loop", action="store_true",
165 165 help="loop tests repeatedly")
166 166 parser.add_option("-n", "--nodiff", action="store_true",
167 167 help="skip showing test changes")
168 168 parser.add_option("-p", "--port", type="int",
169 169 help="port on which servers should listen"
170 170 " (default: $%s or %d)" % defaults['port'])
171 171 parser.add_option("--compiler", type="string",
172 172 help="compiler to build with")
173 173 parser.add_option("--pure", action="store_true",
174 174 help="use pure Python code instead of C extensions")
175 175 parser.add_option("-R", "--restart", action="store_true",
176 176 help="restart at last error")
177 177 parser.add_option("-r", "--retest", action="store_true",
178 178 help="retest failed tests")
179 179 parser.add_option("-S", "--noskips", action="store_true",
180 180 help="don't report skip tests verbosely")
181 181 parser.add_option("--shell", type="string",
182 182 help="shell to use (default: $%s or %s)" % defaults['shell'])
183 183 parser.add_option("-t", "--timeout", type="int",
184 184 help="kill errant tests after TIMEOUT seconds"
185 185 " (default: $%s or %d)" % defaults['timeout'])
186 186 parser.add_option("--time", action="store_true",
187 187 help="time how long each test takes")
188 188 parser.add_option("--tmpdir", type="string",
189 189 help="run tests in the given temporary directory"
190 190 " (implies --keep-tmpdir)")
191 191 parser.add_option("--unittest", action="store_true",
192 192 help="run tests with Python's unittest package"
193 193 " (this is an experimental feature)")
194 194 parser.add_option("-v", "--verbose", action="store_true",
195 195 help="output verbose messages")
196 196 parser.add_option("--view", type="string",
197 197 help="external diff viewer")
198 198 parser.add_option("--with-hg", type="string",
199 199 metavar="HG",
200 200 help="test using specified hg script rather than a "
201 201 "temporary installation")
202 202 parser.add_option("-3", "--py3k-warnings", action="store_true",
203 203 help="enable Py3k warnings on Python 2.6+")
204 204 parser.add_option('--extra-config-opt', action="append",
205 205 help='set the given config opt in the test hgrc')
206 206 parser.add_option('--random', action="store_true",
207 207 help='run tests in random order')
208 208
209 209 for option, (envvar, default) in defaults.items():
210 210 defaults[option] = type(default)(os.environ.get(envvar, default))
211 211 parser.set_defaults(**defaults)
212 212
213 213 return parser
214 214
215 215 def parseargs(args, parser):
216 216 """Parse arguments with our OptionParser and validate results."""
217 217 (options, args) = parser.parse_args(args)
218 218
219 219 # jython is always pure
220 220 if 'java' in sys.platform or '__pypy__' in sys.modules:
221 221 options.pure = True
222 222
223 223 if options.with_hg:
224 224 options.with_hg = os.path.expanduser(options.with_hg)
225 225 if not (os.path.isfile(options.with_hg) and
226 226 os.access(options.with_hg, os.X_OK)):
227 227 parser.error('--with-hg must specify an executable hg script')
228 228 if not os.path.basename(options.with_hg) == 'hg':
229 229 sys.stderr.write('warning: --with-hg should specify an hg script\n')
230 230 if options.local:
231 231 testdir = os.path.dirname(os.path.realpath(sys.argv[0]))
232 232 hgbin = os.path.join(os.path.dirname(testdir), 'hg')
233 233 if os.name != 'nt' and not os.access(hgbin, os.X_OK):
234 234 parser.error('--local specified, but %r not found or not executable'
235 235 % hgbin)
236 236 options.with_hg = hgbin
237 237
238 238 options.anycoverage = options.cover or options.annotate or options.htmlcov
239 239 if options.anycoverage:
240 240 try:
241 241 import coverage
242 242 covver = version.StrictVersion(coverage.__version__).version
243 243 if covver < (3, 3):
244 244 parser.error('coverage options require coverage 3.3 or later')
245 245 except ImportError:
246 246 parser.error('coverage options now require the coverage package')
247 247
248 248 if options.anycoverage and options.local:
249 249 # this needs some path mangling somewhere, I guess
250 250 parser.error("sorry, coverage options do not work when --local "
251 251 "is specified")
252 252
253 253 global verbose
254 254 if options.verbose:
255 255 verbose = ''
256 256
257 257 if options.tmpdir:
258 258 options.tmpdir = os.path.expanduser(options.tmpdir)
259 259
260 260 if options.jobs < 1:
261 261 parser.error('--jobs must be positive')
262 262 if options.unittest:
263 263 if options.jobs > 1:
264 264 sys.stderr.write(
265 265 'warning: --jobs has no effect with --unittest')
266 266 if options.loop:
267 267 sys.stderr.write(
268 268 'warning: --loop has no effect with --unittest')
269 269 if options.interactive and options.debug:
270 270 parser.error("-i/--interactive and -d/--debug are incompatible")
271 271 if options.debug:
272 272 if options.timeout != defaults['timeout']:
273 273 sys.stderr.write(
274 274 'warning: --timeout option ignored with --debug\n')
275 275 options.timeout = 0
276 276 if options.py3k_warnings:
277 277 if sys.version_info[:2] < (2, 6) or sys.version_info[:2] >= (3, 0):
278 278 parser.error('--py3k-warnings can only be used on Python 2.6+')
279 279 if options.blacklist:
280 280 options.blacklist = parselistfiles(options.blacklist, 'blacklist')
281 281 if options.whitelist:
282 282 options.whitelisted = parselistfiles(options.whitelist, 'whitelist')
283 283 else:
284 284 options.whitelisted = {}
285 285
286 286 return (options, args)
287 287
288 288 def rename(src, dst):
289 289 """Like os.rename(), trade atomicity and opened files friendliness
290 290 for existing destination support.
291 291 """
292 292 shutil.copy(src, dst)
293 293 os.remove(src)
294 294
295 295 def showdiff(expected, output, ref, err):
296 296 print
297 297 servefail = False
298 298 for line in difflib.unified_diff(expected, output, ref, err):
299 299 sys.stdout.write(line)
300 300 if not servefail and line.startswith(
301 301 '+ abort: child process failed to start'):
302 302 servefail = True
303 303 return {'servefail': servefail}
304 304
305 305
306 306 verbose = False
307 307 def vlog(*msg):
308 308 if verbose is not False:
309 309 iolock.acquire()
310 310 if verbose:
311 311 print verbose,
312 312 for m in msg:
313 313 print m,
314 314 print
315 315 sys.stdout.flush()
316 316 iolock.release()
317 317
318 318 def log(*msg):
319 319 iolock.acquire()
320 320 if verbose:
321 321 print verbose,
322 322 for m in msg:
323 323 print m,
324 324 print
325 325 sys.stdout.flush()
326 326 iolock.release()
327 327
328 328 def terminate(proc):
329 329 """Terminate subprocess (with fallback for Python versions < 2.6)"""
330 330 vlog('# Terminating process %d' % proc.pid)
331 331 try:
332 332 getattr(proc, 'terminate', lambda : os.kill(proc.pid, signal.SIGTERM))()
333 333 except OSError:
334 334 pass
335 335
336 336 def killdaemons(pidfile):
337 337 return killmod.killdaemons(pidfile, tryhard=False, remove=True,
338 338 logfn=vlog)
339 339
340 340 class Test(object):
341 341 """Encapsulates a single, runnable test.
342 342
343 343 Test instances can be run multiple times via run(). However, multiple
344 344 runs cannot be run concurrently.
345 345 """
346 346
347 347 # Status code reserved for skipped tests (used by hghave).
348 348 SKIPPED_STATUS = 80
349 349
350 350 def __init__(self, runner, test, count, refpath):
351 351 path = os.path.join(runner.testdir, test)
352 352 errpath = os.path.join(runner.testdir, '%s.err' % test)
353 353
354 354 self._runner = runner
355 355 self._testdir = runner.testdir
356 356 self._test = test
357 357 self._path = path
358 358 self._options = runner.options
359 359 self._count = count
360 360 self._daemonpids = []
361 361 self._refpath = refpath
362 362 self._errpath = errpath
363 363
364 364 # If we're not in --debug mode and reference output file exists,
365 365 # check test output against it.
366 366 if runner.options.debug:
367 367 self._refout = None # to match "out is None"
368 368 elif os.path.exists(refpath):
369 369 f = open(refpath, 'r')
370 370 self._refout = f.read().splitlines(True)
371 371 f.close()
372 372 else:
373 373 self._refout = []
374 374
375 375 self._threadtmp = os.path.join(runner.hgtmp, 'child%d' % count)
376 376 os.mkdir(self._threadtmp)
377 377
378 378 def cleanup(self):
379 379 for entry in self._daemonpids:
380 380 killdaemons(entry)
381 381
382 382 if self._threadtmp and not self._options.keep_tmpdir:
383 383 shutil.rmtree(self._threadtmp, True)
384 384
385 385 def run(self):
386 386 """Run this test instance.
387 387
388 388 This will return a tuple describing the result of the test.
389 389 """
390 390 if not os.path.exists(self._path):
391 391 return self.skip("Doesn't exist")
392 392
393 393 options = self._options
394 394 if not (options.whitelisted and self._test in options.whitelisted):
395 395 if options.blacklist and self._test in options.blacklist:
396 396 return self.skip('blacklisted')
397 397
398 398 if options.retest and not os.path.exists('%s.err' % self._test):
399 399 return self.ignore('not retesting')
400 400
401 401 if options.keywords:
402 402 f = open(self._test)
403 403 t = f.read().lower() + self._test.lower()
404 404 f.close()
405 405 for k in options.keywords.lower().split():
406 406 if k in t:
407 407 break
408 408 else:
409 409 return self.ignore("doesn't match keyword")
410 410
411 411 if not os.path.basename(self._test.lower()).startswith('test-'):
412 412 return self.skip('not a test file')
413 413
414 414 # Remove any previous output files.
415 415 if os.path.exists(self._errpath):
416 416 os.remove(self._errpath)
417 417
418 418 testtmp = os.path.join(self._threadtmp, os.path.basename(self._path))
419 419 os.mkdir(testtmp)
420 420 replacements, port = self._getreplacements(testtmp)
421 421 env = self._getenv(testtmp, port)
422 422 self._daemonpids.append(env['DAEMON_PIDS'])
423 423 self._createhgrc(env['HGRCPATH'])
424 424
425 425 vlog('# Test', self._test)
426 426
427 427 starttime = time.time()
428 428 try:
429 429 ret, out = self._run(testtmp, replacements, env)
430 430 duration = time.time() - starttime
431 431 except KeyboardInterrupt:
432 432 duration = time.time() - starttime
433 433 log('INTERRUPTED: %s (after %d seconds)' % (self._test, duration))
434 434 raise
435 435 except Exception, e:
436 436 return self.fail('Exception during execution: %s' % e, 255)
437 437
438 438 killdaemons(env['DAEMON_PIDS'])
439 439
440 440 if not options.keep_tmpdir:
441 441 shutil.rmtree(testtmp)
442 442
443 443 def describe(ret):
444 444 if ret < 0:
445 445 return 'killed by signal: %d' % -ret
446 446 return 'returned error code %d' % ret
447 447
448 448 skipped = False
449 449
450 450 if ret == self.SKIPPED_STATUS:
451 451 if out is None: # Debug mode, nothing to parse.
452 452 missing = ['unknown']
453 453 failed = None
454 454 else:
455 455 missing, failed = TTest.parsehghaveoutput(out)
456 456
457 457 if not missing:
458 458 missing = ['irrelevant']
459 459
460 460 if failed:
461 461 res = self.fail('hg have failed checking for %s' % failed[-1],
462 462 ret)
463 463 else:
464 464 skipped = True
465 465 res = self.skip(missing[-1])
466 466 elif ret == 'timeout':
467 467 res = self.fail('timed out', ret)
468 468 elif out != self._refout:
469 469 info = {}
470 470 if not options.nodiff:
471 471 iolock.acquire()
472 472 if options.view:
473 473 os.system("%s %s %s" % (options.view, self._refpath,
474 474 self._errpath))
475 475 else:
476 476 info = showdiff(self._refout, out, self._refpath,
477 477 self._errpath)
478 478 iolock.release()
479 479 msg = ''
480 480 if info.get('servefail'):
481 481 msg += 'serve failed and '
482 482 if ret:
483 483 msg += 'output changed and ' + describe(ret)
484 484 else:
485 485 msg += 'output changed'
486 486
487 487 if (ret != 0 or out != self._refout) and not skipped \
488 488 and not options.debug:
489 489 f = open(self._errpath, 'wb')
490 490 for line in out:
491 491 f.write(line)
492 492 f.close()
493 493 res = self.fail(msg, ret)
494 494 elif ret:
495 495 res = self.fail(describe(ret), ret)
496 496 else:
497 497 res = self.success()
498 498
499 499
500 500 vlog("# Ret was:", ret)
501 501
502 502 if not options.verbose:
503 503 iolock.acquire()
504 504 sys.stdout.write(res[0])
505 505 sys.stdout.flush()
506 506 iolock.release()
507 507
508 508 self._runner.times.append((self._test, duration))
509 509
510 510 return res
511 511
512 512 def _run(self, testtmp, replacements, env):
513 513 # This should be implemented in child classes to run tests.
514 514 return self._skip('unknown test type')
515 515
516 516 def _getreplacements(self, testtmp):
517 517 port = self._options.port + self._count * 3
518 518 r = [
519 519 (r':%s\b' % port, ':$HGPORT'),
520 520 (r':%s\b' % (port + 1), ':$HGPORT1'),
521 521 (r':%s\b' % (port + 2), ':$HGPORT2'),
522 522 ]
523 523
524 524 if os.name == 'nt':
525 525 r.append(
526 526 (''.join(c.isalpha() and '[%s%s]' % (c.lower(), c.upper()) or
527 527 c in '/\\' and r'[/\\]' or c.isdigit() and c or '\\' + c
528 528 for c in testtmp), '$TESTTMP'))
529 529 else:
530 530 r.append((re.escape(testtmp), '$TESTTMP'))
531 531
532 532 return r, port
533 533
534 534 def _getenv(self, testtmp, port):
535 535 env = os.environ.copy()
536 536 env['TESTTMP'] = testtmp
537 537 env['HOME'] = testtmp
538 538 env["HGPORT"] = str(port)
539 539 env["HGPORT1"] = str(port + 1)
540 540 env["HGPORT2"] = str(port + 2)
541 541 env["HGRCPATH"] = os.path.join(self._threadtmp, '.hgrc')
542 542 env["DAEMON_PIDS"] = os.path.join(self._threadtmp, 'daemon.pids')
543 543 env["HGEDITOR"] = sys.executable + ' -c "import sys; sys.exit(0)"'
544 544 env["HGMERGE"] = "internal:merge"
545 545 env["HGUSER"] = "test"
546 546 env["HGENCODING"] = "ascii"
547 547 env["HGENCODINGMODE"] = "strict"
548 548
549 549 # Reset some environment variables to well-known values so that
550 550 # the tests produce repeatable output.
551 551 env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C'
552 552 env['TZ'] = 'GMT'
553 553 env["EMAIL"] = "Foo Bar <foo.bar@example.com>"
554 554 env['COLUMNS'] = '80'
555 555 env['TERM'] = 'xterm'
556 556
557 557 for k in ('HG HGPROF CDPATH GREP_OPTIONS http_proxy no_proxy ' +
558 558 'NO_PROXY').split():
559 559 if k in env:
560 560 del env[k]
561 561
562 562 # unset env related to hooks
563 563 for k in env.keys():
564 564 if k.startswith('HG_'):
565 565 del env[k]
566 566
567 567 return env
568 568
569 569 def _createhgrc(self, path):
570 570 # create a fresh hgrc
571 571 hgrc = open(path, 'w')
572 572 hgrc.write('[ui]\n')
573 573 hgrc.write('slash = True\n')
574 574 hgrc.write('interactive = False\n')
575 575 hgrc.write('[defaults]\n')
576 576 hgrc.write('backout = -d "0 0"\n')
577 577 hgrc.write('commit = -d "0 0"\n')
578 578 hgrc.write('shelve = --date "0 0"\n')
579 579 hgrc.write('tag = -d "0 0"\n')
580 580 if self._options.extra_config_opt:
581 581 for opt in self._options.extra_config_opt:
582 582 section, key = opt.split('.', 1)
583 583 assert '=' in key, ('extra config opt %s must '
584 584 'have an = for assignment' % opt)
585 585 hgrc.write('[%s]\n%s\n' % (section, key))
586 586 hgrc.close()
587 587
588 588 def success(self):
589 589 return '.', self._test, ''
590 590
591 591 def fail(self, msg, ret):
592 592 warned = ret is False
593 593 if not self._options.nodiff:
594 594 log("\n%s: %s %s" % (warned and 'Warning' or 'ERROR', self._test,
595 595 msg))
596 596 if (not ret and self._options.interactive and
597 597 os.path.exists(self._errpath)):
598 598 iolock.acquire()
599 599 print 'Accept this change? [n] ',
600 600 answer = sys.stdin.readline().strip()
601 601 iolock.release()
602 602 if answer.lower() in ('y', 'yes'):
603 603 if self._test.endswith('.t'):
604 604 rename(self._errpath, self._path)
605 605 else:
606 606 rename(self._errpath, '%s.out' % self._path)
607 607
608 608 return '.', self._test, ''
609 609
610 610 return warned and '~' or '!', self._test, msg
611 611
612 612 def skip(self, msg):
613 613 if self._options.verbose:
614 614 log("\nSkipping %s: %s" % (self._path, msg))
615 615
616 616 return 's', self._test, msg
617 617
618 618 def ignore(self, msg):
619 619 return 'i', self._test, msg
620 620
621 621 class PythonTest(Test):
622 622 """A Python-based test."""
623 623 def _run(self, testtmp, replacements, env):
624 624 py3kswitch = self._options.py3k_warnings and ' -3' or ''
625 625 cmd = '%s%s "%s"' % (PYTHON, py3kswitch, self._path)
626 626 vlog("# Running", cmd)
627 627 if os.name == 'nt':
628 628 replacements.append((r'\r\n', '\n'))
629 629 return run(cmd, testtmp, self._options, replacements, env,
630 630 self._runner.abort)
631 631
632 632 class TTest(Test):
633 633 """A "t test" is a test backed by a .t file."""
634 634
635 635 SKIPPED_PREFIX = 'skipped: '
636 636 FAILED_PREFIX = 'hghave check failed: '
637 637 NEEDESCAPE = re.compile(r'[\x00-\x08\x0b-\x1f\x7f-\xff]').search
638 638
639 639 ESCAPESUB = re.compile(r'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub
640 640 ESCAPEMAP = dict((chr(i), r'\x%02x' % i) for i in range(256)).update(
641 641 {'\\': '\\\\', '\r': r'\r'})
642 642
643 643 def _run(self, testtmp, replacements, env):
644 644 f = open(self._path)
645 645 lines = f.readlines()
646 646 f.close()
647 647
648 648 salt, script, after, expected = self._parsetest(lines, testtmp)
649 649
650 650 # Write out the generated script.
651 651 fname = '%s.sh' % testtmp
652 652 f = open(fname, 'w')
653 653 for l in script:
654 654 f.write(l)
655 655 f.close()
656 656
657 657 cmd = '%s "%s"' % (self._options.shell, fname)
658 658 vlog("# Running", cmd)
659 659
660 660 exitcode, output = run(cmd, testtmp, self._options, replacements, env,
661 661 self._runner.abort)
662 662 # Do not merge output if skipped. Return hghave message instead.
663 663 # Similarly, with --debug, output is None.
664 664 if exitcode == self.SKIPPED_STATUS or output is None:
665 665 return exitcode, output
666 666
667 667 return self._processoutput(exitcode, output, salt, after, expected)
668 668
669 669 def _hghave(self, reqs, testtmp):
670 670 # TODO do something smarter when all other uses of hghave are gone.
671 671 tdir = self._testdir.replace('\\', '/')
672 672 proc = Popen4('%s -c "%s/hghave %s"' %
673 673 (self._options.shell, tdir, ' '.join(reqs)),
674 674 testtmp, 0)
675 675 stdout, stderr = proc.communicate()
676 676 ret = proc.wait()
677 677 if wifexited(ret):
678 678 ret = os.WEXITSTATUS(ret)
679 679 if ret == 2:
680 680 print stdout
681 681 sys.exit(1)
682 682
683 683 return ret == 0
684 684
685 685 def _parsetest(self, lines, testtmp):
686 686 # We generate a shell script which outputs unique markers to line
687 687 # up script results with our source. These markers include input
688 688 # line number and the last return code.
689 689 salt = "SALT" + str(time.time())
690 690 def addsalt(line, inpython):
691 691 if inpython:
692 692 script.append('%s %d 0\n' % (salt, line))
693 693 else:
694 694 script.append('echo %s %s $?\n' % (salt, line))
695 695
696 696 script = []
697 697
698 698 # After we run the shell script, we re-unify the script output
699 699 # with non-active parts of the source, with synchronization by our
700 700 # SALT line number markers. The after table contains the non-active
701 701 # components, ordered by line number.
702 702 after = {}
703 703
704 704 # Expected shell script output.
705 705 expected = {}
706 706
707 707 pos = prepos = -1
708 708
709 709 # True or False when in a true or false conditional section
710 710 skipping = None
711 711
712 712 # We keep track of whether or not we're in a Python block so we
713 713 # can generate the surrounding doctest magic.
714 714 inpython = False
715 715
716 716 if self._options.debug:
717 717 script.append('set -x\n')
718 718 if os.getenv('MSYSTEM'):
719 719 script.append('alias pwd="pwd -W"\n')
720 720
721 721 for n, l in enumerate(lines):
722 722 if not l.endswith('\n'):
723 723 l += '\n'
724 724 if l.startswith('#if'):
725 725 lsplit = l.split()
726 726 if len(lsplit) < 2 or lsplit[0] != '#if':
727 727 after.setdefault(pos, []).append(' !!! invalid #if\n')
728 728 if skipping is not None:
729 729 after.setdefault(pos, []).append(' !!! nested #if\n')
730 730 skipping = not self._hghave(lsplit[1:], testtmp)
731 731 after.setdefault(pos, []).append(l)
732 732 elif l.startswith('#else'):
733 733 if skipping is None:
734 734 after.setdefault(pos, []).append(' !!! missing #if\n')
735 735 skipping = not skipping
736 736 after.setdefault(pos, []).append(l)
737 737 elif l.startswith('#endif'):
738 738 if skipping is None:
739 739 after.setdefault(pos, []).append(' !!! missing #if\n')
740 740 skipping = None
741 741 after.setdefault(pos, []).append(l)
742 742 elif skipping:
743 743 after.setdefault(pos, []).append(l)
744 744 elif l.startswith(' >>> '): # python inlines
745 745 after.setdefault(pos, []).append(l)
746 746 prepos = pos
747 747 pos = n
748 748 if not inpython:
749 749 # We've just entered a Python block. Add the header.
750 750 inpython = True
751 751 addsalt(prepos, False) # Make sure we report the exit code.
752 752 script.append('%s -m heredoctest <<EOF\n' % PYTHON)
753 753 addsalt(n, True)
754 754 script.append(l[2:])
755 755 elif l.startswith(' ... '): # python inlines
756 756 after.setdefault(prepos, []).append(l)
757 757 script.append(l[2:])
758 758 elif l.startswith(' $ '): # commands
759 759 if inpython:
760 760 script.append('EOF\n')
761 761 inpython = False
762 762 after.setdefault(pos, []).append(l)
763 763 prepos = pos
764 764 pos = n
765 765 addsalt(n, False)
766 766 cmd = l[4:].split()
767 767 if len(cmd) == 2 and cmd[0] == 'cd':
768 768 l = ' $ cd %s || exit 1\n' % cmd[1]
769 769 script.append(l[4:])
770 770 elif l.startswith(' > '): # continuations
771 771 after.setdefault(prepos, []).append(l)
772 772 script.append(l[4:])
773 773 elif l.startswith(' '): # results
774 774 # Queue up a list of expected results.
775 775 expected.setdefault(pos, []).append(l[2:])
776 776 else:
777 777 if inpython:
778 778 script.append('EOF\n')
779 779 inpython = False
780 780 # Non-command/result. Queue up for merged output.
781 781 after.setdefault(pos, []).append(l)
782 782
783 783 if inpython:
784 784 script.append('EOF\n')
785 785 if skipping is not None:
786 786 after.setdefault(pos, []).append(' !!! missing #endif\n')
787 787 addsalt(n + 1, False)
788 788
789 789 return salt, script, after, expected
790 790
791 791 def _processoutput(self, exitcode, output, salt, after, expected):
792 792 # Merge the script output back into a unified test.
793 793 warnonly = 1 # 1: not yet; 2: yes; 3: for sure not
794 794 if exitcode != 0:
795 795 warnonly = 3
796 796
797 797 pos = -1
798 798 postout = []
799 799 for l in output:
800 800 lout, lcmd = l, None
801 801 if salt in l:
802 802 lout, lcmd = l.split(salt, 1)
803 803
804 804 if lout:
805 805 if not lout.endswith('\n'):
806 806 lout += ' (no-eol)\n'
807 807
808 808 # Find the expected output at the current position.
809 809 el = None
810 810 if expected.get(pos, None):
811 811 el = expected[pos].pop(0)
812 812
813 813 r = TTest.linematch(el, lout)
814 814 if isinstance(r, str):
815 815 if r == '+glob':
816 816 lout = el[:-1] + ' (glob)\n'
817 817 r = '' # Warn only this line.
818 818 elif r == '-glob':
819 819 lout = ''.join(el.rsplit(' (glob)', 1))
820 820 r = '' # Warn only this line.
821 821 else:
822 822 log('\ninfo, unknown linematch result: %r\n' % r)
823 823 r = False
824 824 if r:
825 825 postout.append(' ' + el)
826 826 else:
827 827 if self.NEEDESCAPE(lout):
828 828 lout = TTest.stringescape('%s (esc)\n' %
829 829 lout.rstrip('\n'))
830 830 postout.append(' ' + lout) # Let diff deal with it.
831 831 if r != '': # If line failed.
832 832 warnonly = 3 # for sure not
833 833 elif warnonly == 1: # Is "not yet" and line is warn only.
834 834 warnonly = 2 # Yes do warn.
835 835
836 836 if lcmd:
837 837 # Add on last return code.
838 838 ret = int(lcmd.split()[1])
839 839 if ret != 0:
840 840 postout.append(' [%s]\n' % ret)
841 841 if pos in after:
842 842 # Merge in non-active test bits.
843 843 postout += after.pop(pos)
844 844 pos = int(lcmd.split()[0])
845 845
846 846 if pos in after:
847 847 postout += after.pop(pos)
848 848
849 849 if warnonly == 2:
850 850 exitcode = False # Set exitcode to warned.
851 851
852 852 return exitcode, postout
853 853
854 854 @staticmethod
855 855 def rematch(el, l):
856 856 try:
857 857 # use \Z to ensure that the regex matches to the end of the string
858 858 if os.name == 'nt':
859 859 return re.match(el + r'\r?\n\Z', l)
860 860 return re.match(el + r'\n\Z', l)
861 861 except re.error:
862 862 # el is an invalid regex
863 863 return False
864 864
865 865 @staticmethod
866 866 def globmatch(el, l):
867 867 # The only supported special characters are * and ? plus / which also
868 868 # matches \ on windows. Escaping of these characters is supported.
869 869 if el + '\n' == l:
870 870 if os.altsep:
871 871 # matching on "/" is not needed for this line
872 872 return '-glob'
873 873 return True
874 874 i, n = 0, len(el)
875 875 res = ''
876 876 while i < n:
877 877 c = el[i]
878 878 i += 1
879 879 if c == '\\' and el[i] in '*?\\/':
880 880 res += el[i - 1:i + 1]
881 881 i += 1
882 882 elif c == '*':
883 883 res += '.*'
884 884 elif c == '?':
885 885 res += '.'
886 886 elif c == '/' and os.altsep:
887 887 res += '[/\\\\]'
888 888 else:
889 889 res += re.escape(c)
890 890 return TTest.rematch(res, l)
891 891
892 892 @staticmethod
893 893 def linematch(el, l):
894 894 if el == l: # perfect match (fast)
895 895 return True
896 896 if el:
897 897 if el.endswith(" (esc)\n"):
898 898 el = el[:-7].decode('string-escape') + '\n'
899 899 if el == l or os.name == 'nt' and el[:-1] + '\r\n' == l:
900 900 return True
901 901 if el.endswith(" (re)\n"):
902 902 return TTest.rematch(el[:-6], l)
903 903 if el.endswith(" (glob)\n"):
904 904 return TTest.globmatch(el[:-8], l)
905 905 if os.altsep and l.replace('\\', '/') == el:
906 906 return '+glob'
907 907 return False
908 908
909 909 @staticmethod
910 910 def parsehghaveoutput(lines):
911 911 '''Parse hghave log lines.
912 912
913 913 Return tuple of lists (missing, failed):
914 914 * the missing/unknown features
915 915 * the features for which existence check failed'''
916 916 missing = []
917 917 failed = []
918 918 for line in lines:
919 919 if line.startswith(TTest.SKIPPED_PREFIX):
920 920 line = line.splitlines()[0]
921 921 missing.append(line[len(TTest.SKIPPED_PREFIX):])
922 922 elif line.startswith(TTest.FAILED_PREFIX):
923 923 line = line.splitlines()[0]
924 924 failed.append(line[len(TTest.FAILED_PREFIX):])
925 925
926 926 return missing, failed
927 927
928 928 @staticmethod
929 929 def _escapef(m):
930 930 return TTest.ESCAPEMAP[m.group(0)]
931 931
932 932 @staticmethod
933 933 def _stringescape(s):
934 934 return TTest.ESCAPESUB(TTest._escapef, s)
935 935
936 936
937 937 wifexited = getattr(os, "WIFEXITED", lambda x: False)
938 938 def run(cmd, wd, options, replacements, env, abort):
939 939 """Run command in a sub-process, capturing the output (stdout and stderr).
940 940 Return a tuple (exitcode, output). output is None in debug mode."""
941 941 # TODO: Use subprocess.Popen if we're running on Python 2.4
942 942 if options.debug:
943 943 proc = subprocess.Popen(cmd, shell=True, cwd=wd, env=env)
944 944 ret = proc.wait()
945 945 return (ret, None)
946 946
947 947 proc = Popen4(cmd, wd, options.timeout, env)
948 948 def cleanup():
949 949 terminate(proc)
950 950 ret = proc.wait()
951 951 if ret == 0:
952 952 ret = signal.SIGTERM << 8
953 953 killdaemons(env['DAEMON_PIDS'])
954 954 return ret
955 955
956 956 output = ''
957 957 proc.tochild.close()
958 958
959 959 try:
960 960 output = proc.fromchild.read()
961 961 except KeyboardInterrupt:
962 962 vlog('# Handling keyboard interrupt')
963 963 cleanup()
964 964 raise
965 965
966 966 ret = proc.wait()
967 967 if wifexited(ret):
968 968 ret = os.WEXITSTATUS(ret)
969 969
970 970 if proc.timeout:
971 971 ret = 'timeout'
972 972
973 973 if ret:
974 974 killdaemons(env['DAEMON_PIDS'])
975 975
976 976 if abort[0]:
977 977 raise KeyboardInterrupt()
978 978
979 979 for s, r in replacements:
980 980 output = re.sub(s, r, output)
981 981 return ret, output.splitlines(True)
982 982
983 983 iolock = threading.Lock()
984 984
985 class TestResult(unittest._TextTestResult):
986 """Holds results when executing via unittest."""
987 # Don't worry too much about accessing the non-public _TextTestResult.
988 # It is relatively common in Python testing tools.
989 def __init__(self, *args, **kwargs):
990 super(TestResult, self).__init__(*args, **kwargs)
991
992 class TextTestRunner(unittest.TextTestRunner):
993 """Custom unittest test runner that uses appropriate settings."""
994
995 def _makeResult(self):
996 return TestResult(self.stream, self.descriptions, self.verbosity)
997
985 998 class TestRunner(object):
986 999 """Holds context for executing tests.
987 1000
988 1001 Tests rely on a lot of state. This object holds it for them.
989 1002 """
990 1003
991 1004 REQUIREDTOOLS = [
992 1005 os.path.basename(sys.executable),
993 1006 'diff',
994 1007 'grep',
995 1008 'unzip',
996 1009 'gunzip',
997 1010 'bunzip2',
998 1011 'sed',
999 1012 ]
1000 1013
1001 1014 TESTTYPES = [
1002 1015 ('.py', PythonTest, '.out'),
1003 1016 ('.t', TTest, ''),
1004 1017 ]
1005 1018
1006 1019 def __init__(self):
1007 1020 self.options = None
1008 1021 self.testdir = None
1009 1022 self.hgtmp = None
1010 1023 self.inst = None
1011 1024 self.bindir = None
1012 1025 self.tmpbinddir = None
1013 1026 self.pythondir = None
1014 1027 self.coveragefile = None
1015 1028 self.times = [] # Holds execution times of tests.
1016 1029 self.results = {
1017 1030 '.': [],
1018 1031 '!': [],
1019 1032 '~': [],
1020 1033 's': [],
1021 1034 'i': [],
1022 1035 }
1023 1036 self.abort = [False]
1024 1037 self._createdfiles = []
1025 1038 self._hgpath = None
1026 1039
1027 1040 def run(self, args, parser=None):
1028 1041 """Run the test suite."""
1029 1042 oldmask = os.umask(022)
1030 1043 try:
1031 1044 parser = parser or getparser()
1032 1045 options, args = parseargs(args, parser)
1033 1046 self.options = options
1034 1047
1035 1048 self._checktools()
1036 1049 tests = self.findtests(args)
1037 1050 return self._run(tests)
1038 1051 finally:
1039 1052 os.umask(oldmask)
1040 1053
1041 1054 def _run(self, tests):
1042 1055 if self.options.random:
1043 1056 random.shuffle(tests)
1044 1057 else:
1045 1058 # keywords for slow tests
1046 1059 slow = 'svn gendoc check-code-hg'.split()
1047 1060 def sortkey(f):
1048 1061 # run largest tests first, as they tend to take the longest
1049 1062 try:
1050 1063 val = -os.stat(f).st_size
1051 1064 except OSError, e:
1052 1065 if e.errno != errno.ENOENT:
1053 1066 raise
1054 1067 return -1e9 # file does not exist, tell early
1055 1068 for kw in slow:
1056 1069 if kw in f:
1057 1070 val *= 10
1058 1071 return val
1059 1072 tests.sort(key=sortkey)
1060 1073
1061 1074 self.testdir = os.environ['TESTDIR'] = os.getcwd()
1062 1075
1063 1076 if 'PYTHONHASHSEED' not in os.environ:
1064 1077 # use a random python hash seed all the time
1065 1078 # we do the randomness ourself to know what seed is used
1066 1079 os.environ['PYTHONHASHSEED'] = str(random.getrandbits(32))
1067 1080
1068 1081 if self.options.tmpdir:
1069 1082 self.options.keep_tmpdir = True
1070 1083 tmpdir = self.options.tmpdir
1071 1084 if os.path.exists(tmpdir):
1072 1085 # Meaning of tmpdir has changed since 1.3: we used to create
1073 1086 # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if
1074 1087 # tmpdir already exists.
1075 1088 print "error: temp dir %r already exists" % tmpdir
1076 1089 return 1
1077 1090
1078 1091 # Automatically removing tmpdir sounds convenient, but could
1079 1092 # really annoy anyone in the habit of using "--tmpdir=/tmp"
1080 1093 # or "--tmpdir=$HOME".
1081 1094 #vlog("# Removing temp dir", tmpdir)
1082 1095 #shutil.rmtree(tmpdir)
1083 1096 os.makedirs(tmpdir)
1084 1097 else:
1085 1098 d = None
1086 1099 if os.name == 'nt':
1087 1100 # without this, we get the default temp dir location, but
1088 1101 # in all lowercase, which causes troubles with paths (issue3490)
1089 1102 d = os.getenv('TMP')
1090 1103 tmpdir = tempfile.mkdtemp('', 'hgtests.', d)
1091 1104 self.hgtmp = os.environ['HGTMP'] = os.path.realpath(tmpdir)
1092 1105
1093 1106 if self.options.with_hg:
1094 1107 self.inst = None
1095 1108 self.bindir = os.path.dirname(os.path.realpath(
1096 1109 self.options.with_hg))
1097 1110 self.tmpbindir = os.path.join(self.hgtmp, 'install', 'bin')
1098 1111 os.makedirs(self.tmpbindir)
1099 1112
1100 1113 # This looks redundant with how Python initializes sys.path from
1101 1114 # the location of the script being executed. Needed because the
1102 1115 # "hg" specified by --with-hg is not the only Python script
1103 1116 # executed in the test suite that needs to import 'mercurial'
1104 1117 # ... which means it's not really redundant at all.
1105 1118 self.pythondir = self.bindir
1106 1119 else:
1107 1120 self.inst = os.path.join(self.hgtmp, "install")
1108 1121 self.bindir = os.environ["BINDIR"] = os.path.join(self.inst,
1109 1122 "bin")
1110 1123 self.tmpbindir = self.bindir
1111 1124 self.pythondir = os.path.join(self.inst, "lib", "python")
1112 1125
1113 1126 os.environ["BINDIR"] = self.bindir
1114 1127 os.environ["PYTHON"] = PYTHON
1115 1128
1116 1129 path = [self.bindir] + os.environ["PATH"].split(os.pathsep)
1117 1130 if self.tmpbindir != self.bindir:
1118 1131 path = [self.tmpbindir] + path
1119 1132 os.environ["PATH"] = os.pathsep.join(path)
1120 1133
1121 1134 # Include TESTDIR in PYTHONPATH so that out-of-tree extensions
1122 1135 # can run .../tests/run-tests.py test-foo where test-foo
1123 1136 # adds an extension to HGRC. Also include run-test.py directory to
1124 1137 # import modules like heredoctest.
1125 1138 pypath = [self.pythondir, self.testdir,
1126 1139 os.path.abspath(os.path.dirname(__file__))]
1127 1140 # We have to augment PYTHONPATH, rather than simply replacing
1128 1141 # it, in case external libraries are only available via current
1129 1142 # PYTHONPATH. (In particular, the Subversion bindings on OS X
1130 1143 # are in /opt/subversion.)
1131 1144 oldpypath = os.environ.get(IMPL_PATH)
1132 1145 if oldpypath:
1133 1146 pypath.append(oldpypath)
1134 1147 os.environ[IMPL_PATH] = os.pathsep.join(pypath)
1135 1148
1136 1149 self.coveragefile = os.path.join(self.testdir, '.coverage')
1137 1150
1138 1151 vlog("# Using TESTDIR", self.testdir)
1139 1152 vlog("# Using HGTMP", self.hgtmp)
1140 1153 vlog("# Using PATH", os.environ["PATH"])
1141 1154 vlog("# Using", IMPL_PATH, os.environ[IMPL_PATH])
1142 1155
1143 1156 try:
1144 1157 return self._runtests(tests) or 0
1145 1158 finally:
1146 1159 time.sleep(.1)
1147 1160 self._cleanup()
1148 1161
1149 1162 def findtests(self, args):
1150 1163 """Finds possible test files from arguments.
1151 1164
1152 1165 If you wish to inject custom tests into the test harness, this would
1153 1166 be a good function to monkeypatch or override in a derived class.
1154 1167 """
1155 1168 if not args:
1156 1169 if self.options.changed:
1157 1170 proc = Popen4('hg st --rev "%s" -man0 .' %
1158 1171 self.options.changed, None, 0)
1159 1172 stdout, stderr = proc.communicate()
1160 1173 args = stdout.strip('\0').split('\0')
1161 1174 else:
1162 1175 args = os.listdir('.')
1163 1176
1164 1177 return [t for t in args
1165 1178 if os.path.basename(t).startswith('test-')
1166 1179 and (t.endswith('.py') or t.endswith('.t'))]
1167 1180
1168 1181 def _runtests(self, tests):
1169 1182 try:
1170 1183 if self.inst:
1171 1184 self._installhg()
1172 1185 self._checkhglib("Testing")
1173 1186 else:
1174 1187 self._usecorrectpython()
1175 1188
1176 1189 if self.options.restart:
1177 1190 orig = list(tests)
1178 1191 while tests:
1179 1192 if os.path.exists(tests[0] + ".err"):
1180 1193 break
1181 1194 tests.pop(0)
1182 1195 if not tests:
1183 1196 print "running all tests"
1184 1197 tests = orig
1185 1198
1186 1199 if self.options.unittest:
1187 1200 suite = unittest.TestSuite()
1188 1201 for count, testpath in enumerate(tests):
1189 1202 suite.addTest(self._gettest(testpath, count, asunit=True))
1190 1203
1191 1204 verbosity = 1
1192 1205 if self.options.verbose:
1193 1206 verbosity = 2
1194 runner = unittest.TextTestRunner(verbosity=verbosity)
1207 runner = TextTestRunner(verbosity=verbosity)
1195 1208 runner.run(suite)
1196 1209 else:
1197 1210 self._executetests(tests)
1198 1211
1199 1212 failed = len(self.results['!'])
1200 1213 warned = len(self.results['~'])
1201 1214 tested = len(self.results['.']) + failed + warned
1202 1215 skipped = len(self.results['s'])
1203 1216 ignored = len(self.results['i'])
1204 1217
1205 1218 print
1206 1219 if not self.options.noskips:
1207 1220 for s in self.results['s']:
1208 1221 print "Skipped %s: %s" % s
1209 1222 for s in self.results['~']:
1210 1223 print "Warned %s: %s" % s
1211 1224 for s in self.results['!']:
1212 1225 print "Failed %s: %s" % s
1213 1226 self._checkhglib("Tested")
1214 1227 print "# Ran %d tests, %d skipped, %d warned, %d failed." % (
1215 1228 tested, skipped + ignored, warned, failed)
1216 1229 if self.results['!']:
1217 1230 print 'python hash seed:', os.environ['PYTHONHASHSEED']
1218 1231 if self.options.time:
1219 1232 self._outputtimes()
1220 1233
1221 1234 if self.options.anycoverage:
1222 1235 self._outputcoverage()
1223 1236 except KeyboardInterrupt:
1224 1237 failed = True
1225 1238 print "\ninterrupted!"
1226 1239
1227 1240 if failed:
1228 1241 return 1
1229 1242 if warned:
1230 1243 return 80
1231 1244
1232 1245 def _gettest(self, test, count, asunit=False):
1233 1246 """Obtain a Test by looking at its filename.
1234 1247
1235 1248 Returns a Test instance. The Test may not be runnable if it doesn't
1236 1249 map to a known type.
1237 1250 """
1238 1251 lctest = test.lower()
1239 1252 refpath = os.path.join(self.testdir, test)
1240 1253
1241 1254 testcls = Test
1242 1255
1243 1256 for ext, cls, out in self.TESTTYPES:
1244 1257 if lctest.endswith(ext):
1245 1258 testcls = cls
1246 1259 refpath = os.path.join(self.testdir, test + out)
1247 1260 break
1248 1261
1249 1262 t = testcls(self, test, count, refpath)
1250 1263
1251 1264 if not asunit:
1252 1265 return t
1253 1266
1254 1267 class MercurialTest(unittest.TestCase):
1255 1268 def __init__(self, name, *args, **kwargs):
1256 1269 super(MercurialTest, self).__init__(*args, **kwargs)
1257 1270 self.name = name
1258 1271
1259 1272 def shortDescription(self):
1260 1273 return self.name
1261 1274
1262 1275 # Need to stash away the TestResult since we do custom things
1263 1276 # with it.
1264 1277 def run(self, result):
1265 1278 self._result = result
1266 1279
1267 1280 return super(MercurialTest, self).run(result)
1268 1281
1269 1282 def runTest(self):
1270 1283 code, tname, msg = t.run()
1271 1284
1272 1285 if code == '!':
1273 1286 self._result.failures.append((self, msg))
1274 1287 elif code == '~':
1275 1288 pass
1276 1289 elif code == '.':
1277 1290 pass
1278 1291 elif code == 's':
1279 1292 pass
1280 1293 elif code == 'i':
1281 1294 pass
1282 1295 else:
1283 1296 self.fail('Unknown test result code: %s' % code)
1284 1297
1285 1298 return MercurialTest(test)
1286 1299
1287 1300 def _cleanup(self):
1288 1301 """Clean up state from this test invocation."""
1289 1302
1290 1303 if self.options.keep_tmpdir:
1291 1304 return
1292 1305
1293 1306 vlog("# Cleaning up HGTMP", self.hgtmp)
1294 1307 shutil.rmtree(self.hgtmp, True)
1295 1308 for f in self._createdfiles:
1296 1309 try:
1297 1310 os.remove(f)
1298 1311 except OSError:
1299 1312 pass
1300 1313
1301 1314 def _usecorrectpython(self):
1302 1315 # Some tests run the Python interpreter. They must use the
1303 1316 # same interpreter or bad things will happen.
1304 1317 pyexename = sys.platform == 'win32' and 'python.exe' or 'python'
1305 1318 if getattr(os, 'symlink', None):
1306 1319 vlog("# Making python executable in test path a symlink to '%s'" %
1307 1320 sys.executable)
1308 1321 mypython = os.path.join(self.tmpbindir, pyexename)
1309 1322 try:
1310 1323 if os.readlink(mypython) == sys.executable:
1311 1324 return
1312 1325 os.unlink(mypython)
1313 1326 except OSError, err:
1314 1327 if err.errno != errno.ENOENT:
1315 1328 raise
1316 1329 if self._findprogram(pyexename) != sys.executable:
1317 1330 try:
1318 1331 os.symlink(sys.executable, mypython)
1319 1332 self._createdfiles.append(mypython)
1320 1333 except OSError, err:
1321 1334 # child processes may race, which is harmless
1322 1335 if err.errno != errno.EEXIST:
1323 1336 raise
1324 1337 else:
1325 1338 exedir, exename = os.path.split(sys.executable)
1326 1339 vlog("# Modifying search path to find %s as %s in '%s'" %
1327 1340 (exename, pyexename, exedir))
1328 1341 path = os.environ['PATH'].split(os.pathsep)
1329 1342 while exedir in path:
1330 1343 path.remove(exedir)
1331 1344 os.environ['PATH'] = os.pathsep.join([exedir] + path)
1332 1345 if not self._findprogram(pyexename):
1333 1346 print "WARNING: Cannot find %s in search path" % pyexename
1334 1347
1335 1348 def _installhg(self):
1336 1349 vlog("# Performing temporary installation of HG")
1337 1350 installerrs = os.path.join("tests", "install.err")
1338 1351 compiler = ''
1339 1352 if self.options.compiler:
1340 1353 compiler = '--compiler ' + self.options.compiler
1341 1354 pure = self.options.pure and "--pure" or ""
1342 1355 py3 = ''
1343 1356 if sys.version_info[0] == 3:
1344 1357 py3 = '--c2to3'
1345 1358
1346 1359 # Run installer in hg root
1347 1360 script = os.path.realpath(sys.argv[0])
1348 1361 hgroot = os.path.dirname(os.path.dirname(script))
1349 1362 os.chdir(hgroot)
1350 1363 nohome = '--home=""'
1351 1364 if os.name == 'nt':
1352 1365 # The --home="" trick works only on OS where os.sep == '/'
1353 1366 # because of a distutils convert_path() fast-path. Avoid it at
1354 1367 # least on Windows for now, deal with .pydistutils.cfg bugs
1355 1368 # when they happen.
1356 1369 nohome = ''
1357 1370 cmd = ('%(exe)s setup.py %(py3)s %(pure)s clean --all'
1358 1371 ' build %(compiler)s --build-base="%(base)s"'
1359 1372 ' install --force --prefix="%(prefix)s"'
1360 1373 ' --install-lib="%(libdir)s"'
1361 1374 ' --install-scripts="%(bindir)s" %(nohome)s >%(logfile)s 2>&1'
1362 1375 % {'exe': sys.executable, 'py3': py3, 'pure': pure,
1363 1376 'compiler': compiler,
1364 1377 'base': os.path.join(self.hgtmp, "build"),
1365 1378 'prefix': self.inst, 'libdir': self.pythondir,
1366 1379 'bindir': self.bindir,
1367 1380 'nohome': nohome, 'logfile': installerrs})
1368 1381 vlog("# Running", cmd)
1369 1382 if os.system(cmd) == 0:
1370 1383 if not self.options.verbose:
1371 1384 os.remove(installerrs)
1372 1385 else:
1373 1386 f = open(installerrs)
1374 1387 for line in f:
1375 1388 print line,
1376 1389 f.close()
1377 1390 sys.exit(1)
1378 1391 os.chdir(self.testdir)
1379 1392
1380 1393 self._usecorrectpython()
1381 1394
1382 1395 if self.options.py3k_warnings and not self.options.anycoverage:
1383 1396 vlog("# Updating hg command to enable Py3k Warnings switch")
1384 1397 f = open(os.path.join(self.bindir, 'hg'), 'r')
1385 1398 lines = [line.rstrip() for line in f]
1386 1399 lines[0] += ' -3'
1387 1400 f.close()
1388 1401 f = open(os.path.join(self.bindir, 'hg'), 'w')
1389 1402 for line in lines:
1390 1403 f.write(line + '\n')
1391 1404 f.close()
1392 1405
1393 1406 hgbat = os.path.join(self.bindir, 'hg.bat')
1394 1407 if os.path.isfile(hgbat):
1395 1408 # hg.bat expects to be put in bin/scripts while run-tests.py
1396 1409 # installation layout put it in bin/ directly. Fix it
1397 1410 f = open(hgbat, 'rb')
1398 1411 data = f.read()
1399 1412 f.close()
1400 1413 if '"%~dp0..\python" "%~dp0hg" %*' in data:
1401 1414 data = data.replace('"%~dp0..\python" "%~dp0hg" %*',
1402 1415 '"%~dp0python" "%~dp0hg" %*')
1403 1416 f = open(hgbat, 'wb')
1404 1417 f.write(data)
1405 1418 f.close()
1406 1419 else:
1407 1420 print 'WARNING: cannot fix hg.bat reference to python.exe'
1408 1421
1409 1422 if self.options.anycoverage:
1410 1423 custom = os.path.join(self.testdir, 'sitecustomize.py')
1411 1424 target = os.path.join(self.pythondir, 'sitecustomize.py')
1412 1425 vlog('# Installing coverage trigger to %s' % target)
1413 1426 shutil.copyfile(custom, target)
1414 1427 rc = os.path.join(self.testdir, '.coveragerc')
1415 1428 vlog('# Installing coverage rc to %s' % rc)
1416 1429 os.environ['COVERAGE_PROCESS_START'] = rc
1417 1430 fn = os.path.join(self.inst, '..', '.coverage')
1418 1431 os.environ['COVERAGE_FILE'] = fn
1419 1432
1420 1433 def _checkhglib(self, verb):
1421 1434 """Ensure that the 'mercurial' package imported by python is
1422 1435 the one we expect it to be. If not, print a warning to stderr."""
1423 1436 expecthg = os.path.join(self.pythondir, 'mercurial')
1424 1437 actualhg = self._gethgpath()
1425 1438 if os.path.abspath(actualhg) != os.path.abspath(expecthg):
1426 1439 sys.stderr.write('warning: %s with unexpected mercurial lib: %s\n'
1427 1440 ' (expected %s)\n'
1428 1441 % (verb, actualhg, expecthg))
1429 1442 def _gethgpath(self):
1430 1443 """Return the path to the mercurial package that is actually found by
1431 1444 the current Python interpreter."""
1432 1445 if self._hgpath is not None:
1433 1446 return self._hgpath
1434 1447
1435 1448 cmd = '%s -c "import mercurial; print (mercurial.__path__[0])"'
1436 1449 pipe = os.popen(cmd % PYTHON)
1437 1450 try:
1438 1451 self._hgpath = pipe.read().strip()
1439 1452 finally:
1440 1453 pipe.close()
1441 1454
1442 1455 return self._hgpath
1443 1456
1444 1457 def _outputtimes(self):
1445 1458 vlog('# Producing time report')
1446 1459 self.times.sort(key=lambda t: (t[1], t[0]), reverse=True)
1447 1460 cols = '%7.3f %s'
1448 1461 print '\n%-7s %s' % ('Time', 'Test')
1449 1462 for test, timetaken in self.times:
1450 1463 print cols % (timetaken, test)
1451 1464
1452 1465 def _outputcoverage(self):
1453 1466 vlog('# Producing coverage report')
1454 1467 os.chdir(self.pythondir)
1455 1468
1456 1469 def covrun(*args):
1457 1470 cmd = 'coverage %s' % ' '.join(args)
1458 1471 vlog('# Running: %s' % cmd)
1459 1472 os.system(cmd)
1460 1473
1461 1474 covrun('-c')
1462 1475 omit = ','.join(os.path.join(x, '*') for x in
1463 1476 [self.bindir, self.testdir])
1464 1477 covrun('-i', '-r', '"--omit=%s"' % omit) # report
1465 1478 if self.options.htmlcov:
1466 1479 htmldir = os.path.join(self.testdir, 'htmlcov')
1467 1480 covrun('-i', '-b', '"--directory=%s"' % htmldir,
1468 1481 '"--omit=%s"' % omit)
1469 1482 if self.options.annotate:
1470 1483 adir = os.path.join(self.testdir, 'annotated')
1471 1484 if not os.path.isdir(adir):
1472 1485 os.mkdir(adir)
1473 1486 covrun('-i', '-a', '"--directory=%s"' % adir, '"--omit=%s"' % omit)
1474 1487
1475 1488 def _executetests(self, tests):
1476 1489 jobs = self.options.jobs
1477 1490 done = queue.Queue()
1478 1491 running = 0
1479 1492 count = 0
1480 1493
1481 1494 def job(test, count):
1482 1495 try:
1483 1496 t = self._gettest(test, count)
1484 1497 done.put(t.run())
1485 1498 t.cleanup()
1486 1499 except KeyboardInterrupt:
1487 1500 pass
1488 1501 except: # re-raises
1489 1502 done.put(('!', test, 'run-test raised an error, see traceback'))
1490 1503 raise
1491 1504
1492 1505 try:
1493 1506 while tests or running:
1494 1507 if not done.empty() or running == jobs or not tests:
1495 1508 try:
1496 1509 code, test, msg = done.get(True, 1)
1497 1510 self.results[code].append((test, msg))
1498 1511 if self.options.first and code not in '.si':
1499 1512 break
1500 1513 except queue.Empty:
1501 1514 continue
1502 1515 running -= 1
1503 1516 if tests and not running == jobs:
1504 1517 test = tests.pop(0)
1505 1518 if self.options.loop:
1506 1519 tests.append(test)
1507 1520 t = threading.Thread(target=job, name=test,
1508 1521 args=(test, count))
1509 1522 t.start()
1510 1523 running += 1
1511 1524 count += 1
1512 1525 except KeyboardInterrupt:
1513 1526 self.abort[0] = True
1514 1527
1515 1528 def _findprogram(self, program):
1516 1529 """Search PATH for a executable program"""
1517 1530 for p in os.environ.get('PATH', os.defpath).split(os.pathsep):
1518 1531 name = os.path.join(p, program)
1519 1532 if os.name == 'nt' or os.access(name, os.X_OK):
1520 1533 return name
1521 1534 return None
1522 1535
1523 1536 def _checktools(self):
1524 1537 # Before we go any further, check for pre-requisite tools
1525 1538 # stuff from coreutils (cat, rm, etc) are not tested
1526 1539 for p in self.REQUIREDTOOLS:
1527 1540 if os.name == 'nt' and not p.endswith('.exe'):
1528 1541 p += '.exe'
1529 1542 found = self._findprogram(p)
1530 1543 if found:
1531 1544 vlog("# Found prerequisite", p, "at", found)
1532 1545 else:
1533 1546 print "WARNING: Did not find prerequisite tool: %s " % p
1534 1547
1535 1548 if __name__ == '__main__':
1536 1549 runner = TestRunner()
1537 1550 sys.exit(runner.run(sys.argv[1:]))
General Comments 0
You need to be logged in to leave comments. Login now