blob: 4d71e219a4299400d1d09e8ae5dfd6ce49a7ce5a [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
Barry Warsaw04f357c2002-07-23 19:04:11 +000067from test import test_support
Fred Drake3a15dac2002-04-11 16:39:16 +000068
69RESOURCE_NAMES = ('curses', 'largefile', 'network')
70
71
Barry Warsaw08fca522001-08-20 22:33:46 +000072def usage(code, msg=''):
73 print __doc__
74 if msg: print msg
75 sys.exit(code)
76
77
Skip Montanaroab1c7912000-06-30 16:39:27 +000078def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
Tim Petersc5000df2002-06-02 21:42:01 +000079 exclude=0, single=0, randomize=0, fromfile=None, findleaks=0,
Barry Warsaw08fca522001-08-20 22:33:46 +000080 use_resources=None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +000081 """Execute a test suite.
82
Thomas Wouters7e474022000-07-16 12:04:32 +000083 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +000084 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000085
86 tests -- a list of strings containing test names (optional)
87 testdir -- the directory in which to look for tests (optional)
88
89 Users other than the Python test suite will certainly want to
90 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +000091 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000092
93 If the tests argument is omitted, the tests listed on the
94 command-line will be used. If that's empty, too, then all *.py
95 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +000096
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000097 The other default arguments (verbose, quiet, generate, exclude,
98 single, randomize, findleaks, and use_resources) allow programmers
99 calling main() directly to set the values that would normally be
100 set by flags on the command line.
Barry Warsawa873b032000-08-03 15:50:37 +0000101
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000102 """
Fred Drake004d5e62000-10-23 17:22:08 +0000103
Tim Peters8dee8092001-09-25 20:05:11 +0000104 test_support.record_original_stdout(sys.stdout)
Guido van Rossum152494a1996-12-20 03:12:20 +0000105 try:
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000106 opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsrf:lu:t:',
Barry Warsaw08fca522001-08-20 22:33:46 +0000107 ['help', 'verbose', 'quiet', 'generate',
Tim Petersc5000df2002-06-02 21:42:01 +0000108 'exclude', 'single', 'random', 'fromfile',
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000109 'findleaks', 'use=', 'threshold='])
Guido van Rossum152494a1996-12-20 03:12:20 +0000110 except getopt.error, msg:
Barry Warsaw08fca522001-08-20 22:33:46 +0000111 usage(2, msg)
112
113 # Defaults
114 if use_resources is None:
115 use_resources = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000116 for o, a in opts:
Barry Warsaw08fca522001-08-20 22:33:46 +0000117 if o in ('-h', '--help'):
118 usage(0)
119 elif o in ('-v', '--verbose'):
120 verbose += 1
121 elif o in ('-q', '--quiet'):
122 quiet = 1;
123 verbose = 0
124 elif o in ('-g', '--generate'):
125 generate = 1
126 elif o in ('-x', '--exclude'):
127 exclude = 1
128 elif o in ('-s', '--single'):
129 single = 1
130 elif o in ('-r', '--randomize'):
131 randomize = 1
Tim Petersc5000df2002-06-02 21:42:01 +0000132 elif o in ('-f', '--fromfile'):
133 fromfile = a
Barry Warsaw08fca522001-08-20 22:33:46 +0000134 elif o in ('-l', '--findleaks'):
135 findleaks = 1
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000136 elif o in ('-t', '--threshold'):
137 import gc
138 gc.set_threshold(int(a))
Barry Warsaw08fca522001-08-20 22:33:46 +0000139 elif o in ('-u', '--use'):
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000140 u = [x.lower() for x in a.split(',')]
141 for r in u:
Fred Drake3a15dac2002-04-11 16:39:16 +0000142 if r == 'all':
143 use_resources = RESOURCE_NAMES
144 break
145 if r not in RESOURCE_NAMES:
146 usage(1, 'Invalid -u/--use option: ' + a)
Fred Drake04a8da52002-04-11 20:58:54 +0000147 if r not in use_resources:
Andrew MacIntyree41abab2002-04-30 12:11:04 +0000148 use_resources.append(r)
Guido van Rossuma4122201997-08-18 20:08:24 +0000149 if generate and verbose:
Barry Warsaw08fca522001-08-20 22:33:46 +0000150 usage(2, "-g and -v don't go together!")
Tim Petersc5000df2002-06-02 21:42:01 +0000151 if single and fromfile:
152 usage(2, "-s and -f don't go together!")
Barry Warsaw08fca522001-08-20 22:33:46 +0000153
Guido van Rossum152494a1996-12-20 03:12:20 +0000154 good = []
155 bad = []
156 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000157
Neil Schemenauerd569f232000-09-22 15:29:28 +0000158 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000159 try:
160 import gc
161 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000162 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +0000163 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +0000164 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000165 # Uncomment the line below to report garbage that is not
166 # freeable by reference counting alone. By default only
167 # garbage that is not collectable by the GC is reported.
168 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000169 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000170
Barry Warsawe11e3de1999-01-28 19:51:51 +0000171 if single:
172 from tempfile import gettempdir
173 filename = os.path.join(gettempdir(), 'pynexttest')
174 try:
175 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000176 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000177 tests = [next]
178 fp.close()
179 except IOError:
180 pass
Tim Petersc5000df2002-06-02 21:42:01 +0000181
182 if fromfile:
183 tests = []
184 fp = open(fromfile)
185 for line in fp:
186 guts = line.split() # assuming no test has whitespace in its name
187 if guts and not guts[0].startswith('#'):
188 tests.extend(guts)
189 fp.close()
190
191 # Strip .py extensions.
192 if args:
193 args = map(removepy, args)
194 if tests:
195 tests = map(removepy, tests)
196
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000197 stdtests = STDTESTS[:]
198 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000199 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000200 for arg in args:
201 if arg in stdtests:
202 stdtests.remove(arg)
203 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000204 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000205 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000206 if single:
207 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000208 if randomize:
209 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000210 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000211 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000212 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000213 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000214 if not quiet:
215 print test
Trent Mickf29f47b2000-08-11 19:02:59 +0000216 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000217 if ok > 0:
218 good.append(test)
219 elif ok == 0:
220 bad.append(test)
221 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000222 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000223 if findleaks:
224 gc.collect()
225 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000226 print "Warning: test created", len(gc.garbage),
227 print "uncollectable object(s)."
228 # move the uncollectable objects somewhere so we don't see
229 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000230 found_garbage.extend(gc.garbage)
231 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000232 # Unload the newly imported modules (best effort finalization)
233 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000234 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000235 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000236
237 # The lists won't be sorted if running with -r
238 good.sort()
239 bad.sort()
240 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000241
Guido van Rossum152494a1996-12-20 03:12:20 +0000242 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000243 if not bad and not skipped and len(good) > 1:
244 print "All",
245 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000246 if verbose:
247 print "CAUTION: stdout isn't compared in verbose mode: a test"
248 print "that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000249 if bad:
Tim Petersa45da922001-08-12 03:45:50 +0000250 print count(len(bad), "test"), "failed:"
251 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000252 if skipped and not quiet:
Tim Petersa45da922001-08-12 03:45:50 +0000253 print count(len(skipped), "test"), "skipped:"
254 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000255
Tim Petersb5b7b782001-08-12 01:20:39 +0000256 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000257 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000258 if e.isvalid():
259 surprise = _Set(skipped) - e.getexpected()
Tim Petersb5b7b782001-08-12 01:20:39 +0000260 if surprise:
261 print count(len(surprise), "skip"), \
Tim Petersa45da922001-08-12 03:45:50 +0000262 "unexpected on", plat + ":"
263 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000264 else:
265 print "Those skips are all expected on", plat + "."
266 else:
267 print "Ask someone to teach regrtest.py about which tests are"
268 print "expected to get skipped on", plat + "."
269
Barry Warsawe11e3de1999-01-28 19:51:51 +0000270 if single:
271 alltests = findtests(testdir, stdtests, nottests)
272 for i in range(len(alltests)):
273 if tests[0] == alltests[i]:
274 if i == len(alltests) - 1:
275 os.unlink(filename)
276 else:
277 fp = open(filename, 'w')
278 fp.write(alltests[i+1] + '\n')
279 fp.close()
280 break
281 else:
282 os.unlink(filename)
283
Barry Warsaw08fca522001-08-20 22:33:46 +0000284 sys.exit(len(bad) > 0)
285
Guido van Rossum152494a1996-12-20 03:12:20 +0000286
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000287STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000288 'test_grammar',
289 'test_opcodes',
290 'test_operations',
291 'test_builtin',
292 'test_exceptions',
293 'test_types',
294 ]
295
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000296NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000297 'test_support',
298 'test_b1',
299 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000300 'test_future1',
301 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000302 'test_future3',
Guido van Rossum152494a1996-12-20 03:12:20 +0000303 ]
304
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000305def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000306 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000307 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000308 names = os.listdir(testdir)
309 tests = []
310 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000311 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000312 modname = name[:-3]
313 if modname not in stdtests and modname not in nottests:
314 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000315 tests.sort()
316 return stdtests + tests
317
Trent Mickf29f47b2000-08-11 19:02:59 +0000318def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000319 """Run a single test.
320 test -- the name of the test
321 generate -- if true, generate output, instead of running the test
322 and comparing it to a previously created output file
323 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000324 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000325 testdir -- test directory
326 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000327 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000328 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000329 outputdir = os.path.join(testdir, "output")
330 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000331 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000332 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000333 else:
Fred Drake88a56852001-09-28 20:16:30 +0000334 cfp = StringIO.StringIO()
Guido van Rossum152494a1996-12-20 03:12:20 +0000335 try:
Tim Peters342ca752001-09-25 19:13:20 +0000336 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000337 try:
338 if cfp:
339 sys.stdout = cfp
340 print test # Output file starts with test name
Tim Petersd9742212001-05-22 18:28:25 +0000341 the_module = __import__(test, globals(), locals(), [])
342 # Most tests run to completion simply as a side-effect of
343 # being imported. For the benefit of tests that can't run
344 # that way (like test_threaded_import), explicitly invoke
345 # their test_main() function (if it exists).
346 indirect_test = getattr(the_module, "test_main", None)
347 if indirect_test is not None:
348 indirect_test()
Guido van Rossum41360a41998-03-26 19:42:58 +0000349 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000350 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000351 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000352 if not quiet:
Guido van Rossumeb949052001-09-18 20:34:19 +0000353 print "test", test, "skipped --", msg
Guido van Rossum41360a41998-03-26 19:42:58 +0000354 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000355 except KeyboardInterrupt:
356 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000357 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000358 print "test", test, "failed --", msg
359 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000360 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000361 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000362 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum41360a41998-03-26 19:42:58 +0000363 if verbose:
364 traceback.print_exc(file=sys.stdout)
365 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000366 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000367 if not cfp:
368 return 1
369 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000370 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000371 if output == test + "\n":
372 if os.path.exists(outputfile):
373 # Write it since it already exists (and the contents
374 # may have changed), but let the user know it isn't
375 # needed:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000376 print "output file", outputfile, \
377 "is no longer needed; consider removing it"
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000378 else:
379 # We don't need it, so don't create it.
380 return 1
381 fp = open(outputfile, "w")
382 fp.write(output)
383 fp.close()
384 return 1
385 if os.path.exists(outputfile):
386 fp = open(outputfile, "r")
387 expected = fp.read()
388 fp.close()
389 else:
390 expected = test + "\n"
391 if output == expected:
392 return 1
393 print "test", test, "produced unexpected output:"
394 reportdiff(expected, output)
395 return 0
396
397def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000398 import difflib
Tim Petersc377b162001-09-22 05:31:03 +0000399 print "*" * 70
400 a = expected.splitlines(1)
401 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000402 sm = difflib.SequenceMatcher(a=a, b=b)
403 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000404
Guido van Rossumcf691932001-09-21 21:06:22 +0000405 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000406 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000407 x0 += 1
408 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000409 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000410 else:
Tim Petersc377b162001-09-22 05:31:03 +0000411 return "lines %d-%d" % (x0, x1)
412
Guido van Rossumcf691932001-09-21 21:06:22 +0000413 for op, a0, a1, b0, b1 in tuples:
414 if op == 'equal':
415 pass
Tim Petersc377b162001-09-22 05:31:03 +0000416
Guido van Rossumcf691932001-09-21 21:06:22 +0000417 elif op == 'delete':
Tim Petersc377b162001-09-22 05:31:03 +0000418 print "***", pair(a0, a1), "of expected output missing:"
Guido van Rossumcf691932001-09-21 21:06:22 +0000419 for line in a[a0:a1]:
Tim Petersc377b162001-09-22 05:31:03 +0000420 print "-", line,
421
Guido van Rossumcf691932001-09-21 21:06:22 +0000422 elif op == 'replace':
Tim Petersc377b162001-09-22 05:31:03 +0000423 print "*** mismatch between", pair(a0, a1), "of expected", \
424 "output and", pair(b0, b1), "of actual output:"
425 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
426 print line,
427
Guido van Rossumcf691932001-09-21 21:06:22 +0000428 elif op == 'insert':
Tim Petersc377b162001-09-22 05:31:03 +0000429 print "***", pair(b0, b1), "of actual output doesn't appear", \
430 "in expected output after line", str(a1)+":"
Guido van Rossumcf691932001-09-21 21:06:22 +0000431 for line in b[b0:b1]:
Tim Petersc377b162001-09-22 05:31:03 +0000432 print "+", line,
433
Guido van Rossumcf691932001-09-21 21:06:22 +0000434 else:
435 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
Tim Petersc377b162001-09-22 05:31:03 +0000436
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000437 print "*" * 70
Guido van Rossum152494a1996-12-20 03:12:20 +0000438
439def findtestdir():
440 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000441 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000442 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000443 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000444 testdir = os.path.dirname(file) or os.curdir
445 return testdir
446
Tim Petersc5000df2002-06-02 21:42:01 +0000447def removepy(name):
448 if name.endswith(os.extsep + "py"):
449 name = name[:-3]
450 return name
451
Guido van Rossum152494a1996-12-20 03:12:20 +0000452def count(n, word):
453 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000454 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000455 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000456 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000457
Tim Petersa45da922001-08-12 03:45:50 +0000458def printlist(x, width=70, indent=4):
459 """Print the elements of a sequence to stdout.
460
461 Optional arg width (default 70) is the maximum line length.
462 Optional arg indent (default 4) is the number of blanks with which to
463 begin each line.
464 """
465
Tim Petersba78bc42002-07-04 19:45:06 +0000466 from textwrap import fill
467 blanks = ' ' * indent
468 print fill(' '.join(map(str, x)), width,
469 initial_indent=blanks, subsequent_indent=blanks)
Tim Petersa45da922001-08-12 03:45:50 +0000470
Tim Petersb5b7b782001-08-12 01:20:39 +0000471class _Set:
472 def __init__(self, seq=[]):
473 data = self.data = {}
474 for x in seq:
475 data[x] = 1
476
477 def __len__(self):
478 return len(self.data)
479
480 def __sub__(self, other):
481 "Return set of all elements in self not in other."
482 result = _Set()
483 data = result.data = self.data.copy()
484 for x in other.data:
485 if x in data:
486 del data[x]
487 return result
488
Jeremy Hylton39f77bc2001-08-12 21:53:08 +0000489 def __iter__(self):
490 return iter(self.data)
491
Tim Petersb5b7b782001-08-12 01:20:39 +0000492 def tolist(self, sorted=1):
493 "Return _Set elements as a list."
494 data = self.data.keys()
495 if sorted:
496 data.sort()
497 return data
498
Tim Petersde14a302002-04-01 05:04:46 +0000499# Map sys.platform to a string containing the basenames of tests
500# expected to be skipped on that platform.
501
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000502_expectations = {
503 'win32':
504 """
505 test_al
506 test_cd
507 test_cl
508 test_commands
509 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000510 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000511 test_dbm
512 test_dl
Tim Petersdeb121a2002-04-11 19:52:58 +0000513 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000514 test_fcntl
515 test_fork1
516 test_gdbm
517 test_gl
518 test_grp
519 test_imgfile
520 test_largefile
521 test_linuxaudiodev
522 test_mhlib
Tim Petersde14a302002-04-01 05:04:46 +0000523 test_mpz
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000524 test_nis
525 test_openpty
526 test_poll
527 test_pty
528 test_pwd
Tim Peters1e33ffa2002-04-23 23:09:02 +0000529 test_resource
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000530 test_signal
Barry Warsaw08fca522001-08-20 22:33:46 +0000531 test_socket_ssl
Tim Petersa86f0c12001-09-18 02:18:57 +0000532 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000533 test_sunaudiodev
534 test_timing
535 """,
536 'linux2':
537 """
538 test_al
539 test_cd
540 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000541 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000542 test_dl
Guido van Rossum6184c112002-04-16 02:14:04 +0000543 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000544 test_gl
545 test_imgfile
546 test_largefile
547 test_nis
548 test_ntpath
Barry Warsaw08fca522001-08-20 22:33:46 +0000549 test_socket_ssl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000550 test_socketserver
551 test_sunaudiodev
552 test_unicode_file
553 test_winreg
554 test_winsound
555 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000556 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000557 """
558 test_al
559 test_bsddb
560 test_cd
561 test_cl
562 test_commands
563 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000564 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000565 test_dbm
566 test_dl
567 test_fcntl
568 test_fork1
569 test_gl
570 test_grp
571 test_imgfile
572 test_largefile
573 test_linuxaudiodev
574 test_locale
575 test_mmap
576 test_nis
577 test_ntpath
578 test_openpty
579 test_poll
580 test_popen2
581 test_pty
582 test_pwd
583 test_signal
584 test_socket_ssl
585 test_socketserver
586 test_sunaudiodev
587 test_sundry
588 test_timing
589 test_unicode_file
590 test_winreg
591 test_winsound
592 """,
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000593 'unixware5':
594 """
595 test_al
596 test_bsddb
597 test_cd
598 test_cl
599 test_dl
600 test_gl
601 test_imgfile
602 test_largefile
603 test_linuxaudiodev
604 test_minidom
605 test_nis
606 test_ntpath
607 test_openpty
608 test_pyexpat
609 test_sax
610 test_socketserver
611 test_sunaudiodev
612 test_sundry
613 test_unicode_file
614 test_winreg
615 test_winsound
616 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000617 'riscos':
618 """
619 test_al
620 test_asynchat
621 test_bsddb
622 test_cd
623 test_cl
624 test_commands
625 test_crypt
626 test_dbm
627 test_dl
628 test_fcntl
629 test_fork1
630 test_gdbm
631 test_gl
632 test_grp
633 test_imgfile
634 test_largefile
635 test_linuxaudiodev
636 test_locale
637 test_mmap
638 test_nis
639 test_ntpath
640 test_openpty
641 test_poll
642 test_popen2
643 test_pty
644 test_pwd
645 test_socket_ssl
646 test_socketserver
647 test_strop
648 test_sunaudiodev
649 test_sundry
650 test_thread
651 test_threaded_import
652 test_threadedtempfile
653 test_threading
654 test_timing
655 test_unicode_file
656 test_winreg
657 test_winsound
658 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000659 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000660 """
661 test_al
662 test_cd
663 test_cl
664 test_curses
665 test_dl
666 test_gdbm
667 test_gl
668 test_imgfile
669 test_largefile
670 test_linuxaudiodev
671 test_minidom
672 test_nis
673 test_ntpath
674 test_poll
675 test_socket_ssl
Jack Jansenf839c272001-12-14 21:28:53 +0000676 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000677 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000678 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000679 test_winreg
680 test_winsound
681 """,
Guido van Rossum11c3f092002-07-17 15:08:24 +0000682 'sunos5':
683 """
684 test_al
685 test_bsddb
686 test_cd
687 test_cl
688 test_curses
689 test_dbm
690 test_email_codecs
691 test_gdbm
692 test_gl
693 test_gzip
694 test_imgfile
695 test_linuxaudiodev
696 test_mpz
697 test_openpty
698 test_socket_ssl
699 test_socketserver
700 test_winreg
701 test_winsound
702 test_zipfile
703 test_zlib
Jeremy Hyltoned375e12002-07-17 15:56:55 +0000704 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000705 'hp-ux11':
706 """
707 test_al
708 test_bsddb
709 test_cd
710 test_cl
711 test_curses
712 test_dl
713 test_gdbm
714 test_gl
715 test_gzip
716 test_imgfile
717 test_largefile
718 test_linuxaudiodev
719 test_locale
720 test_minidom
721 test_nis
722 test_ntpath
723 test_openpty
724 test_pyexpat
725 test_sax
726 test_socket_ssl
727 test_socketserver
728 test_sunaudiodev
729 test_unicode_file
730 test_winreg
731 test_winsound
732 test_zipfile
733 test_zlib
734 """,
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000735 'atheos':
Tim Petersc411dba2002-07-16 21:35:23 +0000736 """
737 test_al
738 test_cd
739 test_cl
740 test_curses
741 test_dl
742 test_email_codecs
743 test_gdbm
744 test_gl
745 test_imgfile
746 test_largefile
747 test_linuxaudiodev
748 test_locale
749 test_mhlib
750 test_mmap
751 test_mpz
752 test_nis
753 test_poll
754 test_popen2
755 test_resource
756 test_socket_ssl
757 test_socketserver
758 test_sunaudiodev
759 test_unicode_file
760 test_winreg
761 test_winsound
762 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000763}
764
Tim Petersb5b7b782001-08-12 01:20:39 +0000765class _ExpectedSkips:
766 def __init__(self):
767 self.valid = 0
Tim Petersde14a302002-04-01 05:04:46 +0000768 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000769 s = _expectations[sys.platform]
Tim Petersb5b7b782001-08-12 01:20:39 +0000770 self.expected = _Set(s.split())
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000771 self.valid = 1
Tim Petersb5b7b782001-08-12 01:20:39 +0000772
773 def isvalid(self):
774 "Return true iff _ExpectedSkips knows about the current platform."
775 return self.valid
776
777 def getexpected(self):
778 """Return set of test names we expect to skip on current platform.
779
780 self.isvalid() must be true.
781 """
782
783 assert self.isvalid()
784 return self.expected
785
Guido van Rossum152494a1996-12-20 03:12:20 +0000786if __name__ == '__main__':
Barry Warsaw08fca522001-08-20 22:33:46 +0000787 main()