##// END OF EJS Templates
run-tests: write out scripts in binary mode...
Augie Fackler -
r21916:792ebd7d default
parent child Browse files
Show More
@@ -1,1821 +1,1821 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, "rb")
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("-v", "--verbose", action="store_true",
191 parser.add_option("-v", "--verbose", action="store_true",
192 help="output verbose messages")
192 help="output verbose messages")
193 parser.add_option("--view", type="string",
193 parser.add_option("--view", type="string",
194 help="external diff viewer")
194 help="external diff viewer")
195 parser.add_option("--with-hg", type="string",
195 parser.add_option("--with-hg", type="string",
196 metavar="HG",
196 metavar="HG",
197 help="test using specified hg script rather than a "
197 help="test using specified hg script rather than a "
198 "temporary installation")
198 "temporary installation")
199 parser.add_option("-3", "--py3k-warnings", action="store_true",
199 parser.add_option("-3", "--py3k-warnings", action="store_true",
200 help="enable Py3k warnings on Python 2.6+")
200 help="enable Py3k warnings on Python 2.6+")
201 parser.add_option('--extra-config-opt', action="append",
201 parser.add_option('--extra-config-opt', action="append",
202 help='set the given config opt in the test hgrc')
202 help='set the given config opt in the test hgrc')
203 parser.add_option('--random', action="store_true",
203 parser.add_option('--random', action="store_true",
204 help='run tests in random order')
204 help='run tests in random order')
205
205
206 for option, (envvar, default) in defaults.items():
206 for option, (envvar, default) in defaults.items():
207 defaults[option] = type(default)(os.environ.get(envvar, default))
207 defaults[option] = type(default)(os.environ.get(envvar, default))
208 parser.set_defaults(**defaults)
208 parser.set_defaults(**defaults)
209
209
210 return parser
210 return parser
211
211
212 def parseargs(args, parser):
212 def parseargs(args, parser):
213 """Parse arguments with our OptionParser and validate results."""
213 """Parse arguments with our OptionParser and validate results."""
214 (options, args) = parser.parse_args(args)
214 (options, args) = parser.parse_args(args)
215
215
216 # jython is always pure
216 # jython is always pure
217 if 'java' in sys.platform or '__pypy__' in sys.modules:
217 if 'java' in sys.platform or '__pypy__' in sys.modules:
218 options.pure = True
218 options.pure = True
219
219
220 if options.with_hg:
220 if options.with_hg:
221 options.with_hg = os.path.expanduser(options.with_hg)
221 options.with_hg = os.path.expanduser(options.with_hg)
222 if not (os.path.isfile(options.with_hg) and
222 if not (os.path.isfile(options.with_hg) and
223 os.access(options.with_hg, os.X_OK)):
223 os.access(options.with_hg, os.X_OK)):
224 parser.error('--with-hg must specify an executable hg script')
224 parser.error('--with-hg must specify an executable hg script')
225 if not os.path.basename(options.with_hg) == 'hg':
225 if not os.path.basename(options.with_hg) == 'hg':
226 sys.stderr.write('warning: --with-hg should specify an hg script\n')
226 sys.stderr.write('warning: --with-hg should specify an hg script\n')
227 if options.local:
227 if options.local:
228 testdir = os.path.dirname(os.path.realpath(sys.argv[0]))
228 testdir = os.path.dirname(os.path.realpath(sys.argv[0]))
229 hgbin = os.path.join(os.path.dirname(testdir), 'hg')
229 hgbin = os.path.join(os.path.dirname(testdir), 'hg')
230 if os.name != 'nt' and not os.access(hgbin, os.X_OK):
230 if os.name != 'nt' and not os.access(hgbin, os.X_OK):
231 parser.error('--local specified, but %r not found or not executable'
231 parser.error('--local specified, but %r not found or not executable'
232 % hgbin)
232 % hgbin)
233 options.with_hg = hgbin
233 options.with_hg = hgbin
234
234
235 options.anycoverage = options.cover or options.annotate or options.htmlcov
235 options.anycoverage = options.cover or options.annotate or options.htmlcov
236 if options.anycoverage:
236 if options.anycoverage:
237 try:
237 try:
238 import coverage
238 import coverage
239 covver = version.StrictVersion(coverage.__version__).version
239 covver = version.StrictVersion(coverage.__version__).version
240 if covver < (3, 3):
240 if covver < (3, 3):
241 parser.error('coverage options require coverage 3.3 or later')
241 parser.error('coverage options require coverage 3.3 or later')
242 except ImportError:
242 except ImportError:
243 parser.error('coverage options now require the coverage package')
243 parser.error('coverage options now require the coverage package')
244
244
245 if options.anycoverage and options.local:
245 if options.anycoverage and options.local:
246 # this needs some path mangling somewhere, I guess
246 # this needs some path mangling somewhere, I guess
247 parser.error("sorry, coverage options do not work when --local "
247 parser.error("sorry, coverage options do not work when --local "
248 "is specified")
248 "is specified")
249
249
250 global verbose
250 global verbose
251 if options.verbose:
251 if options.verbose:
252 verbose = ''
252 verbose = ''
253
253
254 if options.tmpdir:
254 if options.tmpdir:
255 options.tmpdir = os.path.expanduser(options.tmpdir)
255 options.tmpdir = os.path.expanduser(options.tmpdir)
256
256
257 if options.jobs < 1:
257 if options.jobs < 1:
258 parser.error('--jobs must be positive')
258 parser.error('--jobs must be positive')
259 if options.interactive and options.debug:
259 if options.interactive and options.debug:
260 parser.error("-i/--interactive and -d/--debug are incompatible")
260 parser.error("-i/--interactive and -d/--debug are incompatible")
261 if options.debug:
261 if options.debug:
262 if options.timeout != defaults['timeout']:
262 if options.timeout != defaults['timeout']:
263 sys.stderr.write(
263 sys.stderr.write(
264 'warning: --timeout option ignored with --debug\n')
264 'warning: --timeout option ignored with --debug\n')
265 options.timeout = 0
265 options.timeout = 0
266 if options.py3k_warnings:
266 if options.py3k_warnings:
267 if sys.version_info[:2] < (2, 6) or sys.version_info[:2] >= (3, 0):
267 if sys.version_info[:2] < (2, 6) or sys.version_info[:2] >= (3, 0):
268 parser.error('--py3k-warnings can only be used on Python 2.6+')
268 parser.error('--py3k-warnings can only be used on Python 2.6+')
269 if options.blacklist:
269 if options.blacklist:
270 options.blacklist = parselistfiles(options.blacklist, 'blacklist')
270 options.blacklist = parselistfiles(options.blacklist, 'blacklist')
271 if options.whitelist:
271 if options.whitelist:
272 options.whitelisted = parselistfiles(options.whitelist, 'whitelist')
272 options.whitelisted = parselistfiles(options.whitelist, 'whitelist')
273 else:
273 else:
274 options.whitelisted = {}
274 options.whitelisted = {}
275
275
276 return (options, args)
276 return (options, args)
277
277
278 def rename(src, dst):
278 def rename(src, dst):
279 """Like os.rename(), trade atomicity and opened files friendliness
279 """Like os.rename(), trade atomicity and opened files friendliness
280 for existing destination support.
280 for existing destination support.
281 """
281 """
282 shutil.copy(src, dst)
282 shutil.copy(src, dst)
283 os.remove(src)
283 os.remove(src)
284
284
285 def getdiff(expected, output, ref, err):
285 def getdiff(expected, output, ref, err):
286 servefail = False
286 servefail = False
287 lines = []
287 lines = []
288 for line in difflib.unified_diff(expected, output, ref, err):
288 for line in difflib.unified_diff(expected, output, ref, err):
289 if line.startswith('+++') or line.startswith('---'):
289 if line.startswith('+++') or line.startswith('---'):
290 if line.endswith(' \n'):
290 if line.endswith(' \n'):
291 line = line[:-2] + '\n'
291 line = line[:-2] + '\n'
292 lines.append(line)
292 lines.append(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
296
297 return servefail, lines
297 return servefail, lines
298
298
299 verbose = False
299 verbose = False
300 def vlog(*msg):
300 def vlog(*msg):
301 """Log only when in verbose mode."""
301 """Log only when in verbose mode."""
302 if verbose is False:
302 if verbose is False:
303 return
303 return
304
304
305 return log(*msg)
305 return log(*msg)
306
306
307 def log(*msg):
307 def log(*msg):
308 """Log something to stdout.
308 """Log something to stdout.
309
309
310 Arguments are strings to print.
310 Arguments are strings to print.
311 """
311 """
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(unittest.TestCase):
333 class Test(unittest.TestCase):
334 """Encapsulates a single, runnable test.
334 """Encapsulates a single, runnable test.
335
335
336 While this class conforms to the unittest.TestCase API, it differs in that
336 While this class conforms to the unittest.TestCase API, it differs in that
337 instances need to be instantiated manually. (Typically, unittest.TestCase
337 instances need to be instantiated manually. (Typically, unittest.TestCase
338 classes are instantiated automatically by scanning modules.)
338 classes are instantiated automatically by scanning modules.)
339 """
339 """
340
340
341 # Status code reserved for skipped tests (used by hghave).
341 # Status code reserved for skipped tests (used by hghave).
342 SKIPPED_STATUS = 80
342 SKIPPED_STATUS = 80
343
343
344 def __init__(self, path, tmpdir, keeptmpdir=False,
344 def __init__(self, path, tmpdir, keeptmpdir=False,
345 debug=False,
345 debug=False,
346 timeout=defaults['timeout'],
346 timeout=defaults['timeout'],
347 startport=defaults['port'], extraconfigopts=None,
347 startport=defaults['port'], extraconfigopts=None,
348 py3kwarnings=False, shell=None):
348 py3kwarnings=False, shell=None):
349 """Create a test from parameters.
349 """Create a test from parameters.
350
350
351 path is the full path to the file defining the test.
351 path is the full path to the file defining the test.
352
352
353 tmpdir is the main temporary directory to use for this test.
353 tmpdir is the main temporary directory to use for this test.
354
354
355 keeptmpdir determines whether to keep the test's temporary directory
355 keeptmpdir determines whether to keep the test's temporary directory
356 after execution. It defaults to removal (False).
356 after execution. It defaults to removal (False).
357
357
358 debug mode will make the test execute verbosely, with unfiltered
358 debug mode will make the test execute verbosely, with unfiltered
359 output.
359 output.
360
360
361 timeout controls the maximum run time of the test. It is ignored when
361 timeout controls the maximum run time of the test. It is ignored when
362 debug is True.
362 debug is True.
363
363
364 startport controls the starting port number to use for this test. Each
364 startport controls the starting port number to use for this test. Each
365 test will reserve 3 port numbers for execution. It is the caller's
365 test will reserve 3 port numbers for execution. It is the caller's
366 responsibility to allocate a non-overlapping port range to Test
366 responsibility to allocate a non-overlapping port range to Test
367 instances.
367 instances.
368
368
369 extraconfigopts is an iterable of extra hgrc config options. Values
369 extraconfigopts is an iterable of extra hgrc config options. Values
370 must have the form "key=value" (something understood by hgrc). Values
370 must have the form "key=value" (something understood by hgrc). Values
371 of the form "foo.key=value" will result in "[foo] key=value".
371 of the form "foo.key=value" will result in "[foo] key=value".
372
372
373 py3kwarnings enables Py3k warnings.
373 py3kwarnings enables Py3k warnings.
374
374
375 shell is the shell to execute tests in.
375 shell is the shell to execute tests in.
376 """
376 """
377
377
378 self.path = path
378 self.path = path
379 self.name = os.path.basename(path)
379 self.name = os.path.basename(path)
380 self._testdir = os.path.dirname(path)
380 self._testdir = os.path.dirname(path)
381 self.errpath = os.path.join(self._testdir, '%s.err' % self.name)
381 self.errpath = os.path.join(self._testdir, '%s.err' % self.name)
382
382
383 self._threadtmp = tmpdir
383 self._threadtmp = tmpdir
384 self._keeptmpdir = keeptmpdir
384 self._keeptmpdir = keeptmpdir
385 self._debug = debug
385 self._debug = debug
386 self._timeout = timeout
386 self._timeout = timeout
387 self._startport = startport
387 self._startport = startport
388 self._extraconfigopts = extraconfigopts or []
388 self._extraconfigopts = extraconfigopts or []
389 self._py3kwarnings = py3kwarnings
389 self._py3kwarnings = py3kwarnings
390 self._shell = shell
390 self._shell = shell
391
391
392 self._aborted = False
392 self._aborted = False
393 self._daemonpids = []
393 self._daemonpids = []
394 self._finished = None
394 self._finished = None
395 self._ret = None
395 self._ret = None
396 self._out = None
396 self._out = None
397 self._skipped = None
397 self._skipped = None
398 self._testtmp = None
398 self._testtmp = None
399
399
400 # If we're not in --debug mode and reference output file exists,
400 # If we're not in --debug mode and reference output file exists,
401 # check test output against it.
401 # check test output against it.
402 if debug:
402 if debug:
403 self._refout = None # to match "out is None"
403 self._refout = None # to match "out is None"
404 elif os.path.exists(self.refpath):
404 elif os.path.exists(self.refpath):
405 f = open(self.refpath, 'r')
405 f = open(self.refpath, 'rb')
406 self._refout = f.read().splitlines(True)
406 self._refout = f.read().splitlines(True)
407 f.close()
407 f.close()
408 else:
408 else:
409 self._refout = []
409 self._refout = []
410
410
411 def __str__(self):
411 def __str__(self):
412 return self.name
412 return self.name
413
413
414 def shortDescription(self):
414 def shortDescription(self):
415 return self.name
415 return self.name
416
416
417 def setUp(self):
417 def setUp(self):
418 """Tasks to perform before run()."""
418 """Tasks to perform before run()."""
419 self._finished = False
419 self._finished = False
420 self._ret = None
420 self._ret = None
421 self._out = None
421 self._out = None
422 self._skipped = None
422 self._skipped = None
423
423
424 try:
424 try:
425 os.mkdir(self._threadtmp)
425 os.mkdir(self._threadtmp)
426 except OSError, e:
426 except OSError, e:
427 if e.errno != errno.EEXIST:
427 if e.errno != errno.EEXIST:
428 raise
428 raise
429
429
430 self._testtmp = os.path.join(self._threadtmp,
430 self._testtmp = os.path.join(self._threadtmp,
431 os.path.basename(self.path))
431 os.path.basename(self.path))
432 os.mkdir(self._testtmp)
432 os.mkdir(self._testtmp)
433
433
434 # Remove any previous output files.
434 # Remove any previous output files.
435 if os.path.exists(self.errpath):
435 if os.path.exists(self.errpath):
436 os.remove(self.errpath)
436 os.remove(self.errpath)
437
437
438 def run(self, result):
438 def run(self, result):
439 """Run this test and report results against a TestResult instance."""
439 """Run this test and report results against a TestResult instance."""
440 # This function is extremely similar to unittest.TestCase.run(). Once
440 # This function is extremely similar to unittest.TestCase.run(). Once
441 # we require Python 2.7 (or at least its version of unittest), this
441 # we require Python 2.7 (or at least its version of unittest), this
442 # function can largely go away.
442 # function can largely go away.
443 self._result = result
443 self._result = result
444 result.startTest(self)
444 result.startTest(self)
445 try:
445 try:
446 try:
446 try:
447 self.setUp()
447 self.setUp()
448 except (KeyboardInterrupt, SystemExit):
448 except (KeyboardInterrupt, SystemExit):
449 self._aborted = True
449 self._aborted = True
450 raise
450 raise
451 except Exception:
451 except Exception:
452 result.addError(self, sys.exc_info())
452 result.addError(self, sys.exc_info())
453 return
453 return
454
454
455 success = False
455 success = False
456 try:
456 try:
457 self.runTest()
457 self.runTest()
458 except KeyboardInterrupt:
458 except KeyboardInterrupt:
459 self._aborted = True
459 self._aborted = True
460 raise
460 raise
461 except SkipTest, e:
461 except SkipTest, e:
462 result.addSkip(self, str(e))
462 result.addSkip(self, str(e))
463 except IgnoreTest, e:
463 except IgnoreTest, e:
464 result.addIgnore(self, str(e))
464 result.addIgnore(self, str(e))
465 except WarnTest, e:
465 except WarnTest, e:
466 result.addWarn(self, str(e))
466 result.addWarn(self, str(e))
467 except self.failureException, e:
467 except self.failureException, e:
468 # This differs from unittest in that we don't capture
468 # This differs from unittest in that we don't capture
469 # the stack trace. This is for historical reasons and
469 # the stack trace. This is for historical reasons and
470 # this decision could be revisted in the future,
470 # this decision could be revisted in the future,
471 # especially for PythonTest instances.
471 # especially for PythonTest instances.
472 if result.addFailure(self, str(e)):
472 if result.addFailure(self, str(e)):
473 success = True
473 success = True
474 except Exception:
474 except Exception:
475 result.addError(self, sys.exc_info())
475 result.addError(self, sys.exc_info())
476 else:
476 else:
477 success = True
477 success = True
478
478
479 try:
479 try:
480 self.tearDown()
480 self.tearDown()
481 except (KeyboardInterrupt, SystemExit):
481 except (KeyboardInterrupt, SystemExit):
482 self._aborted = True
482 self._aborted = True
483 raise
483 raise
484 except Exception:
484 except Exception:
485 result.addError(self, sys.exc_info())
485 result.addError(self, sys.exc_info())
486 success = False
486 success = False
487
487
488 if success:
488 if success:
489 result.addSuccess(self)
489 result.addSuccess(self)
490 finally:
490 finally:
491 result.stopTest(self, interrupted=self._aborted)
491 result.stopTest(self, interrupted=self._aborted)
492
492
493 def runTest(self):
493 def runTest(self):
494 """Run this test instance.
494 """Run this test instance.
495
495
496 This will return a tuple describing the result of the test.
496 This will return a tuple describing the result of the test.
497 """
497 """
498 replacements = self._getreplacements()
498 replacements = self._getreplacements()
499 env = self._getenv()
499 env = self._getenv()
500 self._daemonpids.append(env['DAEMON_PIDS'])
500 self._daemonpids.append(env['DAEMON_PIDS'])
501 self._createhgrc(env['HGRCPATH'])
501 self._createhgrc(env['HGRCPATH'])
502
502
503 vlog('# Test', self.name)
503 vlog('# Test', self.name)
504
504
505 ret, out = self._run(replacements, env)
505 ret, out = self._run(replacements, env)
506 self._finished = True
506 self._finished = True
507 self._ret = ret
507 self._ret = ret
508 self._out = out
508 self._out = out
509
509
510 def describe(ret):
510 def describe(ret):
511 if ret < 0:
511 if ret < 0:
512 return 'killed by signal: %d' % -ret
512 return 'killed by signal: %d' % -ret
513 return 'returned error code %d' % ret
513 return 'returned error code %d' % ret
514
514
515 self._skipped = False
515 self._skipped = False
516
516
517 if ret == self.SKIPPED_STATUS:
517 if ret == self.SKIPPED_STATUS:
518 if out is None: # Debug mode, nothing to parse.
518 if out is None: # Debug mode, nothing to parse.
519 missing = ['unknown']
519 missing = ['unknown']
520 failed = None
520 failed = None
521 else:
521 else:
522 missing, failed = TTest.parsehghaveoutput(out)
522 missing, failed = TTest.parsehghaveoutput(out)
523
523
524 if not missing:
524 if not missing:
525 missing = ['irrelevant']
525 missing = ['irrelevant']
526
526
527 if failed:
527 if failed:
528 self.fail('hg have failed checking for %s' % failed[-1])
528 self.fail('hg have failed checking for %s' % failed[-1])
529 else:
529 else:
530 self._skipped = True
530 self._skipped = True
531 raise SkipTest(missing[-1])
531 raise SkipTest(missing[-1])
532 elif ret == 'timeout':
532 elif ret == 'timeout':
533 self.fail('timed out')
533 self.fail('timed out')
534 elif ret is False:
534 elif ret is False:
535 raise WarnTest('no result code from test')
535 raise WarnTest('no result code from test')
536 elif out != self._refout:
536 elif out != self._refout:
537 # Diff generation may rely on written .err file.
537 # Diff generation may rely on written .err file.
538 if (ret != 0 or out != self._refout) and not self._skipped \
538 if (ret != 0 or out != self._refout) and not self._skipped \
539 and not self._debug:
539 and not self._debug:
540 f = open(self.errpath, 'wb')
540 f = open(self.errpath, 'wb')
541 for line in out:
541 for line in out:
542 f.write(line)
542 f.write(line)
543 f.close()
543 f.close()
544
544
545 # The result object handles diff calculation for us.
545 # The result object handles diff calculation for us.
546 if self._result.addOutputMismatch(self, ret, out, self._refout):
546 if self._result.addOutputMismatch(self, ret, out, self._refout):
547 # change was accepted, skip failing
547 # change was accepted, skip failing
548 return
548 return
549
549
550 if ret:
550 if ret:
551 msg = 'output changed and ' + describe(ret)
551 msg = 'output changed and ' + describe(ret)
552 else:
552 else:
553 msg = 'output changed'
553 msg = 'output changed'
554
554
555 self.fail(msg)
555 self.fail(msg)
556 elif ret:
556 elif ret:
557 self.fail(describe(ret))
557 self.fail(describe(ret))
558
558
559 def tearDown(self):
559 def tearDown(self):
560 """Tasks to perform after run()."""
560 """Tasks to perform after run()."""
561 for entry in self._daemonpids:
561 for entry in self._daemonpids:
562 killdaemons(entry)
562 killdaemons(entry)
563 self._daemonpids = []
563 self._daemonpids = []
564
564
565 if not self._keeptmpdir:
565 if not self._keeptmpdir:
566 shutil.rmtree(self._testtmp, True)
566 shutil.rmtree(self._testtmp, True)
567 shutil.rmtree(self._threadtmp, True)
567 shutil.rmtree(self._threadtmp, True)
568
568
569 if (self._ret != 0 or self._out != self._refout) and not self._skipped \
569 if (self._ret != 0 or self._out != self._refout) and not self._skipped \
570 and not self._debug and self._out:
570 and not self._debug and self._out:
571 f = open(self.errpath, 'wb')
571 f = open(self.errpath, 'wb')
572 for line in self._out:
572 for line in self._out:
573 f.write(line)
573 f.write(line)
574 f.close()
574 f.close()
575
575
576 vlog("# Ret was:", self._ret)
576 vlog("# Ret was:", self._ret)
577
577
578 def _run(self, replacements, env):
578 def _run(self, replacements, env):
579 # This should be implemented in child classes to run tests.
579 # This should be implemented in child classes to run tests.
580 raise SkipTest('unknown test type')
580 raise SkipTest('unknown test type')
581
581
582 def abort(self):
582 def abort(self):
583 """Terminate execution of this test."""
583 """Terminate execution of this test."""
584 self._aborted = True
584 self._aborted = True
585
585
586 def _getreplacements(self):
586 def _getreplacements(self):
587 """Obtain a mapping of text replacements to apply to test output.
587 """Obtain a mapping of text replacements to apply to test output.
588
588
589 Test output needs to be normalized so it can be compared to expected
589 Test output needs to be normalized so it can be compared to expected
590 output. This function defines how some of that normalization will
590 output. This function defines how some of that normalization will
591 occur.
591 occur.
592 """
592 """
593 r = [
593 r = [
594 (r':%s\b' % self._startport, ':$HGPORT'),
594 (r':%s\b' % self._startport, ':$HGPORT'),
595 (r':%s\b' % (self._startport + 1), ':$HGPORT1'),
595 (r':%s\b' % (self._startport + 1), ':$HGPORT1'),
596 (r':%s\b' % (self._startport + 2), ':$HGPORT2'),
596 (r':%s\b' % (self._startport + 2), ':$HGPORT2'),
597 ]
597 ]
598
598
599 if os.name == 'nt':
599 if os.name == 'nt':
600 r.append(
600 r.append(
601 (''.join(c.isalpha() and '[%s%s]' % (c.lower(), c.upper()) or
601 (''.join(c.isalpha() and '[%s%s]' % (c.lower(), c.upper()) or
602 c in '/\\' and r'[/\\]' or c.isdigit() and c or '\\' + c
602 c in '/\\' and r'[/\\]' or c.isdigit() and c or '\\' + c
603 for c in self._testtmp), '$TESTTMP'))
603 for c in self._testtmp), '$TESTTMP'))
604 else:
604 else:
605 r.append((re.escape(self._testtmp), '$TESTTMP'))
605 r.append((re.escape(self._testtmp), '$TESTTMP'))
606
606
607 return r
607 return r
608
608
609 def _getenv(self):
609 def _getenv(self):
610 """Obtain environment variables to use during test execution."""
610 """Obtain environment variables to use during test execution."""
611 env = os.environ.copy()
611 env = os.environ.copy()
612 env['TESTTMP'] = self._testtmp
612 env['TESTTMP'] = self._testtmp
613 env['HOME'] = self._testtmp
613 env['HOME'] = self._testtmp
614 env["HGPORT"] = str(self._startport)
614 env["HGPORT"] = str(self._startport)
615 env["HGPORT1"] = str(self._startport + 1)
615 env["HGPORT1"] = str(self._startport + 1)
616 env["HGPORT2"] = str(self._startport + 2)
616 env["HGPORT2"] = str(self._startport + 2)
617 env["HGRCPATH"] = os.path.join(self._threadtmp, '.hgrc')
617 env["HGRCPATH"] = os.path.join(self._threadtmp, '.hgrc')
618 env["DAEMON_PIDS"] = os.path.join(self._threadtmp, 'daemon.pids')
618 env["DAEMON_PIDS"] = os.path.join(self._threadtmp, 'daemon.pids')
619 env["HGEDITOR"] = sys.executable + ' -c "import sys; sys.exit(0)"'
619 env["HGEDITOR"] = sys.executable + ' -c "import sys; sys.exit(0)"'
620 env["HGMERGE"] = "internal:merge"
620 env["HGMERGE"] = "internal:merge"
621 env["HGUSER"] = "test"
621 env["HGUSER"] = "test"
622 env["HGENCODING"] = "ascii"
622 env["HGENCODING"] = "ascii"
623 env["HGENCODINGMODE"] = "strict"
623 env["HGENCODINGMODE"] = "strict"
624
624
625 # Reset some environment variables to well-known values so that
625 # Reset some environment variables to well-known values so that
626 # the tests produce repeatable output.
626 # the tests produce repeatable output.
627 env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C'
627 env['LANG'] = env['LC_ALL'] = env['LANGUAGE'] = 'C'
628 env['TZ'] = 'GMT'
628 env['TZ'] = 'GMT'
629 env["EMAIL"] = "Foo Bar <foo.bar@example.com>"
629 env["EMAIL"] = "Foo Bar <foo.bar@example.com>"
630 env['COLUMNS'] = '80'
630 env['COLUMNS'] = '80'
631 env['TERM'] = 'xterm'
631 env['TERM'] = 'xterm'
632
632
633 for k in ('HG HGPROF CDPATH GREP_OPTIONS http_proxy no_proxy ' +
633 for k in ('HG HGPROF CDPATH GREP_OPTIONS http_proxy no_proxy ' +
634 'NO_PROXY').split():
634 'NO_PROXY').split():
635 if k in env:
635 if k in env:
636 del env[k]
636 del env[k]
637
637
638 # unset env related to hooks
638 # unset env related to hooks
639 for k in env.keys():
639 for k in env.keys():
640 if k.startswith('HG_'):
640 if k.startswith('HG_'):
641 del env[k]
641 del env[k]
642
642
643 return env
643 return env
644
644
645 def _createhgrc(self, path):
645 def _createhgrc(self, path):
646 """Create an hgrc file for this test."""
646 """Create an hgrc file for this test."""
647 hgrc = open(path, 'w')
647 hgrc = open(path, 'wb')
648 hgrc.write('[ui]\n')
648 hgrc.write('[ui]\n')
649 hgrc.write('slash = True\n')
649 hgrc.write('slash = True\n')
650 hgrc.write('interactive = False\n')
650 hgrc.write('interactive = False\n')
651 hgrc.write('[defaults]\n')
651 hgrc.write('[defaults]\n')
652 hgrc.write('backout = -d "0 0"\n')
652 hgrc.write('backout = -d "0 0"\n')
653 hgrc.write('commit = -d "0 0"\n')
653 hgrc.write('commit = -d "0 0"\n')
654 hgrc.write('shelve = --date "0 0"\n')
654 hgrc.write('shelve = --date "0 0"\n')
655 hgrc.write('tag = -d "0 0"\n')
655 hgrc.write('tag = -d "0 0"\n')
656 for opt in self._extraconfigopts:
656 for opt in self._extraconfigopts:
657 section, key = opt.split('.', 1)
657 section, key = opt.split('.', 1)
658 assert '=' in key, ('extra config opt %s must '
658 assert '=' in key, ('extra config opt %s must '
659 'have an = for assignment' % opt)
659 'have an = for assignment' % opt)
660 hgrc.write('[%s]\n%s\n' % (section, key))
660 hgrc.write('[%s]\n%s\n' % (section, key))
661 hgrc.close()
661 hgrc.close()
662
662
663 def fail(self, msg):
663 def fail(self, msg):
664 # unittest differentiates between errored and failed.
664 # unittest differentiates between errored and failed.
665 # Failed is denoted by AssertionError (by default at least).
665 # Failed is denoted by AssertionError (by default at least).
666 raise AssertionError(msg)
666 raise AssertionError(msg)
667
667
668 class PythonTest(Test):
668 class PythonTest(Test):
669 """A Python-based test."""
669 """A Python-based test."""
670
670
671 @property
671 @property
672 def refpath(self):
672 def refpath(self):
673 return os.path.join(self._testdir, '%s.out' % self.name)
673 return os.path.join(self._testdir, '%s.out' % self.name)
674
674
675 def _run(self, replacements, env):
675 def _run(self, replacements, env):
676 py3kswitch = self._py3kwarnings and ' -3' or ''
676 py3kswitch = self._py3kwarnings and ' -3' or ''
677 cmd = '%s%s "%s"' % (PYTHON, py3kswitch, self.path)
677 cmd = '%s%s "%s"' % (PYTHON, py3kswitch, self.path)
678 vlog("# Running", cmd)
678 vlog("# Running", cmd)
679 if os.name == 'nt':
679 if os.name == 'nt':
680 replacements.append((r'\r\n', '\n'))
680 replacements.append((r'\r\n', '\n'))
681 result = run(cmd, self._testtmp, replacements, env,
681 result = run(cmd, self._testtmp, replacements, env,
682 debug=self._debug, timeout=self._timeout)
682 debug=self._debug, timeout=self._timeout)
683 if self._aborted:
683 if self._aborted:
684 raise KeyboardInterrupt()
684 raise KeyboardInterrupt()
685
685
686 return result
686 return result
687
687
688 class TTest(Test):
688 class TTest(Test):
689 """A "t test" is a test backed by a .t file."""
689 """A "t test" is a test backed by a .t file."""
690
690
691 SKIPPED_PREFIX = 'skipped: '
691 SKIPPED_PREFIX = 'skipped: '
692 FAILED_PREFIX = 'hghave check failed: '
692 FAILED_PREFIX = 'hghave check failed: '
693 NEEDESCAPE = re.compile(r'[\x00-\x08\x0b-\x1f\x7f-\xff]').search
693 NEEDESCAPE = re.compile(r'[\x00-\x08\x0b-\x1f\x7f-\xff]').search
694
694
695 ESCAPESUB = re.compile(r'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub
695 ESCAPESUB = re.compile(r'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub
696 ESCAPEMAP = dict((chr(i), r'\x%02x' % i) for i in range(256))
696 ESCAPEMAP = dict((chr(i), r'\x%02x' % i) for i in range(256))
697 ESCAPEMAP.update({'\\': '\\\\', '\r': r'\r'})
697 ESCAPEMAP.update({'\\': '\\\\', '\r': r'\r'})
698
698
699 @property
699 @property
700 def refpath(self):
700 def refpath(self):
701 return os.path.join(self._testdir, self.name)
701 return os.path.join(self._testdir, self.name)
702
702
703 def _run(self, replacements, env):
703 def _run(self, replacements, env):
704 f = open(self.path)
704 f = open(self.path, 'rb')
705 lines = f.readlines()
705 lines = f.readlines()
706 f.close()
706 f.close()
707
707
708 salt, script, after, expected = self._parsetest(lines)
708 salt, script, after, expected = self._parsetest(lines)
709
709
710 # Write out the generated script.
710 # Write out the generated script.
711 fname = '%s.sh' % self._testtmp
711 fname = '%s.sh' % self._testtmp
712 f = open(fname, 'w')
712 f = open(fname, 'wb')
713 for l in script:
713 for l in script:
714 f.write(l)
714 f.write(l)
715 f.close()
715 f.close()
716
716
717 cmd = '%s "%s"' % (self._shell, fname)
717 cmd = '%s "%s"' % (self._shell, fname)
718 vlog("# Running", cmd)
718 vlog("# Running", cmd)
719
719
720 exitcode, output = run(cmd, self._testtmp, replacements, env,
720 exitcode, output = run(cmd, self._testtmp, replacements, env,
721 debug=self._debug, timeout=self._timeout)
721 debug=self._debug, timeout=self._timeout)
722
722
723 if self._aborted:
723 if self._aborted:
724 raise KeyboardInterrupt()
724 raise KeyboardInterrupt()
725
725
726 # Do not merge output if skipped. Return hghave message instead.
726 # Do not merge output if skipped. Return hghave message instead.
727 # Similarly, with --debug, output is None.
727 # Similarly, with --debug, output is None.
728 if exitcode == self.SKIPPED_STATUS or output is None:
728 if exitcode == self.SKIPPED_STATUS or output is None:
729 return exitcode, output
729 return exitcode, output
730
730
731 return self._processoutput(exitcode, output, salt, after, expected)
731 return self._processoutput(exitcode, output, salt, after, expected)
732
732
733 def _hghave(self, reqs):
733 def _hghave(self, reqs):
734 # TODO do something smarter when all other uses of hghave are gone.
734 # TODO do something smarter when all other uses of hghave are gone.
735 tdir = self._testdir.replace('\\', '/')
735 tdir = self._testdir.replace('\\', '/')
736 proc = Popen4('%s -c "%s/hghave %s"' %
736 proc = Popen4('%s -c "%s/hghave %s"' %
737 (self._shell, tdir, ' '.join(reqs)),
737 (self._shell, tdir, ' '.join(reqs)),
738 self._testtmp, 0)
738 self._testtmp, 0)
739 stdout, stderr = proc.communicate()
739 stdout, stderr = proc.communicate()
740 ret = proc.wait()
740 ret = proc.wait()
741 if wifexited(ret):
741 if wifexited(ret):
742 ret = os.WEXITSTATUS(ret)
742 ret = os.WEXITSTATUS(ret)
743 if ret == 2:
743 if ret == 2:
744 print stdout
744 print stdout
745 sys.exit(1)
745 sys.exit(1)
746
746
747 return ret == 0
747 return ret == 0
748
748
749 def _parsetest(self, lines):
749 def _parsetest(self, lines):
750 # We generate a shell script which outputs unique markers to line
750 # We generate a shell script which outputs unique markers to line
751 # up script results with our source. These markers include input
751 # up script results with our source. These markers include input
752 # line number and the last return code.
752 # line number and the last return code.
753 salt = "SALT" + str(time.time())
753 salt = "SALT" + str(time.time())
754 def addsalt(line, inpython):
754 def addsalt(line, inpython):
755 if inpython:
755 if inpython:
756 script.append('%s %d 0\n' % (salt, line))
756 script.append('%s %d 0\n' % (salt, line))
757 else:
757 else:
758 script.append('echo %s %s $?\n' % (salt, line))
758 script.append('echo %s %s $?\n' % (salt, line))
759
759
760 script = []
760 script = []
761
761
762 # After we run the shell script, we re-unify the script output
762 # After we run the shell script, we re-unify the script output
763 # with non-active parts of the source, with synchronization by our
763 # with non-active parts of the source, with synchronization by our
764 # SALT line number markers. The after table contains the non-active
764 # SALT line number markers. The after table contains the non-active
765 # components, ordered by line number.
765 # components, ordered by line number.
766 after = {}
766 after = {}
767
767
768 # Expected shell script output.
768 # Expected shell script output.
769 expected = {}
769 expected = {}
770
770
771 pos = prepos = -1
771 pos = prepos = -1
772
772
773 # True or False when in a true or false conditional section
773 # True or False when in a true or false conditional section
774 skipping = None
774 skipping = None
775
775
776 # We keep track of whether or not we're in a Python block so we
776 # We keep track of whether or not we're in a Python block so we
777 # can generate the surrounding doctest magic.
777 # can generate the surrounding doctest magic.
778 inpython = False
778 inpython = False
779
779
780 if self._debug:
780 if self._debug:
781 script.append('set -x\n')
781 script.append('set -x\n')
782 if os.getenv('MSYSTEM'):
782 if os.getenv('MSYSTEM'):
783 script.append('alias pwd="pwd -W"\n')
783 script.append('alias pwd="pwd -W"\n')
784
784
785 for n, l in enumerate(lines):
785 for n, l in enumerate(lines):
786 if not l.endswith('\n'):
786 if not l.endswith('\n'):
787 l += '\n'
787 l += '\n'
788 if l.startswith('#if'):
788 if l.startswith('#if'):
789 lsplit = l.split()
789 lsplit = l.split()
790 if len(lsplit) < 2 or lsplit[0] != '#if':
790 if len(lsplit) < 2 or lsplit[0] != '#if':
791 after.setdefault(pos, []).append(' !!! invalid #if\n')
791 after.setdefault(pos, []).append(' !!! invalid #if\n')
792 if skipping is not None:
792 if skipping is not None:
793 after.setdefault(pos, []).append(' !!! nested #if\n')
793 after.setdefault(pos, []).append(' !!! nested #if\n')
794 skipping = not self._hghave(lsplit[1:])
794 skipping = not self._hghave(lsplit[1:])
795 after.setdefault(pos, []).append(l)
795 after.setdefault(pos, []).append(l)
796 elif l.startswith('#else'):
796 elif l.startswith('#else'):
797 if skipping is None:
797 if skipping is None:
798 after.setdefault(pos, []).append(' !!! missing #if\n')
798 after.setdefault(pos, []).append(' !!! missing #if\n')
799 skipping = not skipping
799 skipping = not skipping
800 after.setdefault(pos, []).append(l)
800 after.setdefault(pos, []).append(l)
801 elif l.startswith('#endif'):
801 elif l.startswith('#endif'):
802 if skipping is None:
802 if skipping is None:
803 after.setdefault(pos, []).append(' !!! missing #if\n')
803 after.setdefault(pos, []).append(' !!! missing #if\n')
804 skipping = None
804 skipping = None
805 after.setdefault(pos, []).append(l)
805 after.setdefault(pos, []).append(l)
806 elif skipping:
806 elif skipping:
807 after.setdefault(pos, []).append(l)
807 after.setdefault(pos, []).append(l)
808 elif l.startswith(' >>> '): # python inlines
808 elif l.startswith(' >>> '): # python inlines
809 after.setdefault(pos, []).append(l)
809 after.setdefault(pos, []).append(l)
810 prepos = pos
810 prepos = pos
811 pos = n
811 pos = n
812 if not inpython:
812 if not inpython:
813 # We've just entered a Python block. Add the header.
813 # We've just entered a Python block. Add the header.
814 inpython = True
814 inpython = True
815 addsalt(prepos, False) # Make sure we report the exit code.
815 addsalt(prepos, False) # Make sure we report the exit code.
816 script.append('%s -m heredoctest <<EOF\n' % PYTHON)
816 script.append('%s -m heredoctest <<EOF\n' % PYTHON)
817 addsalt(n, True)
817 addsalt(n, True)
818 script.append(l[2:])
818 script.append(l[2:])
819 elif l.startswith(' ... '): # python inlines
819 elif l.startswith(' ... '): # python inlines
820 after.setdefault(prepos, []).append(l)
820 after.setdefault(prepos, []).append(l)
821 script.append(l[2:])
821 script.append(l[2:])
822 elif l.startswith(' $ '): # commands
822 elif l.startswith(' $ '): # commands
823 if inpython:
823 if inpython:
824 script.append('EOF\n')
824 script.append('EOF\n')
825 inpython = False
825 inpython = False
826 after.setdefault(pos, []).append(l)
826 after.setdefault(pos, []).append(l)
827 prepos = pos
827 prepos = pos
828 pos = n
828 pos = n
829 addsalt(n, False)
829 addsalt(n, False)
830 cmd = l[4:].split()
830 cmd = l[4:].split()
831 if len(cmd) == 2 and cmd[0] == 'cd':
831 if len(cmd) == 2 and cmd[0] == 'cd':
832 l = ' $ cd %s || exit 1\n' % cmd[1]
832 l = ' $ cd %s || exit 1\n' % cmd[1]
833 script.append(l[4:])
833 script.append(l[4:])
834 elif l.startswith(' > '): # continuations
834 elif l.startswith(' > '): # continuations
835 after.setdefault(prepos, []).append(l)
835 after.setdefault(prepos, []).append(l)
836 script.append(l[4:])
836 script.append(l[4:])
837 elif l.startswith(' '): # results
837 elif l.startswith(' '): # results
838 # Queue up a list of expected results.
838 # Queue up a list of expected results.
839 expected.setdefault(pos, []).append(l[2:])
839 expected.setdefault(pos, []).append(l[2:])
840 else:
840 else:
841 if inpython:
841 if inpython:
842 script.append('EOF\n')
842 script.append('EOF\n')
843 inpython = False
843 inpython = False
844 # Non-command/result. Queue up for merged output.
844 # Non-command/result. Queue up for merged output.
845 after.setdefault(pos, []).append(l)
845 after.setdefault(pos, []).append(l)
846
846
847 if inpython:
847 if inpython:
848 script.append('EOF\n')
848 script.append('EOF\n')
849 if skipping is not None:
849 if skipping is not None:
850 after.setdefault(pos, []).append(' !!! missing #endif\n')
850 after.setdefault(pos, []).append(' !!! missing #endif\n')
851 addsalt(n + 1, False)
851 addsalt(n + 1, False)
852
852
853 return salt, script, after, expected
853 return salt, script, after, expected
854
854
855 def _processoutput(self, exitcode, output, salt, after, expected):
855 def _processoutput(self, exitcode, output, salt, after, expected):
856 # Merge the script output back into a unified test.
856 # Merge the script output back into a unified test.
857 warnonly = 1 # 1: not yet; 2: yes; 3: for sure not
857 warnonly = 1 # 1: not yet; 2: yes; 3: for sure not
858 if exitcode != 0:
858 if exitcode != 0:
859 warnonly = 3
859 warnonly = 3
860
860
861 pos = -1
861 pos = -1
862 postout = []
862 postout = []
863 for l in output:
863 for l in output:
864 lout, lcmd = l, None
864 lout, lcmd = l, None
865 if salt in l:
865 if salt in l:
866 lout, lcmd = l.split(salt, 1)
866 lout, lcmd = l.split(salt, 1)
867
867
868 if lout:
868 if lout:
869 if not lout.endswith('\n'):
869 if not lout.endswith('\n'):
870 lout += ' (no-eol)\n'
870 lout += ' (no-eol)\n'
871
871
872 # Find the expected output at the current position.
872 # Find the expected output at the current position.
873 el = None
873 el = None
874 if expected.get(pos, None):
874 if expected.get(pos, None):
875 el = expected[pos].pop(0)
875 el = expected[pos].pop(0)
876
876
877 r = TTest.linematch(el, lout)
877 r = TTest.linematch(el, lout)
878 if isinstance(r, str):
878 if isinstance(r, str):
879 if r == '+glob':
879 if r == '+glob':
880 lout = el[:-1] + ' (glob)\n'
880 lout = el[:-1] + ' (glob)\n'
881 r = '' # Warn only this line.
881 r = '' # Warn only this line.
882 elif r == '-glob':
882 elif r == '-glob':
883 lout = ''.join(el.rsplit(' (glob)', 1))
883 lout = ''.join(el.rsplit(' (glob)', 1))
884 r = '' # Warn only this line.
884 r = '' # Warn only this line.
885 else:
885 else:
886 log('\ninfo, unknown linematch result: %r\n' % r)
886 log('\ninfo, unknown linematch result: %r\n' % r)
887 r = False
887 r = False
888 if r:
888 if r:
889 postout.append(' ' + el)
889 postout.append(' ' + el)
890 else:
890 else:
891 if self.NEEDESCAPE(lout):
891 if self.NEEDESCAPE(lout):
892 lout = TTest._stringescape('%s (esc)\n' %
892 lout = TTest._stringescape('%s (esc)\n' %
893 lout.rstrip('\n'))
893 lout.rstrip('\n'))
894 postout.append(' ' + lout) # Let diff deal with it.
894 postout.append(' ' + lout) # Let diff deal with it.
895 if r != '': # If line failed.
895 if r != '': # If line failed.
896 warnonly = 3 # for sure not
896 warnonly = 3 # for sure not
897 elif warnonly == 1: # Is "not yet" and line is warn only.
897 elif warnonly == 1: # Is "not yet" and line is warn only.
898 warnonly = 2 # Yes do warn.
898 warnonly = 2 # Yes do warn.
899
899
900 if lcmd:
900 if lcmd:
901 # Add on last return code.
901 # Add on last return code.
902 ret = int(lcmd.split()[1])
902 ret = int(lcmd.split()[1])
903 if ret != 0:
903 if ret != 0:
904 postout.append(' [%s]\n' % ret)
904 postout.append(' [%s]\n' % ret)
905 if pos in after:
905 if pos in after:
906 # Merge in non-active test bits.
906 # Merge in non-active test bits.
907 postout += after.pop(pos)
907 postout += after.pop(pos)
908 pos = int(lcmd.split()[0])
908 pos = int(lcmd.split()[0])
909
909
910 if pos in after:
910 if pos in after:
911 postout += after.pop(pos)
911 postout += after.pop(pos)
912
912
913 if warnonly == 2:
913 if warnonly == 2:
914 exitcode = False # Set exitcode to warned.
914 exitcode = False # Set exitcode to warned.
915
915
916 return exitcode, postout
916 return exitcode, postout
917
917
918 @staticmethod
918 @staticmethod
919 def rematch(el, l):
919 def rematch(el, l):
920 try:
920 try:
921 # use \Z to ensure that the regex matches to the end of the string
921 # use \Z to ensure that the regex matches to the end of the string
922 if os.name == 'nt':
922 if os.name == 'nt':
923 return re.match(el + r'\r?\n\Z', l)
923 return re.match(el + r'\r?\n\Z', l)
924 return re.match(el + r'\n\Z', l)
924 return re.match(el + r'\n\Z', l)
925 except re.error:
925 except re.error:
926 # el is an invalid regex
926 # el is an invalid regex
927 return False
927 return False
928
928
929 @staticmethod
929 @staticmethod
930 def globmatch(el, l):
930 def globmatch(el, l):
931 # The only supported special characters are * and ? plus / which also
931 # The only supported special characters are * and ? plus / which also
932 # matches \ on windows. Escaping of these characters is supported.
932 # matches \ on windows. Escaping of these characters is supported.
933 if el + '\n' == l:
933 if el + '\n' == l:
934 if os.altsep:
934 if os.altsep:
935 # matching on "/" is not needed for this line
935 # matching on "/" is not needed for this line
936 return '-glob'
936 return '-glob'
937 return True
937 return True
938 i, n = 0, len(el)
938 i, n = 0, len(el)
939 res = ''
939 res = ''
940 while i < n:
940 while i < n:
941 c = el[i]
941 c = el[i]
942 i += 1
942 i += 1
943 if c == '\\' and el[i] in '*?\\/':
943 if c == '\\' and el[i] in '*?\\/':
944 res += el[i - 1:i + 1]
944 res += el[i - 1:i + 1]
945 i += 1
945 i += 1
946 elif c == '*':
946 elif c == '*':
947 res += '.*'
947 res += '.*'
948 elif c == '?':
948 elif c == '?':
949 res += '.'
949 res += '.'
950 elif c == '/' and os.altsep:
950 elif c == '/' and os.altsep:
951 res += '[/\\\\]'
951 res += '[/\\\\]'
952 else:
952 else:
953 res += re.escape(c)
953 res += re.escape(c)
954 return TTest.rematch(res, l)
954 return TTest.rematch(res, l)
955
955
956 @staticmethod
956 @staticmethod
957 def linematch(el, l):
957 def linematch(el, l):
958 if el == l: # perfect match (fast)
958 if el == l: # perfect match (fast)
959 return True
959 return True
960 if el:
960 if el:
961 if el.endswith(" (esc)\n"):
961 if el.endswith(" (esc)\n"):
962 el = el[:-7].decode('string-escape') + '\n'
962 el = el[:-7].decode('string-escape') + '\n'
963 if el == l or os.name == 'nt' and el[:-1] + '\r\n' == l:
963 if el == l or os.name == 'nt' and el[:-1] + '\r\n' == l:
964 return True
964 return True
965 if el.endswith(" (re)\n"):
965 if el.endswith(" (re)\n"):
966 return TTest.rematch(el[:-6], l)
966 return TTest.rematch(el[:-6], l)
967 if el.endswith(" (glob)\n"):
967 if el.endswith(" (glob)\n"):
968 return TTest.globmatch(el[:-8], l)
968 return TTest.globmatch(el[:-8], l)
969 if os.altsep and l.replace('\\', '/') == el:
969 if os.altsep and l.replace('\\', '/') == el:
970 return '+glob'
970 return '+glob'
971 return False
971 return False
972
972
973 @staticmethod
973 @staticmethod
974 def parsehghaveoutput(lines):
974 def parsehghaveoutput(lines):
975 '''Parse hghave log lines.
975 '''Parse hghave log lines.
976
976
977 Return tuple of lists (missing, failed):
977 Return tuple of lists (missing, failed):
978 * the missing/unknown features
978 * the missing/unknown features
979 * the features for which existence check failed'''
979 * the features for which existence check failed'''
980 missing = []
980 missing = []
981 failed = []
981 failed = []
982 for line in lines:
982 for line in lines:
983 if line.startswith(TTest.SKIPPED_PREFIX):
983 if line.startswith(TTest.SKIPPED_PREFIX):
984 line = line.splitlines()[0]
984 line = line.splitlines()[0]
985 missing.append(line[len(TTest.SKIPPED_PREFIX):])
985 missing.append(line[len(TTest.SKIPPED_PREFIX):])
986 elif line.startswith(TTest.FAILED_PREFIX):
986 elif line.startswith(TTest.FAILED_PREFIX):
987 line = line.splitlines()[0]
987 line = line.splitlines()[0]
988 failed.append(line[len(TTest.FAILED_PREFIX):])
988 failed.append(line[len(TTest.FAILED_PREFIX):])
989
989
990 return missing, failed
990 return missing, failed
991
991
992 @staticmethod
992 @staticmethod
993 def _escapef(m):
993 def _escapef(m):
994 return TTest.ESCAPEMAP[m.group(0)]
994 return TTest.ESCAPEMAP[m.group(0)]
995
995
996 @staticmethod
996 @staticmethod
997 def _stringescape(s):
997 def _stringescape(s):
998 return TTest.ESCAPESUB(TTest._escapef, s)
998 return TTest.ESCAPESUB(TTest._escapef, s)
999
999
1000
1000
1001 wifexited = getattr(os, "WIFEXITED", lambda x: False)
1001 wifexited = getattr(os, "WIFEXITED", lambda x: False)
1002 def run(cmd, wd, replacements, env, debug=False, timeout=None):
1002 def run(cmd, wd, replacements, env, debug=False, timeout=None):
1003 """Run command in a sub-process, capturing the output (stdout and stderr).
1003 """Run command in a sub-process, capturing the output (stdout and stderr).
1004 Return a tuple (exitcode, output). output is None in debug mode."""
1004 Return a tuple (exitcode, output). output is None in debug mode."""
1005 if debug:
1005 if debug:
1006 proc = subprocess.Popen(cmd, shell=True, cwd=wd, env=env)
1006 proc = subprocess.Popen(cmd, shell=True, cwd=wd, env=env)
1007 ret = proc.wait()
1007 ret = proc.wait()
1008 return (ret, None)
1008 return (ret, None)
1009
1009
1010 proc = Popen4(cmd, wd, timeout, env)
1010 proc = Popen4(cmd, wd, timeout, env)
1011 def cleanup():
1011 def cleanup():
1012 terminate(proc)
1012 terminate(proc)
1013 ret = proc.wait()
1013 ret = proc.wait()
1014 if ret == 0:
1014 if ret == 0:
1015 ret = signal.SIGTERM << 8
1015 ret = signal.SIGTERM << 8
1016 killdaemons(env['DAEMON_PIDS'])
1016 killdaemons(env['DAEMON_PIDS'])
1017 return ret
1017 return ret
1018
1018
1019 output = ''
1019 output = ''
1020 proc.tochild.close()
1020 proc.tochild.close()
1021
1021
1022 try:
1022 try:
1023 output = proc.fromchild.read()
1023 output = proc.fromchild.read()
1024 except KeyboardInterrupt:
1024 except KeyboardInterrupt:
1025 vlog('# Handling keyboard interrupt')
1025 vlog('# Handling keyboard interrupt')
1026 cleanup()
1026 cleanup()
1027 raise
1027 raise
1028
1028
1029 ret = proc.wait()
1029 ret = proc.wait()
1030 if wifexited(ret):
1030 if wifexited(ret):
1031 ret = os.WEXITSTATUS(ret)
1031 ret = os.WEXITSTATUS(ret)
1032
1032
1033 if proc.timeout:
1033 if proc.timeout:
1034 ret = 'timeout'
1034 ret = 'timeout'
1035
1035
1036 if ret:
1036 if ret:
1037 killdaemons(env['DAEMON_PIDS'])
1037 killdaemons(env['DAEMON_PIDS'])
1038
1038
1039 for s, r in replacements:
1039 for s, r in replacements:
1040 output = re.sub(s, r, output)
1040 output = re.sub(s, r, output)
1041 return ret, output.splitlines(True)
1041 return ret, output.splitlines(True)
1042
1042
1043 iolock = threading.Lock()
1043 iolock = threading.Lock()
1044
1044
1045 class SkipTest(Exception):
1045 class SkipTest(Exception):
1046 """Raised to indicate that a test is to be skipped."""
1046 """Raised to indicate that a test is to be skipped."""
1047
1047
1048 class IgnoreTest(Exception):
1048 class IgnoreTest(Exception):
1049 """Raised to indicate that a test is to be ignored."""
1049 """Raised to indicate that a test is to be ignored."""
1050
1050
1051 class WarnTest(Exception):
1051 class WarnTest(Exception):
1052 """Raised to indicate that a test warned."""
1052 """Raised to indicate that a test warned."""
1053
1053
1054 class TestResult(unittest._TextTestResult):
1054 class TestResult(unittest._TextTestResult):
1055 """Holds results when executing via unittest."""
1055 """Holds results when executing via unittest."""
1056 # Don't worry too much about accessing the non-public _TextTestResult.
1056 # Don't worry too much about accessing the non-public _TextTestResult.
1057 # It is relatively common in Python testing tools.
1057 # It is relatively common in Python testing tools.
1058 def __init__(self, options, *args, **kwargs):
1058 def __init__(self, options, *args, **kwargs):
1059 super(TestResult, self).__init__(*args, **kwargs)
1059 super(TestResult, self).__init__(*args, **kwargs)
1060
1060
1061 self._options = options
1061 self._options = options
1062
1062
1063 # unittest.TestResult didn't have skipped until 2.7. We need to
1063 # unittest.TestResult didn't have skipped until 2.7. We need to
1064 # polyfill it.
1064 # polyfill it.
1065 self.skipped = []
1065 self.skipped = []
1066
1066
1067 # We have a custom "ignored" result that isn't present in any Python
1067 # We have a custom "ignored" result that isn't present in any Python
1068 # unittest implementation. It is very similar to skipped. It may make
1068 # unittest implementation. It is very similar to skipped. It may make
1069 # sense to map it into skip some day.
1069 # sense to map it into skip some day.
1070 self.ignored = []
1070 self.ignored = []
1071
1071
1072 # We have a custom "warned" result that isn't present in any Python
1072 # We have a custom "warned" result that isn't present in any Python
1073 # unittest implementation. It is very similar to failed. It may make
1073 # unittest implementation. It is very similar to failed. It may make
1074 # sense to map it into fail some day.
1074 # sense to map it into fail some day.
1075 self.warned = []
1075 self.warned = []
1076
1076
1077 self.times = []
1077 self.times = []
1078 self._started = {}
1078 self._started = {}
1079
1079
1080 def addFailure(self, test, reason):
1080 def addFailure(self, test, reason):
1081 self.failures.append((test, reason))
1081 self.failures.append((test, reason))
1082
1082
1083 if self._options.first:
1083 if self._options.first:
1084 self.stop()
1084 self.stop()
1085 else:
1085 else:
1086 if not self._options.nodiff:
1086 if not self._options.nodiff:
1087 self.stream.write('\nERROR: %s output changed\n' % test)
1087 self.stream.write('\nERROR: %s output changed\n' % test)
1088
1088
1089 self.stream.write('!')
1089 self.stream.write('!')
1090
1090
1091 def addError(self, *args, **kwargs):
1091 def addError(self, *args, **kwargs):
1092 super(TestResult, self).addError(*args, **kwargs)
1092 super(TestResult, self).addError(*args, **kwargs)
1093
1093
1094 if self._options.first:
1094 if self._options.first:
1095 self.stop()
1095 self.stop()
1096
1096
1097 # Polyfill.
1097 # Polyfill.
1098 def addSkip(self, test, reason):
1098 def addSkip(self, test, reason):
1099 self.skipped.append((test, reason))
1099 self.skipped.append((test, reason))
1100
1100
1101 if self.showAll:
1101 if self.showAll:
1102 self.stream.writeln('skipped %s' % reason)
1102 self.stream.writeln('skipped %s' % reason)
1103 else:
1103 else:
1104 self.stream.write('s')
1104 self.stream.write('s')
1105 self.stream.flush()
1105 self.stream.flush()
1106
1106
1107 def addIgnore(self, test, reason):
1107 def addIgnore(self, test, reason):
1108 self.ignored.append((test, reason))
1108 self.ignored.append((test, reason))
1109
1109
1110 if self.showAll:
1110 if self.showAll:
1111 self.stream.writeln('ignored %s' % reason)
1111 self.stream.writeln('ignored %s' % reason)
1112 else:
1112 else:
1113 if reason != 'not retesting':
1113 if reason != 'not retesting':
1114 self.stream.write('i')
1114 self.stream.write('i')
1115 self.stream.flush()
1115 self.stream.flush()
1116
1116
1117 def addWarn(self, test, reason):
1117 def addWarn(self, test, reason):
1118 self.warned.append((test, reason))
1118 self.warned.append((test, reason))
1119
1119
1120 if self._options.first:
1120 if self._options.first:
1121 self.stop()
1121 self.stop()
1122
1122
1123 if self.showAll:
1123 if self.showAll:
1124 self.stream.writeln('warned %s' % reason)
1124 self.stream.writeln('warned %s' % reason)
1125 else:
1125 else:
1126 self.stream.write('~')
1126 self.stream.write('~')
1127 self.stream.flush()
1127 self.stream.flush()
1128
1128
1129 def addOutputMismatch(self, test, ret, got, expected):
1129 def addOutputMismatch(self, test, ret, got, expected):
1130 """Record a mismatch in test output for a particular test."""
1130 """Record a mismatch in test output for a particular test."""
1131
1131
1132 accepted = False
1132 accepted = False
1133
1133
1134 iolock.acquire()
1134 iolock.acquire()
1135 if self._options.nodiff:
1135 if self._options.nodiff:
1136 pass
1136 pass
1137 elif self._options.view:
1137 elif self._options.view:
1138 os.system("%s %s %s" % (self._view, test.refpath, test.errpath))
1138 os.system("%s %s %s" % (self._view, test.refpath, test.errpath))
1139 else:
1139 else:
1140 failed, lines = getdiff(expected, got,
1140 failed, lines = getdiff(expected, got,
1141 test.refpath, test.errpath)
1141 test.refpath, test.errpath)
1142 if failed:
1142 if failed:
1143 self.addFailure(test, 'diff generation failed')
1143 self.addFailure(test, 'diff generation failed')
1144 else:
1144 else:
1145 self.stream.write('\n')
1145 self.stream.write('\n')
1146 for line in lines:
1146 for line in lines:
1147 self.stream.write(line)
1147 self.stream.write(line)
1148 self.stream.flush()
1148 self.stream.flush()
1149
1149
1150 # handle interactive prompt without releasing iolock
1150 # handle interactive prompt without releasing iolock
1151 if self._options.interactive:
1151 if self._options.interactive:
1152 self.stream.write('Accept this change? [n] ')
1152 self.stream.write('Accept this change? [n] ')
1153 answer = sys.stdin.readline().strip()
1153 answer = sys.stdin.readline().strip()
1154 if answer.lower() in ('y', 'yes'):
1154 if answer.lower() in ('y', 'yes'):
1155 if test.name.endswith('.t'):
1155 if test.name.endswith('.t'):
1156 rename(test.errpath, test.path)
1156 rename(test.errpath, test.path)
1157 else:
1157 else:
1158 rename(test.errpath, '%s.out' % test.path)
1158 rename(test.errpath, '%s.out' % test.path)
1159 accepted = True
1159 accepted = True
1160
1160
1161 iolock.release()
1161 iolock.release()
1162
1162
1163 return accepted
1163 return accepted
1164
1164
1165 def startTest(self, test):
1165 def startTest(self, test):
1166 super(TestResult, self).startTest(test)
1166 super(TestResult, self).startTest(test)
1167
1167
1168 self._started[test.name] = time.time()
1168 self._started[test.name] = time.time()
1169
1169
1170 def stopTest(self, test, interrupted=False):
1170 def stopTest(self, test, interrupted=False):
1171 super(TestResult, self).stopTest(test)
1171 super(TestResult, self).stopTest(test)
1172
1172
1173 self.times.append((test.name, time.time() - self._started[test.name]))
1173 self.times.append((test.name, time.time() - self._started[test.name]))
1174 del self._started[test.name]
1174 del self._started[test.name]
1175
1175
1176 if interrupted:
1176 if interrupted:
1177 self.stream.writeln('INTERRUPTED: %s (after %d seconds)' % (
1177 self.stream.writeln('INTERRUPTED: %s (after %d seconds)' % (
1178 test.name, self.times[-1][1]))
1178 test.name, self.times[-1][1]))
1179
1179
1180 class TestSuite(unittest.TestSuite):
1180 class TestSuite(unittest.TestSuite):
1181 """Custom unitest TestSuite that knows how to execute Mercurial tests."""
1181 """Custom unitest TestSuite that knows how to execute Mercurial tests."""
1182
1182
1183 def __init__(self, testdir, jobs=1, whitelist=None, blacklist=None,
1183 def __init__(self, testdir, jobs=1, whitelist=None, blacklist=None,
1184 retest=False, keywords=None, loop=False,
1184 retest=False, keywords=None, loop=False,
1185 *args, **kwargs):
1185 *args, **kwargs):
1186 """Create a new instance that can run tests with a configuration.
1186 """Create a new instance that can run tests with a configuration.
1187
1187
1188 testdir specifies the directory where tests are executed from. This
1188 testdir specifies the directory where tests are executed from. This
1189 is typically the ``tests`` directory from Mercurial's source
1189 is typically the ``tests`` directory from Mercurial's source
1190 repository.
1190 repository.
1191
1191
1192 jobs specifies the number of jobs to run concurrently. Each test
1192 jobs specifies the number of jobs to run concurrently. Each test
1193 executes on its own thread. Tests actually spawn new processes, so
1193 executes on its own thread. Tests actually spawn new processes, so
1194 state mutation should not be an issue.
1194 state mutation should not be an issue.
1195
1195
1196 whitelist and blacklist denote tests that have been whitelisted and
1196 whitelist and blacklist denote tests that have been whitelisted and
1197 blacklisted, respectively. These arguments don't belong in TestSuite.
1197 blacklisted, respectively. These arguments don't belong in TestSuite.
1198 Instead, whitelist and blacklist should be handled by the thing that
1198 Instead, whitelist and blacklist should be handled by the thing that
1199 populates the TestSuite with tests. They are present to preserve
1199 populates the TestSuite with tests. They are present to preserve
1200 backwards compatible behavior which reports skipped tests as part
1200 backwards compatible behavior which reports skipped tests as part
1201 of the results.
1201 of the results.
1202
1202
1203 retest denotes whether to retest failed tests. This arguably belongs
1203 retest denotes whether to retest failed tests. This arguably belongs
1204 outside of TestSuite.
1204 outside of TestSuite.
1205
1205
1206 keywords denotes key words that will be used to filter which tests
1206 keywords denotes key words that will be used to filter which tests
1207 to execute. This arguably belongs outside of TestSuite.
1207 to execute. This arguably belongs outside of TestSuite.
1208
1208
1209 loop denotes whether to loop over tests forever.
1209 loop denotes whether to loop over tests forever.
1210 """
1210 """
1211 super(TestSuite, self).__init__(*args, **kwargs)
1211 super(TestSuite, self).__init__(*args, **kwargs)
1212
1212
1213 self._jobs = jobs
1213 self._jobs = jobs
1214 self._whitelist = whitelist
1214 self._whitelist = whitelist
1215 self._blacklist = blacklist
1215 self._blacklist = blacklist
1216 self._retest = retest
1216 self._retest = retest
1217 self._keywords = keywords
1217 self._keywords = keywords
1218 self._loop = loop
1218 self._loop = loop
1219
1219
1220 def run(self, result):
1220 def run(self, result):
1221 # We have a number of filters that need to be applied. We do this
1221 # We have a number of filters that need to be applied. We do this
1222 # here instead of inside Test because it makes the running logic for
1222 # here instead of inside Test because it makes the running logic for
1223 # Test simpler.
1223 # Test simpler.
1224 tests = []
1224 tests = []
1225 for test in self._tests:
1225 for test in self._tests:
1226 if not os.path.exists(test.path):
1226 if not os.path.exists(test.path):
1227 result.addSkip(test, "Doesn't exist")
1227 result.addSkip(test, "Doesn't exist")
1228 continue
1228 continue
1229
1229
1230 if not (self._whitelist and test.name in self._whitelist):
1230 if not (self._whitelist and test.name in self._whitelist):
1231 if self._blacklist and test.name in self._blacklist:
1231 if self._blacklist and test.name in self._blacklist:
1232 result.addSkip(test, 'blacklisted')
1232 result.addSkip(test, 'blacklisted')
1233 continue
1233 continue
1234
1234
1235 if self._retest and not os.path.exists(test.errpath):
1235 if self._retest and not os.path.exists(test.errpath):
1236 result.addIgnore(test, 'not retesting')
1236 result.addIgnore(test, 'not retesting')
1237 continue
1237 continue
1238
1238
1239 if self._keywords:
1239 if self._keywords:
1240 f = open(test.path)
1240 f = open(test.path, 'rb')
1241 t = f.read().lower() + test.name.lower()
1241 t = f.read().lower() + test.name.lower()
1242 f.close()
1242 f.close()
1243 ignored = False
1243 ignored = False
1244 for k in self._keywords.lower().split():
1244 for k in self._keywords.lower().split():
1245 if k not in t:
1245 if k not in t:
1246 result.addIgnore(test, "doesn't match keyword")
1246 result.addIgnore(test, "doesn't match keyword")
1247 ignored = True
1247 ignored = True
1248 break
1248 break
1249
1249
1250 if ignored:
1250 if ignored:
1251 continue
1251 continue
1252
1252
1253 tests.append(test)
1253 tests.append(test)
1254
1254
1255 runtests = list(tests)
1255 runtests = list(tests)
1256 done = queue.Queue()
1256 done = queue.Queue()
1257 running = 0
1257 running = 0
1258
1258
1259 def job(test, result):
1259 def job(test, result):
1260 try:
1260 try:
1261 test(result)
1261 test(result)
1262 done.put(None)
1262 done.put(None)
1263 except KeyboardInterrupt:
1263 except KeyboardInterrupt:
1264 pass
1264 pass
1265 except: # re-raises
1265 except: # re-raises
1266 done.put(('!', test, 'run-test raised an error, see traceback'))
1266 done.put(('!', test, 'run-test raised an error, see traceback'))
1267 raise
1267 raise
1268
1268
1269 try:
1269 try:
1270 while tests or running:
1270 while tests or running:
1271 if not done.empty() or running == self._jobs or not tests:
1271 if not done.empty() or running == self._jobs or not tests:
1272 try:
1272 try:
1273 done.get(True, 1)
1273 done.get(True, 1)
1274 if result and result.shouldStop:
1274 if result and result.shouldStop:
1275 break
1275 break
1276 except queue.Empty:
1276 except queue.Empty:
1277 continue
1277 continue
1278 running -= 1
1278 running -= 1
1279 if tests and not running == self._jobs:
1279 if tests and not running == self._jobs:
1280 test = tests.pop(0)
1280 test = tests.pop(0)
1281 if self._loop:
1281 if self._loop:
1282 tests.append(test)
1282 tests.append(test)
1283 t = threading.Thread(target=job, name=test.name,
1283 t = threading.Thread(target=job, name=test.name,
1284 args=(test, result))
1284 args=(test, result))
1285 t.start()
1285 t.start()
1286 running += 1
1286 running += 1
1287 except KeyboardInterrupt:
1287 except KeyboardInterrupt:
1288 for test in runtests:
1288 for test in runtests:
1289 test.abort()
1289 test.abort()
1290
1290
1291 return result
1291 return result
1292
1292
1293 class TextTestRunner(unittest.TextTestRunner):
1293 class TextTestRunner(unittest.TextTestRunner):
1294 """Custom unittest test runner that uses appropriate settings."""
1294 """Custom unittest test runner that uses appropriate settings."""
1295
1295
1296 def __init__(self, runner, *args, **kwargs):
1296 def __init__(self, runner, *args, **kwargs):
1297 super(TextTestRunner, self).__init__(*args, **kwargs)
1297 super(TextTestRunner, self).__init__(*args, **kwargs)
1298
1298
1299 self._runner = runner
1299 self._runner = runner
1300
1300
1301 def run(self, test):
1301 def run(self, test):
1302 result = TestResult(self._runner.options, self.stream,
1302 result = TestResult(self._runner.options, self.stream,
1303 self.descriptions, self.verbosity)
1303 self.descriptions, self.verbosity)
1304
1304
1305 test(result)
1305 test(result)
1306
1306
1307 failed = len(result.failures)
1307 failed = len(result.failures)
1308 warned = len(result.warned)
1308 warned = len(result.warned)
1309 skipped = len(result.skipped)
1309 skipped = len(result.skipped)
1310 ignored = len(result.ignored)
1310 ignored = len(result.ignored)
1311
1311
1312 self.stream.writeln('')
1312 self.stream.writeln('')
1313
1313
1314 if not self._runner.options.noskips:
1314 if not self._runner.options.noskips:
1315 for test, msg in result.skipped:
1315 for test, msg in result.skipped:
1316 self.stream.writeln('Skipped %s: %s' % (test.name, msg))
1316 self.stream.writeln('Skipped %s: %s' % (test.name, msg))
1317 for test, msg in result.warned:
1317 for test, msg in result.warned:
1318 self.stream.writeln('Warned %s: %s' % (test.name, msg))
1318 self.stream.writeln('Warned %s: %s' % (test.name, msg))
1319 for test, msg in result.failures:
1319 for test, msg in result.failures:
1320 self.stream.writeln('Failed %s: %s' % (test.name, msg))
1320 self.stream.writeln('Failed %s: %s' % (test.name, msg))
1321 for test, msg in result.errors:
1321 for test, msg in result.errors:
1322 self.stream.writeln('Errored %s: %s' % (test.name, msg))
1322 self.stream.writeln('Errored %s: %s' % (test.name, msg))
1323
1323
1324 self._runner._checkhglib('Tested')
1324 self._runner._checkhglib('Tested')
1325
1325
1326 # When '--retest' is enabled, only failure tests run. At this point
1326 # When '--retest' is enabled, only failure tests run. At this point
1327 # "result.testsRun" holds the count of failure test that has run. But
1327 # "result.testsRun" holds the count of failure test that has run. But
1328 # as while printing output, we have subtracted the skipped and ignored
1328 # as while printing output, we have subtracted the skipped and ignored
1329 # count from "result.testsRun". Therefore, to make the count remain
1329 # count from "result.testsRun". Therefore, to make the count remain
1330 # the same, we need to add skipped and ignored count in here.
1330 # the same, we need to add skipped and ignored count in here.
1331 if self._runner.options.retest:
1331 if self._runner.options.retest:
1332 result.testsRun = result.testsRun + skipped + ignored
1332 result.testsRun = result.testsRun + skipped + ignored
1333
1333
1334 # This differs from unittest's default output in that we don't count
1334 # This differs from unittest's default output in that we don't count
1335 # skipped and ignored tests as part of the total test count.
1335 # skipped and ignored tests as part of the total test count.
1336 self.stream.writeln('# Ran %d tests, %d skipped, %d warned, %d failed.'
1336 self.stream.writeln('# Ran %d tests, %d skipped, %d warned, %d failed.'
1337 % (result.testsRun - skipped - ignored,
1337 % (result.testsRun - skipped - ignored,
1338 skipped + ignored, warned, failed))
1338 skipped + ignored, warned, failed))
1339 if failed:
1339 if failed:
1340 self.stream.writeln('python hash seed: %s' %
1340 self.stream.writeln('python hash seed: %s' %
1341 os.environ['PYTHONHASHSEED'])
1341 os.environ['PYTHONHASHSEED'])
1342 if self._runner.options.time:
1342 if self._runner.options.time:
1343 self.printtimes(result.times)
1343 self.printtimes(result.times)
1344
1344
1345 return result
1345 return result
1346
1346
1347 def printtimes(self, times):
1347 def printtimes(self, times):
1348 self.stream.writeln('# Producing time report')
1348 self.stream.writeln('# Producing time report')
1349 times.sort(key=lambda t: (t[1], t[0]), reverse=True)
1349 times.sort(key=lambda t: (t[1], t[0]), reverse=True)
1350 cols = '%7.3f %s'
1350 cols = '%7.3f %s'
1351 self.stream.writeln('%-7s %s' % ('Time', 'Test'))
1351 self.stream.writeln('%-7s %s' % ('Time', 'Test'))
1352 for test, timetaken in times:
1352 for test, timetaken in times:
1353 self.stream.writeln(cols % (timetaken, test))
1353 self.stream.writeln(cols % (timetaken, test))
1354
1354
1355 class TestRunner(object):
1355 class TestRunner(object):
1356 """Holds context for executing tests.
1356 """Holds context for executing tests.
1357
1357
1358 Tests rely on a lot of state. This object holds it for them.
1358 Tests rely on a lot of state. This object holds it for them.
1359 """
1359 """
1360
1360
1361 # Programs required to run tests.
1361 # Programs required to run tests.
1362 REQUIREDTOOLS = [
1362 REQUIREDTOOLS = [
1363 os.path.basename(sys.executable),
1363 os.path.basename(sys.executable),
1364 'diff',
1364 'diff',
1365 'grep',
1365 'grep',
1366 'unzip',
1366 'unzip',
1367 'gunzip',
1367 'gunzip',
1368 'bunzip2',
1368 'bunzip2',
1369 'sed',
1369 'sed',
1370 ]
1370 ]
1371
1371
1372 # Maps file extensions to test class.
1372 # Maps file extensions to test class.
1373 TESTTYPES = [
1373 TESTTYPES = [
1374 ('.py', PythonTest),
1374 ('.py', PythonTest),
1375 ('.t', TTest),
1375 ('.t', TTest),
1376 ]
1376 ]
1377
1377
1378 def __init__(self):
1378 def __init__(self):
1379 self.options = None
1379 self.options = None
1380 self._testdir = None
1380 self._testdir = None
1381 self._hgtmp = None
1381 self._hgtmp = None
1382 self._installdir = None
1382 self._installdir = None
1383 self._bindir = None
1383 self._bindir = None
1384 self._tmpbinddir = None
1384 self._tmpbinddir = None
1385 self._pythondir = None
1385 self._pythondir = None
1386 self._coveragefile = None
1386 self._coveragefile = None
1387 self._createdfiles = []
1387 self._createdfiles = []
1388 self._hgpath = None
1388 self._hgpath = None
1389
1389
1390 def run(self, args, parser=None):
1390 def run(self, args, parser=None):
1391 """Run the test suite."""
1391 """Run the test suite."""
1392 oldmask = os.umask(022)
1392 oldmask = os.umask(022)
1393 try:
1393 try:
1394 parser = parser or getparser()
1394 parser = parser or getparser()
1395 options, args = parseargs(args, parser)
1395 options, args = parseargs(args, parser)
1396 self.options = options
1396 self.options = options
1397
1397
1398 self._checktools()
1398 self._checktools()
1399 tests = self.findtests(args)
1399 tests = self.findtests(args)
1400 return self._run(tests)
1400 return self._run(tests)
1401 finally:
1401 finally:
1402 os.umask(oldmask)
1402 os.umask(oldmask)
1403
1403
1404 def _run(self, tests):
1404 def _run(self, tests):
1405 if self.options.random:
1405 if self.options.random:
1406 random.shuffle(tests)
1406 random.shuffle(tests)
1407 else:
1407 else:
1408 # keywords for slow tests
1408 # keywords for slow tests
1409 slow = 'svn gendoc check-code-hg'.split()
1409 slow = 'svn gendoc check-code-hg'.split()
1410 def sortkey(f):
1410 def sortkey(f):
1411 # run largest tests first, as they tend to take the longest
1411 # run largest tests first, as they tend to take the longest
1412 try:
1412 try:
1413 val = -os.stat(f).st_size
1413 val = -os.stat(f).st_size
1414 except OSError, e:
1414 except OSError, e:
1415 if e.errno != errno.ENOENT:
1415 if e.errno != errno.ENOENT:
1416 raise
1416 raise
1417 return -1e9 # file does not exist, tell early
1417 return -1e9 # file does not exist, tell early
1418 for kw in slow:
1418 for kw in slow:
1419 if kw in f:
1419 if kw in f:
1420 val *= 10
1420 val *= 10
1421 return val
1421 return val
1422 tests.sort(key=sortkey)
1422 tests.sort(key=sortkey)
1423
1423
1424 self._testdir = os.environ['TESTDIR'] = os.getcwd()
1424 self._testdir = os.environ['TESTDIR'] = os.getcwd()
1425
1425
1426 if 'PYTHONHASHSEED' not in os.environ:
1426 if 'PYTHONHASHSEED' not in os.environ:
1427 # use a random python hash seed all the time
1427 # use a random python hash seed all the time
1428 # we do the randomness ourself to know what seed is used
1428 # we do the randomness ourself to know what seed is used
1429 os.environ['PYTHONHASHSEED'] = str(random.getrandbits(32))
1429 os.environ['PYTHONHASHSEED'] = str(random.getrandbits(32))
1430
1430
1431 if self.options.tmpdir:
1431 if self.options.tmpdir:
1432 self.options.keep_tmpdir = True
1432 self.options.keep_tmpdir = True
1433 tmpdir = self.options.tmpdir
1433 tmpdir = self.options.tmpdir
1434 if os.path.exists(tmpdir):
1434 if os.path.exists(tmpdir):
1435 # Meaning of tmpdir has changed since 1.3: we used to create
1435 # Meaning of tmpdir has changed since 1.3: we used to create
1436 # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if
1436 # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if
1437 # tmpdir already exists.
1437 # tmpdir already exists.
1438 print "error: temp dir %r already exists" % tmpdir
1438 print "error: temp dir %r already exists" % tmpdir
1439 return 1
1439 return 1
1440
1440
1441 # Automatically removing tmpdir sounds convenient, but could
1441 # Automatically removing tmpdir sounds convenient, but could
1442 # really annoy anyone in the habit of using "--tmpdir=/tmp"
1442 # really annoy anyone in the habit of using "--tmpdir=/tmp"
1443 # or "--tmpdir=$HOME".
1443 # or "--tmpdir=$HOME".
1444 #vlog("# Removing temp dir", tmpdir)
1444 #vlog("# Removing temp dir", tmpdir)
1445 #shutil.rmtree(tmpdir)
1445 #shutil.rmtree(tmpdir)
1446 os.makedirs(tmpdir)
1446 os.makedirs(tmpdir)
1447 else:
1447 else:
1448 d = None
1448 d = None
1449 if os.name == 'nt':
1449 if os.name == 'nt':
1450 # without this, we get the default temp dir location, but
1450 # without this, we get the default temp dir location, but
1451 # in all lowercase, which causes troubles with paths (issue3490)
1451 # in all lowercase, which causes troubles with paths (issue3490)
1452 d = os.getenv('TMP')
1452 d = os.getenv('TMP')
1453 tmpdir = tempfile.mkdtemp('', 'hgtests.', d)
1453 tmpdir = tempfile.mkdtemp('', 'hgtests.', d)
1454 self._hgtmp = os.environ['HGTMP'] = os.path.realpath(tmpdir)
1454 self._hgtmp = os.environ['HGTMP'] = os.path.realpath(tmpdir)
1455
1455
1456 if self.options.with_hg:
1456 if self.options.with_hg:
1457 self._installdir = None
1457 self._installdir = None
1458 self._bindir = os.path.dirname(os.path.realpath(
1458 self._bindir = os.path.dirname(os.path.realpath(
1459 self.options.with_hg))
1459 self.options.with_hg))
1460 self._tmpbindir = os.path.join(self._hgtmp, 'install', 'bin')
1460 self._tmpbindir = os.path.join(self._hgtmp, 'install', 'bin')
1461 os.makedirs(self._tmpbindir)
1461 os.makedirs(self._tmpbindir)
1462
1462
1463 # This looks redundant with how Python initializes sys.path from
1463 # This looks redundant with how Python initializes sys.path from
1464 # the location of the script being executed. Needed because the
1464 # the location of the script being executed. Needed because the
1465 # "hg" specified by --with-hg is not the only Python script
1465 # "hg" specified by --with-hg is not the only Python script
1466 # executed in the test suite that needs to import 'mercurial'
1466 # executed in the test suite that needs to import 'mercurial'
1467 # ... which means it's not really redundant at all.
1467 # ... which means it's not really redundant at all.
1468 self._pythondir = self._bindir
1468 self._pythondir = self._bindir
1469 else:
1469 else:
1470 self._installdir = os.path.join(self._hgtmp, "install")
1470 self._installdir = os.path.join(self._hgtmp, "install")
1471 self._bindir = os.environ["BINDIR"] = \
1471 self._bindir = os.environ["BINDIR"] = \
1472 os.path.join(self._installdir, "bin")
1472 os.path.join(self._installdir, "bin")
1473 self._tmpbindir = self._bindir
1473 self._tmpbindir = self._bindir
1474 self._pythondir = os.path.join(self._installdir, "lib", "python")
1474 self._pythondir = os.path.join(self._installdir, "lib", "python")
1475
1475
1476 os.environ["BINDIR"] = self._bindir
1476 os.environ["BINDIR"] = self._bindir
1477 os.environ["PYTHON"] = PYTHON
1477 os.environ["PYTHON"] = PYTHON
1478
1478
1479 path = [self._bindir] + os.environ["PATH"].split(os.pathsep)
1479 path = [self._bindir] + os.environ["PATH"].split(os.pathsep)
1480 if self._tmpbindir != self._bindir:
1480 if self._tmpbindir != self._bindir:
1481 path = [self._tmpbindir] + path
1481 path = [self._tmpbindir] + path
1482 os.environ["PATH"] = os.pathsep.join(path)
1482 os.environ["PATH"] = os.pathsep.join(path)
1483
1483
1484 # Include TESTDIR in PYTHONPATH so that out-of-tree extensions
1484 # Include TESTDIR in PYTHONPATH so that out-of-tree extensions
1485 # can run .../tests/run-tests.py test-foo where test-foo
1485 # can run .../tests/run-tests.py test-foo where test-foo
1486 # adds an extension to HGRC. Also include run-test.py directory to
1486 # adds an extension to HGRC. Also include run-test.py directory to
1487 # import modules like heredoctest.
1487 # import modules like heredoctest.
1488 pypath = [self._pythondir, self._testdir,
1488 pypath = [self._pythondir, self._testdir,
1489 os.path.abspath(os.path.dirname(__file__))]
1489 os.path.abspath(os.path.dirname(__file__))]
1490 # We have to augment PYTHONPATH, rather than simply replacing
1490 # We have to augment PYTHONPATH, rather than simply replacing
1491 # it, in case external libraries are only available via current
1491 # it, in case external libraries are only available via current
1492 # PYTHONPATH. (In particular, the Subversion bindings on OS X
1492 # PYTHONPATH. (In particular, the Subversion bindings on OS X
1493 # are in /opt/subversion.)
1493 # are in /opt/subversion.)
1494 oldpypath = os.environ.get(IMPL_PATH)
1494 oldpypath = os.environ.get(IMPL_PATH)
1495 if oldpypath:
1495 if oldpypath:
1496 pypath.append(oldpypath)
1496 pypath.append(oldpypath)
1497 os.environ[IMPL_PATH] = os.pathsep.join(pypath)
1497 os.environ[IMPL_PATH] = os.pathsep.join(pypath)
1498
1498
1499 self._coveragefile = os.path.join(self._testdir, '.coverage')
1499 self._coveragefile = os.path.join(self._testdir, '.coverage')
1500
1500
1501 vlog("# Using TESTDIR", self._testdir)
1501 vlog("# Using TESTDIR", self._testdir)
1502 vlog("# Using HGTMP", self._hgtmp)
1502 vlog("# Using HGTMP", self._hgtmp)
1503 vlog("# Using PATH", os.environ["PATH"])
1503 vlog("# Using PATH", os.environ["PATH"])
1504 vlog("# Using", IMPL_PATH, os.environ[IMPL_PATH])
1504 vlog("# Using", IMPL_PATH, os.environ[IMPL_PATH])
1505
1505
1506 try:
1506 try:
1507 return self._runtests(tests) or 0
1507 return self._runtests(tests) or 0
1508 finally:
1508 finally:
1509 time.sleep(.1)
1509 time.sleep(.1)
1510 self._cleanup()
1510 self._cleanup()
1511
1511
1512 def findtests(self, args):
1512 def findtests(self, args):
1513 """Finds possible test files from arguments.
1513 """Finds possible test files from arguments.
1514
1514
1515 If you wish to inject custom tests into the test harness, this would
1515 If you wish to inject custom tests into the test harness, this would
1516 be a good function to monkeypatch or override in a derived class.
1516 be a good function to monkeypatch or override in a derived class.
1517 """
1517 """
1518 if not args:
1518 if not args:
1519 if self.options.changed:
1519 if self.options.changed:
1520 proc = Popen4('hg st --rev "%s" -man0 .' %
1520 proc = Popen4('hg st --rev "%s" -man0 .' %
1521 self.options.changed, None, 0)
1521 self.options.changed, None, 0)
1522 stdout, stderr = proc.communicate()
1522 stdout, stderr = proc.communicate()
1523 args = stdout.strip('\0').split('\0')
1523 args = stdout.strip('\0').split('\0')
1524 else:
1524 else:
1525 args = os.listdir('.')
1525 args = os.listdir('.')
1526
1526
1527 return [t for t in args
1527 return [t for t in args
1528 if os.path.basename(t).startswith('test-')
1528 if os.path.basename(t).startswith('test-')
1529 and (t.endswith('.py') or t.endswith('.t'))]
1529 and (t.endswith('.py') or t.endswith('.t'))]
1530
1530
1531 def _runtests(self, tests):
1531 def _runtests(self, tests):
1532 try:
1532 try:
1533 if self._installdir:
1533 if self._installdir:
1534 self._installhg()
1534 self._installhg()
1535 self._checkhglib("Testing")
1535 self._checkhglib("Testing")
1536 else:
1536 else:
1537 self._usecorrectpython()
1537 self._usecorrectpython()
1538
1538
1539 if self.options.restart:
1539 if self.options.restart:
1540 orig = list(tests)
1540 orig = list(tests)
1541 while tests:
1541 while tests:
1542 if os.path.exists(tests[0] + ".err"):
1542 if os.path.exists(tests[0] + ".err"):
1543 break
1543 break
1544 tests.pop(0)
1544 tests.pop(0)
1545 if not tests:
1545 if not tests:
1546 print "running all tests"
1546 print "running all tests"
1547 tests = orig
1547 tests = orig
1548
1548
1549 tests = [self._gettest(t, i) for i, t in enumerate(tests)]
1549 tests = [self._gettest(t, i) for i, t in enumerate(tests)]
1550
1550
1551 failed = False
1551 failed = False
1552 warned = False
1552 warned = False
1553
1553
1554 suite = TestSuite(self._testdir,
1554 suite = TestSuite(self._testdir,
1555 jobs=self.options.jobs,
1555 jobs=self.options.jobs,
1556 whitelist=self.options.whitelisted,
1556 whitelist=self.options.whitelisted,
1557 blacklist=self.options.blacklist,
1557 blacklist=self.options.blacklist,
1558 retest=self.options.retest,
1558 retest=self.options.retest,
1559 keywords=self.options.keywords,
1559 keywords=self.options.keywords,
1560 loop=self.options.loop,
1560 loop=self.options.loop,
1561 tests=tests)
1561 tests=tests)
1562 verbosity = 1
1562 verbosity = 1
1563 if self.options.verbose:
1563 if self.options.verbose:
1564 verbosity = 2
1564 verbosity = 2
1565 runner = TextTestRunner(self, verbosity=verbosity)
1565 runner = TextTestRunner(self, verbosity=verbosity)
1566 result = runner.run(suite)
1566 result = runner.run(suite)
1567
1567
1568 if result.failures:
1568 if result.failures:
1569 failed = True
1569 failed = True
1570 if result.warned:
1570 if result.warned:
1571 warned = True
1571 warned = True
1572
1572
1573 if self.options.anycoverage:
1573 if self.options.anycoverage:
1574 self._outputcoverage()
1574 self._outputcoverage()
1575 except KeyboardInterrupt:
1575 except KeyboardInterrupt:
1576 failed = True
1576 failed = True
1577 print "\ninterrupted!"
1577 print "\ninterrupted!"
1578
1578
1579 if failed:
1579 if failed:
1580 return 1
1580 return 1
1581 if warned:
1581 if warned:
1582 return 80
1582 return 80
1583
1583
1584 def _gettest(self, test, count):
1584 def _gettest(self, test, count):
1585 """Obtain a Test by looking at its filename.
1585 """Obtain a Test by looking at its filename.
1586
1586
1587 Returns a Test instance. The Test may not be runnable if it doesn't
1587 Returns a Test instance. The Test may not be runnable if it doesn't
1588 map to a known type.
1588 map to a known type.
1589 """
1589 """
1590 lctest = test.lower()
1590 lctest = test.lower()
1591 testcls = Test
1591 testcls = Test
1592
1592
1593 for ext, cls in self.TESTTYPES:
1593 for ext, cls in self.TESTTYPES:
1594 if lctest.endswith(ext):
1594 if lctest.endswith(ext):
1595 testcls = cls
1595 testcls = cls
1596 break
1596 break
1597
1597
1598 refpath = os.path.join(self._testdir, test)
1598 refpath = os.path.join(self._testdir, test)
1599 tmpdir = os.path.join(self._hgtmp, 'child%d' % count)
1599 tmpdir = os.path.join(self._hgtmp, 'child%d' % count)
1600
1600
1601 return testcls(refpath, tmpdir,
1601 return testcls(refpath, tmpdir,
1602 keeptmpdir=self.options.keep_tmpdir,
1602 keeptmpdir=self.options.keep_tmpdir,
1603 debug=self.options.debug,
1603 debug=self.options.debug,
1604 timeout=self.options.timeout,
1604 timeout=self.options.timeout,
1605 startport=self.options.port + count * 3,
1605 startport=self.options.port + count * 3,
1606 extraconfigopts=self.options.extra_config_opt,
1606 extraconfigopts=self.options.extra_config_opt,
1607 py3kwarnings=self.options.py3k_warnings,
1607 py3kwarnings=self.options.py3k_warnings,
1608 shell=self.options.shell)
1608 shell=self.options.shell)
1609
1609
1610 def _cleanup(self):
1610 def _cleanup(self):
1611 """Clean up state from this test invocation."""
1611 """Clean up state from this test invocation."""
1612
1612
1613 if self.options.keep_tmpdir:
1613 if self.options.keep_tmpdir:
1614 return
1614 return
1615
1615
1616 vlog("# Cleaning up HGTMP", self._hgtmp)
1616 vlog("# Cleaning up HGTMP", self._hgtmp)
1617 shutil.rmtree(self._hgtmp, True)
1617 shutil.rmtree(self._hgtmp, True)
1618 for f in self._createdfiles:
1618 for f in self._createdfiles:
1619 try:
1619 try:
1620 os.remove(f)
1620 os.remove(f)
1621 except OSError:
1621 except OSError:
1622 pass
1622 pass
1623
1623
1624 def _usecorrectpython(self):
1624 def _usecorrectpython(self):
1625 """Configure the environment to use the appropriate Python in tests."""
1625 """Configure the environment to use the appropriate Python in tests."""
1626 # Tests must use the same interpreter as us or bad things will happen.
1626 # Tests must use the same interpreter as us or bad things will happen.
1627 pyexename = sys.platform == 'win32' and 'python.exe' or 'python'
1627 pyexename = sys.platform == 'win32' and 'python.exe' or 'python'
1628 if getattr(os, 'symlink', None):
1628 if getattr(os, 'symlink', None):
1629 vlog("# Making python executable in test path a symlink to '%s'" %
1629 vlog("# Making python executable in test path a symlink to '%s'" %
1630 sys.executable)
1630 sys.executable)
1631 mypython = os.path.join(self._tmpbindir, pyexename)
1631 mypython = os.path.join(self._tmpbindir, pyexename)
1632 try:
1632 try:
1633 if os.readlink(mypython) == sys.executable:
1633 if os.readlink(mypython) == sys.executable:
1634 return
1634 return
1635 os.unlink(mypython)
1635 os.unlink(mypython)
1636 except OSError, err:
1636 except OSError, err:
1637 if err.errno != errno.ENOENT:
1637 if err.errno != errno.ENOENT:
1638 raise
1638 raise
1639 if self._findprogram(pyexename) != sys.executable:
1639 if self._findprogram(pyexename) != sys.executable:
1640 try:
1640 try:
1641 os.symlink(sys.executable, mypython)
1641 os.symlink(sys.executable, mypython)
1642 self._createdfiles.append(mypython)
1642 self._createdfiles.append(mypython)
1643 except OSError, err:
1643 except OSError, err:
1644 # child processes may race, which is harmless
1644 # child processes may race, which is harmless
1645 if err.errno != errno.EEXIST:
1645 if err.errno != errno.EEXIST:
1646 raise
1646 raise
1647 else:
1647 else:
1648 exedir, exename = os.path.split(sys.executable)
1648 exedir, exename = os.path.split(sys.executable)
1649 vlog("# Modifying search path to find %s as %s in '%s'" %
1649 vlog("# Modifying search path to find %s as %s in '%s'" %
1650 (exename, pyexename, exedir))
1650 (exename, pyexename, exedir))
1651 path = os.environ['PATH'].split(os.pathsep)
1651 path = os.environ['PATH'].split(os.pathsep)
1652 while exedir in path:
1652 while exedir in path:
1653 path.remove(exedir)
1653 path.remove(exedir)
1654 os.environ['PATH'] = os.pathsep.join([exedir] + path)
1654 os.environ['PATH'] = os.pathsep.join([exedir] + path)
1655 if not self._findprogram(pyexename):
1655 if not self._findprogram(pyexename):
1656 print "WARNING: Cannot find %s in search path" % pyexename
1656 print "WARNING: Cannot find %s in search path" % pyexename
1657
1657
1658 def _installhg(self):
1658 def _installhg(self):
1659 """Install hg into the test environment.
1659 """Install hg into the test environment.
1660
1660
1661 This will also configure hg with the appropriate testing settings.
1661 This will also configure hg with the appropriate testing settings.
1662 """
1662 """
1663 vlog("# Performing temporary installation of HG")
1663 vlog("# Performing temporary installation of HG")
1664 installerrs = os.path.join("tests", "install.err")
1664 installerrs = os.path.join("tests", "install.err")
1665 compiler = ''
1665 compiler = ''
1666 if self.options.compiler:
1666 if self.options.compiler:
1667 compiler = '--compiler ' + self.options.compiler
1667 compiler = '--compiler ' + self.options.compiler
1668 pure = self.options.pure and "--pure" or ""
1668 pure = self.options.pure and "--pure" or ""
1669 py3 = ''
1669 py3 = ''
1670 if sys.version_info[0] == 3:
1670 if sys.version_info[0] == 3:
1671 py3 = '--c2to3'
1671 py3 = '--c2to3'
1672
1672
1673 # Run installer in hg root
1673 # Run installer in hg root
1674 script = os.path.realpath(sys.argv[0])
1674 script = os.path.realpath(sys.argv[0])
1675 hgroot = os.path.dirname(os.path.dirname(script))
1675 hgroot = os.path.dirname(os.path.dirname(script))
1676 os.chdir(hgroot)
1676 os.chdir(hgroot)
1677 nohome = '--home=""'
1677 nohome = '--home=""'
1678 if os.name == 'nt':
1678 if os.name == 'nt':
1679 # The --home="" trick works only on OS where os.sep == '/'
1679 # The --home="" trick works only on OS where os.sep == '/'
1680 # because of a distutils convert_path() fast-path. Avoid it at
1680 # because of a distutils convert_path() fast-path. Avoid it at
1681 # least on Windows for now, deal with .pydistutils.cfg bugs
1681 # least on Windows for now, deal with .pydistutils.cfg bugs
1682 # when they happen.
1682 # when they happen.
1683 nohome = ''
1683 nohome = ''
1684 cmd = ('%(exe)s setup.py %(py3)s %(pure)s clean --all'
1684 cmd = ('%(exe)s setup.py %(py3)s %(pure)s clean --all'
1685 ' build %(compiler)s --build-base="%(base)s"'
1685 ' build %(compiler)s --build-base="%(base)s"'
1686 ' install --force --prefix="%(prefix)s"'
1686 ' install --force --prefix="%(prefix)s"'
1687 ' --install-lib="%(libdir)s"'
1687 ' --install-lib="%(libdir)s"'
1688 ' --install-scripts="%(bindir)s" %(nohome)s >%(logfile)s 2>&1'
1688 ' --install-scripts="%(bindir)s" %(nohome)s >%(logfile)s 2>&1'
1689 % {'exe': sys.executable, 'py3': py3, 'pure': pure,
1689 % {'exe': sys.executable, 'py3': py3, 'pure': pure,
1690 'compiler': compiler,
1690 'compiler': compiler,
1691 'base': os.path.join(self._hgtmp, "build"),
1691 'base': os.path.join(self._hgtmp, "build"),
1692 'prefix': self._installdir, 'libdir': self._pythondir,
1692 'prefix': self._installdir, 'libdir': self._pythondir,
1693 'bindir': self._bindir,
1693 'bindir': self._bindir,
1694 'nohome': nohome, 'logfile': installerrs})
1694 'nohome': nohome, 'logfile': installerrs})
1695 vlog("# Running", cmd)
1695 vlog("# Running", cmd)
1696 if os.system(cmd) == 0:
1696 if os.system(cmd) == 0:
1697 if not self.options.verbose:
1697 if not self.options.verbose:
1698 os.remove(installerrs)
1698 os.remove(installerrs)
1699 else:
1699 else:
1700 f = open(installerrs)
1700 f = open(installerrs, 'rb')
1701 for line in f:
1701 for line in f:
1702 print line,
1702 print line,
1703 f.close()
1703 f.close()
1704 sys.exit(1)
1704 sys.exit(1)
1705 os.chdir(self._testdir)
1705 os.chdir(self._testdir)
1706
1706
1707 self._usecorrectpython()
1707 self._usecorrectpython()
1708
1708
1709 if self.options.py3k_warnings and not self.options.anycoverage:
1709 if self.options.py3k_warnings and not self.options.anycoverage:
1710 vlog("# Updating hg command to enable Py3k Warnings switch")
1710 vlog("# Updating hg command to enable Py3k Warnings switch")
1711 f = open(os.path.join(self._bindir, 'hg'), 'r')
1711 f = open(os.path.join(self._bindir, 'hg'), 'rb')
1712 lines = [line.rstrip() for line in f]
1712 lines = [line.rstrip() for line in f]
1713 lines[0] += ' -3'
1713 lines[0] += ' -3'
1714 f.close()
1714 f.close()
1715 f = open(os.path.join(self._bindir, 'hg'), 'w')
1715 f = open(os.path.join(self._bindir, 'hg'), 'wb')
1716 for line in lines:
1716 for line in lines:
1717 f.write(line + '\n')
1717 f.write(line + '\n')
1718 f.close()
1718 f.close()
1719
1719
1720 hgbat = os.path.join(self._bindir, 'hg.bat')
1720 hgbat = os.path.join(self._bindir, 'hg.bat')
1721 if os.path.isfile(hgbat):
1721 if os.path.isfile(hgbat):
1722 # hg.bat expects to be put in bin/scripts while run-tests.py
1722 # hg.bat expects to be put in bin/scripts while run-tests.py
1723 # installation layout put it in bin/ directly. Fix it
1723 # installation layout put it in bin/ directly. Fix it
1724 f = open(hgbat, 'rb')
1724 f = open(hgbat, 'rb')
1725 data = f.read()
1725 data = f.read()
1726 f.close()
1726 f.close()
1727 if '"%~dp0..\python" "%~dp0hg" %*' in data:
1727 if '"%~dp0..\python" "%~dp0hg" %*' in data:
1728 data = data.replace('"%~dp0..\python" "%~dp0hg" %*',
1728 data = data.replace('"%~dp0..\python" "%~dp0hg" %*',
1729 '"%~dp0python" "%~dp0hg" %*')
1729 '"%~dp0python" "%~dp0hg" %*')
1730 f = open(hgbat, 'wb')
1730 f = open(hgbat, 'wb')
1731 f.write(data)
1731 f.write(data)
1732 f.close()
1732 f.close()
1733 else:
1733 else:
1734 print 'WARNING: cannot fix hg.bat reference to python.exe'
1734 print 'WARNING: cannot fix hg.bat reference to python.exe'
1735
1735
1736 if self.options.anycoverage:
1736 if self.options.anycoverage:
1737 custom = os.path.join(self._testdir, 'sitecustomize.py')
1737 custom = os.path.join(self._testdir, 'sitecustomize.py')
1738 target = os.path.join(self._pythondir, 'sitecustomize.py')
1738 target = os.path.join(self._pythondir, 'sitecustomize.py')
1739 vlog('# Installing coverage trigger to %s' % target)
1739 vlog('# Installing coverage trigger to %s' % target)
1740 shutil.copyfile(custom, target)
1740 shutil.copyfile(custom, target)
1741 rc = os.path.join(self._testdir, '.coveragerc')
1741 rc = os.path.join(self._testdir, '.coveragerc')
1742 vlog('# Installing coverage rc to %s' % rc)
1742 vlog('# Installing coverage rc to %s' % rc)
1743 os.environ['COVERAGE_PROCESS_START'] = rc
1743 os.environ['COVERAGE_PROCESS_START'] = rc
1744 fn = os.path.join(self._installdir, '..', '.coverage')
1744 fn = os.path.join(self._installdir, '..', '.coverage')
1745 os.environ['COVERAGE_FILE'] = fn
1745 os.environ['COVERAGE_FILE'] = fn
1746
1746
1747 def _checkhglib(self, verb):
1747 def _checkhglib(self, verb):
1748 """Ensure that the 'mercurial' package imported by python is
1748 """Ensure that the 'mercurial' package imported by python is
1749 the one we expect it to be. If not, print a warning to stderr."""
1749 the one we expect it to be. If not, print a warning to stderr."""
1750 if ((self._bindir == self._pythondir) and
1750 if ((self._bindir == self._pythondir) and
1751 (self._bindir != self._tmpbindir)):
1751 (self._bindir != self._tmpbindir)):
1752 # The pythondir has been infered from --with-hg flag.
1752 # The pythondir has been infered from --with-hg flag.
1753 # We cannot expect anything sensible here
1753 # We cannot expect anything sensible here
1754 return
1754 return
1755 expecthg = os.path.join(self._pythondir, 'mercurial')
1755 expecthg = os.path.join(self._pythondir, 'mercurial')
1756 actualhg = self._gethgpath()
1756 actualhg = self._gethgpath()
1757 if os.path.abspath(actualhg) != os.path.abspath(expecthg):
1757 if os.path.abspath(actualhg) != os.path.abspath(expecthg):
1758 sys.stderr.write('warning: %s with unexpected mercurial lib: %s\n'
1758 sys.stderr.write('warning: %s with unexpected mercurial lib: %s\n'
1759 ' (expected %s)\n'
1759 ' (expected %s)\n'
1760 % (verb, actualhg, expecthg))
1760 % (verb, actualhg, expecthg))
1761 def _gethgpath(self):
1761 def _gethgpath(self):
1762 """Return the path to the mercurial package that is actually found by
1762 """Return the path to the mercurial package that is actually found by
1763 the current Python interpreter."""
1763 the current Python interpreter."""
1764 if self._hgpath is not None:
1764 if self._hgpath is not None:
1765 return self._hgpath
1765 return self._hgpath
1766
1766
1767 cmd = '%s -c "import mercurial; print (mercurial.__path__[0])"'
1767 cmd = '%s -c "import mercurial; print (mercurial.__path__[0])"'
1768 pipe = os.popen(cmd % PYTHON)
1768 pipe = os.popen(cmd % PYTHON)
1769 try:
1769 try:
1770 self._hgpath = pipe.read().strip()
1770 self._hgpath = pipe.read().strip()
1771 finally:
1771 finally:
1772 pipe.close()
1772 pipe.close()
1773
1773
1774 return self._hgpath
1774 return self._hgpath
1775
1775
1776 def _outputcoverage(self):
1776 def _outputcoverage(self):
1777 """Produce code coverage output."""
1777 """Produce code coverage output."""
1778 vlog('# Producing coverage report')
1778 vlog('# Producing coverage report')
1779 os.chdir(self._pythondir)
1779 os.chdir(self._pythondir)
1780
1780
1781 def covrun(*args):
1781 def covrun(*args):
1782 cmd = 'coverage %s' % ' '.join(args)
1782 cmd = 'coverage %s' % ' '.join(args)
1783 vlog('# Running: %s' % cmd)
1783 vlog('# Running: %s' % cmd)
1784 os.system(cmd)
1784 os.system(cmd)
1785
1785
1786 covrun('-c')
1786 covrun('-c')
1787 omit = ','.join(os.path.join(x, '*') for x in
1787 omit = ','.join(os.path.join(x, '*') for x in
1788 [self._bindir, self._testdir])
1788 [self._bindir, self._testdir])
1789 covrun('-i', '-r', '"--omit=%s"' % omit) # report
1789 covrun('-i', '-r', '"--omit=%s"' % omit) # report
1790 if self.options.htmlcov:
1790 if self.options.htmlcov:
1791 htmldir = os.path.join(self._testdir, 'htmlcov')
1791 htmldir = os.path.join(self._testdir, 'htmlcov')
1792 covrun('-i', '-b', '"--directory=%s"' % htmldir,
1792 covrun('-i', '-b', '"--directory=%s"' % htmldir,
1793 '"--omit=%s"' % omit)
1793 '"--omit=%s"' % omit)
1794 if self.options.annotate:
1794 if self.options.annotate:
1795 adir = os.path.join(self._testdir, 'annotated')
1795 adir = os.path.join(self._testdir, 'annotated')
1796 if not os.path.isdir(adir):
1796 if not os.path.isdir(adir):
1797 os.mkdir(adir)
1797 os.mkdir(adir)
1798 covrun('-i', '-a', '"--directory=%s"' % adir, '"--omit=%s"' % omit)
1798 covrun('-i', '-a', '"--directory=%s"' % adir, '"--omit=%s"' % omit)
1799
1799
1800 def _findprogram(self, program):
1800 def _findprogram(self, program):
1801 """Search PATH for a executable program"""
1801 """Search PATH for a executable program"""
1802 for p in os.environ.get('PATH', os.defpath).split(os.pathsep):
1802 for p in os.environ.get('PATH', os.defpath).split(os.pathsep):
1803 name = os.path.join(p, program)
1803 name = os.path.join(p, program)
1804 if os.name == 'nt' or os.access(name, os.X_OK):
1804 if os.name == 'nt' or os.access(name, os.X_OK):
1805 return name
1805 return name
1806 return None
1806 return None
1807
1807
1808 def _checktools(self):
1808 def _checktools(self):
1809 """Ensure tools required to run tests are present."""
1809 """Ensure tools required to run tests are present."""
1810 for p in self.REQUIREDTOOLS:
1810 for p in self.REQUIREDTOOLS:
1811 if os.name == 'nt' and not p.endswith('.exe'):
1811 if os.name == 'nt' and not p.endswith('.exe'):
1812 p += '.exe'
1812 p += '.exe'
1813 found = self._findprogram(p)
1813 found = self._findprogram(p)
1814 if found:
1814 if found:
1815 vlog("# Found prerequisite", p, "at", found)
1815 vlog("# Found prerequisite", p, "at", found)
1816 else:
1816 else:
1817 print "WARNING: Did not find prerequisite tool: %s " % p
1817 print "WARNING: Did not find prerequisite tool: %s " % p
1818
1818
1819 if __name__ == '__main__':
1819 if __name__ == '__main__':
1820 runner = TestRunner()
1820 runner = TestRunner()
1821 sys.exit(runner.run(sys.argv[1:]))
1821 sys.exit(runner.run(sys.argv[1:]))
General Comments 0
You need to be logged in to leave comments. Login now