blob: 324a6c482fd025a6844762d2c671e2db62483ddf [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
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000039 curses - Tests that use curses and will modify the terminal's
40 state and output modes.
Tim Peters1633a2e2001-10-30 05:56:40 +000041
Barry Warsaw08fca522001-08-20 22:33:46 +000042 largefile - It is okay to run some test that may create huge files. These
43 tests can take a long time and may consume >2GB of disk space
44 temporarily.
45
46 network - It is okay to run tests that use external network resource,
47 e.g. testing SSL support for sockets.
Guido van Rossum152494a1996-12-20 03:12:20 +000048"""
49
50import sys
Guido van Rossum152494a1996-12-20 03:12:20 +000051import os
52import getopt
Guido van Rossum9e48b271997-07-16 01:56:13 +000053import traceback
Skip Montanaroab1c7912000-06-30 16:39:27 +000054import random
Fred Drakeae1bb172001-05-21 21:08:12 +000055import StringIO
Guido van Rossum152494a1996-12-20 03:12:20 +000056
57import test_support
58
Barry Warsaw08fca522001-08-20 22:33:46 +000059def usage(code, msg=''):
60 print __doc__
61 if msg: print msg
62 sys.exit(code)
63
64
Skip Montanaroab1c7912000-06-30 16:39:27 +000065def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
Neil Schemenauerd569f232000-09-22 15:29:28 +000066 exclude=0, single=0, randomize=0, findleaks=0,
Barry Warsaw08fca522001-08-20 22:33:46 +000067 use_resources=None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +000068 """Execute a test suite.
69
Thomas Wouters7e474022000-07-16 12:04:32 +000070 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +000071 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000072
73 tests -- a list of strings containing test names (optional)
74 testdir -- the directory in which to look for tests (optional)
75
76 Users other than the Python test suite will certainly want to
77 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +000078 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +000079
80 If the tests argument is omitted, the tests listed on the
81 command-line will be used. If that's empty, too, then all *.py
82 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +000083
Barry Warsaw08fca522001-08-20 22:33:46 +000084 The other default arguments (verbose, quiet, generate, exclude, single,
85 randomize, findleaks, and use_resources) allow programmers calling main()
Barry Warsawa873b032000-08-03 15:50:37 +000086 directly to set the values that would normally be set by flags on the
87 command line.
88
Guido van Rossum6fd83b71998-08-01 17:04:08 +000089 """
Fred Drake004d5e62000-10-23 17:22:08 +000090
Tim Peters8dee8092001-09-25 20:05:11 +000091 test_support.record_original_stdout(sys.stdout)
Guido van Rossum152494a1996-12-20 03:12:20 +000092 try:
Barry Warsaw08fca522001-08-20 22:33:46 +000093 opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsrlu:',
94 ['help', 'verbose', 'quiet', 'generate',
95 'exclude', 'single', 'random',
96 'findleaks', 'use='])
Guido van Rossum152494a1996-12-20 03:12:20 +000097 except getopt.error, msg:
Barry Warsaw08fca522001-08-20 22:33:46 +000098 usage(2, msg)
99
100 # Defaults
101 if use_resources is None:
102 use_resources = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000103 for o, a in opts:
Barry Warsaw08fca522001-08-20 22:33:46 +0000104 if o in ('-h', '--help'):
105 usage(0)
106 elif o in ('-v', '--verbose'):
107 verbose += 1
108 elif o in ('-q', '--quiet'):
109 quiet = 1;
110 verbose = 0
111 elif o in ('-g', '--generate'):
112 generate = 1
113 elif o in ('-x', '--exclude'):
114 exclude = 1
115 elif o in ('-s', '--single'):
116 single = 1
117 elif o in ('-r', '--randomize'):
118 randomize = 1
119 elif o in ('-l', '--findleaks'):
120 findleaks = 1
121 elif o in ('-u', '--use'):
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000122 u = [x.lower() for x in a.split(',')]
123 for r in u:
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000124 if r not in ('curses', 'largefile', 'network'):
Barry Warsaw08fca522001-08-20 22:33:46 +0000125 usage(1, 'Invalid -u/--use option: %s' % a)
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000126 use_resources.extend(u)
Guido van Rossuma4122201997-08-18 20:08:24 +0000127 if generate and verbose:
Barry Warsaw08fca522001-08-20 22:33:46 +0000128 usage(2, "-g and -v don't go together!")
129
Guido van Rossum152494a1996-12-20 03:12:20 +0000130 good = []
131 bad = []
132 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000133
Neil Schemenauerd569f232000-09-22 15:29:28 +0000134 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000135 try:
136 import gc
137 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000138 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +0000139 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +0000140 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000141 # Uncomment the line below to report garbage that is not
142 # freeable by reference counting alone. By default only
143 # garbage that is not collectable by the GC is reported.
144 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000145 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000146
Barry Warsawe11e3de1999-01-28 19:51:51 +0000147 if single:
148 from tempfile import gettempdir
149 filename = os.path.join(gettempdir(), 'pynexttest')
150 try:
151 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000152 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000153 tests = [next]
154 fp.close()
155 except IOError:
156 pass
Guido van Rossuma4122201997-08-18 20:08:24 +0000157 for i in range(len(args)):
Guido van Rossum41360a41998-03-26 19:42:58 +0000158 # Strip trailing ".py" from arguments
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000159 if args[i][-3:] == os.extsep+'py':
Guido van Rossum41360a41998-03-26 19:42:58 +0000160 args[i] = args[i][:-3]
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000161 stdtests = STDTESTS[:]
162 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000163 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000164 for arg in args:
165 if arg in stdtests:
166 stdtests.remove(arg)
167 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000168 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000169 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000170 if single:
171 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000172 if randomize:
173 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000174 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000175 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000176 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000177 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000178 if not quiet:
179 print test
Trent Mickf29f47b2000-08-11 19:02:59 +0000180 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000181 if ok > 0:
182 good.append(test)
183 elif ok == 0:
184 bad.append(test)
185 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000186 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000187 if findleaks:
188 gc.collect()
189 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000190 print "Warning: test created", len(gc.garbage),
191 print "uncollectable object(s)."
192 # move the uncollectable objects somewhere so we don't see
193 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000194 found_garbage.extend(gc.garbage)
195 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000196 # Unload the newly imported modules (best effort finalization)
197 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000198 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000199 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000200
201 # The lists won't be sorted if running with -r
202 good.sort()
203 bad.sort()
204 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000205
Guido van Rossum152494a1996-12-20 03:12:20 +0000206 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000207 if not bad and not skipped and len(good) > 1:
208 print "All",
209 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000210 if verbose:
211 print "CAUTION: stdout isn't compared in verbose mode: a test"
212 print "that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000213 if bad:
Tim Petersa45da922001-08-12 03:45:50 +0000214 print count(len(bad), "test"), "failed:"
215 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000216 if skipped and not quiet:
Tim Petersa45da922001-08-12 03:45:50 +0000217 print count(len(skipped), "test"), "skipped:"
218 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000219
Tim Petersb5b7b782001-08-12 01:20:39 +0000220 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000221 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000222 if e.isvalid():
223 surprise = _Set(skipped) - e.getexpected()
Tim Petersb5b7b782001-08-12 01:20:39 +0000224 if surprise:
225 print count(len(surprise), "skip"), \
Tim Petersa45da922001-08-12 03:45:50 +0000226 "unexpected on", plat + ":"
227 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000228 else:
229 print "Those skips are all expected on", plat + "."
230 else:
231 print "Ask someone to teach regrtest.py about which tests are"
232 print "expected to get skipped on", plat + "."
233
Barry Warsawe11e3de1999-01-28 19:51:51 +0000234 if single:
235 alltests = findtests(testdir, stdtests, nottests)
236 for i in range(len(alltests)):
237 if tests[0] == alltests[i]:
238 if i == len(alltests) - 1:
239 os.unlink(filename)
240 else:
241 fp = open(filename, 'w')
242 fp.write(alltests[i+1] + '\n')
243 fp.close()
244 break
245 else:
246 os.unlink(filename)
247
Barry Warsaw08fca522001-08-20 22:33:46 +0000248 sys.exit(len(bad) > 0)
249
Guido van Rossum152494a1996-12-20 03:12:20 +0000250
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000251STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000252 'test_grammar',
253 'test_opcodes',
254 'test_operations',
255 'test_builtin',
256 'test_exceptions',
257 'test_types',
258 ]
259
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000260NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000261 'test_support',
262 'test_b1',
263 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000264 'test_future1',
265 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000266 'test_future3',
Guido van Rossum152494a1996-12-20 03:12:20 +0000267 ]
268
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000269def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000270 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000271 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000272 names = os.listdir(testdir)
273 tests = []
274 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000275 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000276 modname = name[:-3]
277 if modname not in stdtests and modname not in nottests:
278 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000279 tests.sort()
280 return stdtests + tests
281
Trent Mickf29f47b2000-08-11 19:02:59 +0000282def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000283 """Run a single test.
284 test -- the name of the test
285 generate -- if true, generate output, instead of running the test
286 and comparing it to a previously created output file
287 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000288 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000289 testdir -- test directory
290 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000291 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000292 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000293 outputdir = os.path.join(testdir, "output")
294 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000295 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000296 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000297 else:
Fred Drake88a56852001-09-28 20:16:30 +0000298 cfp = StringIO.StringIO()
Guido van Rossum152494a1996-12-20 03:12:20 +0000299 try:
Tim Peters342ca752001-09-25 19:13:20 +0000300 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000301 try:
302 if cfp:
303 sys.stdout = cfp
304 print test # Output file starts with test name
Tim Petersd9742212001-05-22 18:28:25 +0000305 the_module = __import__(test, globals(), locals(), [])
306 # Most tests run to completion simply as a side-effect of
307 # being imported. For the benefit of tests that can't run
308 # that way (like test_threaded_import), explicitly invoke
309 # their test_main() function (if it exists).
310 indirect_test = getattr(the_module, "test_main", None)
311 if indirect_test is not None:
312 indirect_test()
Guido van Rossum41360a41998-03-26 19:42:58 +0000313 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000314 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000315 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000316 if not quiet:
Guido van Rossumeb949052001-09-18 20:34:19 +0000317 print "test", test, "skipped --", msg
Guido van Rossum41360a41998-03-26 19:42:58 +0000318 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000319 except KeyboardInterrupt:
320 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000321 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000322 print "test", test, "failed --", msg
323 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000324 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000325 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000326 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum41360a41998-03-26 19:42:58 +0000327 if verbose:
328 traceback.print_exc(file=sys.stdout)
329 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000330 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000331 if not cfp:
332 return 1
333 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000334 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000335 if output == test + "\n":
336 if os.path.exists(outputfile):
337 # Write it since it already exists (and the contents
338 # may have changed), but let the user know it isn't
339 # needed:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000340 print "output file", outputfile, \
341 "is no longer needed; consider removing it"
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000342 else:
343 # We don't need it, so don't create it.
344 return 1
345 fp = open(outputfile, "w")
346 fp.write(output)
347 fp.close()
348 return 1
349 if os.path.exists(outputfile):
350 fp = open(outputfile, "r")
351 expected = fp.read()
352 fp.close()
353 else:
354 expected = test + "\n"
355 if output == expected:
356 return 1
357 print "test", test, "produced unexpected output:"
358 reportdiff(expected, output)
359 return 0
360
361def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000362 import difflib
Tim Petersc377b162001-09-22 05:31:03 +0000363 print "*" * 70
364 a = expected.splitlines(1)
365 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000366 sm = difflib.SequenceMatcher(a=a, b=b)
367 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000368
Guido van Rossumcf691932001-09-21 21:06:22 +0000369 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000370 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000371 x0 += 1
372 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000373 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000374 else:
Tim Petersc377b162001-09-22 05:31:03 +0000375 return "lines %d-%d" % (x0, x1)
376
Guido van Rossumcf691932001-09-21 21:06:22 +0000377 for op, a0, a1, b0, b1 in tuples:
378 if op == 'equal':
379 pass
Tim Petersc377b162001-09-22 05:31:03 +0000380
Guido van Rossumcf691932001-09-21 21:06:22 +0000381 elif op == 'delete':
Tim Petersc377b162001-09-22 05:31:03 +0000382 print "***", pair(a0, a1), "of expected output missing:"
Guido van Rossumcf691932001-09-21 21:06:22 +0000383 for line in a[a0:a1]:
Tim Petersc377b162001-09-22 05:31:03 +0000384 print "-", line,
385
Guido van Rossumcf691932001-09-21 21:06:22 +0000386 elif op == 'replace':
Tim Petersc377b162001-09-22 05:31:03 +0000387 print "*** mismatch between", pair(a0, a1), "of expected", \
388 "output and", pair(b0, b1), "of actual output:"
389 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
390 print line,
391
Guido van Rossumcf691932001-09-21 21:06:22 +0000392 elif op == 'insert':
Tim Petersc377b162001-09-22 05:31:03 +0000393 print "***", pair(b0, b1), "of actual output doesn't appear", \
394 "in expected output after line", str(a1)+":"
Guido van Rossumcf691932001-09-21 21:06:22 +0000395 for line in b[b0:b1]:
Tim Petersc377b162001-09-22 05:31:03 +0000396 print "+", line,
397
Guido van Rossumcf691932001-09-21 21:06:22 +0000398 else:
399 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
Tim Petersc377b162001-09-22 05:31:03 +0000400
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000401 print "*" * 70
Guido van Rossum152494a1996-12-20 03:12:20 +0000402
403def findtestdir():
404 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000405 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000406 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000407 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000408 testdir = os.path.dirname(file) or os.curdir
409 return testdir
410
411def count(n, word):
412 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000413 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000414 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000415 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000416
Tim Petersa45da922001-08-12 03:45:50 +0000417def printlist(x, width=70, indent=4):
418 """Print the elements of a sequence to stdout.
419
420 Optional arg width (default 70) is the maximum line length.
421 Optional arg indent (default 4) is the number of blanks with which to
422 begin each line.
423 """
424
425 line = ' ' * indent
426 for one in map(str, x):
427 w = len(line) + len(one)
428 if line[-1:] == ' ':
429 pad = ''
430 else:
431 pad = ' '
432 w += 1
433 if w > width:
434 print line
435 line = ' ' * indent + one
436 else:
437 line += pad + one
438 if len(line) > indent:
439 print line
440
Tim Petersb5b7b782001-08-12 01:20:39 +0000441class _Set:
442 def __init__(self, seq=[]):
443 data = self.data = {}
444 for x in seq:
445 data[x] = 1
446
447 def __len__(self):
448 return len(self.data)
449
450 def __sub__(self, other):
451 "Return set of all elements in self not in other."
452 result = _Set()
453 data = result.data = self.data.copy()
454 for x in other.data:
455 if x in data:
456 del data[x]
457 return result
458
Jeremy Hylton39f77bc2001-08-12 21:53:08 +0000459 def __iter__(self):
460 return iter(self.data)
461
Tim Petersb5b7b782001-08-12 01:20:39 +0000462 def tolist(self, sorted=1):
463 "Return _Set elements as a list."
464 data = self.data.keys()
465 if sorted:
466 data.sort()
467 return data
468
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000469_expectations = {
470 'win32':
471 """
472 test_al
473 test_cd
474 test_cl
475 test_commands
476 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000477 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000478 test_dbm
479 test_dl
480 test_fcntl
481 test_fork1
482 test_gdbm
483 test_gl
484 test_grp
485 test_imgfile
486 test_largefile
487 test_linuxaudiodev
488 test_mhlib
489 test_nis
490 test_openpty
491 test_poll
492 test_pty
493 test_pwd
494 test_signal
Barry Warsaw08fca522001-08-20 22:33:46 +0000495 test_socket_ssl
Tim Petersa86f0c12001-09-18 02:18:57 +0000496 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000497 test_sunaudiodev
498 test_timing
499 """,
500 'linux2':
501 """
502 test_al
503 test_cd
504 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000505 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000506 test_dl
507 test_gl
508 test_imgfile
509 test_largefile
510 test_nis
511 test_ntpath
Barry Warsaw08fca522001-08-20 22:33:46 +0000512 test_socket_ssl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000513 test_socketserver
514 test_sunaudiodev
515 test_unicode_file
516 test_winreg
517 test_winsound
518 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000519 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000520 """
521 test_al
522 test_bsddb
523 test_cd
524 test_cl
525 test_commands
526 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000527 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000528 test_dbm
529 test_dl
530 test_fcntl
531 test_fork1
532 test_gl
533 test_grp
534 test_imgfile
535 test_largefile
536 test_linuxaudiodev
537 test_locale
538 test_mmap
539 test_nis
540 test_ntpath
541 test_openpty
542 test_poll
543 test_popen2
544 test_pty
545 test_pwd
546 test_signal
547 test_socket_ssl
548 test_socketserver
549 test_sunaudiodev
550 test_sundry
551 test_timing
552 test_unicode_file
553 test_winreg
554 test_winsound
555 """,
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000556 'unixware5':
557 """
558 test_al
559 test_bsddb
560 test_cd
561 test_cl
562 test_dl
563 test_gl
564 test_imgfile
565 test_largefile
566 test_linuxaudiodev
567 test_minidom
568 test_nis
569 test_ntpath
570 test_openpty
571 test_pyexpat
572 test_sax
573 test_socketserver
574 test_sunaudiodev
575 test_sundry
576 test_unicode_file
577 test_winreg
578 test_winsound
579 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000580 'riscos':
581 """
582 test_al
583 test_asynchat
584 test_bsddb
585 test_cd
586 test_cl
587 test_commands
588 test_crypt
589 test_dbm
590 test_dl
591 test_fcntl
592 test_fork1
593 test_gdbm
594 test_gl
595 test_grp
596 test_imgfile
597 test_largefile
598 test_linuxaudiodev
599 test_locale
600 test_mmap
601 test_nis
602 test_ntpath
603 test_openpty
604 test_poll
605 test_popen2
606 test_pty
607 test_pwd
608 test_socket_ssl
609 test_socketserver
610 test_strop
611 test_sunaudiodev
612 test_sundry
613 test_thread
614 test_threaded_import
615 test_threadedtempfile
616 test_threading
617 test_timing
618 test_unicode_file
619 test_winreg
620 test_winsound
621 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000622 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000623 """
624 test_al
625 test_cd
626 test_cl
627 test_curses
628 test_dl
629 test_gdbm
630 test_gl
631 test_imgfile
632 test_largefile
633 test_linuxaudiodev
634 test_minidom
635 test_nis
636 test_ntpath
637 test_poll
638 test_socket_ssl
Jack Jansenf839c272001-12-14 21:28:53 +0000639 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000640 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000641 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000642 test_winreg
643 test_winsound
644 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000645}
646
Tim Petersb5b7b782001-08-12 01:20:39 +0000647class _ExpectedSkips:
648 def __init__(self):
649 self.valid = 0
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000650 if _expectations.has_key(sys.platform):
651 s = _expectations[sys.platform]
Tim Petersb5b7b782001-08-12 01:20:39 +0000652 self.expected = _Set(s.split())
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000653 self.valid = 1
Tim Petersb5b7b782001-08-12 01:20:39 +0000654
655 def isvalid(self):
656 "Return true iff _ExpectedSkips knows about the current platform."
657 return self.valid
658
659 def getexpected(self):
660 """Return set of test names we expect to skip on current platform.
661
662 self.isvalid() must be true.
663 """
664
665 assert self.isvalid()
666 return self.expected
667
Guido van Rossum152494a1996-12-20 03:12:20 +0000668if __name__ == '__main__':
Barry Warsaw08fca522001-08-20 22:33:46 +0000669 main()