blob: 3ae73b427386999c5029e6530e2a255354708b82 [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
67
68# I see no other way to suppress these warnings;
69# putting them in test_grammar.py has no effect:
Guido van Rossum88b1def2002-08-14 17:54:48 +000070warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
Guido van Rossumdc15c272002-08-12 21:55:51 +000071 ".*test.test_grammar$")
Guido van Rossum152494a1996-12-20 03:12:20 +000072
Barry Warsaw04f357c2002-07-23 19:04:11 +000073from test import test_support
Fred Drake3a15dac2002-04-11 16:39:16 +000074
75RESOURCE_NAMES = ('curses', 'largefile', 'network')
76
77
Barry Warsaw08fca522001-08-20 22:33:46 +000078def usage(code, msg=''):
79 print __doc__
80 if msg: print msg
81 sys.exit(code)
82
83
Skip Montanaroab1c7912000-06-30 16:39:27 +000084def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
Tim Petersc5000df2002-06-02 21:42:01 +000085 exclude=0, single=0, randomize=0, fromfile=None, findleaks=0,
Barry Warsaw08fca522001-08-20 22:33:46 +000086 use_resources=None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +000087 """Execute a test suite.
88
Thomas Wouters7e474022000-07-16 12:04:32 +000089 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +000090 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000091
92 tests -- a list of strings containing test names (optional)
93 testdir -- the directory in which to look for tests (optional)
94
95 Users other than the Python test suite will certainly want to
96 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +000097 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000098
99 If the tests argument is omitted, the tests listed on the
100 command-line will be used. If that's empty, too, then all *.py
101 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +0000102
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000103 The other default arguments (verbose, quiet, generate, exclude,
104 single, randomize, findleaks, and use_resources) allow programmers
105 calling main() directly to set the values that would normally be
106 set by flags on the command line.
Barry Warsawa873b032000-08-03 15:50:37 +0000107
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000108 """
Fred Drake004d5e62000-10-23 17:22:08 +0000109
Tim Peters8dee8092001-09-25 20:05:11 +0000110 test_support.record_original_stdout(sys.stdout)
Guido van Rossum152494a1996-12-20 03:12:20 +0000111 try:
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000112 opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsrf:lu:t:',
Barry Warsaw08fca522001-08-20 22:33:46 +0000113 ['help', 'verbose', 'quiet', 'generate',
Tim Petersc5000df2002-06-02 21:42:01 +0000114 'exclude', 'single', 'random', 'fromfile',
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000115 'findleaks', 'use=', 'threshold='])
Guido van Rossum152494a1996-12-20 03:12:20 +0000116 except getopt.error, msg:
Barry Warsaw08fca522001-08-20 22:33:46 +0000117 usage(2, msg)
118
119 # Defaults
120 if use_resources is None:
121 use_resources = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000122 for o, a in opts:
Barry Warsaw08fca522001-08-20 22:33:46 +0000123 if o in ('-h', '--help'):
124 usage(0)
125 elif o in ('-v', '--verbose'):
126 verbose += 1
127 elif o in ('-q', '--quiet'):
128 quiet = 1;
129 verbose = 0
130 elif o in ('-g', '--generate'):
131 generate = 1
132 elif o in ('-x', '--exclude'):
133 exclude = 1
134 elif o in ('-s', '--single'):
135 single = 1
136 elif o in ('-r', '--randomize'):
137 randomize = 1
Tim Petersc5000df2002-06-02 21:42:01 +0000138 elif o in ('-f', '--fromfile'):
139 fromfile = a
Barry Warsaw08fca522001-08-20 22:33:46 +0000140 elif o in ('-l', '--findleaks'):
141 findleaks = 1
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000142 elif o in ('-t', '--threshold'):
143 import gc
144 gc.set_threshold(int(a))
Barry Warsaw08fca522001-08-20 22:33:46 +0000145 elif o in ('-u', '--use'):
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000146 u = [x.lower() for x in a.split(',')]
147 for r in u:
Fred Drake3a15dac2002-04-11 16:39:16 +0000148 if r == 'all':
149 use_resources = RESOURCE_NAMES
150 break
151 if r not in RESOURCE_NAMES:
152 usage(1, 'Invalid -u/--use option: ' + a)
Fred Drake04a8da52002-04-11 20:58:54 +0000153 if r not in use_resources:
Andrew MacIntyree41abab2002-04-30 12:11:04 +0000154 use_resources.append(r)
Guido van Rossuma4122201997-08-18 20:08:24 +0000155 if generate and verbose:
Barry Warsaw08fca522001-08-20 22:33:46 +0000156 usage(2, "-g and -v don't go together!")
Tim Petersc5000df2002-06-02 21:42:01 +0000157 if single and fromfile:
158 usage(2, "-s and -f don't go together!")
Barry Warsaw08fca522001-08-20 22:33:46 +0000159
Guido van Rossum152494a1996-12-20 03:12:20 +0000160 good = []
161 bad = []
162 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000163
Neil Schemenauerd569f232000-09-22 15:29:28 +0000164 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000165 try:
166 import gc
167 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000168 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +0000169 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +0000170 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000171 # Uncomment the line below to report garbage that is not
172 # freeable by reference counting alone. By default only
173 # garbage that is not collectable by the GC is reported.
174 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000175 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000176
Barry Warsawe11e3de1999-01-28 19:51:51 +0000177 if single:
178 from tempfile import gettempdir
179 filename = os.path.join(gettempdir(), 'pynexttest')
180 try:
181 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000182 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000183 tests = [next]
184 fp.close()
185 except IOError:
186 pass
Tim Petersc5000df2002-06-02 21:42:01 +0000187
188 if fromfile:
189 tests = []
190 fp = open(fromfile)
191 for line in fp:
192 guts = line.split() # assuming no test has whitespace in its name
193 if guts and not guts[0].startswith('#'):
194 tests.extend(guts)
195 fp.close()
196
197 # Strip .py extensions.
198 if args:
199 args = map(removepy, args)
200 if tests:
201 tests = map(removepy, tests)
202
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000203 stdtests = STDTESTS[:]
204 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000205 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000206 for arg in args:
207 if arg in stdtests:
208 stdtests.remove(arg)
209 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000210 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000211 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000212 if single:
213 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000214 if randomize:
215 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000216 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000217 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000218 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000219 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000220 if not quiet:
221 print test
Trent Mickf29f47b2000-08-11 19:02:59 +0000222 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000223 if ok > 0:
224 good.append(test)
225 elif ok == 0:
226 bad.append(test)
227 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000228 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000229 if findleaks:
230 gc.collect()
231 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000232 print "Warning: test created", len(gc.garbage),
233 print "uncollectable object(s)."
234 # move the uncollectable objects somewhere so we don't see
235 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000236 found_garbage.extend(gc.garbage)
237 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000238 # Unload the newly imported modules (best effort finalization)
239 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000240 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000241 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000242
243 # The lists won't be sorted if running with -r
244 good.sort()
245 bad.sort()
246 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000247
Guido van Rossum152494a1996-12-20 03:12:20 +0000248 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000249 if not bad and not skipped and len(good) > 1:
250 print "All",
251 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000252 if verbose:
Barry Warsaw408b6d32002-07-30 23:27:12 +0000253 print "CAUTION: stdout isn't compared in verbose mode:"
254 print "a test that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000255 if bad:
Tim Petersa45da922001-08-12 03:45:50 +0000256 print count(len(bad), "test"), "failed:"
257 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000258 if skipped and not quiet:
Tim Petersa45da922001-08-12 03:45:50 +0000259 print count(len(skipped), "test"), "skipped:"
260 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000261
Tim Petersb5b7b782001-08-12 01:20:39 +0000262 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000263 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000264 if e.isvalid():
265 surprise = _Set(skipped) - e.getexpected()
Tim Petersb5b7b782001-08-12 01:20:39 +0000266 if surprise:
267 print count(len(surprise), "skip"), \
Tim Petersa45da922001-08-12 03:45:50 +0000268 "unexpected on", plat + ":"
269 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000270 else:
271 print "Those skips are all expected on", plat + "."
272 else:
273 print "Ask someone to teach regrtest.py about which tests are"
274 print "expected to get skipped on", plat + "."
275
Barry Warsawe11e3de1999-01-28 19:51:51 +0000276 if single:
277 alltests = findtests(testdir, stdtests, nottests)
278 for i in range(len(alltests)):
279 if tests[0] == alltests[i]:
280 if i == len(alltests) - 1:
281 os.unlink(filename)
282 else:
283 fp = open(filename, 'w')
284 fp.write(alltests[i+1] + '\n')
285 fp.close()
286 break
287 else:
288 os.unlink(filename)
289
Barry Warsaw08fca522001-08-20 22:33:46 +0000290 sys.exit(len(bad) > 0)
291
Guido van Rossum152494a1996-12-20 03:12:20 +0000292
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000293STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000294 'test_grammar',
295 'test_opcodes',
296 'test_operations',
297 'test_builtin',
298 'test_exceptions',
299 'test_types',
300 ]
301
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000302NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000303 'test_support',
304 'test_b1',
305 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000306 'test_future1',
307 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000308 'test_future3',
Guido van Rossum152494a1996-12-20 03:12:20 +0000309 ]
310
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000311def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000312 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000313 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000314 names = os.listdir(testdir)
315 tests = []
316 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000317 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000318 modname = name[:-3]
319 if modname not in stdtests and modname not in nottests:
320 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000321 tests.sort()
322 return stdtests + tests
323
Trent Mickf29f47b2000-08-11 19:02:59 +0000324def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000325 """Run a single test.
326 test -- the name of the test
327 generate -- if true, generate output, instead of running the test
328 and comparing it to a previously created output file
329 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000330 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000331 testdir -- test directory
332 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000333 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000334 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000335 outputdir = os.path.join(testdir, "output")
336 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000337 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000338 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000339 else:
Fred Drake88a56852001-09-28 20:16:30 +0000340 cfp = StringIO.StringIO()
Guido van Rossum152494a1996-12-20 03:12:20 +0000341 try:
Tim Peters342ca752001-09-25 19:13:20 +0000342 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000343 try:
344 if cfp:
345 sys.stdout = cfp
346 print test # Output file starts with test name
Barry Warsaw408b6d32002-07-30 23:27:12 +0000347 if test.startswith('test.'):
348 abstest = test
349 else:
350 # Always import it from the test package
351 abstest = 'test.' + test
352 the_package = __import__(abstest, globals(), locals(), [])
353 the_module = getattr(the_package, test)
Tim Petersd9742212001-05-22 18:28:25 +0000354 # Most tests run to completion simply as a side-effect of
355 # being imported. For the benefit of tests that can't run
356 # that way (like test_threaded_import), explicitly invoke
357 # their test_main() function (if it exists).
358 indirect_test = getattr(the_module, "test_main", None)
359 if indirect_test is not None:
360 indirect_test()
Guido van Rossum41360a41998-03-26 19:42:58 +0000361 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000362 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000363 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000364 if not quiet:
Guido van Rossumeb949052001-09-18 20:34:19 +0000365 print "test", test, "skipped --", msg
Guido van Rossum41360a41998-03-26 19:42:58 +0000366 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000367 except KeyboardInterrupt:
368 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000369 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000370 print "test", test, "failed --", msg
371 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000372 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000373 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000374 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum41360a41998-03-26 19:42:58 +0000375 if verbose:
376 traceback.print_exc(file=sys.stdout)
377 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000378 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000379 if not cfp:
380 return 1
381 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000382 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000383 if output == test + "\n":
384 if os.path.exists(outputfile):
385 # Write it since it already exists (and the contents
386 # may have changed), but let the user know it isn't
387 # needed:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000388 print "output file", outputfile, \
389 "is no longer needed; consider removing it"
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000390 else:
391 # We don't need it, so don't create it.
392 return 1
393 fp = open(outputfile, "w")
394 fp.write(output)
395 fp.close()
396 return 1
397 if os.path.exists(outputfile):
398 fp = open(outputfile, "r")
399 expected = fp.read()
400 fp.close()
401 else:
402 expected = test + "\n"
403 if output == expected:
404 return 1
405 print "test", test, "produced unexpected output:"
406 reportdiff(expected, output)
407 return 0
408
409def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000410 import difflib
Tim Petersc377b162001-09-22 05:31:03 +0000411 print "*" * 70
412 a = expected.splitlines(1)
413 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000414 sm = difflib.SequenceMatcher(a=a, b=b)
415 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000416
Guido van Rossumcf691932001-09-21 21:06:22 +0000417 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000418 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000419 x0 += 1
420 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000421 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000422 else:
Tim Petersc377b162001-09-22 05:31:03 +0000423 return "lines %d-%d" % (x0, x1)
424
Guido van Rossumcf691932001-09-21 21:06:22 +0000425 for op, a0, a1, b0, b1 in tuples:
426 if op == 'equal':
427 pass
Tim Petersc377b162001-09-22 05:31:03 +0000428
Guido van Rossumcf691932001-09-21 21:06:22 +0000429 elif op == 'delete':
Tim Petersc377b162001-09-22 05:31:03 +0000430 print "***", pair(a0, a1), "of expected output missing:"
Guido van Rossumcf691932001-09-21 21:06:22 +0000431 for line in a[a0:a1]:
Tim Petersc377b162001-09-22 05:31:03 +0000432 print "-", line,
433
Guido van Rossumcf691932001-09-21 21:06:22 +0000434 elif op == 'replace':
Tim Petersc377b162001-09-22 05:31:03 +0000435 print "*** mismatch between", pair(a0, a1), "of expected", \
436 "output and", pair(b0, b1), "of actual output:"
437 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
438 print line,
439
Guido van Rossumcf691932001-09-21 21:06:22 +0000440 elif op == 'insert':
Tim Petersc377b162001-09-22 05:31:03 +0000441 print "***", pair(b0, b1), "of actual output doesn't appear", \
442 "in expected output after line", str(a1)+":"
Guido van Rossumcf691932001-09-21 21:06:22 +0000443 for line in b[b0:b1]:
Tim Petersc377b162001-09-22 05:31:03 +0000444 print "+", line,
445
Guido van Rossumcf691932001-09-21 21:06:22 +0000446 else:
447 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
Tim Petersc377b162001-09-22 05:31:03 +0000448
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000449 print "*" * 70
Guido van Rossum152494a1996-12-20 03:12:20 +0000450
451def findtestdir():
452 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000453 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000454 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000455 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000456 testdir = os.path.dirname(file) or os.curdir
457 return testdir
458
Tim Petersc5000df2002-06-02 21:42:01 +0000459def removepy(name):
460 if name.endswith(os.extsep + "py"):
461 name = name[:-3]
462 return name
463
Guido van Rossum152494a1996-12-20 03:12:20 +0000464def count(n, word):
465 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000466 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000467 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000468 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000469
Tim Petersa45da922001-08-12 03:45:50 +0000470def printlist(x, width=70, indent=4):
471 """Print the elements of a sequence to stdout.
472
473 Optional arg width (default 70) is the maximum line length.
474 Optional arg indent (default 4) is the number of blanks with which to
475 begin each line.
476 """
477
Tim Petersba78bc42002-07-04 19:45:06 +0000478 from textwrap import fill
479 blanks = ' ' * indent
480 print fill(' '.join(map(str, x)), width,
481 initial_indent=blanks, subsequent_indent=blanks)
Tim Petersa45da922001-08-12 03:45:50 +0000482
Tim Petersb5b7b782001-08-12 01:20:39 +0000483class _Set:
484 def __init__(self, seq=[]):
485 data = self.data = {}
486 for x in seq:
487 data[x] = 1
488
489 def __len__(self):
490 return len(self.data)
491
492 def __sub__(self, other):
493 "Return set of all elements in self not in other."
494 result = _Set()
495 data = result.data = self.data.copy()
496 for x in other.data:
497 if x in data:
498 del data[x]
499 return result
500
Jeremy Hylton39f77bc2001-08-12 21:53:08 +0000501 def __iter__(self):
502 return iter(self.data)
503
Tim Petersb5b7b782001-08-12 01:20:39 +0000504 def tolist(self, sorted=1):
505 "Return _Set elements as a list."
506 data = self.data.keys()
507 if sorted:
508 data.sort()
509 return data
510
Tim Petersde14a302002-04-01 05:04:46 +0000511# Map sys.platform to a string containing the basenames of tests
512# expected to be skipped on that platform.
513
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000514_expectations = {
515 'win32':
516 """
517 test_al
518 test_cd
519 test_cl
520 test_commands
521 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000522 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000523 test_dbm
524 test_dl
Tim Petersdeb121a2002-04-11 19:52:58 +0000525 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000526 test_fcntl
527 test_fork1
528 test_gdbm
529 test_gl
530 test_grp
531 test_imgfile
532 test_largefile
533 test_linuxaudiodev
534 test_mhlib
Tim Petersde14a302002-04-01 05:04:46 +0000535 test_mpz
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000536 test_nis
537 test_openpty
538 test_poll
539 test_pty
540 test_pwd
Tim Peters1e33ffa2002-04-23 23:09:02 +0000541 test_resource
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000542 test_signal
Barry Warsaw08fca522001-08-20 22:33:46 +0000543 test_socket_ssl
Tim Petersa86f0c12001-09-18 02:18:57 +0000544 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000545 test_sunaudiodev
546 test_timing
547 """,
548 'linux2':
549 """
550 test_al
551 test_cd
552 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000553 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000554 test_dl
Guido van Rossum6184c112002-04-16 02:14:04 +0000555 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000556 test_gl
557 test_imgfile
558 test_largefile
559 test_nis
560 test_ntpath
Barry Warsaw08fca522001-08-20 22:33:46 +0000561 test_socket_ssl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000562 test_socketserver
563 test_sunaudiodev
564 test_unicode_file
565 test_winreg
566 test_winsound
567 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000568 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000569 """
570 test_al
571 test_bsddb
572 test_cd
573 test_cl
574 test_commands
575 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000576 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000577 test_dbm
578 test_dl
579 test_fcntl
580 test_fork1
581 test_gl
582 test_grp
583 test_imgfile
584 test_largefile
585 test_linuxaudiodev
586 test_locale
587 test_mmap
588 test_nis
589 test_ntpath
590 test_openpty
591 test_poll
592 test_popen2
593 test_pty
594 test_pwd
595 test_signal
596 test_socket_ssl
597 test_socketserver
598 test_sunaudiodev
599 test_sundry
600 test_timing
601 test_unicode_file
602 test_winreg
603 test_winsound
604 """,
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000605 'unixware5':
606 """
607 test_al
608 test_bsddb
609 test_cd
610 test_cl
611 test_dl
612 test_gl
613 test_imgfile
614 test_largefile
615 test_linuxaudiodev
616 test_minidom
617 test_nis
618 test_ntpath
619 test_openpty
620 test_pyexpat
621 test_sax
622 test_socketserver
623 test_sunaudiodev
624 test_sundry
625 test_unicode_file
626 test_winreg
627 test_winsound
628 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000629 'riscos':
630 """
631 test_al
632 test_asynchat
633 test_bsddb
634 test_cd
635 test_cl
636 test_commands
637 test_crypt
638 test_dbm
639 test_dl
640 test_fcntl
641 test_fork1
642 test_gdbm
643 test_gl
644 test_grp
645 test_imgfile
646 test_largefile
647 test_linuxaudiodev
648 test_locale
649 test_mmap
650 test_nis
651 test_ntpath
652 test_openpty
653 test_poll
654 test_popen2
655 test_pty
656 test_pwd
657 test_socket_ssl
658 test_socketserver
659 test_strop
660 test_sunaudiodev
661 test_sundry
662 test_thread
663 test_threaded_import
664 test_threadedtempfile
665 test_threading
666 test_timing
667 test_unicode_file
668 test_winreg
669 test_winsound
670 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000671 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000672 """
673 test_al
674 test_cd
675 test_cl
676 test_curses
677 test_dl
678 test_gdbm
679 test_gl
680 test_imgfile
681 test_largefile
682 test_linuxaudiodev
683 test_minidom
684 test_nis
685 test_ntpath
686 test_poll
687 test_socket_ssl
Jack Jansenf839c272001-12-14 21:28:53 +0000688 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000689 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000690 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000691 test_winreg
692 test_winsound
693 """,
Guido van Rossum11c3f092002-07-17 15:08:24 +0000694 'sunos5':
695 """
696 test_al
697 test_bsddb
698 test_cd
699 test_cl
700 test_curses
701 test_dbm
702 test_email_codecs
703 test_gdbm
704 test_gl
705 test_gzip
706 test_imgfile
707 test_linuxaudiodev
708 test_mpz
709 test_openpty
710 test_socket_ssl
711 test_socketserver
712 test_winreg
713 test_winsound
714 test_zipfile
715 test_zlib
Jeremy Hyltoned375e12002-07-17 15:56:55 +0000716 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000717 'hp-ux11':
718 """
719 test_al
720 test_bsddb
721 test_cd
722 test_cl
723 test_curses
724 test_dl
725 test_gdbm
726 test_gl
727 test_gzip
728 test_imgfile
729 test_largefile
730 test_linuxaudiodev
731 test_locale
732 test_minidom
733 test_nis
734 test_ntpath
735 test_openpty
736 test_pyexpat
737 test_sax
738 test_socket_ssl
739 test_socketserver
740 test_sunaudiodev
741 test_unicode_file
742 test_winreg
743 test_winsound
744 test_zipfile
745 test_zlib
746 """,
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000747 'atheos':
Tim Petersc411dba2002-07-16 21:35:23 +0000748 """
749 test_al
750 test_cd
751 test_cl
752 test_curses
753 test_dl
754 test_email_codecs
755 test_gdbm
756 test_gl
757 test_imgfile
758 test_largefile
759 test_linuxaudiodev
760 test_locale
761 test_mhlib
762 test_mmap
763 test_mpz
764 test_nis
765 test_poll
766 test_popen2
767 test_resource
768 test_socket_ssl
769 test_socketserver
770 test_sunaudiodev
771 test_unicode_file
772 test_winreg
773 test_winsound
774 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000775}
776
Tim Petersb5b7b782001-08-12 01:20:39 +0000777class _ExpectedSkips:
778 def __init__(self):
779 self.valid = 0
Tim Petersde14a302002-04-01 05:04:46 +0000780 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000781 s = _expectations[sys.platform]
Tim Petersb5b7b782001-08-12 01:20:39 +0000782 self.expected = _Set(s.split())
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000783 self.valid = 1
Tim Petersb5b7b782001-08-12 01:20:39 +0000784
785 def isvalid(self):
786 "Return true iff _ExpectedSkips knows about the current platform."
787 return self.valid
788
789 def getexpected(self):
790 """Return set of test names we expect to skip on current platform.
791
792 self.isvalid() must be true.
793 """
794
795 assert self.isvalid()
796 return self.expected
797
Guido van Rossum152494a1996-12-20 03:12:20 +0000798if __name__ == '__main__':
Barry Warsaw408b6d32002-07-30 23:27:12 +0000799 # Remove regrtest.py's own directory from the module search path. This
800 # prevents relative imports from working, and relative imports will screw
801 # up the testing framework. E.g. if both test.test_support and
802 # test_support are imported, they will not contain the same globals, and
803 # much of the testing framework relies on the globals in the
804 # test.test_support module.
805 mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
806 i = pathlen = len(sys.path)
807 while i >= 0:
808 i -= 1
809 if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
810 del sys.path[i]
811 if len(sys.path) == pathlen:
812 print 'Could not find %r in sys.path to remove it' % mydir
Barry Warsaw08fca522001-08-20 22:33:46 +0000813 main()