blob: 843deefbec1321e2623ff910935459ad63060309 [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
Neil Schemenauer8a00abc2000-10-13 01:32:42 +000017-l: findleaks -- if GC is available detect tests that leak memory
Barry Warsaw08fca522001-08-20 22:33:46 +000018-u: use -- specify which special resource intensive tests to run
19-h: help -- print this text and exit
Guido van Rossum152494a1996-12-20 03:12:20 +000020
21If non-option arguments are present, they are names for tests to run,
22unless -x is given, in which case they are names for tests not to run.
23If no test names are given, all tests are run.
Guido van Rossumf58ed251997-03-07 21:04:33 +000024
Guido van Rossuma4122201997-08-18 20:08:24 +000025-v is incompatible with -g and does not compare test output files.
Barry Warsawe11e3de1999-01-28 19:51:51 +000026
Barry Warsaw22e41822001-02-23 18:31:40 +000027-s means to run only a single test and exit. This is useful when doing memory
Neal Norwitzaf642632002-02-08 20:13:53 +000028analysis on the Python interpreter (which tend to consume too many resources to
Barry Warsaw22e41822001-02-23 18:31:40 +000029run the full regression test non-stop). The file /tmp/pynexttest is read to
30find the next test to run. If this file is missing, the first test_*.py file
31in testdir or on the command line is used. (actually tempfile.gettempdir() is
32used instead of /tmp).
Barry Warsawe11e3de1999-01-28 19:51:51 +000033
Barry Warsaw08fca522001-08-20 22:33:46 +000034-u is used to specify which special resource intensive tests to run, such as
35those requiring large file support or network connectivity. The argument is a
36comma-separated list of words indicating the resources to test. Currently
37only the following are defined:
38
Fred Drake3a15dac2002-04-11 16:39:16 +000039 all - Enable all special resources.
40
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000041 curses - Tests that use curses and will modify the terminal's
42 state and output modes.
Tim Peters1633a2e2001-10-30 05:56:40 +000043
Barry Warsaw08fca522001-08-20 22:33:46 +000044 largefile - It is okay to run some test that may create huge files. These
45 tests can take a long time and may consume >2GB of disk space
46 temporarily.
47
48 network - It is okay to run tests that use external network resource,
49 e.g. testing SSL support for sockets.
Guido van Rossum152494a1996-12-20 03:12:20 +000050"""
51
52import sys
Guido van Rossum152494a1996-12-20 03:12:20 +000053import os
54import getopt
Guido van Rossum9e48b271997-07-16 01:56:13 +000055import traceback
Skip Montanaroab1c7912000-06-30 16:39:27 +000056import random
Fred Drakeae1bb172001-05-21 21:08:12 +000057import StringIO
Guido van Rossum152494a1996-12-20 03:12:20 +000058
59import test_support
60
Fred Drake3a15dac2002-04-11 16:39:16 +000061
62RESOURCE_NAMES = ('curses', 'largefile', 'network')
63
64
Barry Warsaw08fca522001-08-20 22:33:46 +000065def usage(code, msg=''):
66 print __doc__
67 if msg: print msg
68 sys.exit(code)
69
70
Skip Montanaroab1c7912000-06-30 16:39:27 +000071def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
Neil Schemenauerd569f232000-09-22 15:29:28 +000072 exclude=0, single=0, randomize=0, findleaks=0,
Barry Warsaw08fca522001-08-20 22:33:46 +000073 use_resources=None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +000074 """Execute a test suite.
75
Thomas Wouters7e474022000-07-16 12:04:32 +000076 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +000077 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000078
79 tests -- a list of strings containing test names (optional)
80 testdir -- the directory in which to look for tests (optional)
81
82 Users other than the Python test suite will certainly want to
83 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +000084 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000085
86 If the tests argument is omitted, the tests listed on the
87 command-line will be used. If that's empty, too, then all *.py
88 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +000089
Barry Warsaw08fca522001-08-20 22:33:46 +000090 The other default arguments (verbose, quiet, generate, exclude, single,
91 randomize, findleaks, and use_resources) allow programmers calling main()
Barry Warsawa873b032000-08-03 15:50:37 +000092 directly to set the values that would normally be set by flags on the
93 command line.
94
Guido van Rossum6fd83b71998-08-01 17:04:08 +000095 """
Fred Drake004d5e62000-10-23 17:22:08 +000096
Tim Peters8dee8092001-09-25 20:05:11 +000097 test_support.record_original_stdout(sys.stdout)
Guido van Rossum152494a1996-12-20 03:12:20 +000098 try:
Barry Warsaw08fca522001-08-20 22:33:46 +000099 opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsrlu:',
100 ['help', 'verbose', 'quiet', 'generate',
101 'exclude', 'single', 'random',
102 'findleaks', 'use='])
Guido van Rossum152494a1996-12-20 03:12:20 +0000103 except getopt.error, msg:
Barry Warsaw08fca522001-08-20 22:33:46 +0000104 usage(2, msg)
105
106 # Defaults
107 if use_resources is None:
108 use_resources = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000109 for o, a in opts:
Barry Warsaw08fca522001-08-20 22:33:46 +0000110 if o in ('-h', '--help'):
111 usage(0)
112 elif o in ('-v', '--verbose'):
113 verbose += 1
114 elif o in ('-q', '--quiet'):
115 quiet = 1;
116 verbose = 0
117 elif o in ('-g', '--generate'):
118 generate = 1
119 elif o in ('-x', '--exclude'):
120 exclude = 1
121 elif o in ('-s', '--single'):
122 single = 1
123 elif o in ('-r', '--randomize'):
124 randomize = 1
125 elif o in ('-l', '--findleaks'):
126 findleaks = 1
127 elif o in ('-u', '--use'):
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000128 u = [x.lower() for x in a.split(',')]
129 for r in u:
Fred Drake3a15dac2002-04-11 16:39:16 +0000130 if r == 'all':
131 use_resources = RESOURCE_NAMES
132 break
133 if r not in RESOURCE_NAMES:
134 usage(1, 'Invalid -u/--use option: ' + a)
Fred Drake04a8da52002-04-11 20:58:54 +0000135 if r not in use_resources:
136 use_resources.extend(r)
Guido van Rossuma4122201997-08-18 20:08:24 +0000137 if generate and verbose:
Barry Warsaw08fca522001-08-20 22:33:46 +0000138 usage(2, "-g and -v don't go together!")
139
Guido van Rossum152494a1996-12-20 03:12:20 +0000140 good = []
141 bad = []
142 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000143
Neil Schemenauerd569f232000-09-22 15:29:28 +0000144 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000145 try:
146 import gc
147 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000148 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +0000149 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +0000150 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000151 # Uncomment the line below to report garbage that is not
152 # freeable by reference counting alone. By default only
153 # garbage that is not collectable by the GC is reported.
154 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000155 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000156
Barry Warsawe11e3de1999-01-28 19:51:51 +0000157 if single:
158 from tempfile import gettempdir
159 filename = os.path.join(gettempdir(), 'pynexttest')
160 try:
161 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000162 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000163 tests = [next]
164 fp.close()
165 except IOError:
166 pass
Guido van Rossuma4122201997-08-18 20:08:24 +0000167 for i in range(len(args)):
Guido van Rossum41360a41998-03-26 19:42:58 +0000168 # Strip trailing ".py" from arguments
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000169 if args[i][-3:] == os.extsep+'py':
Guido van Rossum41360a41998-03-26 19:42:58 +0000170 args[i] = args[i][:-3]
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000171 stdtests = STDTESTS[:]
172 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000173 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000174 for arg in args:
175 if arg in stdtests:
176 stdtests.remove(arg)
177 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000178 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000179 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000180 if single:
181 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000182 if randomize:
183 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000184 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000185 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000186 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000187 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000188 if not quiet:
189 print test
Trent Mickf29f47b2000-08-11 19:02:59 +0000190 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000191 if ok > 0:
192 good.append(test)
193 elif ok == 0:
194 bad.append(test)
195 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000196 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000197 if findleaks:
198 gc.collect()
199 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000200 print "Warning: test created", len(gc.garbage),
201 print "uncollectable object(s)."
202 # move the uncollectable objects somewhere so we don't see
203 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000204 found_garbage.extend(gc.garbage)
205 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000206 # Unload the newly imported modules (best effort finalization)
207 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000208 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000209 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000210
211 # The lists won't be sorted if running with -r
212 good.sort()
213 bad.sort()
214 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000215
Guido van Rossum152494a1996-12-20 03:12:20 +0000216 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000217 if not bad and not skipped and len(good) > 1:
218 print "All",
219 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000220 if verbose:
221 print "CAUTION: stdout isn't compared in verbose mode: a test"
222 print "that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000223 if bad:
Tim Petersa45da922001-08-12 03:45:50 +0000224 print count(len(bad), "test"), "failed:"
225 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000226 if skipped and not quiet:
Tim Petersa45da922001-08-12 03:45:50 +0000227 print count(len(skipped), "test"), "skipped:"
228 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000229
Tim Petersb5b7b782001-08-12 01:20:39 +0000230 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000231 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000232 if e.isvalid():
233 surprise = _Set(skipped) - e.getexpected()
Tim Petersb5b7b782001-08-12 01:20:39 +0000234 if surprise:
235 print count(len(surprise), "skip"), \
Tim Petersa45da922001-08-12 03:45:50 +0000236 "unexpected on", plat + ":"
237 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000238 else:
239 print "Those skips are all expected on", plat + "."
240 else:
241 print "Ask someone to teach regrtest.py about which tests are"
242 print "expected to get skipped on", plat + "."
243
Barry Warsawe11e3de1999-01-28 19:51:51 +0000244 if single:
245 alltests = findtests(testdir, stdtests, nottests)
246 for i in range(len(alltests)):
247 if tests[0] == alltests[i]:
248 if i == len(alltests) - 1:
249 os.unlink(filename)
250 else:
251 fp = open(filename, 'w')
252 fp.write(alltests[i+1] + '\n')
253 fp.close()
254 break
255 else:
256 os.unlink(filename)
257
Barry Warsaw08fca522001-08-20 22:33:46 +0000258 sys.exit(len(bad) > 0)
259
Guido van Rossum152494a1996-12-20 03:12:20 +0000260
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000261STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000262 'test_grammar',
263 'test_opcodes',
264 'test_operations',
265 'test_builtin',
266 'test_exceptions',
267 'test_types',
268 ]
269
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000270NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000271 'test_support',
272 'test_b1',
273 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000274 'test_future1',
275 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000276 'test_future3',
Guido van Rossum152494a1996-12-20 03:12:20 +0000277 ]
278
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000279def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000280 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000281 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000282 names = os.listdir(testdir)
283 tests = []
284 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000285 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000286 modname = name[:-3]
287 if modname not in stdtests and modname not in nottests:
288 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000289 tests.sort()
290 return stdtests + tests
291
Trent Mickf29f47b2000-08-11 19:02:59 +0000292def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000293 """Run a single test.
294 test -- the name of the test
295 generate -- if true, generate output, instead of running the test
296 and comparing it to a previously created output file
297 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000298 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000299 testdir -- test directory
300 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000301 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000302 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000303 outputdir = os.path.join(testdir, "output")
304 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000305 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000306 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000307 else:
Fred Drake88a56852001-09-28 20:16:30 +0000308 cfp = StringIO.StringIO()
Guido van Rossum152494a1996-12-20 03:12:20 +0000309 try:
Tim Peters342ca752001-09-25 19:13:20 +0000310 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000311 try:
312 if cfp:
313 sys.stdout = cfp
314 print test # Output file starts with test name
Tim Petersd9742212001-05-22 18:28:25 +0000315 the_module = __import__(test, globals(), locals(), [])
316 # Most tests run to completion simply as a side-effect of
317 # being imported. For the benefit of tests that can't run
318 # that way (like test_threaded_import), explicitly invoke
319 # their test_main() function (if it exists).
320 indirect_test = getattr(the_module, "test_main", None)
321 if indirect_test is not None:
322 indirect_test()
Guido van Rossum41360a41998-03-26 19:42:58 +0000323 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000324 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000325 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000326 if not quiet:
Guido van Rossumeb949052001-09-18 20:34:19 +0000327 print "test", test, "skipped --", msg
Guido van Rossum41360a41998-03-26 19:42:58 +0000328 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000329 except KeyboardInterrupt:
330 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000331 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000332 print "test", test, "failed --", msg
333 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000334 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000335 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000336 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum41360a41998-03-26 19:42:58 +0000337 if verbose:
338 traceback.print_exc(file=sys.stdout)
339 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000340 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000341 if not cfp:
342 return 1
343 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000344 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000345 if output == test + "\n":
346 if os.path.exists(outputfile):
347 # Write it since it already exists (and the contents
348 # may have changed), but let the user know it isn't
349 # needed:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000350 print "output file", outputfile, \
351 "is no longer needed; consider removing it"
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000352 else:
353 # We don't need it, so don't create it.
354 return 1
355 fp = open(outputfile, "w")
356 fp.write(output)
357 fp.close()
358 return 1
359 if os.path.exists(outputfile):
360 fp = open(outputfile, "r")
361 expected = fp.read()
362 fp.close()
363 else:
364 expected = test + "\n"
365 if output == expected:
366 return 1
367 print "test", test, "produced unexpected output:"
368 reportdiff(expected, output)
369 return 0
370
371def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000372 import difflib
Tim Petersc377b162001-09-22 05:31:03 +0000373 print "*" * 70
374 a = expected.splitlines(1)
375 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000376 sm = difflib.SequenceMatcher(a=a, b=b)
377 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000378
Guido van Rossumcf691932001-09-21 21:06:22 +0000379 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000380 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000381 x0 += 1
382 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000383 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000384 else:
Tim Petersc377b162001-09-22 05:31:03 +0000385 return "lines %d-%d" % (x0, x1)
386
Guido van Rossumcf691932001-09-21 21:06:22 +0000387 for op, a0, a1, b0, b1 in tuples:
388 if op == 'equal':
389 pass
Tim Petersc377b162001-09-22 05:31:03 +0000390
Guido van Rossumcf691932001-09-21 21:06:22 +0000391 elif op == 'delete':
Tim Petersc377b162001-09-22 05:31:03 +0000392 print "***", pair(a0, a1), "of expected output missing:"
Guido van Rossumcf691932001-09-21 21:06:22 +0000393 for line in a[a0:a1]:
Tim Petersc377b162001-09-22 05:31:03 +0000394 print "-", line,
395
Guido van Rossumcf691932001-09-21 21:06:22 +0000396 elif op == 'replace':
Tim Petersc377b162001-09-22 05:31:03 +0000397 print "*** mismatch between", pair(a0, a1), "of expected", \
398 "output and", pair(b0, b1), "of actual output:"
399 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
400 print line,
401
Guido van Rossumcf691932001-09-21 21:06:22 +0000402 elif op == 'insert':
Tim Petersc377b162001-09-22 05:31:03 +0000403 print "***", pair(b0, b1), "of actual output doesn't appear", \
404 "in expected output after line", str(a1)+":"
Guido van Rossumcf691932001-09-21 21:06:22 +0000405 for line in b[b0:b1]:
Tim Petersc377b162001-09-22 05:31:03 +0000406 print "+", line,
407
Guido van Rossumcf691932001-09-21 21:06:22 +0000408 else:
409 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
Tim Petersc377b162001-09-22 05:31:03 +0000410
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000411 print "*" * 70
Guido van Rossum152494a1996-12-20 03:12:20 +0000412
413def findtestdir():
414 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000415 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000416 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000417 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000418 testdir = os.path.dirname(file) or os.curdir
419 return testdir
420
421def count(n, word):
422 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000423 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000424 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000425 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000426
Tim Petersa45da922001-08-12 03:45:50 +0000427def printlist(x, width=70, indent=4):
428 """Print the elements of a sequence to stdout.
429
430 Optional arg width (default 70) is the maximum line length.
431 Optional arg indent (default 4) is the number of blanks with which to
432 begin each line.
433 """
434
435 line = ' ' * indent
436 for one in map(str, x):
437 w = len(line) + len(one)
438 if line[-1:] == ' ':
439 pad = ''
440 else:
441 pad = ' '
442 w += 1
443 if w > width:
444 print line
445 line = ' ' * indent + one
446 else:
447 line += pad + one
448 if len(line) > indent:
449 print line
450
Tim Petersb5b7b782001-08-12 01:20:39 +0000451class _Set:
452 def __init__(self, seq=[]):
453 data = self.data = {}
454 for x in seq:
455 data[x] = 1
456
457 def __len__(self):
458 return len(self.data)
459
460 def __sub__(self, other):
461 "Return set of all elements in self not in other."
462 result = _Set()
463 data = result.data = self.data.copy()
464 for x in other.data:
465 if x in data:
466 del data[x]
467 return result
468
Jeremy Hylton39f77bc2001-08-12 21:53:08 +0000469 def __iter__(self):
470 return iter(self.data)
471
Tim Petersb5b7b782001-08-12 01:20:39 +0000472 def tolist(self, sorted=1):
473 "Return _Set elements as a list."
474 data = self.data.keys()
475 if sorted:
476 data.sort()
477 return data
478
Tim Petersde14a302002-04-01 05:04:46 +0000479# Map sys.platform to a string containing the basenames of tests
480# expected to be skipped on that platform.
481
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000482_expectations = {
483 'win32':
484 """
485 test_al
486 test_cd
487 test_cl
488 test_commands
489 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000490 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000491 test_dbm
492 test_dl
Tim Petersdeb121a2002-04-11 19:52:58 +0000493 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000494 test_fcntl
495 test_fork1
496 test_gdbm
497 test_gl
498 test_grp
499 test_imgfile
500 test_largefile
501 test_linuxaudiodev
502 test_mhlib
Tim Petersde14a302002-04-01 05:04:46 +0000503 test_mpz
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000504 test_nis
505 test_openpty
506 test_poll
507 test_pty
508 test_pwd
509 test_signal
Barry Warsaw08fca522001-08-20 22:33:46 +0000510 test_socket_ssl
Tim Petersa86f0c12001-09-18 02:18:57 +0000511 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000512 test_sunaudiodev
513 test_timing
514 """,
515 'linux2':
516 """
517 test_al
518 test_cd
519 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000520 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000521 test_dl
522 test_gl
523 test_imgfile
524 test_largefile
525 test_nis
526 test_ntpath
Barry Warsaw08fca522001-08-20 22:33:46 +0000527 test_socket_ssl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000528 test_socketserver
529 test_sunaudiodev
530 test_unicode_file
531 test_winreg
532 test_winsound
533 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000534 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000535 """
536 test_al
537 test_bsddb
538 test_cd
539 test_cl
540 test_commands
541 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000542 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000543 test_dbm
544 test_dl
545 test_fcntl
546 test_fork1
547 test_gl
548 test_grp
549 test_imgfile
550 test_largefile
551 test_linuxaudiodev
552 test_locale
553 test_mmap
554 test_nis
555 test_ntpath
556 test_openpty
557 test_poll
558 test_popen2
559 test_pty
560 test_pwd
561 test_signal
562 test_socket_ssl
563 test_socketserver
564 test_sunaudiodev
565 test_sundry
566 test_timing
567 test_unicode_file
568 test_winreg
569 test_winsound
570 """,
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000571 'unixware5':
572 """
573 test_al
574 test_bsddb
575 test_cd
576 test_cl
577 test_dl
578 test_gl
579 test_imgfile
580 test_largefile
581 test_linuxaudiodev
582 test_minidom
583 test_nis
584 test_ntpath
585 test_openpty
586 test_pyexpat
587 test_sax
588 test_socketserver
589 test_sunaudiodev
590 test_sundry
591 test_unicode_file
592 test_winreg
593 test_winsound
594 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000595 'riscos':
596 """
597 test_al
598 test_asynchat
599 test_bsddb
600 test_cd
601 test_cl
602 test_commands
603 test_crypt
604 test_dbm
605 test_dl
606 test_fcntl
607 test_fork1
608 test_gdbm
609 test_gl
610 test_grp
611 test_imgfile
612 test_largefile
613 test_linuxaudiodev
614 test_locale
615 test_mmap
616 test_nis
617 test_ntpath
618 test_openpty
619 test_poll
620 test_popen2
621 test_pty
622 test_pwd
623 test_socket_ssl
624 test_socketserver
625 test_strop
626 test_sunaudiodev
627 test_sundry
628 test_thread
629 test_threaded_import
630 test_threadedtempfile
631 test_threading
632 test_timing
633 test_unicode_file
634 test_winreg
635 test_winsound
636 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000637 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000638 """
639 test_al
640 test_cd
641 test_cl
642 test_curses
643 test_dl
644 test_gdbm
645 test_gl
646 test_imgfile
647 test_largefile
648 test_linuxaudiodev
649 test_minidom
650 test_nis
651 test_ntpath
652 test_poll
653 test_socket_ssl
Jack Jansenf839c272001-12-14 21:28:53 +0000654 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000655 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000656 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000657 test_winreg
658 test_winsound
659 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000660 'hp-ux11':
661 """
662 test_al
663 test_bsddb
664 test_cd
665 test_cl
666 test_curses
667 test_dl
668 test_gdbm
669 test_gl
670 test_gzip
671 test_imgfile
672 test_largefile
673 test_linuxaudiodev
674 test_locale
675 test_minidom
676 test_nis
677 test_ntpath
678 test_openpty
679 test_pyexpat
680 test_sax
681 test_socket_ssl
682 test_socketserver
683 test_sunaudiodev
684 test_unicode_file
685 test_winreg
686 test_winsound
687 test_zipfile
688 test_zlib
689 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000690}
691
Tim Petersb5b7b782001-08-12 01:20:39 +0000692class _ExpectedSkips:
693 def __init__(self):
694 self.valid = 0
Tim Petersde14a302002-04-01 05:04:46 +0000695 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000696 s = _expectations[sys.platform]
Tim Petersb5b7b782001-08-12 01:20:39 +0000697 self.expected = _Set(s.split())
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000698 self.valid = 1
Tim Petersb5b7b782001-08-12 01:20:39 +0000699
700 def isvalid(self):
701 "Return true iff _ExpectedSkips knows about the current platform."
702 return self.valid
703
704 def getexpected(self):
705 """Return set of test names we expect to skip on current platform.
706
707 self.isvalid() must be true.
708 """
709
710 assert self.isvalid()
711 return self.expected
712
Guido van Rossum152494a1996-12-20 03:12:20 +0000713if __name__ == '__main__':
Barry Warsaw08fca522001-08-20 22:33:46 +0000714 main()