blob: e734bf9cb321919df0b554ed8bbcfc6c48db0db9 [file] [log] [blame]
Guido van Rossum152494a1996-12-20 03:12:20 +00001#! /usr/bin/env python
2
3"""Regression test.
4
5This will find all modules whose name is "test_*" in the test
6directory, and run them. Various command line options provide
7additional facilities.
8
9Command line options:
10
Barry Warsawa873b032000-08-03 15:50:37 +000011-v: verbose -- run tests in verbose mode with output to stdout
12-q: quiet -- don't print anything except if a test fails
13-g: generate -- write the output file for a test instead of comparing it
14-x: exclude -- arguments are tests to *exclude*
15-s: single -- run only a single test (see below)
16-r: random -- randomize test execution order
Tim Petersc5000df2002-06-02 21:42:01 +000017-f: fromfile -- read names of tests to run from a file (see below)
Neil Schemenauer8a00abc2000-10-13 01:32:42 +000018-l: findleaks -- if GC is available detect tests that leak memory
Barry Warsaw08fca522001-08-20 22:33:46 +000019-u: use -- specify which special resource intensive tests to run
20-h: help -- print this text and exit
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000021-t: threshold -- call gc.set_threshold(N)
Guido van Rossum152494a1996-12-20 03:12:20 +000022
23If non-option arguments are present, they are names for tests to run,
24unless -x is given, in which case they are names for tests not to run.
25If no test names are given, all tests are run.
Guido van Rossumf58ed251997-03-07 21:04:33 +000026
Guido van Rossuma4122201997-08-18 20:08:24 +000027-v is incompatible with -g and does not compare test output files.
Barry Warsawe11e3de1999-01-28 19:51:51 +000028
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000029-s means to run only a single test and exit. This is useful when
30doing memory analysis on the Python interpreter (which tend to consume
31too many resources to run the full regression test non-stop). The
32file /tmp/pynexttest is read to find the next test to run. If this
33file is missing, the first test_*.py file in testdir or on the command
34line is used. (actually tempfile.gettempdir() is used instead of
35/tmp).
Barry Warsawe11e3de1999-01-28 19:51:51 +000036
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000037-f reads the names of tests from the file given as f's argument, one
38or more test names per line. Whitespace is ignored. Blank lines and
39lines beginning with '#' are ignored. This is especially useful for
40whittling down failures involving interactions among tests.
Tim Petersc5000df2002-06-02 21:42:01 +000041
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000042-u is used to specify which special resource intensive tests to run,
43such as those requiring large file support or network connectivity.
44The argument is a comma-separated list of words indicating the
45resources to test. Currently only the following are defined:
Barry Warsaw08fca522001-08-20 22:33:46 +000046
Fred Drake3a15dac2002-04-11 16:39:16 +000047 all - Enable all special resources.
48
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000049 curses - Tests that use curses and will modify the terminal's
50 state and output modes.
Tim Peters1633a2e2001-10-30 05:56:40 +000051
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000052 largefile - It is okay to run some test that may create huge
53 files. These tests can take a long time and may
54 consume >2GB of disk space temporarily.
Barry Warsaw08fca522001-08-20 22:33:46 +000055
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000056 network - It is okay to run tests that use external network
57 resource, e.g. testing SSL support for sockets.
Martin v. Löwis1c6b1a22002-11-19 17:47:07 +000058
59 bsddb - It is okay to run the bsddb testsuite, which takes
60 a long time to complete.
Fred Drake4dd0f7e2002-11-26 21:44:56 +000061
62To enable all resources except one, use '-uall,-<resource>'. For
63example, to run all the tests except for the bsddb tests, give the
64option '-uall,-bsddb'.
Guido van Rossum152494a1996-12-20 03:12:20 +000065"""
66
67import sys
Guido van Rossum152494a1996-12-20 03:12:20 +000068import os
69import getopt
Guido van Rossum9e48b271997-07-16 01:56:13 +000070import traceback
Skip Montanaroab1c7912000-06-30 16:39:27 +000071import random
Fred Drakeae1bb172001-05-21 21:08:12 +000072import StringIO
Guido van Rossumdc15c272002-08-12 21:55:51 +000073import warnings
Tim Peters7c7efe92002-08-23 17:55:54 +000074from sets import Set
Guido van Rossumdc15c272002-08-12 21:55:51 +000075
76# I see no other way to suppress these warnings;
77# putting them in test_grammar.py has no effect:
Guido van Rossum88b1def2002-08-14 17:54:48 +000078warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
Guido van Rossumdc15c272002-08-12 21:55:51 +000079 ".*test.test_grammar$")
Guido van Rossumc34c4fc2002-09-19 00:42:16 +000080if sys.maxint > 0x7fffffff:
81 # Also suppress them in <string>, because for 64-bit platforms,
82 # that's where test_grammar.py hides them.
83 warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
84 "<string>")
Guido van Rossum152494a1996-12-20 03:12:20 +000085
Guido van Rossumbb484652002-12-02 09:56:21 +000086# MacOSX (a.k.a. Darwin) has a default stack size that is too small
87# for deeply recursive regular expressions. We see this as crashes in
88# the Python test suite when running test_re.py and test_sre.py. The
89# fix is to set the stack limit to 2048.
90# This approach may also be useful for other Unixy platforms that
91# suffer from small default stack limits.
92if sys.platform == 'darwin':
93 try:
94 import resource
95 except ImportError:
96 pass
97 else:
98 soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
99 newsoft = min(hard, max(soft, 1024*2048))
100 resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
101
Barry Warsaw04f357c2002-07-23 19:04:11 +0000102from test import test_support
Fred Drake3a15dac2002-04-11 16:39:16 +0000103
Martin v. Löwis1c6b1a22002-11-19 17:47:07 +0000104RESOURCE_NAMES = ('curses', 'largefile', 'network', 'bsddb')
Fred Drake3a15dac2002-04-11 16:39:16 +0000105
106
Barry Warsaw08fca522001-08-20 22:33:46 +0000107def usage(code, msg=''):
108 print __doc__
109 if msg: print msg
110 sys.exit(code)
111
112
Skip Montanaroab1c7912000-06-30 16:39:27 +0000113def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
Tim Petersc5000df2002-06-02 21:42:01 +0000114 exclude=0, single=0, randomize=0, fromfile=None, findleaks=0,
Barry Warsaw08fca522001-08-20 22:33:46 +0000115 use_resources=None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000116 """Execute a test suite.
117
Thomas Wouters7e474022000-07-16 12:04:32 +0000118 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +0000119 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000120
121 tests -- a list of strings containing test names (optional)
122 testdir -- the directory in which to look for tests (optional)
123
124 Users other than the Python test suite will certainly want to
125 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +0000126 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000127
128 If the tests argument is omitted, the tests listed on the
129 command-line will be used. If that's empty, too, then all *.py
130 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +0000131
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000132 The other default arguments (verbose, quiet, generate, exclude,
133 single, randomize, findleaks, and use_resources) allow programmers
134 calling main() directly to set the values that would normally be
135 set by flags on the command line.
Barry Warsawa873b032000-08-03 15:50:37 +0000136
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000137 """
Fred Drake004d5e62000-10-23 17:22:08 +0000138
Tim Peters8dee8092001-09-25 20:05:11 +0000139 test_support.record_original_stdout(sys.stdout)
Guido van Rossum152494a1996-12-20 03:12:20 +0000140 try:
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000141 opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsrf:lu:t:',
Barry Warsaw08fca522001-08-20 22:33:46 +0000142 ['help', 'verbose', 'quiet', 'generate',
Tim Petersc5000df2002-06-02 21:42:01 +0000143 'exclude', 'single', 'random', 'fromfile',
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000144 'findleaks', 'use=', 'threshold='])
Guido van Rossum152494a1996-12-20 03:12:20 +0000145 except getopt.error, msg:
Barry Warsaw08fca522001-08-20 22:33:46 +0000146 usage(2, msg)
147
148 # Defaults
149 if use_resources is None:
150 use_resources = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000151 for o, a in opts:
Barry Warsaw08fca522001-08-20 22:33:46 +0000152 if o in ('-h', '--help'):
153 usage(0)
154 elif o in ('-v', '--verbose'):
155 verbose += 1
156 elif o in ('-q', '--quiet'):
157 quiet = 1;
158 verbose = 0
159 elif o in ('-g', '--generate'):
160 generate = 1
161 elif o in ('-x', '--exclude'):
162 exclude = 1
163 elif o in ('-s', '--single'):
164 single = 1
165 elif o in ('-r', '--randomize'):
166 randomize = 1
Tim Petersc5000df2002-06-02 21:42:01 +0000167 elif o in ('-f', '--fromfile'):
168 fromfile = a
Barry Warsaw08fca522001-08-20 22:33:46 +0000169 elif o in ('-l', '--findleaks'):
170 findleaks = 1
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000171 elif o in ('-t', '--threshold'):
172 import gc
173 gc.set_threshold(int(a))
Barry Warsaw08fca522001-08-20 22:33:46 +0000174 elif o in ('-u', '--use'):
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000175 u = [x.lower() for x in a.split(',')]
176 for r in u:
Fred Drake3a15dac2002-04-11 16:39:16 +0000177 if r == 'all':
Fred Drake4dd0f7e2002-11-26 21:44:56 +0000178 use_resources[:] = RESOURCE_NAMES
179 continue
180 remove = False
181 if r[0] == '-':
182 remove = True
183 r = r[1:]
Fred Drake3a15dac2002-04-11 16:39:16 +0000184 if r not in RESOURCE_NAMES:
185 usage(1, 'Invalid -u/--use option: ' + a)
Fred Drake4dd0f7e2002-11-26 21:44:56 +0000186 if remove:
187 if r in use_resources:
188 use_resources.remove(r)
189 elif r not in use_resources:
Andrew MacIntyree41abab2002-04-30 12:11:04 +0000190 use_resources.append(r)
Guido van Rossuma4122201997-08-18 20:08:24 +0000191 if generate and verbose:
Barry Warsaw08fca522001-08-20 22:33:46 +0000192 usage(2, "-g and -v don't go together!")
Tim Petersc5000df2002-06-02 21:42:01 +0000193 if single and fromfile:
194 usage(2, "-s and -f don't go together!")
Barry Warsaw08fca522001-08-20 22:33:46 +0000195
Guido van Rossum152494a1996-12-20 03:12:20 +0000196 good = []
197 bad = []
198 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000199
Neil Schemenauerd569f232000-09-22 15:29:28 +0000200 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000201 try:
202 import gc
203 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000204 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +0000205 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +0000206 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000207 # Uncomment the line below to report garbage that is not
208 # freeable by reference counting alone. By default only
209 # garbage that is not collectable by the GC is reported.
210 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000211 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000212
Barry Warsawe11e3de1999-01-28 19:51:51 +0000213 if single:
214 from tempfile import gettempdir
215 filename = os.path.join(gettempdir(), 'pynexttest')
216 try:
217 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000218 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000219 tests = [next]
220 fp.close()
221 except IOError:
222 pass
Tim Petersc5000df2002-06-02 21:42:01 +0000223
224 if fromfile:
225 tests = []
226 fp = open(fromfile)
227 for line in fp:
228 guts = line.split() # assuming no test has whitespace in its name
229 if guts and not guts[0].startswith('#'):
230 tests.extend(guts)
231 fp.close()
232
233 # Strip .py extensions.
234 if args:
235 args = map(removepy, args)
236 if tests:
237 tests = map(removepy, tests)
238
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000239 stdtests = STDTESTS[:]
240 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000241 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000242 for arg in args:
243 if arg in stdtests:
244 stdtests.remove(arg)
245 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000246 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000247 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000248 if single:
249 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000250 if randomize:
251 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000252 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000253 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000254 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000255 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000256 if not quiet:
257 print test
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000258 sys.stdout.flush()
Trent Mickf29f47b2000-08-11 19:02:59 +0000259 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000260 if ok > 0:
261 good.append(test)
262 elif ok == 0:
263 bad.append(test)
264 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000265 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000266 if findleaks:
267 gc.collect()
268 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000269 print "Warning: test created", len(gc.garbage),
270 print "uncollectable object(s)."
271 # move the uncollectable objects somewhere so we don't see
272 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000273 found_garbage.extend(gc.garbage)
274 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000275 # Unload the newly imported modules (best effort finalization)
276 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000277 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000278 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000279
280 # The lists won't be sorted if running with -r
281 good.sort()
282 bad.sort()
283 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000284
Guido van Rossum152494a1996-12-20 03:12:20 +0000285 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000286 if not bad and not skipped and len(good) > 1:
287 print "All",
288 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000289 if verbose:
Barry Warsaw408b6d32002-07-30 23:27:12 +0000290 print "CAUTION: stdout isn't compared in verbose mode:"
291 print "a test that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000292 if bad:
Tim Petersa45da922001-08-12 03:45:50 +0000293 print count(len(bad), "test"), "failed:"
294 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000295 if skipped and not quiet:
Tim Petersa45da922001-08-12 03:45:50 +0000296 print count(len(skipped), "test"), "skipped:"
297 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000298
Tim Petersb5b7b782001-08-12 01:20:39 +0000299 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000300 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000301 if e.isvalid():
Tim Peters7c7efe92002-08-23 17:55:54 +0000302 surprise = Set(skipped) - e.getexpected()
Tim Petersb5b7b782001-08-12 01:20:39 +0000303 if surprise:
304 print count(len(surprise), "skip"), \
Tim Petersa45da922001-08-12 03:45:50 +0000305 "unexpected on", plat + ":"
306 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000307 else:
308 print "Those skips are all expected on", plat + "."
309 else:
310 print "Ask someone to teach regrtest.py about which tests are"
311 print "expected to get skipped on", plat + "."
312
Barry Warsawe11e3de1999-01-28 19:51:51 +0000313 if single:
314 alltests = findtests(testdir, stdtests, nottests)
315 for i in range(len(alltests)):
316 if tests[0] == alltests[i]:
317 if i == len(alltests) - 1:
318 os.unlink(filename)
319 else:
320 fp = open(filename, 'w')
321 fp.write(alltests[i+1] + '\n')
322 fp.close()
323 break
324 else:
325 os.unlink(filename)
326
Barry Warsaw08fca522001-08-20 22:33:46 +0000327 sys.exit(len(bad) > 0)
328
Guido van Rossum152494a1996-12-20 03:12:20 +0000329
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000330STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000331 'test_grammar',
332 'test_opcodes',
333 'test_operations',
334 'test_builtin',
335 'test_exceptions',
336 'test_types',
337 ]
338
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000339NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000340 'test_support',
341 'test_b1',
342 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000343 'test_future1',
344 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000345 'test_future3',
Guido van Rossum152494a1996-12-20 03:12:20 +0000346 ]
347
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000348def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000349 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000350 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000351 names = os.listdir(testdir)
352 tests = []
353 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000354 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000355 modname = name[:-3]
356 if modname not in stdtests and modname not in nottests:
357 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000358 tests.sort()
359 return stdtests + tests
360
Trent Mickf29f47b2000-08-11 19:02:59 +0000361def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000362 """Run a single test.
363 test -- the name of the test
364 generate -- if true, generate output, instead of running the test
365 and comparing it to a previously created output file
366 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000367 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000368 testdir -- test directory
369 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000370 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000371 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000372 outputdir = os.path.join(testdir, "output")
373 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000374 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000375 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000376 else:
Fred Drake88a56852001-09-28 20:16:30 +0000377 cfp = StringIO.StringIO()
Guido van Rossum152494a1996-12-20 03:12:20 +0000378 try:
Tim Peters342ca752001-09-25 19:13:20 +0000379 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000380 try:
381 if cfp:
382 sys.stdout = cfp
383 print test # Output file starts with test name
Barry Warsaw408b6d32002-07-30 23:27:12 +0000384 if test.startswith('test.'):
385 abstest = test
386 else:
387 # Always import it from the test package
388 abstest = 'test.' + test
389 the_package = __import__(abstest, globals(), locals(), [])
390 the_module = getattr(the_package, test)
Tim Petersd9742212001-05-22 18:28:25 +0000391 # Most tests run to completion simply as a side-effect of
392 # being imported. For the benefit of tests that can't run
393 # that way (like test_threaded_import), explicitly invoke
394 # their test_main() function (if it exists).
395 indirect_test = getattr(the_module, "test_main", None)
396 if indirect_test is not None:
397 indirect_test()
Guido van Rossum41360a41998-03-26 19:42:58 +0000398 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000399 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000400 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000401 if not quiet:
Fred Drakede4742b2002-10-17 20:36:08 +0000402 print test, "skipped --", msg
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000403 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000404 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000405 except KeyboardInterrupt:
406 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000407 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000408 print "test", test, "failed --", msg
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000409 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000410 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000411 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000412 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000413 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000414 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000415 if verbose:
416 traceback.print_exc(file=sys.stdout)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000417 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000418 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000419 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000420 if not cfp:
421 return 1
422 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000423 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000424 if output == test + "\n":
425 if os.path.exists(outputfile):
426 # Write it since it already exists (and the contents
427 # may have changed), but let the user know it isn't
428 # needed:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000429 print "output file", outputfile, \
430 "is no longer needed; consider removing it"
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000431 else:
432 # We don't need it, so don't create it.
433 return 1
434 fp = open(outputfile, "w")
435 fp.write(output)
436 fp.close()
437 return 1
438 if os.path.exists(outputfile):
439 fp = open(outputfile, "r")
440 expected = fp.read()
441 fp.close()
442 else:
443 expected = test + "\n"
444 if output == expected:
445 return 1
446 print "test", test, "produced unexpected output:"
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000447 sys.stdout.flush()
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000448 reportdiff(expected, output)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000449 sys.stdout.flush()
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000450 return 0
451
452def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000453 import difflib
Tim Petersc377b162001-09-22 05:31:03 +0000454 print "*" * 70
455 a = expected.splitlines(1)
456 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000457 sm = difflib.SequenceMatcher(a=a, b=b)
458 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000459
Guido van Rossumcf691932001-09-21 21:06:22 +0000460 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000461 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000462 x0 += 1
463 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000464 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000465 else:
Tim Petersc377b162001-09-22 05:31:03 +0000466 return "lines %d-%d" % (x0, x1)
467
Guido van Rossumcf691932001-09-21 21:06:22 +0000468 for op, a0, a1, b0, b1 in tuples:
469 if op == 'equal':
470 pass
Tim Petersc377b162001-09-22 05:31:03 +0000471
Guido van Rossumcf691932001-09-21 21:06:22 +0000472 elif op == 'delete':
Tim Petersc377b162001-09-22 05:31:03 +0000473 print "***", pair(a0, a1), "of expected output missing:"
Guido van Rossumcf691932001-09-21 21:06:22 +0000474 for line in a[a0:a1]:
Tim Petersc377b162001-09-22 05:31:03 +0000475 print "-", line,
476
Guido van Rossumcf691932001-09-21 21:06:22 +0000477 elif op == 'replace':
Tim Petersc377b162001-09-22 05:31:03 +0000478 print "*** mismatch between", pair(a0, a1), "of expected", \
479 "output and", pair(b0, b1), "of actual output:"
480 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
481 print line,
482
Guido van Rossumcf691932001-09-21 21:06:22 +0000483 elif op == 'insert':
Tim Petersc377b162001-09-22 05:31:03 +0000484 print "***", pair(b0, b1), "of actual output doesn't appear", \
485 "in expected output after line", str(a1)+":"
Guido van Rossumcf691932001-09-21 21:06:22 +0000486 for line in b[b0:b1]:
Tim Petersc377b162001-09-22 05:31:03 +0000487 print "+", line,
488
Guido van Rossumcf691932001-09-21 21:06:22 +0000489 else:
490 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
Tim Petersc377b162001-09-22 05:31:03 +0000491
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000492 print "*" * 70
Guido van Rossum152494a1996-12-20 03:12:20 +0000493
494def findtestdir():
495 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000496 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000497 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000498 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000499 testdir = os.path.dirname(file) or os.curdir
500 return testdir
501
Tim Petersc5000df2002-06-02 21:42:01 +0000502def removepy(name):
503 if name.endswith(os.extsep + "py"):
504 name = name[:-3]
505 return name
506
Guido van Rossum152494a1996-12-20 03:12:20 +0000507def count(n, word):
508 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000509 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000510 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000511 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000512
Tim Petersa45da922001-08-12 03:45:50 +0000513def printlist(x, width=70, indent=4):
Tim Peters7c7efe92002-08-23 17:55:54 +0000514 """Print the elements of iterable x to stdout.
Tim Petersa45da922001-08-12 03:45:50 +0000515
516 Optional arg width (default 70) is the maximum line length.
517 Optional arg indent (default 4) is the number of blanks with which to
518 begin each line.
519 """
520
Tim Petersba78bc42002-07-04 19:45:06 +0000521 from textwrap import fill
522 blanks = ' ' * indent
523 print fill(' '.join(map(str, x)), width,
524 initial_indent=blanks, subsequent_indent=blanks)
Tim Petersa45da922001-08-12 03:45:50 +0000525
Tim Petersde14a302002-04-01 05:04:46 +0000526# Map sys.platform to a string containing the basenames of tests
527# expected to be skipped on that platform.
Tim Peters2a182db2002-10-09 01:07:11 +0000528#
529# Special cases:
530# test_pep277
531# The _ExpectedSkips constructor adds this to the set of expected
532# skips if not os.path.supports_unicode_filenames.
Tim Peters1b445d32002-11-24 18:53:11 +0000533# test_normalization
534# Whether a skip is expected here depends on whether a large test
535# input file has been downloaded. test_normalization.skip_expected
Tim Peters1babdfc2002-11-24 19:19:09 +0000536# controls that.
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000537# test_socket_ssl
538# Controlled by test_socket_ssl.skip_expected. Requires the network
539# resource, and a socket module with ssl support.
Tim Petersde14a302002-04-01 05:04:46 +0000540
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000541_expectations = {
542 'win32':
543 """
544 test_al
Tim Peters78e35f92002-11-22 20:00:34 +0000545 test_bsddb3
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000546 test_cd
547 test_cl
548 test_commands
549 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000550 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000551 test_dbm
552 test_dl
Tim Petersdeb121a2002-04-11 19:52:58 +0000553 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000554 test_fcntl
555 test_fork1
556 test_gdbm
557 test_gl
558 test_grp
Tim Peters40e1ce42003-01-27 16:45:03 +0000559 test_iconv_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000560 test_imgfile
561 test_largefile
562 test_linuxaudiodev
563 test_mhlib
Tim Petersde14a302002-04-01 05:04:46 +0000564 test_mpz
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000565 test_nis
566 test_openpty
Tim Petersefc4b122002-12-10 18:47:56 +0000567 test_ossaudiodev
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000568 test_poll
569 test_pty
570 test_pwd
Tim Peters1e33ffa2002-04-23 23:09:02 +0000571 test_resource
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000572 test_signal
Tim Petersa86f0c12001-09-18 02:18:57 +0000573 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000574 test_sunaudiodev
575 test_timing
576 """,
577 'linux2':
578 """
579 test_al
580 test_cd
581 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000582 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000583 test_dl
Guido van Rossum6184c112002-04-16 02:14:04 +0000584 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000585 test_gl
586 test_imgfile
587 test_largefile
588 test_nis
589 test_ntpath
590 test_socketserver
591 test_sunaudiodev
592 test_unicode_file
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000593 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000594 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000595 """
596 test_al
Jack Jansen67975142003-01-08 16:31:11 +0000597 test_atexit
Guido van Rossumaa782362001-09-02 03:58:41 +0000598 test_bsddb
Jack Jansen67975142003-01-08 16:31:11 +0000599 test_bsddb3
600 test_bz2
Guido van Rossumaa782362001-09-02 03:58:41 +0000601 test_cd
602 test_cl
603 test_commands
604 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000605 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000606 test_dbm
607 test_dl
Jack Jansen67975142003-01-08 16:31:11 +0000608 test_email_codecs
Guido van Rossumaa782362001-09-02 03:58:41 +0000609 test_fcntl
610 test_fork1
611 test_gl
612 test_grp
Tim Peters40e1ce42003-01-27 16:45:03 +0000613 test_iconv_codecs
Guido van Rossumaa782362001-09-02 03:58:41 +0000614 test_imgfile
615 test_largefile
616 test_linuxaudiodev
617 test_locale
618 test_mmap
Jack Jansen67975142003-01-08 16:31:11 +0000619 test_mpz
Guido van Rossumaa782362001-09-02 03:58:41 +0000620 test_nis
621 test_ntpath
622 test_openpty
Jack Jansen67975142003-01-08 16:31:11 +0000623 test_ossaudiodev
Guido van Rossumaa782362001-09-02 03:58:41 +0000624 test_poll
Jack Jansen67975142003-01-08 16:31:11 +0000625 test_popen
Guido van Rossumaa782362001-09-02 03:58:41 +0000626 test_popen2
627 test_pty
628 test_pwd
Jack Jansen67975142003-01-08 16:31:11 +0000629 test_resource
Guido van Rossumaa782362001-09-02 03:58:41 +0000630 test_signal
Guido van Rossumaa782362001-09-02 03:58:41 +0000631 test_socketserver
632 test_sunaudiodev
633 test_sundry
634 test_timing
635 test_unicode_file
Guido van Rossumaa782362001-09-02 03:58:41 +0000636 """,
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000637 'unixware7':
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000638 """
639 test_al
640 test_bsddb
641 test_cd
642 test_cl
643 test_dl
644 test_gl
645 test_imgfile
646 test_largefile
647 test_linuxaudiodev
648 test_minidom
649 test_nis
650 test_ntpath
651 test_openpty
652 test_pyexpat
653 test_sax
654 test_socketserver
655 test_sunaudiodev
656 test_sundry
657 test_unicode_file
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000658 """,
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000659 'openunix8':
660 """
661 test_al
662 test_bsddb
663 test_cd
664 test_cl
665 test_dl
666 test_gl
667 test_imgfile
668 test_largefile
669 test_linuxaudiodev
670 test_minidom
671 test_nis
672 test_ntpath
673 test_openpty
674 test_pyexpat
675 test_sax
676 test_socketserver
677 test_sunaudiodev
678 test_sundry
679 test_unicode_file
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000680 """,
681 'sco_sv3':
682 """
683 test_al
684 test_asynchat
685 test_bsddb
686 test_cd
687 test_cl
688 test_dl
689 test_fork1
690 test_gettext
691 test_gl
692 test_imgfile
693 test_largefile
694 test_linuxaudiodev
695 test_locale
696 test_minidom
697 test_nis
698 test_ntpath
699 test_openpty
700 test_pyexpat
701 test_queue
702 test_sax
703 test_socketserver
704 test_sunaudiodev
705 test_sundry
706 test_thread
707 test_threaded_import
708 test_threadedtempfile
709 test_threading
710 test_unicode_file
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000711 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000712 'riscos':
713 """
714 test_al
715 test_asynchat
716 test_bsddb
717 test_cd
718 test_cl
719 test_commands
720 test_crypt
721 test_dbm
722 test_dl
723 test_fcntl
724 test_fork1
725 test_gdbm
726 test_gl
727 test_grp
728 test_imgfile
729 test_largefile
730 test_linuxaudiodev
731 test_locale
732 test_mmap
733 test_nis
734 test_ntpath
735 test_openpty
736 test_poll
737 test_popen2
738 test_pty
739 test_pwd
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000740 test_socketserver
741 test_strop
742 test_sunaudiodev
743 test_sundry
744 test_thread
745 test_threaded_import
746 test_threadedtempfile
747 test_threading
748 test_timing
749 test_unicode_file
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000750 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000751 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000752 """
753 test_al
Jack Jansenacda3392002-12-30 23:03:13 +0000754 test_bsddb
Guido van Rossum9d427002002-12-03 10:24:56 +0000755 test_bsddb3
Jack Jansen398c2362001-12-02 21:41:36 +0000756 test_cd
757 test_cl
758 test_curses
759 test_dl
Guido van Rossum9d427002002-12-03 10:24:56 +0000760 test_email_codecs
Jack Jansen398c2362001-12-02 21:41:36 +0000761 test_gdbm
762 test_gl
763 test_imgfile
764 test_largefile
765 test_linuxaudiodev
Jack Jansenacda3392002-12-30 23:03:13 +0000766 test_locale
Jack Jansen398c2362001-12-02 21:41:36 +0000767 test_minidom
Guido van Rossum9d427002002-12-03 10:24:56 +0000768 test_mpz
Jack Jansen398c2362001-12-02 21:41:36 +0000769 test_nis
770 test_ntpath
Jack Jansenacda3392002-12-30 23:03:13 +0000771 test_ossaudiodev
Jack Jansen398c2362001-12-02 21:41:36 +0000772 test_poll
Jack Jansenf839c272001-12-14 21:28:53 +0000773 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000774 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000775 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000776 """,
Guido van Rossum11c3f092002-07-17 15:08:24 +0000777 'sunos5':
778 """
779 test_al
780 test_bsddb
781 test_cd
782 test_cl
783 test_curses
784 test_dbm
785 test_email_codecs
786 test_gdbm
787 test_gl
788 test_gzip
789 test_imgfile
790 test_linuxaudiodev
791 test_mpz
792 test_openpty
Guido van Rossum11c3f092002-07-17 15:08:24 +0000793 test_socketserver
Guido van Rossum11c3f092002-07-17 15:08:24 +0000794 test_zipfile
795 test_zlib
Jeremy Hyltoned375e12002-07-17 15:56:55 +0000796 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000797 'hp-ux11':
798 """
799 test_al
800 test_bsddb
801 test_cd
802 test_cl
803 test_curses
804 test_dl
805 test_gdbm
806 test_gl
807 test_gzip
808 test_imgfile
809 test_largefile
810 test_linuxaudiodev
811 test_locale
812 test_minidom
813 test_nis
814 test_ntpath
815 test_openpty
816 test_pyexpat
817 test_sax
Skip Montanarob3230212002-03-15 02:54:03 +0000818 test_socketserver
819 test_sunaudiodev
820 test_unicode_file
Skip Montanarob3230212002-03-15 02:54:03 +0000821 test_zipfile
822 test_zlib
823 """,
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000824 'atheos':
Tim Petersc411dba2002-07-16 21:35:23 +0000825 """
826 test_al
827 test_cd
828 test_cl
829 test_curses
830 test_dl
831 test_email_codecs
832 test_gdbm
833 test_gl
834 test_imgfile
835 test_largefile
836 test_linuxaudiodev
837 test_locale
838 test_mhlib
839 test_mmap
840 test_mpz
841 test_nis
842 test_poll
843 test_popen2
844 test_resource
Tim Petersc411dba2002-07-16 21:35:23 +0000845 test_socketserver
846 test_sunaudiodev
847 test_unicode_file
Tim Petersc411dba2002-07-16 21:35:23 +0000848 """,
Jason Tishler25115942002-12-05 15:18:15 +0000849 'cygwin':
850 """
851 test_al
Tim Petersb0f89e02002-12-05 17:20:25 +0000852 test_bsddb3
Jason Tishler25115942002-12-05 15:18:15 +0000853 test_cd
854 test_cl
855 test_curses
856 test_dbm
857 test_email_codecs
858 test_gl
859 test_imgfile
860 test_largefile
861 test_linuxaudiodev
862 test_locale
863 test_mpz
864 test_nis
Jason Tishler25115942002-12-05 15:18:15 +0000865 test_socketserver
866 test_sunaudiodev
867 test_unicode_file
Jason Tishler25115942002-12-05 15:18:15 +0000868 """,
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +0000869 'os2emx':
870 """
871 test_al
872 test_audioop
873 test_bsddb3
874 test_cd
875 test_cl
876 test_commands
877 test_curses
878 test_dl
879 test_email_codecs
880 test_gl
Tim Peters40e1ce42003-01-27 16:45:03 +0000881 test_iconv_codecs
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +0000882 test_imgfile
883 test_largefile
884 test_linuxaudiodev
885 test_mhlib
886 test_mmap
887 test_nis
888 test_openpty
889 test_ossaudiodev
890 test_pty
891 test_resource
892 test_signal
893 test_sunaudiodev
894 test_unicode_file
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +0000895 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000896}
897
Tim Petersb5b7b782001-08-12 01:20:39 +0000898class _ExpectedSkips:
899 def __init__(self):
Tim Peters2a182db2002-10-09 01:07:11 +0000900 import os.path
Tim Peters1b445d32002-11-24 18:53:11 +0000901 from test import test_normalization
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000902 from test import test_socket_ssl
Tim Peters1b445d32002-11-24 18:53:11 +0000903
Tim Peters7c7efe92002-08-23 17:55:54 +0000904 self.valid = False
Tim Petersde14a302002-04-01 05:04:46 +0000905 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000906 s = _expectations[sys.platform]
Tim Peters7c7efe92002-08-23 17:55:54 +0000907 self.expected = Set(s.split())
Tim Peters1b445d32002-11-24 18:53:11 +0000908
Tim Peters2a182db2002-10-09 01:07:11 +0000909 if not os.path.supports_unicode_filenames:
910 self.expected.add('test_pep277')
Tim Peters1b445d32002-11-24 18:53:11 +0000911
912 if test_normalization.skip_expected:
913 self.expected.add('test_normalization')
914
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000915 if test_socket_ssl.skip_expected:
916 self.expected.add('test_socket_ssl')
917
Jack Jansen6afc5e02003-01-29 16:24:16 +0000918 if not sys.platform in ("mac", "darwin"):
Tim Petersecd79eb2003-01-29 00:35:32 +0000919 self.expected.add("test_macostools")
920 self.expected.add("test_macfs")
Jack Jansen6afc5e02003-01-29 16:24:16 +0000921 self.expected.add("test_aepack")
Tim Petersecd79eb2003-01-29 00:35:32 +0000922
923 if sys.platform != "win32":
924 self.expected.add("test_winreg")
925 self.expected.add("test_winsound")
926
Tim Peters7c7efe92002-08-23 17:55:54 +0000927 self.valid = True
Tim Petersb5b7b782001-08-12 01:20:39 +0000928
929 def isvalid(self):
930 "Return true iff _ExpectedSkips knows about the current platform."
931 return self.valid
932
933 def getexpected(self):
934 """Return set of test names we expect to skip on current platform.
935
936 self.isvalid() must be true.
937 """
938
939 assert self.isvalid()
940 return self.expected
941
Guido van Rossum152494a1996-12-20 03:12:20 +0000942if __name__ == '__main__':
Barry Warsaw408b6d32002-07-30 23:27:12 +0000943 # Remove regrtest.py's own directory from the module search path. This
944 # prevents relative imports from working, and relative imports will screw
945 # up the testing framework. E.g. if both test.test_support and
946 # test_support are imported, they will not contain the same globals, and
947 # much of the testing framework relies on the globals in the
948 # test.test_support module.
949 mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
950 i = pathlen = len(sys.path)
951 while i >= 0:
952 i -= 1
953 if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
954 del sys.path[i]
955 if len(sys.path) == pathlen:
956 print 'Could not find %r in sys.path to remove it' % mydir
Barry Warsaw08fca522001-08-20 22:33:46 +0000957 main()