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