blob: c1740833286ecf9e4337c0b3e19ad02289a1c937 [file] [log] [blame]
Guido van Rossum152494a1996-12-20 03:12:20 +00001#! /usr/bin/env python
2
3"""Regression test.
4
5This will find all modules whose name is "test_*" in the test
6directory, and run them. Various command line options provide
7additional facilities.
8
9Command line options:
10
Barry Warsawa873b032000-08-03 15:50:37 +000011-v: verbose -- run tests in verbose mode with output to stdout
12-q: quiet -- don't print anything except if a test fails
13-g: generate -- write the output file for a test instead of comparing it
14-x: exclude -- arguments are tests to *exclude*
15-s: single -- run only a single test (see below)
16-r: random -- randomize test execution order
Tim Petersc5000df2002-06-02 21:42:01 +000017-f: fromfile -- read names of tests to run from a file (see below)
Neil Schemenauer8a00abc2000-10-13 01:32:42 +000018-l: findleaks -- if GC is available detect tests that leak memory
Barry Warsaw08fca522001-08-20 22:33:46 +000019-u: use -- specify which special resource intensive tests to run
20-h: help -- print this text and exit
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000021-t: threshold -- call gc.set_threshold(N)
Guido van Rossum152494a1996-12-20 03:12:20 +000022
23If non-option arguments are present, they are names for tests to run,
24unless -x is given, in which case they are names for tests not to run.
25If no test names are given, all tests are run.
Guido van Rossumf58ed251997-03-07 21:04:33 +000026
Guido van Rossuma4122201997-08-18 20:08:24 +000027-v is incompatible with -g and does not compare test output files.
Barry Warsawe11e3de1999-01-28 19:51:51 +000028
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000029-s means to run only a single test and exit. This is useful when
30doing memory analysis on the Python interpreter (which tend to consume
31too many resources to run the full regression test non-stop). The
32file /tmp/pynexttest is read to find the next test to run. If this
33file is missing, the first test_*.py file in testdir or on the command
34line is used. (actually tempfile.gettempdir() is used instead of
35/tmp).
Barry Warsawe11e3de1999-01-28 19:51:51 +000036
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000037-f reads the names of tests from the file given as f's argument, one
38or more test names per line. Whitespace is ignored. Blank lines and
39lines beginning with '#' are ignored. This is especially useful for
40whittling down failures involving interactions among tests.
Tim Petersc5000df2002-06-02 21:42:01 +000041
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000042-u is used to specify which special resource intensive tests to run,
43such as those requiring large file support or network connectivity.
44The argument is a comma-separated list of words indicating the
45resources to test. Currently only the following are defined:
Barry Warsaw08fca522001-08-20 22:33:46 +000046
Fred Drake3a15dac2002-04-11 16:39:16 +000047 all - Enable all special resources.
48
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000049 curses - Tests that use curses and will modify the terminal's
50 state and output modes.
Tim Peters1633a2e2001-10-30 05:56:40 +000051
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000052 largefile - It is okay to run some test that may create huge
53 files. These tests can take a long time and may
54 consume >2GB of disk space temporarily.
Barry Warsaw08fca522001-08-20 22:33:46 +000055
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000056 network - It is okay to run tests that use external network
57 resource, e.g. testing SSL support for sockets.
Guido van Rossum152494a1996-12-20 03:12:20 +000058"""
59
60import sys
Guido van Rossum152494a1996-12-20 03:12:20 +000061import os
62import getopt
Guido van Rossum9e48b271997-07-16 01:56:13 +000063import traceback
Skip Montanaroab1c7912000-06-30 16:39:27 +000064import random
Fred Drakeae1bb172001-05-21 21:08:12 +000065import StringIO
Guido van Rossumdc15c272002-08-12 21:55:51 +000066import warnings
Tim Peters7c7efe92002-08-23 17:55:54 +000067from sets import Set
Guido van Rossumdc15c272002-08-12 21:55:51 +000068
69# I see no other way to suppress these warnings;
70# putting them in test_grammar.py has no effect:
Guido van Rossum88b1def2002-08-14 17:54:48 +000071warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
Guido van Rossumdc15c272002-08-12 21:55:51 +000072 ".*test.test_grammar$")
Guido van Rossum152494a1996-12-20 03:12:20 +000073
Barry Warsaw04f357c2002-07-23 19:04:11 +000074from test import test_support
Fred Drake3a15dac2002-04-11 16:39:16 +000075
76RESOURCE_NAMES = ('curses', 'largefile', 'network')
77
78
Barry Warsaw08fca522001-08-20 22:33:46 +000079def usage(code, msg=''):
80 print __doc__
81 if msg: print msg
82 sys.exit(code)
83
84
Skip Montanaroab1c7912000-06-30 16:39:27 +000085def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
Tim Petersc5000df2002-06-02 21:42:01 +000086 exclude=0, single=0, randomize=0, fromfile=None, findleaks=0,
Barry Warsaw08fca522001-08-20 22:33:46 +000087 use_resources=None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +000088 """Execute a test suite.
89
Thomas Wouters7e474022000-07-16 12:04:32 +000090 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +000091 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000092
93 tests -- a list of strings containing test names (optional)
94 testdir -- the directory in which to look for tests (optional)
95
96 Users other than the Python test suite will certainly want to
97 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +000098 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000099
100 If the tests argument is omitted, the tests listed on the
101 command-line will be used. If that's empty, too, then all *.py
102 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +0000103
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000104 The other default arguments (verbose, quiet, generate, exclude,
105 single, randomize, findleaks, and use_resources) allow programmers
106 calling main() directly to set the values that would normally be
107 set by flags on the command line.
Barry Warsawa873b032000-08-03 15:50:37 +0000108
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000109 """
Fred Drake004d5e62000-10-23 17:22:08 +0000110
Tim Peters8dee8092001-09-25 20:05:11 +0000111 test_support.record_original_stdout(sys.stdout)
Guido van Rossum152494a1996-12-20 03:12:20 +0000112 try:
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000113 opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsrf:lu:t:',
Barry Warsaw08fca522001-08-20 22:33:46 +0000114 ['help', 'verbose', 'quiet', 'generate',
Tim Petersc5000df2002-06-02 21:42:01 +0000115 'exclude', 'single', 'random', 'fromfile',
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000116 'findleaks', 'use=', 'threshold='])
Guido van Rossum152494a1996-12-20 03:12:20 +0000117 except getopt.error, msg:
Barry Warsaw08fca522001-08-20 22:33:46 +0000118 usage(2, msg)
119
120 # Defaults
121 if use_resources is None:
122 use_resources = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000123 for o, a in opts:
Barry Warsaw08fca522001-08-20 22:33:46 +0000124 if o in ('-h', '--help'):
125 usage(0)
126 elif o in ('-v', '--verbose'):
127 verbose += 1
128 elif o in ('-q', '--quiet'):
129 quiet = 1;
130 verbose = 0
131 elif o in ('-g', '--generate'):
132 generate = 1
133 elif o in ('-x', '--exclude'):
134 exclude = 1
135 elif o in ('-s', '--single'):
136 single = 1
137 elif o in ('-r', '--randomize'):
138 randomize = 1
Tim Petersc5000df2002-06-02 21:42:01 +0000139 elif o in ('-f', '--fromfile'):
140 fromfile = a
Barry Warsaw08fca522001-08-20 22:33:46 +0000141 elif o in ('-l', '--findleaks'):
142 findleaks = 1
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000143 elif o in ('-t', '--threshold'):
144 import gc
145 gc.set_threshold(int(a))
Barry Warsaw08fca522001-08-20 22:33:46 +0000146 elif o in ('-u', '--use'):
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000147 u = [x.lower() for x in a.split(',')]
148 for r in u:
Fred Drake3a15dac2002-04-11 16:39:16 +0000149 if r == 'all':
150 use_resources = RESOURCE_NAMES
151 break
152 if r not in RESOURCE_NAMES:
153 usage(1, 'Invalid -u/--use option: ' + a)
Fred Drake04a8da52002-04-11 20:58:54 +0000154 if r not in use_resources:
Andrew MacIntyree41abab2002-04-30 12:11:04 +0000155 use_resources.append(r)
Guido van Rossuma4122201997-08-18 20:08:24 +0000156 if generate and verbose:
Barry Warsaw08fca522001-08-20 22:33:46 +0000157 usage(2, "-g and -v don't go together!")
Tim Petersc5000df2002-06-02 21:42:01 +0000158 if single and fromfile:
159 usage(2, "-s and -f don't go together!")
Barry Warsaw08fca522001-08-20 22:33:46 +0000160
Guido van Rossum152494a1996-12-20 03:12:20 +0000161 good = []
162 bad = []
163 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000164
Neil Schemenauerd569f232000-09-22 15:29:28 +0000165 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000166 try:
167 import gc
168 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000169 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +0000170 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +0000171 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000172 # Uncomment the line below to report garbage that is not
173 # freeable by reference counting alone. By default only
174 # garbage that is not collectable by the GC is reported.
175 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000176 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000177
Barry Warsawe11e3de1999-01-28 19:51:51 +0000178 if single:
179 from tempfile import gettempdir
180 filename = os.path.join(gettempdir(), 'pynexttest')
181 try:
182 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000183 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000184 tests = [next]
185 fp.close()
186 except IOError:
187 pass
Tim Petersc5000df2002-06-02 21:42:01 +0000188
189 if fromfile:
190 tests = []
191 fp = open(fromfile)
192 for line in fp:
193 guts = line.split() # assuming no test has whitespace in its name
194 if guts and not guts[0].startswith('#'):
195 tests.extend(guts)
196 fp.close()
197
198 # Strip .py extensions.
199 if args:
200 args = map(removepy, args)
201 if tests:
202 tests = map(removepy, tests)
203
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000204 stdtests = STDTESTS[:]
205 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000206 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000207 for arg in args:
208 if arg in stdtests:
209 stdtests.remove(arg)
210 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000211 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000212 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000213 if single:
214 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000215 if randomize:
216 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000217 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000218 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000219 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000220 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000221 if not quiet:
222 print test
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000223 sys.stdout.flush()
Trent Mickf29f47b2000-08-11 19:02:59 +0000224 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000225 if ok > 0:
226 good.append(test)
227 elif ok == 0:
228 bad.append(test)
229 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000230 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000231 if findleaks:
232 gc.collect()
233 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000234 print "Warning: test created", len(gc.garbage),
235 print "uncollectable object(s)."
236 # move the uncollectable objects somewhere so we don't see
237 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000238 found_garbage.extend(gc.garbage)
239 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000240 # Unload the newly imported modules (best effort finalization)
241 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000242 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000243 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000244
245 # The lists won't be sorted if running with -r
246 good.sort()
247 bad.sort()
248 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000249
Guido van Rossum152494a1996-12-20 03:12:20 +0000250 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000251 if not bad and not skipped and len(good) > 1:
252 print "All",
253 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000254 if verbose:
Barry Warsaw408b6d32002-07-30 23:27:12 +0000255 print "CAUTION: stdout isn't compared in verbose mode:"
256 print "a test that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000257 if bad:
Tim Petersa45da922001-08-12 03:45:50 +0000258 print count(len(bad), "test"), "failed:"
259 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000260 if skipped and not quiet:
Tim Petersa45da922001-08-12 03:45:50 +0000261 print count(len(skipped), "test"), "skipped:"
262 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000263
Tim Petersb5b7b782001-08-12 01:20:39 +0000264 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000265 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000266 if e.isvalid():
Tim Peters7c7efe92002-08-23 17:55:54 +0000267 surprise = Set(skipped) - e.getexpected()
Tim Petersb5b7b782001-08-12 01:20:39 +0000268 if surprise:
269 print count(len(surprise), "skip"), \
Tim Petersa45da922001-08-12 03:45:50 +0000270 "unexpected on", plat + ":"
271 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000272 else:
273 print "Those skips are all expected on", plat + "."
274 else:
275 print "Ask someone to teach regrtest.py about which tests are"
276 print "expected to get skipped on", plat + "."
277
Barry Warsawe11e3de1999-01-28 19:51:51 +0000278 if single:
279 alltests = findtests(testdir, stdtests, nottests)
280 for i in range(len(alltests)):
281 if tests[0] == alltests[i]:
282 if i == len(alltests) - 1:
283 os.unlink(filename)
284 else:
285 fp = open(filename, 'w')
286 fp.write(alltests[i+1] + '\n')
287 fp.close()
288 break
289 else:
290 os.unlink(filename)
291
Barry Warsaw08fca522001-08-20 22:33:46 +0000292 sys.exit(len(bad) > 0)
293
Guido van Rossum152494a1996-12-20 03:12:20 +0000294
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000295STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000296 'test_grammar',
297 'test_opcodes',
298 'test_operations',
299 'test_builtin',
300 'test_exceptions',
301 'test_types',
302 ]
303
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000304NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000305 'test_support',
306 'test_b1',
307 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000308 'test_future1',
309 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000310 'test_future3',
Guido van Rossum152494a1996-12-20 03:12:20 +0000311 ]
312
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000313def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000314 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000315 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000316 names = os.listdir(testdir)
317 tests = []
318 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000319 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000320 modname = name[:-3]
321 if modname not in stdtests and modname not in nottests:
322 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000323 tests.sort()
324 return stdtests + tests
325
Trent Mickf29f47b2000-08-11 19:02:59 +0000326def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000327 """Run a single test.
328 test -- the name of the test
329 generate -- if true, generate output, instead of running the test
330 and comparing it to a previously created output file
331 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000332 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000333 testdir -- test directory
334 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000335 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000336 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000337 outputdir = os.path.join(testdir, "output")
338 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000339 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000340 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000341 else:
Fred Drake88a56852001-09-28 20:16:30 +0000342 cfp = StringIO.StringIO()
Guido van Rossum152494a1996-12-20 03:12:20 +0000343 try:
Tim Peters342ca752001-09-25 19:13:20 +0000344 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000345 try:
346 if cfp:
347 sys.stdout = cfp
348 print test # Output file starts with test name
Barry Warsaw408b6d32002-07-30 23:27:12 +0000349 if test.startswith('test.'):
350 abstest = test
351 else:
352 # Always import it from the test package
353 abstest = 'test.' + test
354 the_package = __import__(abstest, globals(), locals(), [])
355 the_module = getattr(the_package, test)
Tim Petersd9742212001-05-22 18:28:25 +0000356 # Most tests run to completion simply as a side-effect of
357 # being imported. For the benefit of tests that can't run
358 # that way (like test_threaded_import), explicitly invoke
359 # their test_main() function (if it exists).
360 indirect_test = getattr(the_module, "test_main", None)
361 if indirect_test is not None:
362 indirect_test()
Guido van Rossum41360a41998-03-26 19:42:58 +0000363 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000364 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000365 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000366 if not quiet:
Guido van Rossumeb949052001-09-18 20:34:19 +0000367 print "test", test, "skipped --", msg
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000368 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000369 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000370 except KeyboardInterrupt:
371 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000372 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000373 print "test", test, "failed --", msg
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000374 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000375 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000376 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000377 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000378 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000379 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000380 if verbose:
381 traceback.print_exc(file=sys.stdout)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000382 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000383 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000384 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000385 if not cfp:
386 return 1
387 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000388 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000389 if output == test + "\n":
390 if os.path.exists(outputfile):
391 # Write it since it already exists (and the contents
392 # may have changed), but let the user know it isn't
393 # needed:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000394 print "output file", outputfile, \
395 "is no longer needed; consider removing it"
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000396 else:
397 # We don't need it, so don't create it.
398 return 1
399 fp = open(outputfile, "w")
400 fp.write(output)
401 fp.close()
402 return 1
403 if os.path.exists(outputfile):
404 fp = open(outputfile, "r")
405 expected = fp.read()
406 fp.close()
407 else:
408 expected = test + "\n"
409 if output == expected:
410 return 1
411 print "test", test, "produced unexpected output:"
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000412 sys.stdout.flush()
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000413 reportdiff(expected, output)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000414 sys.stdout.flush()
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000415 return 0
416
417def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000418 import difflib
Tim Petersc377b162001-09-22 05:31:03 +0000419 print "*" * 70
420 a = expected.splitlines(1)
421 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000422 sm = difflib.SequenceMatcher(a=a, b=b)
423 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000424
Guido van Rossumcf691932001-09-21 21:06:22 +0000425 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000426 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000427 x0 += 1
428 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000429 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000430 else:
Tim Petersc377b162001-09-22 05:31:03 +0000431 return "lines %d-%d" % (x0, x1)
432
Guido van Rossumcf691932001-09-21 21:06:22 +0000433 for op, a0, a1, b0, b1 in tuples:
434 if op == 'equal':
435 pass
Tim Petersc377b162001-09-22 05:31:03 +0000436
Guido van Rossumcf691932001-09-21 21:06:22 +0000437 elif op == 'delete':
Tim Petersc377b162001-09-22 05:31:03 +0000438 print "***", pair(a0, a1), "of expected output missing:"
Guido van Rossumcf691932001-09-21 21:06:22 +0000439 for line in a[a0:a1]:
Tim Petersc377b162001-09-22 05:31:03 +0000440 print "-", line,
441
Guido van Rossumcf691932001-09-21 21:06:22 +0000442 elif op == 'replace':
Tim Petersc377b162001-09-22 05:31:03 +0000443 print "*** mismatch between", pair(a0, a1), "of expected", \
444 "output and", pair(b0, b1), "of actual output:"
445 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
446 print line,
447
Guido van Rossumcf691932001-09-21 21:06:22 +0000448 elif op == 'insert':
Tim Petersc377b162001-09-22 05:31:03 +0000449 print "***", pair(b0, b1), "of actual output doesn't appear", \
450 "in expected output after line", str(a1)+":"
Guido van Rossumcf691932001-09-21 21:06:22 +0000451 for line in b[b0:b1]:
Tim Petersc377b162001-09-22 05:31:03 +0000452 print "+", line,
453
Guido van Rossumcf691932001-09-21 21:06:22 +0000454 else:
455 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
Tim Petersc377b162001-09-22 05:31:03 +0000456
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000457 print "*" * 70
Guido van Rossum152494a1996-12-20 03:12:20 +0000458
459def findtestdir():
460 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000461 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000462 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000463 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000464 testdir = os.path.dirname(file) or os.curdir
465 return testdir
466
Tim Petersc5000df2002-06-02 21:42:01 +0000467def removepy(name):
468 if name.endswith(os.extsep + "py"):
469 name = name[:-3]
470 return name
471
Guido van Rossum152494a1996-12-20 03:12:20 +0000472def count(n, word):
473 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000474 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000475 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000476 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000477
Tim Petersa45da922001-08-12 03:45:50 +0000478def printlist(x, width=70, indent=4):
Tim Peters7c7efe92002-08-23 17:55:54 +0000479 """Print the elements of iterable x to stdout.
Tim Petersa45da922001-08-12 03:45:50 +0000480
481 Optional arg width (default 70) is the maximum line length.
482 Optional arg indent (default 4) is the number of blanks with which to
483 begin each line.
484 """
485
Tim Petersba78bc42002-07-04 19:45:06 +0000486 from textwrap import fill
487 blanks = ' ' * indent
488 print fill(' '.join(map(str, x)), width,
489 initial_indent=blanks, subsequent_indent=blanks)
Tim Petersa45da922001-08-12 03:45:50 +0000490
Tim Petersde14a302002-04-01 05:04:46 +0000491# Map sys.platform to a string containing the basenames of tests
492# expected to be skipped on that platform.
493
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000494_expectations = {
495 'win32':
496 """
497 test_al
498 test_cd
499 test_cl
500 test_commands
501 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000502 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000503 test_dbm
504 test_dl
Tim Petersdeb121a2002-04-11 19:52:58 +0000505 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000506 test_fcntl
507 test_fork1
508 test_gdbm
509 test_gl
510 test_grp
511 test_imgfile
512 test_largefile
513 test_linuxaudiodev
514 test_mhlib
Tim Petersde14a302002-04-01 05:04:46 +0000515 test_mpz
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000516 test_nis
517 test_openpty
518 test_poll
519 test_pty
520 test_pwd
Tim Peters1e33ffa2002-04-23 23:09:02 +0000521 test_resource
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000522 test_signal
Barry Warsaw08fca522001-08-20 22:33:46 +0000523 test_socket_ssl
Tim Petersa86f0c12001-09-18 02:18:57 +0000524 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000525 test_sunaudiodev
526 test_timing
527 """,
528 'linux2':
529 """
530 test_al
531 test_cd
532 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000533 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000534 test_dl
Guido van Rossum6184c112002-04-16 02:14:04 +0000535 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000536 test_gl
537 test_imgfile
538 test_largefile
539 test_nis
540 test_ntpath
Barry Warsaw08fca522001-08-20 22:33:46 +0000541 test_socket_ssl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000542 test_socketserver
543 test_sunaudiodev
544 test_unicode_file
545 test_winreg
546 test_winsound
547 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000548 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000549 """
550 test_al
551 test_bsddb
552 test_cd
553 test_cl
554 test_commands
555 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000556 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000557 test_dbm
558 test_dl
559 test_fcntl
560 test_fork1
561 test_gl
562 test_grp
563 test_imgfile
564 test_largefile
565 test_linuxaudiodev
566 test_locale
567 test_mmap
568 test_nis
569 test_ntpath
570 test_openpty
571 test_poll
572 test_popen2
573 test_pty
574 test_pwd
575 test_signal
576 test_socket_ssl
577 test_socketserver
578 test_sunaudiodev
579 test_sundry
580 test_timing
581 test_unicode_file
582 test_winreg
583 test_winsound
584 """,
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000585 'unixware5':
586 """
587 test_al
588 test_bsddb
589 test_cd
590 test_cl
591 test_dl
592 test_gl
593 test_imgfile
594 test_largefile
595 test_linuxaudiodev
596 test_minidom
597 test_nis
598 test_ntpath
599 test_openpty
600 test_pyexpat
601 test_sax
602 test_socketserver
603 test_sunaudiodev
604 test_sundry
605 test_unicode_file
606 test_winreg
607 test_winsound
608 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000609 'riscos':
610 """
611 test_al
612 test_asynchat
613 test_bsddb
614 test_cd
615 test_cl
616 test_commands
617 test_crypt
618 test_dbm
619 test_dl
620 test_fcntl
621 test_fork1
622 test_gdbm
623 test_gl
624 test_grp
625 test_imgfile
626 test_largefile
627 test_linuxaudiodev
628 test_locale
629 test_mmap
630 test_nis
631 test_ntpath
632 test_openpty
633 test_poll
634 test_popen2
635 test_pty
636 test_pwd
637 test_socket_ssl
638 test_socketserver
639 test_strop
640 test_sunaudiodev
641 test_sundry
642 test_thread
643 test_threaded_import
644 test_threadedtempfile
645 test_threading
646 test_timing
647 test_unicode_file
648 test_winreg
649 test_winsound
650 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000651 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000652 """
653 test_al
654 test_cd
655 test_cl
656 test_curses
657 test_dl
658 test_gdbm
659 test_gl
660 test_imgfile
661 test_largefile
662 test_linuxaudiodev
663 test_minidom
664 test_nis
665 test_ntpath
666 test_poll
667 test_socket_ssl
Jack Jansenf839c272001-12-14 21:28:53 +0000668 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000669 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000670 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000671 test_winreg
672 test_winsound
673 """,
Guido van Rossum11c3f092002-07-17 15:08:24 +0000674 'sunos5':
675 """
676 test_al
677 test_bsddb
678 test_cd
679 test_cl
680 test_curses
681 test_dbm
682 test_email_codecs
683 test_gdbm
684 test_gl
685 test_gzip
686 test_imgfile
687 test_linuxaudiodev
688 test_mpz
689 test_openpty
690 test_socket_ssl
691 test_socketserver
692 test_winreg
693 test_winsound
694 test_zipfile
695 test_zlib
Jeremy Hyltoned375e12002-07-17 15:56:55 +0000696 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000697 'hp-ux11':
698 """
699 test_al
700 test_bsddb
701 test_cd
702 test_cl
703 test_curses
704 test_dl
705 test_gdbm
706 test_gl
707 test_gzip
708 test_imgfile
709 test_largefile
710 test_linuxaudiodev
711 test_locale
712 test_minidom
713 test_nis
714 test_ntpath
715 test_openpty
716 test_pyexpat
717 test_sax
718 test_socket_ssl
719 test_socketserver
720 test_sunaudiodev
721 test_unicode_file
722 test_winreg
723 test_winsound
724 test_zipfile
725 test_zlib
726 """,
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000727 'atheos':
Tim Petersc411dba2002-07-16 21:35:23 +0000728 """
729 test_al
730 test_cd
731 test_cl
732 test_curses
733 test_dl
734 test_email_codecs
735 test_gdbm
736 test_gl
737 test_imgfile
738 test_largefile
739 test_linuxaudiodev
740 test_locale
741 test_mhlib
742 test_mmap
743 test_mpz
744 test_nis
745 test_poll
746 test_popen2
747 test_resource
748 test_socket_ssl
749 test_socketserver
750 test_sunaudiodev
751 test_unicode_file
752 test_winreg
753 test_winsound
754 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000755}
756
Tim Petersb5b7b782001-08-12 01:20:39 +0000757class _ExpectedSkips:
758 def __init__(self):
Tim Peters7c7efe92002-08-23 17:55:54 +0000759 self.valid = False
Tim Petersde14a302002-04-01 05:04:46 +0000760 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000761 s = _expectations[sys.platform]
Tim Peters7c7efe92002-08-23 17:55:54 +0000762 self.expected = Set(s.split())
763 self.valid = True
Tim Petersb5b7b782001-08-12 01:20:39 +0000764
765 def isvalid(self):
766 "Return true iff _ExpectedSkips knows about the current platform."
767 return self.valid
768
769 def getexpected(self):
770 """Return set of test names we expect to skip on current platform.
771
772 self.isvalid() must be true.
773 """
774
775 assert self.isvalid()
776 return self.expected
777
Guido van Rossum152494a1996-12-20 03:12:20 +0000778if __name__ == '__main__':
Barry Warsaw408b6d32002-07-30 23:27:12 +0000779 # Remove regrtest.py's own directory from the module search path. This
780 # prevents relative imports from working, and relative imports will screw
781 # up the testing framework. E.g. if both test.test_support and
782 # test_support are imported, they will not contain the same globals, and
783 # much of the testing framework relies on the globals in the
784 # test.test_support module.
785 mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
786 i = pathlen = len(sys.path)
787 while i >= 0:
788 i -= 1
789 if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
790 del sys.path[i]
791 if len(sys.path) == pathlen:
792 print 'Could not find %r in sys.path to remove it' % mydir
Barry Warsaw08fca522001-08-20 22:33:46 +0000793 main()