##// END OF EJS Templates
run-tests: only call WIFEXITED on systems it exists...
Simon Heimberg -
r13348:31fdb04c default
parent child Browse files
Show More
@@ -1,1113 +1,1114 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 re
56 56
57 57 closefds = os.name == 'posix'
58 58 def Popen4(cmd, bufsize=-1):
59 59 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
60 60 close_fds=closefds,
61 61 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
62 62 stderr=subprocess.STDOUT)
63 63 p.fromchild = p.stdout
64 64 p.tochild = p.stdin
65 65 p.childerr = p.stderr
66 66 return p
67 67
68 68 # reserved exit code to skip test (used by hghave)
69 69 SKIPPED_STATUS = 80
70 70 SKIPPED_PREFIX = 'skipped: '
71 71 FAILED_PREFIX = 'hghave check failed: '
72 72 PYTHON = sys.executable
73 73 IMPL_PATH = 'PYTHONPATH'
74 74 if 'java' in sys.platform:
75 75 IMPL_PATH = 'JYTHONPATH'
76 76
77 77 requiredtools = ["python", "diff", "grep", "unzip", "gunzip", "bunzip2", "sed"]
78 78
79 79 defaults = {
80 80 'jobs': ('HGTEST_JOBS', 1),
81 81 'timeout': ('HGTEST_TIMEOUT', 180),
82 82 'port': ('HGTEST_PORT', 20059),
83 83 }
84 84
85 85 def parseargs():
86 86 parser = optparse.OptionParser("%prog [options] [tests]")
87 87
88 88 # keep these sorted
89 89 parser.add_option("--blacklist", action="append",
90 90 help="skip tests listed in the specified blacklist file")
91 91 parser.add_option("-C", "--annotate", action="store_true",
92 92 help="output files annotated with coverage")
93 93 parser.add_option("--child", type="int",
94 94 help="run as child process, summary to given fd")
95 95 parser.add_option("-c", "--cover", action="store_true",
96 96 help="print a test coverage report")
97 97 parser.add_option("-d", "--debug", action="store_true",
98 98 help="debug mode: write output of test scripts to console"
99 99 " rather than capturing and diff'ing it (disables timeout)")
100 100 parser.add_option("-f", "--first", action="store_true",
101 101 help="exit on the first test failure")
102 102 parser.add_option("--inotify", action="store_true",
103 103 help="enable inotify extension when running tests")
104 104 parser.add_option("-i", "--interactive", action="store_true",
105 105 help="prompt to accept changed output")
106 106 parser.add_option("-j", "--jobs", type="int",
107 107 help="number of jobs to run in parallel"
108 108 " (default: $%s or %d)" % defaults['jobs'])
109 109 parser.add_option("--keep-tmpdir", action="store_true",
110 110 help="keep temporary directory after running tests")
111 111 parser.add_option("-k", "--keywords",
112 112 help="run tests matching keywords")
113 113 parser.add_option("-l", "--local", action="store_true",
114 114 help="shortcut for --with-hg=<testdir>/../hg")
115 115 parser.add_option("-n", "--nodiff", action="store_true",
116 116 help="skip showing test changes")
117 117 parser.add_option("-p", "--port", type="int",
118 118 help="port on which servers should listen"
119 119 " (default: $%s or %d)" % defaults['port'])
120 120 parser.add_option("--pure", action="store_true",
121 121 help="use pure Python code instead of C extensions")
122 122 parser.add_option("-R", "--restart", action="store_true",
123 123 help="restart at last error")
124 124 parser.add_option("-r", "--retest", action="store_true",
125 125 help="retest failed tests")
126 126 parser.add_option("-S", "--noskips", action="store_true",
127 127 help="don't report skip tests verbosely")
128 128 parser.add_option("-t", "--timeout", type="int",
129 129 help="kill errant tests after TIMEOUT seconds"
130 130 " (default: $%s or %d)" % defaults['timeout'])
131 131 parser.add_option("--tmpdir", type="string",
132 132 help="run tests in the given temporary directory"
133 133 " (implies --keep-tmpdir)")
134 134 parser.add_option("-v", "--verbose", action="store_true",
135 135 help="output verbose messages")
136 136 parser.add_option("--view", type="string",
137 137 help="external diff viewer")
138 138 parser.add_option("--with-hg", type="string",
139 139 metavar="HG",
140 140 help="test using specified hg script rather than a "
141 141 "temporary installation")
142 142 parser.add_option("-3", "--py3k-warnings", action="store_true",
143 143 help="enable Py3k warnings on Python 2.6+")
144 144
145 145 for option, default in defaults.items():
146 146 defaults[option] = int(os.environ.get(*default))
147 147 parser.set_defaults(**defaults)
148 148 (options, args) = parser.parse_args()
149 149
150 150 # jython is always pure
151 151 if 'java' in sys.platform or '__pypy__' in sys.modules:
152 152 options.pure = True
153 153
154 154 if options.with_hg:
155 155 if not (os.path.isfile(options.with_hg) and
156 156 os.access(options.with_hg, os.X_OK)):
157 157 parser.error('--with-hg must specify an executable hg script')
158 158 if not os.path.basename(options.with_hg) == 'hg':
159 159 sys.stderr.write('warning: --with-hg should specify an hg script')
160 160 if options.local:
161 161 testdir = os.path.dirname(os.path.realpath(sys.argv[0]))
162 162 hgbin = os.path.join(os.path.dirname(testdir), 'hg')
163 163 if not os.access(hgbin, os.X_OK):
164 164 parser.error('--local specified, but %r not found or not executable'
165 165 % hgbin)
166 166 options.with_hg = hgbin
167 167
168 168 options.anycoverage = options.cover or options.annotate
169 169 if options.anycoverage:
170 170 try:
171 171 import coverage
172 172 covver = version.StrictVersion(coverage.__version__).version
173 173 if covver < (3, 3):
174 174 parser.error('coverage options require coverage 3.3 or later')
175 175 except ImportError:
176 176 parser.error('coverage options now require the coverage package')
177 177
178 178 if options.anycoverage and options.local:
179 179 # this needs some path mangling somewhere, I guess
180 180 parser.error("sorry, coverage options do not work when --local "
181 181 "is specified")
182 182
183 183 global vlog
184 184 if options.verbose:
185 185 if options.jobs > 1 or options.child is not None:
186 186 pid = "[%d]" % os.getpid()
187 187 else:
188 188 pid = None
189 189 def vlog(*msg):
190 190 if pid:
191 191 print pid,
192 192 for m in msg:
193 193 print m,
194 194 print
195 195 sys.stdout.flush()
196 196 else:
197 197 vlog = lambda *msg: None
198 198
199 199 if options.tmpdir:
200 200 options.tmpdir = os.path.expanduser(options.tmpdir)
201 201
202 202 if options.jobs < 1:
203 203 parser.error('--jobs must be positive')
204 204 if options.interactive and options.jobs > 1:
205 205 print '(--interactive overrides --jobs)'
206 206 options.jobs = 1
207 207 if options.interactive and options.debug:
208 208 parser.error("-i/--interactive and -d/--debug are incompatible")
209 209 if options.debug:
210 210 if options.timeout != defaults['timeout']:
211 211 sys.stderr.write(
212 212 'warning: --timeout option ignored with --debug\n')
213 213 options.timeout = 0
214 214 if options.py3k_warnings:
215 215 if sys.version_info[:2] < (2, 6) or sys.version_info[:2] >= (3, 0):
216 216 parser.error('--py3k-warnings can only be used on Python 2.6+')
217 217 if options.blacklist:
218 218 blacklist = dict()
219 219 for filename in options.blacklist:
220 220 try:
221 221 path = os.path.expanduser(os.path.expandvars(filename))
222 222 f = open(path, "r")
223 223 except IOError, err:
224 224 if err.errno != errno.ENOENT:
225 225 raise
226 226 print "warning: no such blacklist file: %s" % filename
227 227 continue
228 228
229 229 for line in f.readlines():
230 230 line = line.strip()
231 231 if line and not line.startswith('#'):
232 232 blacklist[line] = filename
233 233
234 234 options.blacklist = blacklist
235 235
236 236 return (options, args)
237 237
238 238 def rename(src, dst):
239 239 """Like os.rename(), trade atomicity and opened files friendliness
240 240 for existing destination support.
241 241 """
242 242 shutil.copy(src, dst)
243 243 os.remove(src)
244 244
245 245 def splitnewlines(text):
246 246 '''like str.splitlines, but only split on newlines.
247 247 keep line endings.'''
248 248 i = 0
249 249 lines = []
250 250 while True:
251 251 n = text.find('\n', i)
252 252 if n == -1:
253 253 last = text[i:]
254 254 if last:
255 255 lines.append(last)
256 256 return lines
257 257 lines.append(text[i:n + 1])
258 258 i = n + 1
259 259
260 260 def parsehghaveoutput(lines):
261 261 '''Parse hghave log lines.
262 262 Return tuple of lists (missing, failed):
263 263 * the missing/unknown features
264 264 * the features for which existence check failed'''
265 265 missing = []
266 266 failed = []
267 267 for line in lines:
268 268 if line.startswith(SKIPPED_PREFIX):
269 269 line = line.splitlines()[0]
270 270 missing.append(line[len(SKIPPED_PREFIX):])
271 271 elif line.startswith(FAILED_PREFIX):
272 272 line = line.splitlines()[0]
273 273 failed.append(line[len(FAILED_PREFIX):])
274 274
275 275 return missing, failed
276 276
277 277 def showdiff(expected, output, ref, err):
278 278 for line in difflib.unified_diff(expected, output, ref, err):
279 279 sys.stdout.write(line)
280 280
281 281 def findprogram(program):
282 282 """Search PATH for a executable program"""
283 283 for p in os.environ.get('PATH', os.defpath).split(os.pathsep):
284 284 name = os.path.join(p, program)
285 285 if os.access(name, os.X_OK):
286 286 return name
287 287 return None
288 288
289 289 def checktools():
290 290 # Before we go any further, check for pre-requisite tools
291 291 # stuff from coreutils (cat, rm, etc) are not tested
292 292 for p in requiredtools:
293 293 if os.name == 'nt':
294 294 p += '.exe'
295 295 found = findprogram(p)
296 296 if found:
297 297 vlog("# Found prerequisite", p, "at", found)
298 298 else:
299 299 print "WARNING: Did not find prerequisite tool: "+p
300 300
301 301 def killdaemons():
302 302 # Kill off any leftover daemon processes
303 303 try:
304 304 fp = open(DAEMON_PIDS)
305 305 for line in fp:
306 306 try:
307 307 pid = int(line)
308 308 except ValueError:
309 309 continue
310 310 try:
311 311 os.kill(pid, 0)
312 312 vlog('# Killing daemon process %d' % pid)
313 313 os.kill(pid, signal.SIGTERM)
314 314 time.sleep(0.25)
315 315 os.kill(pid, 0)
316 316 vlog('# Daemon process %d is stuck - really killing it' % pid)
317 317 os.kill(pid, signal.SIGKILL)
318 318 except OSError, err:
319 319 if err.errno != errno.ESRCH:
320 320 raise
321 321 fp.close()
322 322 os.unlink(DAEMON_PIDS)
323 323 except IOError:
324 324 pass
325 325
326 326 def cleanup(options):
327 327 if not options.keep_tmpdir:
328 328 vlog("# Cleaning up HGTMP", HGTMP)
329 329 shutil.rmtree(HGTMP, True)
330 330
331 331 def usecorrectpython():
332 332 # some tests run python interpreter. they must use same
333 333 # interpreter we use or bad things will happen.
334 334 exedir, exename = os.path.split(sys.executable)
335 335 if exename == 'python':
336 336 path = findprogram('python')
337 337 if os.path.dirname(path) == exedir:
338 338 return
339 339 vlog('# Making python executable in test path use correct Python')
340 340 mypython = os.path.join(BINDIR, 'python')
341 341 try:
342 342 os.symlink(sys.executable, mypython)
343 343 except AttributeError:
344 344 # windows fallback
345 345 shutil.copyfile(sys.executable, mypython)
346 346 shutil.copymode(sys.executable, mypython)
347 347
348 348 def installhg(options):
349 349 vlog("# Performing temporary installation of HG")
350 350 installerrs = os.path.join("tests", "install.err")
351 351 pure = options.pure and "--pure" or ""
352 352
353 353 # Run installer in hg root
354 354 script = os.path.realpath(sys.argv[0])
355 355 hgroot = os.path.dirname(os.path.dirname(script))
356 356 os.chdir(hgroot)
357 357 nohome = '--home=""'
358 358 if os.name == 'nt':
359 359 # The --home="" trick works only on OS where os.sep == '/'
360 360 # because of a distutils convert_path() fast-path. Avoid it at
361 361 # least on Windows for now, deal with .pydistutils.cfg bugs
362 362 # when they happen.
363 363 nohome = ''
364 364 cmd = ('%s setup.py %s clean --all'
365 365 ' build --build-base="%s"'
366 366 ' install --force --prefix="%s" --install-lib="%s"'
367 367 ' --install-scripts="%s" %s >%s 2>&1'
368 368 % (sys.executable, pure, os.path.join(HGTMP, "build"),
369 369 INST, PYTHONDIR, BINDIR, nohome, installerrs))
370 370 vlog("# Running", cmd)
371 371 if os.system(cmd) == 0:
372 372 if not options.verbose:
373 373 os.remove(installerrs)
374 374 else:
375 375 f = open(installerrs)
376 376 for line in f:
377 377 print line,
378 378 f.close()
379 379 sys.exit(1)
380 380 os.chdir(TESTDIR)
381 381
382 382 usecorrectpython()
383 383
384 384 vlog("# Installing dummy diffstat")
385 385 f = open(os.path.join(BINDIR, 'diffstat'), 'w')
386 386 f.write('#!' + sys.executable + '\n'
387 387 'import sys\n'
388 388 'files = 0\n'
389 389 'for line in sys.stdin:\n'
390 390 ' if line.startswith("diff "):\n'
391 391 ' files += 1\n'
392 392 'sys.stdout.write("files patched: %d\\n" % files)\n')
393 393 f.close()
394 394 os.chmod(os.path.join(BINDIR, 'diffstat'), 0700)
395 395
396 396 if options.py3k_warnings and not options.anycoverage:
397 397 vlog("# Updating hg command to enable Py3k Warnings switch")
398 398 f = open(os.path.join(BINDIR, 'hg'), 'r')
399 399 lines = [line.rstrip() for line in f]
400 400 lines[0] += ' -3'
401 401 f.close()
402 402 f = open(os.path.join(BINDIR, 'hg'), 'w')
403 403 for line in lines:
404 404 f.write(line + '\n')
405 405 f.close()
406 406
407 407 if options.anycoverage:
408 408 custom = os.path.join(TESTDIR, 'sitecustomize.py')
409 409 target = os.path.join(PYTHONDIR, 'sitecustomize.py')
410 410 vlog('# Installing coverage trigger to %s' % target)
411 411 shutil.copyfile(custom, target)
412 412 rc = os.path.join(TESTDIR, '.coveragerc')
413 413 vlog('# Installing coverage rc to %s' % rc)
414 414 os.environ['COVERAGE_PROCESS_START'] = rc
415 415 fn = os.path.join(INST, '..', '.coverage')
416 416 os.environ['COVERAGE_FILE'] = fn
417 417
418 418 def outputcoverage(options):
419 419
420 420 vlog('# Producing coverage report')
421 421 os.chdir(PYTHONDIR)
422 422
423 423 def covrun(*args):
424 424 cmd = 'coverage %s' % ' '.join(args)
425 425 vlog('# Running: %s' % cmd)
426 426 os.system(cmd)
427 427
428 428 if options.child:
429 429 return
430 430
431 431 covrun('-c')
432 432 omit = ','.join([BINDIR, TESTDIR])
433 433 covrun('-i', '-r', '"--omit=%s"' % omit) # report
434 434 if options.annotate:
435 435 adir = os.path.join(TESTDIR, 'annotated')
436 436 if not os.path.isdir(adir):
437 437 os.mkdir(adir)
438 438 covrun('-i', '-a', '"--directory=%s"' % adir, '"--omit=%s"' % omit)
439 439
440 440 class Timeout(Exception):
441 441 pass
442 442
443 443 def alarmed(signum, frame):
444 444 raise Timeout
445 445
446 446 def pytest(test, options, replacements):
447 447 py3kswitch = options.py3k_warnings and ' -3' or ''
448 448 cmd = '%s%s "%s"' % (PYTHON, py3kswitch, test)
449 449 vlog("# Running", cmd)
450 450 return run(cmd, options, replacements)
451 451
452 452 def shtest(test, options, replacements):
453 453 cmd = '"%s"' % test
454 454 vlog("# Running", cmd)
455 455 return run(cmd, options, replacements)
456 456
457 457 needescape = re.compile(r'[\x00-\x08\x0b-\x1f\x7f-\xff]').search
458 458 escapesub = re.compile(r'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub
459 459 escapemap = dict((chr(i), r'\x%02x' % i) for i in range(256))
460 460 escapemap.update({'\\': '\\\\', '\r': r'\r'})
461 461 def escapef(m):
462 462 return escapemap[m.group(0)]
463 463 def stringescape(s):
464 464 return escapesub(escapef, s)
465 465
466 466 def tsttest(test, options, replacements):
467 467 t = open(test)
468 468 out = []
469 469 script = []
470 470 salt = "SALT" + str(time.time())
471 471
472 472 pos = prepos = -1
473 473 after = {}
474 474 expected = {}
475 475 for n, l in enumerate(t):
476 476 if not l.endswith('\n'):
477 477 l += '\n'
478 478 if l.startswith(' $ '): # commands
479 479 after.setdefault(pos, []).append(l)
480 480 prepos = pos
481 481 pos = n
482 482 script.append('echo %s %s $?\n' % (salt, n))
483 483 script.append(l[4:])
484 484 elif l.startswith(' > '): # continuations
485 485 after.setdefault(prepos, []).append(l)
486 486 script.append(l[4:])
487 487 elif l.startswith(' '): # results
488 488 # queue up a list of expected results
489 489 expected.setdefault(pos, []).append(l[2:])
490 490 else:
491 491 # non-command/result - queue up for merged output
492 492 after.setdefault(pos, []).append(l)
493 493
494 494 script.append('echo %s %s $?\n' % (salt, n + 1))
495 495
496 496 fd, name = tempfile.mkstemp(suffix='hg-tst')
497 497
498 498 try:
499 499 for l in script:
500 500 os.write(fd, l)
501 501 os.close(fd)
502 502
503 503 cmd = '/bin/sh "%s"' % name
504 504 vlog("# Running", cmd)
505 505 exitcode, output = run(cmd, options, replacements)
506 506 # do not merge output if skipped, return hghave message instead
507 507 # similarly, with --debug, output is None
508 508 if exitcode == SKIPPED_STATUS or output is None:
509 509 return exitcode, output
510 510 finally:
511 511 os.remove(name)
512 512
513 513 def rematch(el, l):
514 514 try:
515 515 # ensure that the regex matches to the end of the string
516 516 return re.match(el + r'\Z', l)
517 517 except re.error:
518 518 # el is an invalid regex
519 519 return False
520 520
521 521 def globmatch(el, l):
522 522 # The only supported special characters are * and ?. Escaping is
523 523 # supported.
524 524 i, n = 0, len(el)
525 525 res = ''
526 526 while i < n:
527 527 c = el[i]
528 528 i += 1
529 529 if c == '\\' and el[i] in '*?\\':
530 530 res += el[i - 1:i + 1]
531 531 i += 1
532 532 elif c == '*':
533 533 res += '.*'
534 534 elif c == '?':
535 535 res += '.'
536 536 else:
537 537 res += re.escape(c)
538 538 return rematch(res, l)
539 539
540 540 pos = -1
541 541 postout = []
542 542 ret = 0
543 543 for n, l in enumerate(output):
544 544 lout, lcmd = l, None
545 545 if salt in l:
546 546 lout, lcmd = l.split(salt, 1)
547 547
548 548 if lout:
549 549 if lcmd:
550 550 lout += ' (no-eol)\n'
551 551
552 552 el = None
553 553 if pos in expected and expected[pos]:
554 554 el = expected[pos].pop(0)
555 555
556 556 if el == lout: # perfect match (fast)
557 557 postout.append(" " + lout)
558 558 elif (el and
559 559 (el.endswith(" (re)\n") and rematch(el[:-6] + '\n', lout) or
560 560 el.endswith(" (glob)\n") and globmatch(el[:-8] + '\n', lout)
561 561 or el.endswith(" (esc)\n") and
562 562 el.decode('string-escape') == l)):
563 563 postout.append(" " + el) # fallback regex/glob/esc match
564 564 else:
565 565 if needescape(lout):
566 566 lout = stringescape(lout.rstrip('\n')) + " (esc)\n"
567 567 postout.append(" " + lout) # let diff deal with it
568 568
569 569 if lcmd:
570 570 # add on last return code
571 571 ret = int(lcmd.split()[1])
572 572 if ret != 0:
573 573 postout.append(" [%s]\n" % ret)
574 574 if pos in after:
575 575 postout += after.pop(pos)
576 576 pos = int(lcmd.split()[0])
577 577
578 578 if pos in after:
579 579 postout += after.pop(pos)
580 580
581 581 return exitcode, postout
582 582
583 wifexited = getattr(os, "WIFEXITED", lambda x: False)
583 584 def run(cmd, options, replacements):
584 585 """Run command in a sub-process, capturing the output (stdout and stderr).
585 586 Return a tuple (exitcode, output). output is None in debug mode."""
586 587 # TODO: Use subprocess.Popen if we're running on Python 2.4
587 588 if options.debug:
588 589 proc = subprocess.Popen(cmd, shell=True)
589 590 ret = proc.wait()
590 591 return (ret, None)
591 592
592 593 if os.name == 'nt' or sys.platform.startswith('java'):
593 594 tochild, fromchild = os.popen4(cmd)
594 595 tochild.close()
595 596 output = fromchild.read()
596 597 ret = fromchild.close()
597 598 if ret is None:
598 599 ret = 0
599 600 else:
600 601 proc = Popen4(cmd)
601 602 def cleanup():
602 603 os.kill(proc.pid, signal.SIGTERM)
603 604 ret = proc.wait()
604 605 if ret == 0:
605 606 ret = signal.SIGTERM << 8
606 607 killdaemons()
607 608 return ret
608 609
609 610 try:
610 611 output = ''
611 612 proc.tochild.close()
612 613 output = proc.fromchild.read()
613 614 ret = proc.wait()
614 if os.WIFEXITED(ret):
615 if wifexited(ret):
615 616 ret = os.WEXITSTATUS(ret)
616 617 except Timeout:
617 618 vlog('# Process %d timed out - killing it' % proc.pid)
618 619 ret = cleanup()
619 620 output += ("\n### Abort: timeout after %d seconds.\n"
620 621 % options.timeout)
621 622 except KeyboardInterrupt:
622 623 vlog('# Handling keyboard interrupt')
623 624 cleanup()
624 625 raise
625 626
626 627 for s, r in replacements:
627 628 output = re.sub(s, r, output)
628 629 return ret, splitnewlines(output)
629 630
630 631 def runone(options, test, skips, fails):
631 632 '''tristate output:
632 633 None -> skipped
633 634 True -> passed
634 635 False -> failed'''
635 636
636 637 def skip(msg):
637 638 if not options.verbose:
638 639 skips.append((test, msg))
639 640 else:
640 641 print "\nSkipping %s: %s" % (testpath, msg)
641 642 return None
642 643
643 644 def fail(msg):
644 645 fails.append((test, msg))
645 646 if not options.nodiff:
646 647 print "\nERROR: %s %s" % (testpath, msg)
647 648 return None
648 649
649 650 vlog("# Test", test)
650 651
651 652 # create a fresh hgrc
652 653 hgrc = open(HGRCPATH, 'w+')
653 654 hgrc.write('[ui]\n')
654 655 hgrc.write('slash = True\n')
655 656 hgrc.write('[defaults]\n')
656 657 hgrc.write('backout = -d "0 0"\n')
657 658 hgrc.write('commit = -d "0 0"\n')
658 659 hgrc.write('tag = -d "0 0"\n')
659 660 if options.inotify:
660 661 hgrc.write('[extensions]\n')
661 662 hgrc.write('inotify=\n')
662 663 hgrc.write('[inotify]\n')
663 664 hgrc.write('pidfile=%s\n' % DAEMON_PIDS)
664 665 hgrc.write('appendpid=True\n')
665 666 hgrc.close()
666 667
667 668 testpath = os.path.join(TESTDIR, test)
668 669 ref = os.path.join(TESTDIR, test+".out")
669 670 err = os.path.join(TESTDIR, test+".err")
670 671 if os.path.exists(err):
671 672 os.remove(err) # Remove any previous output files
672 673 try:
673 674 tf = open(testpath)
674 675 firstline = tf.readline().rstrip()
675 676 tf.close()
676 677 except:
677 678 firstline = ''
678 679 lctest = test.lower()
679 680
680 681 if lctest.endswith('.py') or firstline == '#!/usr/bin/env python':
681 682 runner = pytest
682 683 elif lctest.endswith('.t'):
683 684 runner = tsttest
684 685 ref = testpath
685 686 else:
686 687 # do not try to run non-executable programs
687 688 if not os.access(testpath, os.X_OK):
688 689 return skip("not executable")
689 690 runner = shtest
690 691
691 692 # Make a tmp subdirectory to work in
692 693 testtmp = os.environ["TESTTMP"] = os.path.join(HGTMP, test)
693 694 os.mkdir(testtmp)
694 695 os.chdir(testtmp)
695 696
696 697 if options.timeout > 0:
697 698 signal.alarm(options.timeout)
698 699
699 700 ret, out = runner(testpath, options, [
700 701 (re.escape(testtmp), '$TESTTMP'),
701 702 (r':%s\b' % options.port, ':$HGPORT'),
702 703 (r':%s\b' % (options.port + 1), ':$HGPORT1'),
703 704 (r':%s\b' % (options.port + 2), ':$HGPORT2'),
704 705 ])
705 706 vlog("# Ret was:", ret)
706 707
707 708 if options.timeout > 0:
708 709 signal.alarm(0)
709 710
710 711 mark = '.'
711 712
712 713 skipped = (ret == SKIPPED_STATUS)
713 714
714 715 # If we're not in --debug mode and reference output file exists,
715 716 # check test output against it.
716 717 if options.debug:
717 718 refout = None # to match "out is None"
718 719 elif os.path.exists(ref):
719 720 f = open(ref, "r")
720 721 refout = splitnewlines(f.read())
721 722 f.close()
722 723 else:
723 724 refout = []
724 725
725 726 if (ret != 0 or out != refout) and not skipped and not options.debug:
726 727 # Save errors to a file for diagnosis
727 728 f = open(err, "wb")
728 729 for line in out:
729 730 f.write(line)
730 731 f.close()
731 732
732 733 if skipped:
733 734 mark = 's'
734 735 if out is None: # debug mode: nothing to parse
735 736 missing = ['unknown']
736 737 failed = None
737 738 else:
738 739 missing, failed = parsehghaveoutput(out)
739 740 if not missing:
740 741 missing = ['irrelevant']
741 742 if failed:
742 743 fail("hghave failed checking for %s" % failed[-1])
743 744 skipped = False
744 745 else:
745 746 skip(missing[-1])
746 747 elif out != refout:
747 748 mark = '!'
748 749 if ret:
749 750 fail("output changed and returned error code %d" % ret)
750 751 else:
751 752 fail("output changed")
752 753 if not options.nodiff:
753 754 if options.view:
754 755 os.system("%s %s %s" % (options.view, ref, err))
755 756 else:
756 757 showdiff(refout, out, ref, err)
757 758 ret = 1
758 759 elif ret:
759 760 mark = '!'
760 761 fail("returned error code %d" % ret)
761 762
762 763 if not options.verbose:
763 764 sys.stdout.write(mark)
764 765 sys.stdout.flush()
765 766
766 767 killdaemons()
767 768
768 769 os.chdir(TESTDIR)
769 770 if not options.keep_tmpdir:
770 771 shutil.rmtree(testtmp, True)
771 772 if skipped:
772 773 return None
773 774 return ret == 0
774 775
775 776 _hgpath = None
776 777
777 778 def _gethgpath():
778 779 """Return the path to the mercurial package that is actually found by
779 780 the current Python interpreter."""
780 781 global _hgpath
781 782 if _hgpath is not None:
782 783 return _hgpath
783 784
784 785 cmd = '%s -c "import mercurial; print mercurial.__path__[0]"'
785 786 pipe = os.popen(cmd % PYTHON)
786 787 try:
787 788 _hgpath = pipe.read().strip()
788 789 finally:
789 790 pipe.close()
790 791 return _hgpath
791 792
792 793 def _checkhglib(verb):
793 794 """Ensure that the 'mercurial' package imported by python is
794 795 the one we expect it to be. If not, print a warning to stderr."""
795 796 expecthg = os.path.join(PYTHONDIR, 'mercurial')
796 797 actualhg = _gethgpath()
797 798 if actualhg != expecthg:
798 799 sys.stderr.write('warning: %s with unexpected mercurial lib: %s\n'
799 800 ' (expected %s)\n'
800 801 % (verb, actualhg, expecthg))
801 802
802 803 def runchildren(options, tests):
803 804 if INST:
804 805 installhg(options)
805 806 _checkhglib("Testing")
806 807
807 808 optcopy = dict(options.__dict__)
808 809 optcopy['jobs'] = 1
809 810 del optcopy['blacklist']
810 811 if optcopy['with_hg'] is None:
811 812 optcopy['with_hg'] = os.path.join(BINDIR, "hg")
812 813 optcopy.pop('anycoverage', None)
813 814
814 815 opts = []
815 816 for opt, value in optcopy.iteritems():
816 817 name = '--' + opt.replace('_', '-')
817 818 if value is True:
818 819 opts.append(name)
819 820 elif value is not None:
820 821 opts.append(name + '=' + str(value))
821 822
822 823 tests.reverse()
823 824 jobs = [[] for j in xrange(options.jobs)]
824 825 while tests:
825 826 for job in jobs:
826 827 if not tests:
827 828 break
828 829 job.append(tests.pop())
829 830 fps = {}
830 831
831 832 for j, job in enumerate(jobs):
832 833 if not job:
833 834 continue
834 835 rfd, wfd = os.pipe()
835 836 childopts = ['--child=%d' % wfd, '--port=%d' % (options.port + j * 3)]
836 837 childtmp = os.path.join(HGTMP, 'child%d' % j)
837 838 childopts += ['--tmpdir', childtmp]
838 839 cmdline = [PYTHON, sys.argv[0]] + opts + childopts + job
839 840 vlog(' '.join(cmdline))
840 841 fps[os.spawnvp(os.P_NOWAIT, cmdline[0], cmdline)] = os.fdopen(rfd, 'r')
841 842 os.close(wfd)
842 843 signal.signal(signal.SIGINT, signal.SIG_IGN)
843 844 failures = 0
844 845 tested, skipped, failed = 0, 0, 0
845 846 skips = []
846 847 fails = []
847 848 while fps:
848 849 pid, status = os.wait()
849 850 fp = fps.pop(pid)
850 851 l = fp.read().splitlines()
851 852 try:
852 853 test, skip, fail = map(int, l[:3])
853 854 except ValueError:
854 855 test, skip, fail = 0, 0, 0
855 856 split = -fail or len(l)
856 857 for s in l[3:split]:
857 858 skips.append(s.split(" ", 1))
858 859 for s in l[split:]:
859 860 fails.append(s.split(" ", 1))
860 861 tested += test
861 862 skipped += skip
862 863 failed += fail
863 864 vlog('pid %d exited, status %d' % (pid, status))
864 865 failures |= status
865 866 print
866 867 if not options.noskips:
867 868 for s in skips:
868 869 print "Skipped %s: %s" % (s[0], s[1])
869 870 for s in fails:
870 871 print "Failed %s: %s" % (s[0], s[1])
871 872
872 873 _checkhglib("Tested")
873 874 print "# Ran %d tests, %d skipped, %d failed." % (
874 875 tested, skipped, failed)
875 876
876 877 if options.anycoverage:
877 878 outputcoverage(options)
878 879 sys.exit(failures != 0)
879 880
880 881 def runtests(options, tests):
881 882 global DAEMON_PIDS, HGRCPATH
882 883 DAEMON_PIDS = os.environ["DAEMON_PIDS"] = os.path.join(HGTMP, 'daemon.pids')
883 884 HGRCPATH = os.environ["HGRCPATH"] = os.path.join(HGTMP, '.hgrc')
884 885
885 886 try:
886 887 if INST:
887 888 installhg(options)
888 889 _checkhglib("Testing")
889 890
890 891 if options.timeout > 0:
891 892 try:
892 893 signal.signal(signal.SIGALRM, alarmed)
893 894 vlog('# Running each test with %d second timeout' %
894 895 options.timeout)
895 896 except AttributeError:
896 897 print 'WARNING: cannot run tests with timeouts'
897 898 options.timeout = 0
898 899
899 900 tested = 0
900 901 failed = 0
901 902 skipped = 0
902 903
903 904 if options.restart:
904 905 orig = list(tests)
905 906 while tests:
906 907 if os.path.exists(tests[0] + ".err"):
907 908 break
908 909 tests.pop(0)
909 910 if not tests:
910 911 print "running all tests"
911 912 tests = orig
912 913
913 914 skips = []
914 915 fails = []
915 916
916 917 for test in tests:
917 918 if options.blacklist:
918 919 filename = options.blacklist.get(test)
919 920 if filename is not None:
920 921 skips.append((test, "blacklisted (%s)" % filename))
921 922 skipped += 1
922 923 continue
923 924
924 925 if options.retest and not os.path.exists(test + ".err"):
925 926 skipped += 1
926 927 continue
927 928
928 929 if options.keywords:
929 930 t = open(test).read().lower() + test.lower()
930 931 for k in options.keywords.lower().split():
931 932 if k in t:
932 933 break
933 934 else:
934 935 skipped += 1
935 936 continue
936 937
937 938 ret = runone(options, test, skips, fails)
938 939 if ret is None:
939 940 skipped += 1
940 941 elif not ret:
941 942 if options.interactive:
942 943 print "Accept this change? [n] ",
943 944 answer = sys.stdin.readline().strip()
944 945 if answer.lower() in "y yes".split():
945 946 if test.endswith(".t"):
946 947 rename(test + ".err", test)
947 948 else:
948 949 rename(test + ".err", test + ".out")
949 950 tested += 1
950 951 fails.pop()
951 952 continue
952 953 failed += 1
953 954 if options.first:
954 955 break
955 956 tested += 1
956 957
957 958 if options.child:
958 959 fp = os.fdopen(options.child, 'w')
959 960 fp.write('%d\n%d\n%d\n' % (tested, skipped, failed))
960 961 for s in skips:
961 962 fp.write("%s %s\n" % s)
962 963 for s in fails:
963 964 fp.write("%s %s\n" % s)
964 965 fp.close()
965 966 else:
966 967 print
967 968 for s in skips:
968 969 print "Skipped %s: %s" % s
969 970 for s in fails:
970 971 print "Failed %s: %s" % s
971 972 _checkhglib("Tested")
972 973 print "# Ran %d tests, %d skipped, %d failed." % (
973 974 tested, skipped, failed)
974 975
975 976 if options.anycoverage:
976 977 outputcoverage(options)
977 978 except KeyboardInterrupt:
978 979 failed = True
979 980 print "\ninterrupted!"
980 981
981 982 if failed:
982 983 sys.exit(1)
983 984
984 985 def main():
985 986 (options, args) = parseargs()
986 987 if not options.child:
987 988 os.umask(022)
988 989
989 990 checktools()
990 991
991 992 if len(args) == 0:
992 993 args = os.listdir(".")
993 994 args.sort()
994 995
995 996 tests = []
996 997 skipped = []
997 998 for test in args:
998 999 if (test.startswith("test-") and '~' not in test and
999 1000 ('.' not in test or test.endswith('.py') or
1000 1001 test.endswith('.bat') or test.endswith('.t'))):
1001 1002 if not os.path.exists(test):
1002 1003 skipped.append(test)
1003 1004 else:
1004 1005 tests.append(test)
1005 1006 if not tests:
1006 1007 for test in skipped:
1007 1008 print 'Skipped %s: does not exist' % test
1008 1009 print "# Ran 0 tests, %d skipped, 0 failed." % len(skipped)
1009 1010 return
1010 1011 tests = tests + skipped
1011 1012
1012 1013 # Reset some environment variables to well-known values so that
1013 1014 # the tests produce repeatable output.
1014 1015 os.environ['LANG'] = os.environ['LC_ALL'] = os.environ['LANGUAGE'] = 'C'
1015 1016 os.environ['TZ'] = 'GMT'
1016 1017 os.environ["EMAIL"] = "Foo Bar <foo.bar@example.com>"
1017 1018 os.environ['CDPATH'] = ''
1018 1019 os.environ['COLUMNS'] = '80'
1019 1020 os.environ['GREP_OPTIONS'] = ''
1020 1021 os.environ['http_proxy'] = ''
1021 1022
1022 1023 # unset env related to hooks
1023 1024 for k in os.environ.keys():
1024 1025 if k.startswith('HG_'):
1025 1026 # can't remove on solaris
1026 1027 os.environ[k] = ''
1027 1028 del os.environ[k]
1028 1029
1029 1030 global TESTDIR, HGTMP, INST, BINDIR, PYTHONDIR, COVERAGE_FILE
1030 1031 TESTDIR = os.environ["TESTDIR"] = os.getcwd()
1031 1032 if options.tmpdir:
1032 1033 options.keep_tmpdir = True
1033 1034 tmpdir = options.tmpdir
1034 1035 if os.path.exists(tmpdir):
1035 1036 # Meaning of tmpdir has changed since 1.3: we used to create
1036 1037 # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if
1037 1038 # tmpdir already exists.
1038 1039 sys.exit("error: temp dir %r already exists" % tmpdir)
1039 1040
1040 1041 # Automatically removing tmpdir sounds convenient, but could
1041 1042 # really annoy anyone in the habit of using "--tmpdir=/tmp"
1042 1043 # or "--tmpdir=$HOME".
1043 1044 #vlog("# Removing temp dir", tmpdir)
1044 1045 #shutil.rmtree(tmpdir)
1045 1046 os.makedirs(tmpdir)
1046 1047 else:
1047 1048 tmpdir = tempfile.mkdtemp('', 'hgtests.')
1048 1049 HGTMP = os.environ['HGTMP'] = os.path.realpath(tmpdir)
1049 1050 DAEMON_PIDS = None
1050 1051 HGRCPATH = None
1051 1052
1052 1053 os.environ["HGEDITOR"] = sys.executable + ' -c "import sys; sys.exit(0)"'
1053 1054 os.environ["HGMERGE"] = "internal:merge"
1054 1055 os.environ["HGUSER"] = "test"
1055 1056 os.environ["HGENCODING"] = "ascii"
1056 1057 os.environ["HGENCODINGMODE"] = "strict"
1057 1058 os.environ["HGPORT"] = str(options.port)
1058 1059 os.environ["HGPORT1"] = str(options.port + 1)
1059 1060 os.environ["HGPORT2"] = str(options.port + 2)
1060 1061
1061 1062 if options.with_hg:
1062 1063 INST = None
1063 1064 BINDIR = os.path.dirname(os.path.realpath(options.with_hg))
1064 1065
1065 1066 # This looks redundant with how Python initializes sys.path from
1066 1067 # the location of the script being executed. Needed because the
1067 1068 # "hg" specified by --with-hg is not the only Python script
1068 1069 # executed in the test suite that needs to import 'mercurial'
1069 1070 # ... which means it's not really redundant at all.
1070 1071 PYTHONDIR = BINDIR
1071 1072 else:
1072 1073 INST = os.path.join(HGTMP, "install")
1073 1074 BINDIR = os.environ["BINDIR"] = os.path.join(INST, "bin")
1074 1075 PYTHONDIR = os.path.join(INST, "lib", "python")
1075 1076
1076 1077 os.environ["BINDIR"] = BINDIR
1077 1078 os.environ["PYTHON"] = PYTHON
1078 1079
1079 1080 if not options.child:
1080 1081 path = [BINDIR] + os.environ["PATH"].split(os.pathsep)
1081 1082 os.environ["PATH"] = os.pathsep.join(path)
1082 1083
1083 1084 # Include TESTDIR in PYTHONPATH so that out-of-tree extensions
1084 1085 # can run .../tests/run-tests.py test-foo where test-foo
1085 1086 # adds an extension to HGRC
1086 1087 pypath = [PYTHONDIR, TESTDIR]
1087 1088 # We have to augment PYTHONPATH, rather than simply replacing
1088 1089 # it, in case external libraries are only available via current
1089 1090 # PYTHONPATH. (In particular, the Subversion bindings on OS X
1090 1091 # are in /opt/subversion.)
1091 1092 oldpypath = os.environ.get(IMPL_PATH)
1092 1093 if oldpypath:
1093 1094 pypath.append(oldpypath)
1094 1095 os.environ[IMPL_PATH] = os.pathsep.join(pypath)
1095 1096
1096 1097 COVERAGE_FILE = os.path.join(TESTDIR, ".coverage")
1097 1098
1098 1099 vlog("# Using TESTDIR", TESTDIR)
1099 1100 vlog("# Using HGTMP", HGTMP)
1100 1101 vlog("# Using PATH", os.environ["PATH"])
1101 1102 vlog("# Using", IMPL_PATH, os.environ[IMPL_PATH])
1102 1103
1103 1104 try:
1104 1105 if len(tests) > 1 and options.jobs > 1:
1105 1106 runchildren(options, tests)
1106 1107 else:
1107 1108 runtests(options, tests)
1108 1109 finally:
1109 1110 time.sleep(1)
1110 1111 cleanup(options)
1111 1112
1112 1113 if __name__ == '__main__':
1113 1114 main()
General Comments 0
You need to be logged in to leave comments. Login now