blob: b5ae97b160a37f41b909a7e367cd44a60a0d414e [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)
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000135 use_resources.extend(u)
Guido van Rossuma4122201997-08-18 20:08:24 +0000136 if generate and verbose:
Barry Warsaw08fca522001-08-20 22:33:46 +0000137 usage(2, "-g and -v don't go together!")
138
Guido van Rossum152494a1996-12-20 03:12:20 +0000139 good = []
140 bad = []
141 skipped = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000142
Neil Schemenauerd569f232000-09-22 15:29:28 +0000143 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000144 try:
145 import gc
146 except ImportError:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000147 print 'No GC available, disabling findleaks.'
Neil Schemenauerd569f232000-09-22 15:29:28 +0000148 findleaks = 0
Barry Warsawa873b032000-08-03 15:50:37 +0000149 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000150 # Uncomment the line below to report garbage that is not
151 # freeable by reference counting alone. By default only
152 # garbage that is not collectable by the GC is reported.
153 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000154 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000155
Barry Warsawe11e3de1999-01-28 19:51:51 +0000156 if single:
157 from tempfile import gettempdir
158 filename = os.path.join(gettempdir(), 'pynexttest')
159 try:
160 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000161 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000162 tests = [next]
163 fp.close()
164 except IOError:
165 pass
Guido van Rossuma4122201997-08-18 20:08:24 +0000166 for i in range(len(args)):
Guido van Rossum41360a41998-03-26 19:42:58 +0000167 # Strip trailing ".py" from arguments
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000168 if args[i][-3:] == os.extsep+'py':
Guido van Rossum41360a41998-03-26 19:42:58 +0000169 args[i] = args[i][:-3]
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000170 stdtests = STDTESTS[:]
171 nottests = NOTTESTS[:]
Guido van Rossum152494a1996-12-20 03:12:20 +0000172 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000173 for arg in args:
174 if arg in stdtests:
175 stdtests.remove(arg)
176 nottests[:0] = args
Guido van Rossum41360a41998-03-26 19:42:58 +0000177 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000178 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000179 if single:
180 tests = tests[:1]
Skip Montanaroab1c7912000-06-30 16:39:27 +0000181 if randomize:
182 random.shuffle(tests)
Guido van Rossum41360a41998-03-26 19:42:58 +0000183 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000184 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000185 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000186 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000187 if not quiet:
188 print test
Trent Mickf29f47b2000-08-11 19:02:59 +0000189 ok = runtest(test, generate, verbose, quiet, testdir)
Guido van Rossum41360a41998-03-26 19:42:58 +0000190 if ok > 0:
191 good.append(test)
192 elif ok == 0:
193 bad.append(test)
194 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000195 skipped.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000196 if findleaks:
197 gc.collect()
198 if gc.garbage:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000199 print "Warning: test created", len(gc.garbage),
200 print "uncollectable object(s)."
201 # move the uncollectable objects somewhere so we don't see
202 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000203 found_garbage.extend(gc.garbage)
204 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000205 # Unload the newly imported modules (best effort finalization)
206 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000207 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000208 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000209
210 # The lists won't be sorted if running with -r
211 good.sort()
212 bad.sort()
213 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000214
Guido van Rossum152494a1996-12-20 03:12:20 +0000215 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000216 if not bad and not skipped and len(good) > 1:
217 print "All",
218 print count(len(good), "test"), "OK."
Tim Peters1a4d77b2000-12-30 22:21:22 +0000219 if verbose:
220 print "CAUTION: stdout isn't compared in verbose mode: a test"
221 print "that passes in verbose mode may fail without it."
Guido van Rossum152494a1996-12-20 03:12:20 +0000222 if bad:
Tim Petersa45da922001-08-12 03:45:50 +0000223 print count(len(bad), "test"), "failed:"
224 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000225 if skipped and not quiet:
Tim Petersa45da922001-08-12 03:45:50 +0000226 print count(len(skipped), "test"), "skipped:"
227 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000228
Tim Petersb5b7b782001-08-12 01:20:39 +0000229 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000230 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000231 if e.isvalid():
232 surprise = _Set(skipped) - e.getexpected()
Tim Petersb5b7b782001-08-12 01:20:39 +0000233 if surprise:
234 print count(len(surprise), "skip"), \
Tim Petersa45da922001-08-12 03:45:50 +0000235 "unexpected on", plat + ":"
236 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000237 else:
238 print "Those skips are all expected on", plat + "."
239 else:
240 print "Ask someone to teach regrtest.py about which tests are"
241 print "expected to get skipped on", plat + "."
242
Barry Warsawe11e3de1999-01-28 19:51:51 +0000243 if single:
244 alltests = findtests(testdir, stdtests, nottests)
245 for i in range(len(alltests)):
246 if tests[0] == alltests[i]:
247 if i == len(alltests) - 1:
248 os.unlink(filename)
249 else:
250 fp = open(filename, 'w')
251 fp.write(alltests[i+1] + '\n')
252 fp.close()
253 break
254 else:
255 os.unlink(filename)
256
Barry Warsaw08fca522001-08-20 22:33:46 +0000257 sys.exit(len(bad) > 0)
258
Guido van Rossum152494a1996-12-20 03:12:20 +0000259
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000260STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000261 'test_grammar',
262 'test_opcodes',
263 'test_operations',
264 'test_builtin',
265 'test_exceptions',
266 'test_types',
267 ]
268
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000269NOTTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000270 'test_support',
271 'test_b1',
272 'test_b2',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000273 'test_future1',
274 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000275 'test_future3',
Guido van Rossum152494a1996-12-20 03:12:20 +0000276 ]
277
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000278def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000279 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000280 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000281 names = os.listdir(testdir)
282 tests = []
283 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000284 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000285 modname = name[:-3]
286 if modname not in stdtests and modname not in nottests:
287 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000288 tests.sort()
289 return stdtests + tests
290
Trent Mickf29f47b2000-08-11 19:02:59 +0000291def runtest(test, generate, verbose, quiet, testdir = None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000292 """Run a single test.
293 test -- the name of the test
294 generate -- if true, generate output, instead of running the test
295 and comparing it to a previously created output file
296 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000297 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000298 testdir -- test directory
299 """
Guido van Rossum152494a1996-12-20 03:12:20 +0000300 test_support.unload(test)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000301 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000302 outputdir = os.path.join(testdir, "output")
303 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000304 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000305 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000306 else:
Fred Drake88a56852001-09-28 20:16:30 +0000307 cfp = StringIO.StringIO()
Guido van Rossum152494a1996-12-20 03:12:20 +0000308 try:
Tim Peters342ca752001-09-25 19:13:20 +0000309 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000310 try:
311 if cfp:
312 sys.stdout = cfp
313 print test # Output file starts with test name
Tim Petersd9742212001-05-22 18:28:25 +0000314 the_module = __import__(test, globals(), locals(), [])
315 # Most tests run to completion simply as a side-effect of
316 # being imported. For the benefit of tests that can't run
317 # that way (like test_threaded_import), explicitly invoke
318 # their test_main() function (if it exists).
319 indirect_test = getattr(the_module, "test_main", None)
320 if indirect_test is not None:
321 indirect_test()
Guido van Rossum41360a41998-03-26 19:42:58 +0000322 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000323 sys.stdout = save_stdout
Thomas Wouters3af826e2000-08-04 13:17:51 +0000324 except (ImportError, test_support.TestSkipped), msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000325 if not quiet:
Guido van Rossumeb949052001-09-18 20:34:19 +0000326 print "test", test, "skipped --", msg
Guido van Rossum41360a41998-03-26 19:42:58 +0000327 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000328 except KeyboardInterrupt:
329 raise
Guido van Rossum152494a1996-12-20 03:12:20 +0000330 except test_support.TestFailed, msg:
Guido van Rossum41360a41998-03-26 19:42:58 +0000331 print "test", test, "failed --", msg
332 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000333 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000334 type, value = sys.exc_info()[:2]
Fred Drake27c4b392000-08-23 20:34:40 +0000335 print "test", test, "crashed --", str(type) + ":", value
Guido van Rossum41360a41998-03-26 19:42:58 +0000336 if verbose:
337 traceback.print_exc(file=sys.stdout)
338 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000339 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000340 if not cfp:
341 return 1
342 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000343 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000344 if output == test + "\n":
345 if os.path.exists(outputfile):
346 # Write it since it already exists (and the contents
347 # may have changed), but let the user know it isn't
348 # needed:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000349 print "output file", outputfile, \
350 "is no longer needed; consider removing it"
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000351 else:
352 # We don't need it, so don't create it.
353 return 1
354 fp = open(outputfile, "w")
355 fp.write(output)
356 fp.close()
357 return 1
358 if os.path.exists(outputfile):
359 fp = open(outputfile, "r")
360 expected = fp.read()
361 fp.close()
362 else:
363 expected = test + "\n"
364 if output == expected:
365 return 1
366 print "test", test, "produced unexpected output:"
367 reportdiff(expected, output)
368 return 0
369
370def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000371 import difflib
Tim Petersc377b162001-09-22 05:31:03 +0000372 print "*" * 70
373 a = expected.splitlines(1)
374 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000375 sm = difflib.SequenceMatcher(a=a, b=b)
376 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000377
Guido van Rossumcf691932001-09-21 21:06:22 +0000378 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000379 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000380 x0 += 1
381 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000382 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000383 else:
Tim Petersc377b162001-09-22 05:31:03 +0000384 return "lines %d-%d" % (x0, x1)
385
Guido van Rossumcf691932001-09-21 21:06:22 +0000386 for op, a0, a1, b0, b1 in tuples:
387 if op == 'equal':
388 pass
Tim Petersc377b162001-09-22 05:31:03 +0000389
Guido van Rossumcf691932001-09-21 21:06:22 +0000390 elif op == 'delete':
Tim Petersc377b162001-09-22 05:31:03 +0000391 print "***", pair(a0, a1), "of expected output missing:"
Guido van Rossumcf691932001-09-21 21:06:22 +0000392 for line in a[a0:a1]:
Tim Petersc377b162001-09-22 05:31:03 +0000393 print "-", line,
394
Guido van Rossumcf691932001-09-21 21:06:22 +0000395 elif op == 'replace':
Tim Petersc377b162001-09-22 05:31:03 +0000396 print "*** mismatch between", pair(a0, a1), "of expected", \
397 "output and", pair(b0, b1), "of actual output:"
398 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
399 print line,
400
Guido van Rossumcf691932001-09-21 21:06:22 +0000401 elif op == 'insert':
Tim Petersc377b162001-09-22 05:31:03 +0000402 print "***", pair(b0, b1), "of actual output doesn't appear", \
403 "in expected output after line", str(a1)+":"
Guido van Rossumcf691932001-09-21 21:06:22 +0000404 for line in b[b0:b1]:
Tim Petersc377b162001-09-22 05:31:03 +0000405 print "+", line,
406
Guido van Rossumcf691932001-09-21 21:06:22 +0000407 else:
408 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
Tim Petersc377b162001-09-22 05:31:03 +0000409
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000410 print "*" * 70
Guido van Rossum152494a1996-12-20 03:12:20 +0000411
412def findtestdir():
413 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000414 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000415 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000416 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000417 testdir = os.path.dirname(file) or os.curdir
418 return testdir
419
420def count(n, word):
421 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000422 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000423 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000424 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000425
Tim Petersa45da922001-08-12 03:45:50 +0000426def printlist(x, width=70, indent=4):
427 """Print the elements of a sequence to stdout.
428
429 Optional arg width (default 70) is the maximum line length.
430 Optional arg indent (default 4) is the number of blanks with which to
431 begin each line.
432 """
433
434 line = ' ' * indent
435 for one in map(str, x):
436 w = len(line) + len(one)
437 if line[-1:] == ' ':
438 pad = ''
439 else:
440 pad = ' '
441 w += 1
442 if w > width:
443 print line
444 line = ' ' * indent + one
445 else:
446 line += pad + one
447 if len(line) > indent:
448 print line
449
Tim Petersb5b7b782001-08-12 01:20:39 +0000450class _Set:
451 def __init__(self, seq=[]):
452 data = self.data = {}
453 for x in seq:
454 data[x] = 1
455
456 def __len__(self):
457 return len(self.data)
458
459 def __sub__(self, other):
460 "Return set of all elements in self not in other."
461 result = _Set()
462 data = result.data = self.data.copy()
463 for x in other.data:
464 if x in data:
465 del data[x]
466 return result
467
Jeremy Hylton39f77bc2001-08-12 21:53:08 +0000468 def __iter__(self):
469 return iter(self.data)
470
Tim Petersb5b7b782001-08-12 01:20:39 +0000471 def tolist(self, sorted=1):
472 "Return _Set elements as a list."
473 data = self.data.keys()
474 if sorted:
475 data.sort()
476 return data
477
Tim Petersde14a302002-04-01 05:04:46 +0000478# Map sys.platform to a string containing the basenames of tests
479# expected to be skipped on that platform.
480
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000481_expectations = {
482 'win32':
483 """
484 test_al
485 test_cd
486 test_cl
487 test_commands
488 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000489 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000490 test_dbm
491 test_dl
Tim Petersdeb121a2002-04-11 19:52:58 +0000492 test_email_codecs
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000493 test_fcntl
494 test_fork1
495 test_gdbm
496 test_gl
497 test_grp
498 test_imgfile
499 test_largefile
500 test_linuxaudiodev
501 test_mhlib
Tim Petersde14a302002-04-01 05:04:46 +0000502 test_mpz
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000503 test_nis
504 test_openpty
505 test_poll
506 test_pty
507 test_pwd
508 test_signal
Barry Warsaw08fca522001-08-20 22:33:46 +0000509 test_socket_ssl
Tim Petersa86f0c12001-09-18 02:18:57 +0000510 test_socketserver
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000511 test_sunaudiodev
512 test_timing
513 """,
514 'linux2':
515 """
516 test_al
517 test_cd
518 test_cl
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000519 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000520 test_dl
521 test_gl
522 test_imgfile
523 test_largefile
524 test_nis
525 test_ntpath
Barry Warsaw08fca522001-08-20 22:33:46 +0000526 test_socket_ssl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000527 test_socketserver
528 test_sunaudiodev
529 test_unicode_file
530 test_winreg
531 test_winsound
532 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000533 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000534 """
535 test_al
536 test_bsddb
537 test_cd
538 test_cl
539 test_commands
540 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000541 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000542 test_dbm
543 test_dl
544 test_fcntl
545 test_fork1
546 test_gl
547 test_grp
548 test_imgfile
549 test_largefile
550 test_linuxaudiodev
551 test_locale
552 test_mmap
553 test_nis
554 test_ntpath
555 test_openpty
556 test_poll
557 test_popen2
558 test_pty
559 test_pwd
560 test_signal
561 test_socket_ssl
562 test_socketserver
563 test_sunaudiodev
564 test_sundry
565 test_timing
566 test_unicode_file
567 test_winreg
568 test_winsound
569 """,
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000570 'unixware5':
571 """
572 test_al
573 test_bsddb
574 test_cd
575 test_cl
576 test_dl
577 test_gl
578 test_imgfile
579 test_largefile
580 test_linuxaudiodev
581 test_minidom
582 test_nis
583 test_ntpath
584 test_openpty
585 test_pyexpat
586 test_sax
587 test_socketserver
588 test_sunaudiodev
589 test_sundry
590 test_unicode_file
591 test_winreg
592 test_winsound
593 """,
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000594 'riscos':
595 """
596 test_al
597 test_asynchat
598 test_bsddb
599 test_cd
600 test_cl
601 test_commands
602 test_crypt
603 test_dbm
604 test_dl
605 test_fcntl
606 test_fork1
607 test_gdbm
608 test_gl
609 test_grp
610 test_imgfile
611 test_largefile
612 test_linuxaudiodev
613 test_locale
614 test_mmap
615 test_nis
616 test_ntpath
617 test_openpty
618 test_poll
619 test_popen2
620 test_pty
621 test_pwd
622 test_socket_ssl
623 test_socketserver
624 test_strop
625 test_sunaudiodev
626 test_sundry
627 test_thread
628 test_threaded_import
629 test_threadedtempfile
630 test_threading
631 test_timing
632 test_unicode_file
633 test_winreg
634 test_winsound
635 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000636 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000637 """
638 test_al
639 test_cd
640 test_cl
641 test_curses
642 test_dl
643 test_gdbm
644 test_gl
645 test_imgfile
646 test_largefile
647 test_linuxaudiodev
648 test_minidom
649 test_nis
650 test_ntpath
651 test_poll
652 test_socket_ssl
Jack Jansenf839c272001-12-14 21:28:53 +0000653 test_socketserver
Jack Jansen398c2362001-12-02 21:41:36 +0000654 test_sunaudiodev
Jack Jansenf839c272001-12-14 21:28:53 +0000655 test_unicode_file
Jack Jansen398c2362001-12-02 21:41:36 +0000656 test_winreg
657 test_winsound
658 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000659 'hp-ux11':
660 """
661 test_al
662 test_bsddb
663 test_cd
664 test_cl
665 test_curses
666 test_dl
667 test_gdbm
668 test_gl
669 test_gzip
670 test_imgfile
671 test_largefile
672 test_linuxaudiodev
673 test_locale
674 test_minidom
675 test_nis
676 test_ntpath
677 test_openpty
678 test_pyexpat
679 test_sax
680 test_socket_ssl
681 test_socketserver
682 test_sunaudiodev
683 test_unicode_file
684 test_winreg
685 test_winsound
686 test_zipfile
687 test_zlib
688 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000689}
690
Tim Petersb5b7b782001-08-12 01:20:39 +0000691class _ExpectedSkips:
692 def __init__(self):
693 self.valid = 0
Tim Petersde14a302002-04-01 05:04:46 +0000694 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000695 s = _expectations[sys.platform]
Tim Petersb5b7b782001-08-12 01:20:39 +0000696 self.expected = _Set(s.split())
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000697 self.valid = 1
Tim Petersb5b7b782001-08-12 01:20:39 +0000698
699 def isvalid(self):
700 "Return true iff _ExpectedSkips knows about the current platform."
701 return self.valid
702
703 def getexpected(self):
704 """Return set of test names we expect to skip on current platform.
705
706 self.isvalid() must be true.
707 """
708
709 assert self.isvalid()
710 return self.expected
711
Guido van Rossum152494a1996-12-20 03:12:20 +0000712if __name__ == '__main__':
Barry Warsaw08fca522001-08-20 22:33:46 +0000713 main()