blob: cd2442d1814a1060cbfed458f19db5e212f0385b [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:
Andrew MacIntyree41abab2002-04-30 12:11:04 +0000136 use_resources.append(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
Tim Peters1e33ffa2002-04-23 23:09:02 +0000509 test_resource
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000510 test_signal
Barry Warsaw08fca522001-08-20 22:33:46 +0000511 test_socket_ssl
Tim Petersa86f0c12001-09-18 02:18:57 +0000512 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000513 test_sunaudiodev
514 test_timing
515 """,
516 'linux2':
517 """
518 test_al
519 test_cd
520 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000521 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000522 test_dl
Guido van Rossum6184c112002-04-16 02:14:04 +0000523 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000524 test_gl
525 test_imgfile
526 test_largefile
527 test_nis
528 test_ntpath
Barry Warsaw08fca522001-08-20 22:33:46 +0000529 test_socket_ssl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000530 test_socketserver
531 test_sunaudiodev
532 test_unicode_file
533 test_winreg
534 test_winsound
535 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000536 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000537 """
538 test_al
539 test_bsddb
540 test_cd
541 test_cl
542 test_commands
543 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000544 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000545 test_dbm
546 test_dl
547 test_fcntl
548 test_fork1
549 test_gl
550 test_grp
551 test_imgfile
552 test_largefile
553 test_linuxaudiodev
554 test_locale
555 test_mmap
556 test_nis
557 test_ntpath
558 test_openpty
559 test_poll
560 test_popen2
561 test_pty
562 test_pwd
563 test_signal
564 test_socket_ssl
565 test_socketserver
566 test_sunaudiodev
567 test_sundry
568 test_timing
569 test_unicode_file
570 test_winreg
571 test_winsound
572 """,
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000573 'unixware5':
574 """
575 test_al
576 test_bsddb
577 test_cd
578 test_cl
579 test_dl
580 test_gl
581 test_imgfile
582 test_largefile
583 test_linuxaudiodev
584 test_minidom
585 test_nis
586 test_ntpath
587 test_openpty
588 test_pyexpat
589 test_sax
590 test_socketserver
591 test_sunaudiodev
592 test_sundry
593 test_unicode_file
594 test_winreg
595 test_winsound
596 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000597 'riscos':
598 """
599 test_al
600 test_asynchat
601 test_bsddb
602 test_cd
603 test_cl
604 test_commands
605 test_crypt
606 test_dbm
607 test_dl
608 test_fcntl
609 test_fork1
610 test_gdbm
611 test_gl
612 test_grp
613 test_imgfile
614 test_largefile
615 test_linuxaudiodev
616 test_locale
617 test_mmap
618 test_nis
619 test_ntpath
620 test_openpty
621 test_poll
622 test_popen2
623 test_pty
624 test_pwd
625 test_socket_ssl
626 test_socketserver
627 test_strop
628 test_sunaudiodev
629 test_sundry
630 test_thread
631 test_threaded_import
632 test_threadedtempfile
633 test_threading
634 test_timing
635 test_unicode_file
636 test_winreg
637 test_winsound
638 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000639 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000640 """
641 test_al
642 test_cd
643 test_cl
644 test_curses
645 test_dl
646 test_gdbm
647 test_gl
648 test_imgfile
649 test_largefile
650 test_linuxaudiodev
651 test_minidom
652 test_nis
653 test_ntpath
654 test_poll
655 test_socket_ssl
Jack Jansenf839c272001-12-14 21:28:53 +0000656 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000657 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000658 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000659 test_winreg
660 test_winsound
661 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000662 'hp-ux11':
663 """
664 test_al
665 test_bsddb
666 test_cd
667 test_cl
668 test_curses
669 test_dl
670 test_gdbm
671 test_gl
672 test_gzip
673 test_imgfile
674 test_largefile
675 test_linuxaudiodev
676 test_locale
677 test_minidom
678 test_nis
679 test_ntpath
680 test_openpty
681 test_pyexpat
682 test_sax
683 test_socket_ssl
684 test_socketserver
685 test_sunaudiodev
686 test_unicode_file
687 test_winreg
688 test_winsound
689 test_zipfile
690 test_zlib
691 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000692}
693
Tim Petersb5b7b782001-08-12 01:20:39 +0000694class _ExpectedSkips:
695 def __init__(self):
696 self.valid = 0
Tim Petersde14a302002-04-01 05:04:46 +0000697 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000698 s = _expectations[sys.platform]
Tim Petersb5b7b782001-08-12 01:20:39 +0000699 self.expected = _Set(s.split())
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000700 self.valid = 1
Tim Petersb5b7b782001-08-12 01:20:39 +0000701
702 def isvalid(self):
703 "Return true iff _ExpectedSkips knows about the current platform."
704 return self.valid
705
706 def getexpected(self):
707 """Return set of test names we expect to skip on current platform.
708
709 self.isvalid() must be true.
710 """
711
712 assert self.isvalid()
713 return self.expected
714
Guido van Rossum152494a1996-12-20 03:12:20 +0000715if __name__ == '__main__':
Barry Warsaw08fca522001-08-20 22:33:46 +0000716 main()