blob: 2870daa77c12c4477159d7539f06d50d6a7193f6 [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.
Guido van Rossum152494a1996-12-20 03:12:20 +000058"""
59
60import sys
Guido van Rossum152494a1996-12-20 03:12:20 +000061import os
62import getopt
Guido van Rossum9e48b271997-07-16 01:56:13 +000063import traceback
Skip Montanaroab1c7912000-06-30 16:39:27 +000064import random
Fred Drakeae1bb172001-05-21 21:08:12 +000065import StringIO
Guido van Rossumdc15c272002-08-12 21:55:51 +000066import warnings
Tim Peters7c7efe92002-08-23 17:55:54 +000067from sets import Set
Guido van Rossumdc15c272002-08-12 21:55:51 +000068
69# I see no other way to suppress these warnings;
70# putting them in test_grammar.py has no effect:
Guido van Rossum88b1def2002-08-14 17:54:48 +000071warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
Guido van Rossumdc15c272002-08-12 21:55:51 +000072 ".*test.test_grammar$")
Guido van Rossumc34c4fc2002-09-19 00:42:16 +000073if sys.maxint > 0x7fffffff:
74 # Also suppress them in <string>, because for 64-bit platforms,
75 # that's where test_grammar.py hides them.
76 warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
77 "<string>")
Guido van Rossum152494a1996-12-20 03:12:20 +000078
Barry Warsaw04f357c2002-07-23 19:04:11 +000079from test import test_support
Fred Drake3a15dac2002-04-11 16:39:16 +000080
81RESOURCE_NAMES = ('curses', 'largefile', 'network')
82
83
Barry Warsaw08fca522001-08-20 22:33:46 +000084def usage(code, msg=''):
85 print __doc__
86 if msg: print msg
87 sys.exit(code)
88
89
Skip Montanaroab1c7912000-06-30 16:39:27 +000090def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
Tim Petersc5000df2002-06-02 21:42:01 +000091 exclude=0, single=0, randomize=0, fromfile=None, findleaks=0,
Barry Warsaw08fca522001-08-20 22:33:46 +000092 use_resources=None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +000093 """Execute a test suite.
94
Thomas Wouters7e474022000-07-16 12:04:32 +000095 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +000096 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000097
98 tests -- a list of strings containing test names (optional)
99 testdir -- the directory in which to look for tests (optional)
100
101 Users other than the Python test suite will certainly want to
102 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +0000103 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000104
105 If the tests argument is omitted, the tests listed on the
106 command-line will be used. If that's empty, too, then all *.py
107 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +0000108
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000109 The other default arguments (verbose, quiet, generate, exclude,
110 single, randomize, findleaks, and use_resources) allow programmers
111 calling main() directly to set the values that would normally be
112 set by flags on the command line.
Barry Warsawa873b032000-08-03 15:50:37 +0000113
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000114 """
Fred Drake004d5e62000-10-23 17:22:08 +0000115
Tim Peters8dee8092001-09-25 20:05:11 +0000116 test_support.record_original_stdout(sys.stdout)
Guido van Rossum152494a1996-12-20 03:12:20 +0000117 try:
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000118 opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsrf:lu:t:',
Barry Warsaw08fca522001-08-20 22:33:46 +0000119 ['help', 'verbose', 'quiet', 'generate',
Tim Petersc5000df2002-06-02 21:42:01 +0000120 'exclude', 'single', 'random', 'fromfile',
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000121 'findleaks', 'use=', 'threshold='])
Guido van Rossum152494a1996-12-20 03:12:20 +0000122 except getopt.error, msg:
Barry Warsaw08fca522001-08-20 22:33:46 +0000123 usage(2, msg)
124
125 # Defaults
126 if use_resources is None:
127 use_resources = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000128 for o, a in opts:
Barry Warsaw08fca522001-08-20 22:33:46 +0000129 if o in ('-h', '--help'):
130 usage(0)
131 elif o in ('-v', '--verbose'):
132 verbose += 1
133 elif o in ('-q', '--quiet'):
134 quiet = 1;
135 verbose = 0
136 elif o in ('-g', '--generate'):
137 generate = 1
138 elif o in ('-x', '--exclude'):
139 exclude = 1
140 elif o in ('-s', '--single'):
141 single = 1
142 elif o in ('-r', '--randomize'):
143 randomize = 1
Tim Petersc5000df2002-06-02 21:42:01 +0000144 elif o in ('-f', '--fromfile'):
145 fromfile = a
Barry Warsaw08fca522001-08-20 22:33:46 +0000146 elif o in ('-l', '--findleaks'):
147 findleaks = 1
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000148 elif o in ('-t', '--threshold'):
149 import gc
150 gc.set_threshold(int(a))
Barry Warsaw08fca522001-08-20 22:33:46 +0000151 elif o in ('-u', '--use'):
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000152 u = [x.lower() for x in a.split(',')]
153 for r in u:
Fred Drake3a15dac2002-04-11 16:39:16 +0000154 if r == 'all':
155 use_resources = RESOURCE_NAMES
156 break
157 if r not in RESOURCE_NAMES:
158 usage(1, 'Invalid -u/--use option: ' + a)
Fred Drake04a8da52002-04-11 20:58:54 +0000159 if r not in use_resources:
Andrew MacIntyree41abab2002-04-30 12:11:04 +0000160 use_resources.append(r)
Guido van Rossuma4122201997-08-18 20:08:24 +0000161 if generate and verbose:
Barry Warsaw08fca522001-08-20 22:33:46 +0000162 usage(2, "-g and -v don't go together!")
Tim Petersc5000df2002-06-02 21:42:01 +0000163 if single and fromfile:
164 usage(2, "-s and -f don't go together!")
Barry Warsaw08fca522001-08-20 22:33:46 +0000165
Guido van Rossum152494a1996-12-20 03:12:20 +0000166 good = []
167 bad = []
168 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000169
Neil Schemenauerd569f232000-09-22 15:29:28 +0000170 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000171 try:
172 import gc
173 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000174 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +0000175 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +0000176 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000177 # Uncomment the line below to report garbage that is not
178 # freeable by reference counting alone. By default only
179 # garbage that is not collectable by the GC is reported.
180 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000181 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000182
Barry Warsawe11e3de1999-01-28 19:51:51 +0000183 if single:
184 from tempfile import gettempdir
185 filename = os.path.join(gettempdir(), 'pynexttest')
186 try:
187 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000188 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000189 tests = [next]
190 fp.close()
191 except IOError:
192 pass
Tim Petersc5000df2002-06-02 21:42:01 +0000193
194 if fromfile:
195 tests = []
196 fp = open(fromfile)
197 for line in fp:
198 guts = line.split() # assuming no test has whitespace in its name
199 if guts and not guts[0].startswith('#'):
200 tests.extend(guts)
201 fp.close()
202
203 # Strip .py extensions.
204 if args:
205 args = map(removepy, args)
206 if tests:
207 tests = map(removepy, tests)
208
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000209 stdtests = STDTESTS[:]
210 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000211 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000212 for arg in args:
213 if arg in stdtests:
214 stdtests.remove(arg)
215 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000216 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000217 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000218 if single:
219 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000220 if randomize:
221 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000222 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000223 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000224 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000225 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000226 if not quiet:
227 print test
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000228 sys.stdout.flush()
Trent Mickf29f47b2000-08-11 19:02:59 +0000229 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000230 if ok > 0:
231 good.append(test)
232 elif ok == 0:
233 bad.append(test)
234 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000235 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000236 if findleaks:
237 gc.collect()
238 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000239 print "Warning: test created", len(gc.garbage),
240 print "uncollectable object(s)."
241 # move the uncollectable objects somewhere so we don't see
242 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000243 found_garbage.extend(gc.garbage)
244 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000245 # Unload the newly imported modules (best effort finalization)
246 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000247 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000248 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000249
250 # The lists won't be sorted if running with -r
251 good.sort()
252 bad.sort()
253 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000254
Guido van Rossum152494a1996-12-20 03:12:20 +0000255 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000256 if not bad and not skipped and len(good) > 1:
257 print "All",
258 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000259 if verbose:
Barry Warsaw408b6d32002-07-30 23:27:12 +0000260 print "CAUTION: stdout isn't compared in verbose mode:"
261 print "a test that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000262 if bad:
Tim Petersa45da922001-08-12 03:45:50 +0000263 print count(len(bad), "test"), "failed:"
264 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000265 if skipped and not quiet:
Tim Petersa45da922001-08-12 03:45:50 +0000266 print count(len(skipped), "test"), "skipped:"
267 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000268
Tim Petersb5b7b782001-08-12 01:20:39 +0000269 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000270 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000271 if e.isvalid():
Tim Peters7c7efe92002-08-23 17:55:54 +0000272 surprise = Set(skipped) - e.getexpected()
Tim Petersb5b7b782001-08-12 01:20:39 +0000273 if surprise:
274 print count(len(surprise), "skip"), \
Tim Petersa45da922001-08-12 03:45:50 +0000275 "unexpected on", plat + ":"
276 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000277 else:
278 print "Those skips are all expected on", plat + "."
279 else:
280 print "Ask someone to teach regrtest.py about which tests are"
281 print "expected to get skipped on", plat + "."
282
Barry Warsawe11e3de1999-01-28 19:51:51 +0000283 if single:
284 alltests = findtests(testdir, stdtests, nottests)
285 for i in range(len(alltests)):
286 if tests[0] == alltests[i]:
287 if i == len(alltests) - 1:
288 os.unlink(filename)
289 else:
290 fp = open(filename, 'w')
291 fp.write(alltests[i+1] + '\n')
292 fp.close()
293 break
294 else:
295 os.unlink(filename)
296
Barry Warsaw08fca522001-08-20 22:33:46 +0000297 sys.exit(len(bad) > 0)
298
Guido van Rossum152494a1996-12-20 03:12:20 +0000299
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000300STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000301 'test_grammar',
302 'test_opcodes',
303 'test_operations',
304 'test_builtin',
305 'test_exceptions',
306 'test_types',
307 ]
308
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000309NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000310 'test_support',
311 'test_b1',
312 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000313 'test_future1',
314 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000315 'test_future3',
Guido van Rossum152494a1996-12-20 03:12:20 +0000316 ]
317
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000318def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000319 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000320 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000321 names = os.listdir(testdir)
322 tests = []
323 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000324 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000325 modname = name[:-3]
326 if modname not in stdtests and modname not in nottests:
327 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000328 tests.sort()
329 return stdtests + tests
330
Trent Mickf29f47b2000-08-11 19:02:59 +0000331def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000332 """Run a single test.
333 test -- the name of the test
334 generate -- if true, generate output, instead of running the test
335 and comparing it to a previously created output file
336 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000337 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000338 testdir -- test directory
339 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000340 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000341 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000342 outputdir = os.path.join(testdir, "output")
343 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000344 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000345 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000346 else:
Fred Drake88a56852001-09-28 20:16:30 +0000347 cfp = StringIO.StringIO()
Guido van Rossum152494a1996-12-20 03:12:20 +0000348 try:
Tim Peters342ca752001-09-25 19:13:20 +0000349 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000350 try:
351 if cfp:
352 sys.stdout = cfp
353 print test # Output file starts with test name
Barry Warsaw408b6d32002-07-30 23:27:12 +0000354 if test.startswith('test.'):
355 abstest = test
356 else:
357 # Always import it from the test package
358 abstest = 'test.' + test
359 the_package = __import__(abstest, globals(), locals(), [])
360 the_module = getattr(the_package, test)
Tim Petersd9742212001-05-22 18:28:25 +0000361 # Most tests run to completion simply as a side-effect of
362 # being imported. For the benefit of tests that can't run
363 # that way (like test_threaded_import), explicitly invoke
364 # their test_main() function (if it exists).
365 indirect_test = getattr(the_module, "test_main", None)
366 if indirect_test is not None:
367 indirect_test()
Guido van Rossum41360a41998-03-26 19:42:58 +0000368 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000369 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000370 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000371 if not quiet:
Guido van Rossumeb949052001-09-18 20:34:19 +0000372 print "test", test, "skipped --", msg
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000373 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000374 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000375 except KeyboardInterrupt:
376 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000377 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000378 print "test", test, "failed --", msg
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000379 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000380 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000381 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000382 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000383 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000384 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000385 if verbose:
386 traceback.print_exc(file=sys.stdout)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000387 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000388 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000389 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000390 if not cfp:
391 return 1
392 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000393 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000394 if output == test + "\n":
395 if os.path.exists(outputfile):
396 # Write it since it already exists (and the contents
397 # may have changed), but let the user know it isn't
398 # needed:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000399 print "output file", outputfile, \
400 "is no longer needed; consider removing it"
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000401 else:
402 # We don't need it, so don't create it.
403 return 1
404 fp = open(outputfile, "w")
405 fp.write(output)
406 fp.close()
407 return 1
408 if os.path.exists(outputfile):
409 fp = open(outputfile, "r")
410 expected = fp.read()
411 fp.close()
412 else:
413 expected = test + "\n"
414 if output == expected:
415 return 1
416 print "test", test, "produced unexpected output:"
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000417 sys.stdout.flush()
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000418 reportdiff(expected, output)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000419 sys.stdout.flush()
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000420 return 0
421
422def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000423 import difflib
Tim Petersc377b162001-09-22 05:31:03 +0000424 print "*" * 70
425 a = expected.splitlines(1)
426 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000427 sm = difflib.SequenceMatcher(a=a, b=b)
428 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000429
Guido van Rossumcf691932001-09-21 21:06:22 +0000430 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000431 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000432 x0 += 1
433 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000434 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000435 else:
Tim Petersc377b162001-09-22 05:31:03 +0000436 return "lines %d-%d" % (x0, x1)
437
Guido van Rossumcf691932001-09-21 21:06:22 +0000438 for op, a0, a1, b0, b1 in tuples:
439 if op == 'equal':
440 pass
Tim Petersc377b162001-09-22 05:31:03 +0000441
Guido van Rossumcf691932001-09-21 21:06:22 +0000442 elif op == 'delete':
Tim Petersc377b162001-09-22 05:31:03 +0000443 print "***", pair(a0, a1), "of expected output missing:"
Guido van Rossumcf691932001-09-21 21:06:22 +0000444 for line in a[a0:a1]:
Tim Petersc377b162001-09-22 05:31:03 +0000445 print "-", line,
446
Guido van Rossumcf691932001-09-21 21:06:22 +0000447 elif op == 'replace':
Tim Petersc377b162001-09-22 05:31:03 +0000448 print "*** mismatch between", pair(a0, a1), "of expected", \
449 "output and", pair(b0, b1), "of actual output:"
450 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
451 print line,
452
Guido van Rossumcf691932001-09-21 21:06:22 +0000453 elif op == 'insert':
Tim Petersc377b162001-09-22 05:31:03 +0000454 print "***", pair(b0, b1), "of actual output doesn't appear", \
455 "in expected output after line", str(a1)+":"
Guido van Rossumcf691932001-09-21 21:06:22 +0000456 for line in b[b0:b1]:
Tim Petersc377b162001-09-22 05:31:03 +0000457 print "+", line,
458
Guido van Rossumcf691932001-09-21 21:06:22 +0000459 else:
460 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
Tim Petersc377b162001-09-22 05:31:03 +0000461
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000462 print "*" * 70
Guido van Rossum152494a1996-12-20 03:12:20 +0000463
464def findtestdir():
465 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000466 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000467 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000468 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000469 testdir = os.path.dirname(file) or os.curdir
470 return testdir
471
Tim Petersc5000df2002-06-02 21:42:01 +0000472def removepy(name):
473 if name.endswith(os.extsep + "py"):
474 name = name[:-3]
475 return name
476
Guido van Rossum152494a1996-12-20 03:12:20 +0000477def count(n, word):
478 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000479 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000480 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000481 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000482
Tim Petersa45da922001-08-12 03:45:50 +0000483def printlist(x, width=70, indent=4):
Tim Peters7c7efe92002-08-23 17:55:54 +0000484 """Print the elements of iterable x to stdout.
Tim Petersa45da922001-08-12 03:45:50 +0000485
486 Optional arg width (default 70) is the maximum line length.
487 Optional arg indent (default 4) is the number of blanks with which to
488 begin each line.
489 """
490
Tim Petersba78bc42002-07-04 19:45:06 +0000491 from textwrap import fill
492 blanks = ' ' * indent
493 print fill(' '.join(map(str, x)), width,
494 initial_indent=blanks, subsequent_indent=blanks)
Tim Petersa45da922001-08-12 03:45:50 +0000495
Tim Petersde14a302002-04-01 05:04:46 +0000496# Map sys.platform to a string containing the basenames of tests
497# expected to be skipped on that platform.
498
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000499_expectations = {
500 'win32':
501 """
502 test_al
503 test_cd
504 test_cl
505 test_commands
506 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000507 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000508 test_dbm
509 test_dl
Tim Petersdeb121a2002-04-11 19:52:58 +0000510 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000511 test_fcntl
512 test_fork1
513 test_gdbm
514 test_gl
515 test_grp
516 test_imgfile
517 test_largefile
518 test_linuxaudiodev
519 test_mhlib
Tim Petersde14a302002-04-01 05:04:46 +0000520 test_mpz
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000521 test_nis
522 test_openpty
523 test_poll
524 test_pty
525 test_pwd
Tim Peters1e33ffa2002-04-23 23:09:02 +0000526 test_resource
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000527 test_signal
Barry Warsaw08fca522001-08-20 22:33:46 +0000528 test_socket_ssl
Tim Petersa86f0c12001-09-18 02:18:57 +0000529 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000530 test_sunaudiodev
531 test_timing
532 """,
533 'linux2':
534 """
535 test_al
536 test_cd
537 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000538 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000539 test_dl
Guido van Rossum6184c112002-04-16 02:14:04 +0000540 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000541 test_gl
542 test_imgfile
543 test_largefile
544 test_nis
545 test_ntpath
Barry Warsaw08fca522001-08-20 22:33:46 +0000546 test_socket_ssl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000547 test_socketserver
548 test_sunaudiodev
549 test_unicode_file
550 test_winreg
551 test_winsound
552 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000553 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000554 """
555 test_al
556 test_bsddb
557 test_cd
558 test_cl
559 test_commands
560 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000561 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000562 test_dbm
563 test_dl
564 test_fcntl
565 test_fork1
566 test_gl
567 test_grp
568 test_imgfile
569 test_largefile
570 test_linuxaudiodev
571 test_locale
572 test_mmap
573 test_nis
574 test_ntpath
575 test_openpty
576 test_poll
577 test_popen2
578 test_pty
579 test_pwd
580 test_signal
581 test_socket_ssl
582 test_socketserver
583 test_sunaudiodev
584 test_sundry
585 test_timing
586 test_unicode_file
587 test_winreg
588 test_winsound
589 """,
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000590 'unixware5':
591 """
592 test_al
593 test_bsddb
594 test_cd
595 test_cl
596 test_dl
597 test_gl
598 test_imgfile
599 test_largefile
600 test_linuxaudiodev
601 test_minidom
602 test_nis
603 test_ntpath
604 test_openpty
605 test_pyexpat
606 test_sax
607 test_socketserver
608 test_sunaudiodev
609 test_sundry
610 test_unicode_file
611 test_winreg
612 test_winsound
613 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000614 'riscos':
615 """
616 test_al
617 test_asynchat
618 test_bsddb
619 test_cd
620 test_cl
621 test_commands
622 test_crypt
623 test_dbm
624 test_dl
625 test_fcntl
626 test_fork1
627 test_gdbm
628 test_gl
629 test_grp
630 test_imgfile
631 test_largefile
632 test_linuxaudiodev
633 test_locale
634 test_mmap
635 test_nis
636 test_ntpath
637 test_openpty
638 test_poll
639 test_popen2
640 test_pty
641 test_pwd
642 test_socket_ssl
643 test_socketserver
644 test_strop
645 test_sunaudiodev
646 test_sundry
647 test_thread
648 test_threaded_import
649 test_threadedtempfile
650 test_threading
651 test_timing
652 test_unicode_file
653 test_winreg
654 test_winsound
655 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000656 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000657 """
658 test_al
659 test_cd
660 test_cl
661 test_curses
662 test_dl
663 test_gdbm
664 test_gl
665 test_imgfile
666 test_largefile
667 test_linuxaudiodev
668 test_minidom
669 test_nis
670 test_ntpath
671 test_poll
672 test_socket_ssl
Jack Jansenf839c272001-12-14 21:28:53 +0000673 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000674 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000675 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000676 test_winreg
677 test_winsound
678 """,
Guido van Rossum11c3f092002-07-17 15:08:24 +0000679 'sunos5':
680 """
681 test_al
682 test_bsddb
683 test_cd
684 test_cl
685 test_curses
686 test_dbm
687 test_email_codecs
688 test_gdbm
689 test_gl
690 test_gzip
691 test_imgfile
692 test_linuxaudiodev
693 test_mpz
694 test_openpty
695 test_socket_ssl
696 test_socketserver
697 test_winreg
698 test_winsound
699 test_zipfile
700 test_zlib
Jeremy Hyltoned375e12002-07-17 15:56:55 +0000701 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000702 'hp-ux11':
703 """
704 test_al
705 test_bsddb
706 test_cd
707 test_cl
708 test_curses
709 test_dl
710 test_gdbm
711 test_gl
712 test_gzip
713 test_imgfile
714 test_largefile
715 test_linuxaudiodev
716 test_locale
717 test_minidom
718 test_nis
719 test_ntpath
720 test_openpty
721 test_pyexpat
722 test_sax
723 test_socket_ssl
724 test_socketserver
725 test_sunaudiodev
726 test_unicode_file
727 test_winreg
728 test_winsound
729 test_zipfile
730 test_zlib
731 """,
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000732 'atheos':
Tim Petersc411dba2002-07-16 21:35:23 +0000733 """
734 test_al
735 test_cd
736 test_cl
737 test_curses
738 test_dl
739 test_email_codecs
740 test_gdbm
741 test_gl
742 test_imgfile
743 test_largefile
744 test_linuxaudiodev
745 test_locale
746 test_mhlib
747 test_mmap
748 test_mpz
749 test_nis
750 test_poll
751 test_popen2
752 test_resource
753 test_socket_ssl
754 test_socketserver
755 test_sunaudiodev
756 test_unicode_file
757 test_winreg
758 test_winsound
759 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000760}
761
Tim Petersb5b7b782001-08-12 01:20:39 +0000762class _ExpectedSkips:
763 def __init__(self):
Tim Peters7c7efe92002-08-23 17:55:54 +0000764 self.valid = False
Tim Petersde14a302002-04-01 05:04:46 +0000765 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000766 s = _expectations[sys.platform]
Tim Peters7c7efe92002-08-23 17:55:54 +0000767 self.expected = Set(s.split())
768 self.valid = True
Tim Petersb5b7b782001-08-12 01:20:39 +0000769
770 def isvalid(self):
771 "Return true iff _ExpectedSkips knows about the current platform."
772 return self.valid
773
774 def getexpected(self):
775 """Return set of test names we expect to skip on current platform.
776
777 self.isvalid() must be true.
778 """
779
780 assert self.isvalid()
781 return self.expected
782
Guido van Rossum152494a1996-12-20 03:12:20 +0000783if __name__ == '__main__':
Barry Warsaw408b6d32002-07-30 23:27:12 +0000784 # Remove regrtest.py's own directory from the module search path. This
785 # prevents relative imports from working, and relative imports will screw
786 # up the testing framework. E.g. if both test.test_support and
787 # test_support are imported, they will not contain the same globals, and
788 # much of the testing framework relies on the globals in the
789 # test.test_support module.
790 mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
791 i = pathlen = len(sys.path)
792 while i >= 0:
793 i -= 1
794 if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
795 del sys.path[i]
796 if len(sys.path) == pathlen:
797 print 'Could not find %r in sys.path to remove it' % mydir
Barry Warsaw08fca522001-08-20 22:33:46 +0000798 main()