blob: 9c20c54030f32f7d5db8ef549194d213cf4631b0 [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 Rossum152494a1996-12-20 03:12:20 +000066
67import test_support
68
Fred Drake3a15dac2002-04-11 16:39:16 +000069
70RESOURCE_NAMES = ('curses', 'largefile', 'network')
71
72
Barry Warsaw08fca522001-08-20 22:33:46 +000073def usage(code, msg=''):
74 print __doc__
75 if msg: print msg
76 sys.exit(code)
77
78
Skip Montanaroab1c7912000-06-30 16:39:27 +000079def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
Tim Petersc5000df2002-06-02 21:42:01 +000080 exclude=0, single=0, randomize=0, fromfile=None, findleaks=0,
Barry Warsaw08fca522001-08-20 22:33:46 +000081 use_resources=None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +000082 """Execute a test suite.
83
Thomas Wouters7e474022000-07-16 12:04:32 +000084 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +000085 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000086
87 tests -- a list of strings containing test names (optional)
88 testdir -- the directory in which to look for tests (optional)
89
90 Users other than the Python test suite will certainly want to
91 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +000092 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000093
94 If the tests argument is omitted, the tests listed on the
95 command-line will be used. If that's empty, too, then all *.py
96 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +000097
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000098 The other default arguments (verbose, quiet, generate, exclude,
99 single, randomize, findleaks, and use_resources) allow programmers
100 calling main() directly to set the values that would normally be
101 set by flags on the command line.
Barry Warsawa873b032000-08-03 15:50:37 +0000102
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000103 """
Fred Drake004d5e62000-10-23 17:22:08 +0000104
Tim Peters8dee8092001-09-25 20:05:11 +0000105 test_support.record_original_stdout(sys.stdout)
Guido van Rossum152494a1996-12-20 03:12:20 +0000106 try:
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000107 opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsrf:lu:t:',
Barry Warsaw08fca522001-08-20 22:33:46 +0000108 ['help', 'verbose', 'quiet', 'generate',
Tim Petersc5000df2002-06-02 21:42:01 +0000109 'exclude', 'single', 'random', 'fromfile',
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000110 'findleaks', 'use=', 'threshold='])
Guido van Rossum152494a1996-12-20 03:12:20 +0000111 except getopt.error, msg:
Barry Warsaw08fca522001-08-20 22:33:46 +0000112 usage(2, msg)
113
114 # Defaults
115 if use_resources is None:
116 use_resources = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000117 for o, a in opts:
Barry Warsaw08fca522001-08-20 22:33:46 +0000118 if o in ('-h', '--help'):
119 usage(0)
120 elif o in ('-v', '--verbose'):
121 verbose += 1
122 elif o in ('-q', '--quiet'):
123 quiet = 1;
124 verbose = 0
125 elif o in ('-g', '--generate'):
126 generate = 1
127 elif o in ('-x', '--exclude'):
128 exclude = 1
129 elif o in ('-s', '--single'):
130 single = 1
131 elif o in ('-r', '--randomize'):
132 randomize = 1
Tim Petersc5000df2002-06-02 21:42:01 +0000133 elif o in ('-f', '--fromfile'):
134 fromfile = a
Barry Warsaw08fca522001-08-20 22:33:46 +0000135 elif o in ('-l', '--findleaks'):
136 findleaks = 1
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000137 elif o in ('-t', '--threshold'):
138 import gc
139 gc.set_threshold(int(a))
Barry Warsaw08fca522001-08-20 22:33:46 +0000140 elif o in ('-u', '--use'):
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000141 u = [x.lower() for x in a.split(',')]
142 for r in u:
Fred Drake3a15dac2002-04-11 16:39:16 +0000143 if r == 'all':
144 use_resources = RESOURCE_NAMES
145 break
146 if r not in RESOURCE_NAMES:
147 usage(1, 'Invalid -u/--use option: ' + a)
Fred Drake04a8da52002-04-11 20:58:54 +0000148 if r not in use_resources:
Andrew MacIntyree41abab2002-04-30 12:11:04 +0000149 use_resources.append(r)
Guido van Rossuma4122201997-08-18 20:08:24 +0000150 if generate and verbose:
Barry Warsaw08fca522001-08-20 22:33:46 +0000151 usage(2, "-g and -v don't go together!")
Tim Petersc5000df2002-06-02 21:42:01 +0000152 if single and fromfile:
153 usage(2, "-s and -f don't go together!")
Barry Warsaw08fca522001-08-20 22:33:46 +0000154
Guido van Rossum152494a1996-12-20 03:12:20 +0000155 good = []
156 bad = []
157 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000158
Neil Schemenauerd569f232000-09-22 15:29:28 +0000159 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000160 try:
161 import gc
162 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000163 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +0000164 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +0000165 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000166 # Uncomment the line below to report garbage that is not
167 # freeable by reference counting alone. By default only
168 # garbage that is not collectable by the GC is reported.
169 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000170 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000171
Barry Warsawe11e3de1999-01-28 19:51:51 +0000172 if single:
173 from tempfile import gettempdir
174 filename = os.path.join(gettempdir(), 'pynexttest')
175 try:
176 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000177 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000178 tests = [next]
179 fp.close()
180 except IOError:
181 pass
Tim Petersc5000df2002-06-02 21:42:01 +0000182
183 if fromfile:
184 tests = []
185 fp = open(fromfile)
186 for line in fp:
187 guts = line.split() # assuming no test has whitespace in its name
188 if guts and not guts[0].startswith('#'):
189 tests.extend(guts)
190 fp.close()
191
192 # Strip .py extensions.
193 if args:
194 args = map(removepy, args)
195 if tests:
196 tests = map(removepy, tests)
197
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000198 stdtests = STDTESTS[:]
199 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000200 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000201 for arg in args:
202 if arg in stdtests:
203 stdtests.remove(arg)
204 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000205 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000206 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000207 if single:
208 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000209 if randomize:
210 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000211 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000212 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000213 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000214 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000215 if not quiet:
216 print test
Trent Mickf29f47b2000-08-11 19:02:59 +0000217 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000218 if ok > 0:
219 good.append(test)
220 elif ok == 0:
221 bad.append(test)
222 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000223 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000224 if findleaks:
225 gc.collect()
226 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000227 print "Warning: test created", len(gc.garbage),
228 print "uncollectable object(s)."
229 # move the uncollectable objects somewhere so we don't see
230 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000231 found_garbage.extend(gc.garbage)
232 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000233 # Unload the newly imported modules (best effort finalization)
234 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000235 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000236 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000237
238 # The lists won't be sorted if running with -r
239 good.sort()
240 bad.sort()
241 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000242
Guido van Rossum152494a1996-12-20 03:12:20 +0000243 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000244 if not bad and not skipped and len(good) > 1:
245 print "All",
246 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000247 if verbose:
248 print "CAUTION: stdout isn't compared in verbose mode: a test"
249 print "that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000250 if bad:
Tim Petersa45da922001-08-12 03:45:50 +0000251 print count(len(bad), "test"), "failed:"
252 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000253 if skipped and not quiet:
Tim Petersa45da922001-08-12 03:45:50 +0000254 print count(len(skipped), "test"), "skipped:"
255 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000256
Tim Petersb5b7b782001-08-12 01:20:39 +0000257 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000258 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000259 if e.isvalid():
260 surprise = _Set(skipped) - e.getexpected()
Tim Petersb5b7b782001-08-12 01:20:39 +0000261 if surprise:
262 print count(len(surprise), "skip"), \
Tim Petersa45da922001-08-12 03:45:50 +0000263 "unexpected on", plat + ":"
264 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000265 else:
266 print "Those skips are all expected on", plat + "."
267 else:
268 print "Ask someone to teach regrtest.py about which tests are"
269 print "expected to get skipped on", plat + "."
270
Barry Warsawe11e3de1999-01-28 19:51:51 +0000271 if single:
272 alltests = findtests(testdir, stdtests, nottests)
273 for i in range(len(alltests)):
274 if tests[0] == alltests[i]:
275 if i == len(alltests) - 1:
276 os.unlink(filename)
277 else:
278 fp = open(filename, 'w')
279 fp.write(alltests[i+1] + '\n')
280 fp.close()
281 break
282 else:
283 os.unlink(filename)
284
Barry Warsaw08fca522001-08-20 22:33:46 +0000285 sys.exit(len(bad) > 0)
286
Guido van Rossum152494a1996-12-20 03:12:20 +0000287
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000288STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000289 'test_grammar',
290 'test_opcodes',
291 'test_operations',
292 'test_builtin',
293 'test_exceptions',
294 'test_types',
295 ]
296
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000297NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000298 'test_support',
299 'test_b1',
300 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000301 'test_future1',
302 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000303 'test_future3',
Guido van Rossum152494a1996-12-20 03:12:20 +0000304 ]
305
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000306def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000307 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000308 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000309 names = os.listdir(testdir)
310 tests = []
311 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000312 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000313 modname = name[:-3]
314 if modname not in stdtests and modname not in nottests:
315 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000316 tests.sort()
317 return stdtests + tests
318
Trent Mickf29f47b2000-08-11 19:02:59 +0000319def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000320 """Run a single test.
321 test -- the name of the test
322 generate -- if true, generate output, instead of running the test
323 and comparing it to a previously created output file
324 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000325 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000326 testdir -- test directory
327 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000328 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000329 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000330 outputdir = os.path.join(testdir, "output")
331 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000332 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000333 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000334 else:
Fred Drake88a56852001-09-28 20:16:30 +0000335 cfp = StringIO.StringIO()
Guido van Rossum152494a1996-12-20 03:12:20 +0000336 try:
Tim Peters342ca752001-09-25 19:13:20 +0000337 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000338 try:
339 if cfp:
340 sys.stdout = cfp
341 print test # Output file starts with test name
Tim Petersd9742212001-05-22 18:28:25 +0000342 the_module = __import__(test, globals(), locals(), [])
343 # Most tests run to completion simply as a side-effect of
344 # being imported. For the benefit of tests that can't run
345 # that way (like test_threaded_import), explicitly invoke
346 # their test_main() function (if it exists).
347 indirect_test = getattr(the_module, "test_main", None)
348 if indirect_test is not None:
349 indirect_test()
Guido van Rossum41360a41998-03-26 19:42:58 +0000350 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000351 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000352 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000353 if not quiet:
Guido van Rossumeb949052001-09-18 20:34:19 +0000354 print "test", test, "skipped --", msg
Guido van Rossum41360a41998-03-26 19:42:58 +0000355 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000356 except KeyboardInterrupt:
357 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000358 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000359 print "test", test, "failed --", msg
360 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000361 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000362 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000363 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum41360a41998-03-26 19:42:58 +0000364 if verbose:
365 traceback.print_exc(file=sys.stdout)
366 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000367 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000368 if not cfp:
369 return 1
370 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000371 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000372 if output == test + "\n":
373 if os.path.exists(outputfile):
374 # Write it since it already exists (and the contents
375 # may have changed), but let the user know it isn't
376 # needed:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000377 print "output file", outputfile, \
378 "is no longer needed; consider removing it"
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000379 else:
380 # We don't need it, so don't create it.
381 return 1
382 fp = open(outputfile, "w")
383 fp.write(output)
384 fp.close()
385 return 1
386 if os.path.exists(outputfile):
387 fp = open(outputfile, "r")
388 expected = fp.read()
389 fp.close()
390 else:
391 expected = test + "\n"
392 if output == expected:
393 return 1
394 print "test", test, "produced unexpected output:"
395 reportdiff(expected, output)
396 return 0
397
398def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000399 import difflib
Tim Petersc377b162001-09-22 05:31:03 +0000400 print "*" * 70
401 a = expected.splitlines(1)
402 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000403 sm = difflib.SequenceMatcher(a=a, b=b)
404 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000405
Guido van Rossumcf691932001-09-21 21:06:22 +0000406 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000407 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000408 x0 += 1
409 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000410 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000411 else:
Tim Petersc377b162001-09-22 05:31:03 +0000412 return "lines %d-%d" % (x0, x1)
413
Guido van Rossumcf691932001-09-21 21:06:22 +0000414 for op, a0, a1, b0, b1 in tuples:
415 if op == 'equal':
416 pass
Tim Petersc377b162001-09-22 05:31:03 +0000417
Guido van Rossumcf691932001-09-21 21:06:22 +0000418 elif op == 'delete':
Tim Petersc377b162001-09-22 05:31:03 +0000419 print "***", pair(a0, a1), "of expected output missing:"
Guido van Rossumcf691932001-09-21 21:06:22 +0000420 for line in a[a0:a1]:
Tim Petersc377b162001-09-22 05:31:03 +0000421 print "-", line,
422
Guido van Rossumcf691932001-09-21 21:06:22 +0000423 elif op == 'replace':
Tim Petersc377b162001-09-22 05:31:03 +0000424 print "*** mismatch between", pair(a0, a1), "of expected", \
425 "output and", pair(b0, b1), "of actual output:"
426 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
427 print line,
428
Guido van Rossumcf691932001-09-21 21:06:22 +0000429 elif op == 'insert':
Tim Petersc377b162001-09-22 05:31:03 +0000430 print "***", pair(b0, b1), "of actual output doesn't appear", \
431 "in expected output after line", str(a1)+":"
Guido van Rossumcf691932001-09-21 21:06:22 +0000432 for line in b[b0:b1]:
Tim Petersc377b162001-09-22 05:31:03 +0000433 print "+", line,
434
Guido van Rossumcf691932001-09-21 21:06:22 +0000435 else:
436 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
Tim Petersc377b162001-09-22 05:31:03 +0000437
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000438 print "*" * 70
Guido van Rossum152494a1996-12-20 03:12:20 +0000439
440def findtestdir():
441 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000442 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000443 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000444 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000445 testdir = os.path.dirname(file) or os.curdir
446 return testdir
447
Tim Petersc5000df2002-06-02 21:42:01 +0000448def removepy(name):
449 if name.endswith(os.extsep + "py"):
450 name = name[:-3]
451 return name
452
Guido van Rossum152494a1996-12-20 03:12:20 +0000453def count(n, word):
454 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000455 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000456 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000457 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000458
Tim Petersa45da922001-08-12 03:45:50 +0000459def printlist(x, width=70, indent=4):
460 """Print the elements of a sequence to stdout.
461
462 Optional arg width (default 70) is the maximum line length.
463 Optional arg indent (default 4) is the number of blanks with which to
464 begin each line.
465 """
466
Tim Petersba78bc42002-07-04 19:45:06 +0000467 from textwrap import fill
468 blanks = ' ' * indent
469 print fill(' '.join(map(str, x)), width,
470 initial_indent=blanks, subsequent_indent=blanks)
Tim Petersa45da922001-08-12 03:45:50 +0000471
Tim Petersb5b7b782001-08-12 01:20:39 +0000472class _Set:
473 def __init__(self, seq=[]):
474 data = self.data = {}
475 for x in seq:
476 data[x] = 1
477
478 def __len__(self):
479 return len(self.data)
480
481 def __sub__(self, other):
482 "Return set of all elements in self not in other."
483 result = _Set()
484 data = result.data = self.data.copy()
485 for x in other.data:
486 if x in data:
487 del data[x]
488 return result
489
Jeremy Hylton39f77bc2001-08-12 21:53:08 +0000490 def __iter__(self):
491 return iter(self.data)
492
Tim Petersb5b7b782001-08-12 01:20:39 +0000493 def tolist(self, sorted=1):
494 "Return _Set elements as a list."
495 data = self.data.keys()
496 if sorted:
497 data.sort()
498 return data
499
Tim Petersde14a302002-04-01 05:04:46 +0000500# Map sys.platform to a string containing the basenames of tests
501# expected to be skipped on that platform.
502
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000503_expectations = {
504 'win32':
505 """
506 test_al
507 test_cd
508 test_cl
509 test_commands
510 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000511 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000512 test_dbm
513 test_dl
Tim Petersdeb121a2002-04-11 19:52:58 +0000514 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000515 test_fcntl
516 test_fork1
517 test_gdbm
518 test_gl
519 test_grp
520 test_imgfile
521 test_largefile
522 test_linuxaudiodev
523 test_mhlib
Tim Petersde14a302002-04-01 05:04:46 +0000524 test_mpz
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000525 test_nis
526 test_openpty
527 test_poll
528 test_pty
529 test_pwd
Tim Peters1e33ffa2002-04-23 23:09:02 +0000530 test_resource
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000531 test_signal
Barry Warsaw08fca522001-08-20 22:33:46 +0000532 test_socket_ssl
Tim Petersa86f0c12001-09-18 02:18:57 +0000533 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000534 test_sunaudiodev
535 test_timing
536 """,
537 'linux2':
538 """
539 test_al
540 test_cd
541 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000542 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000543 test_dl
Guido van Rossum6184c112002-04-16 02:14:04 +0000544 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000545 test_gl
546 test_imgfile
547 test_largefile
548 test_nis
549 test_ntpath
Barry Warsaw08fca522001-08-20 22:33:46 +0000550 test_socket_ssl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000551 test_socketserver
552 test_sunaudiodev
553 test_unicode_file
554 test_winreg
555 test_winsound
556 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000557 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000558 """
559 test_al
560 test_bsddb
561 test_cd
562 test_cl
563 test_commands
564 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000565 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000566 test_dbm
567 test_dl
568 test_fcntl
569 test_fork1
570 test_gl
571 test_grp
572 test_imgfile
573 test_largefile
574 test_linuxaudiodev
575 test_locale
576 test_mmap
577 test_nis
578 test_ntpath
579 test_openpty
580 test_poll
581 test_popen2
582 test_pty
583 test_pwd
584 test_signal
585 test_socket_ssl
586 test_socketserver
587 test_sunaudiodev
588 test_sundry
589 test_timing
590 test_unicode_file
591 test_winreg
592 test_winsound
593 """,
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000594 'unixware5':
595 """
596 test_al
597 test_bsddb
598 test_cd
599 test_cl
600 test_dl
601 test_gl
602 test_imgfile
603 test_largefile
604 test_linuxaudiodev
605 test_minidom
606 test_nis
607 test_ntpath
608 test_openpty
609 test_pyexpat
610 test_sax
611 test_socketserver
612 test_sunaudiodev
613 test_sundry
614 test_unicode_file
615 test_winreg
616 test_winsound
617 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000618 'riscos':
619 """
620 test_al
621 test_asynchat
622 test_bsddb
623 test_cd
624 test_cl
625 test_commands
626 test_crypt
627 test_dbm
628 test_dl
629 test_fcntl
630 test_fork1
631 test_gdbm
632 test_gl
633 test_grp
634 test_imgfile
635 test_largefile
636 test_linuxaudiodev
637 test_locale
638 test_mmap
639 test_nis
640 test_ntpath
641 test_openpty
642 test_poll
643 test_popen2
644 test_pty
645 test_pwd
646 test_socket_ssl
647 test_socketserver
648 test_strop
649 test_sunaudiodev
650 test_sundry
651 test_thread
652 test_threaded_import
653 test_threadedtempfile
654 test_threading
655 test_timing
656 test_unicode_file
657 test_winreg
658 test_winsound
659 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000660 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000661 """
662 test_al
663 test_cd
664 test_cl
665 test_curses
666 test_dl
667 test_gdbm
668 test_gl
669 test_imgfile
670 test_largefile
671 test_linuxaudiodev
672 test_minidom
673 test_nis
674 test_ntpath
675 test_poll
676 test_socket_ssl
Jack Jansenf839c272001-12-14 21:28:53 +0000677 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000678 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000679 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000680 test_winreg
681 test_winsound
682 """,
Guido van Rossum11c3f092002-07-17 15:08:24 +0000683 'sunos5':
684 """
685 test_al
686 test_bsddb
687 test_cd
688 test_cl
689 test_curses
690 test_dbm
691 test_email_codecs
692 test_gdbm
693 test_gl
694 test_gzip
695 test_imgfile
696 test_linuxaudiodev
697 test_mpz
698 test_openpty
699 test_socket_ssl
700 test_socketserver
701 test_winreg
702 test_winsound
703 test_zipfile
704 test_zlib
Jeremy Hyltoned375e12002-07-17 15:56:55 +0000705 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000706 'hp-ux11':
707 """
708 test_al
709 test_bsddb
710 test_cd
711 test_cl
712 test_curses
713 test_dl
714 test_gdbm
715 test_gl
716 test_gzip
717 test_imgfile
718 test_largefile
719 test_linuxaudiodev
720 test_locale
721 test_minidom
722 test_nis
723 test_ntpath
724 test_openpty
725 test_pyexpat
726 test_sax
727 test_socket_ssl
728 test_socketserver
729 test_sunaudiodev
730 test_unicode_file
731 test_winreg
732 test_winsound
733 test_zipfile
734 test_zlib
735 """,
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000736 'atheos':
Tim Petersc411dba2002-07-16 21:35:23 +0000737 """
738 test_al
739 test_cd
740 test_cl
741 test_curses
742 test_dl
743 test_email_codecs
744 test_gdbm
745 test_gl
746 test_imgfile
747 test_largefile
748 test_linuxaudiodev
749 test_locale
750 test_mhlib
751 test_mmap
752 test_mpz
753 test_nis
754 test_poll
755 test_popen2
756 test_resource
757 test_socket_ssl
758 test_socketserver
759 test_sunaudiodev
760 test_unicode_file
761 test_winreg
762 test_winsound
763 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000764}
765
Tim Petersb5b7b782001-08-12 01:20:39 +0000766class _ExpectedSkips:
767 def __init__(self):
768 self.valid = 0
Tim Petersde14a302002-04-01 05:04:46 +0000769 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000770 s = _expectations[sys.platform]
Tim Petersb5b7b782001-08-12 01:20:39 +0000771 self.expected = _Set(s.split())
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000772 self.valid = 1
Tim Petersb5b7b782001-08-12 01:20:39 +0000773
774 def isvalid(self):
775 "Return true iff _ExpectedSkips knows about the current platform."
776 return self.valid
777
778 def getexpected(self):
779 """Return set of test names we expect to skip on current platform.
780
781 self.isvalid() must be true.
782 """
783
784 assert self.isvalid()
785 return self.expected
786
Guido van Rossum152494a1996-12-20 03:12:20 +0000787if __name__ == '__main__':
Barry Warsaw08fca522001-08-20 22:33:46 +0000788 main()