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