Bump version number to 2.4.2 to pick up the latest minor bug fixes.
[python/dscho.git] / Lib / test / regrtest.py
blob248bad45b82f8d6cbdc0789313b3372ee8a08ee6
1 #! /usr/bin/env python
3 """Regression test.
5 This will find all modules whose name is "test_*" in the test
6 directory, and run them. Various command line options provide
7 additional facilities.
9 Command line options:
11 -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
17 -f: fromfile -- read names of tests to run from a file (see below)
18 -l: findleaks -- if GC is available detect tests that leak memory
19 -u: use -- specify which special resource intensive tests to run
20 -h: help -- print this text and exit
21 -t: threshold -- call gc.set_threshold(N)
23 If non-option arguments are present, they are names for tests to run,
24 unless -x is given, in which case they are names for tests not to run.
25 If no test names are given, all tests are run.
27 -v is incompatible with -g and does not compare test output files.
29 -s means to run only a single test and exit. This is useful when
30 doing memory analysis on the Python interpreter (which tend to consume
31 too many resources to run the full regression test non-stop). The
32 file /tmp/pynexttest is read to find the next test to run. If this
33 file is missing, the first test_*.py file in testdir or on the command
34 line is used. (actually tempfile.gettempdir() is used instead of
35 /tmp).
37 -f reads the names of tests from the file given as f's argument, one
38 or more test names per line. Whitespace is ignored. Blank lines and
39 lines beginning with '#' are ignored. This is especially useful for
40 whittling down failures involving interactions among tests.
42 -u is used to specify which special resource intensive tests to run,
43 such as those requiring large file support or network connectivity.
44 The argument is a comma-separated list of words indicating the
45 resources to test. Currently only the following are defined:
47 all - Enable all special resources.
49 curses - Tests that use curses and will modify the terminal's
50 state and output modes.
52 largefile - It is okay to run some test that may create huge
53 files. These tests can take a long time and may
54 consume >2GB of disk space temporarily.
56 network - It is okay to run tests that use external network
57 resource, e.g. testing SSL support for sockets.
58 """
60 import sys
61 import os
62 import getopt
63 import traceback
64 import random
65 import StringIO
66 import warnings
67 from sets import Set
69 # I see no other way to suppress these warnings;
70 # putting them in test_grammar.py has no effect:
71 warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
72 ".*test.test_grammar$")
73 if sys.maxint > 0x7fffffff:
74 # Also suppress them in <string>, because for 64-bit platforms,
75 # that's where test_grammar.py hides them.
76 warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
77 "<string>")
79 from test import test_support
81 RESOURCE_NAMES = ('curses', 'largefile', 'network')
84 def usage(code, msg=''):
85 print __doc__
86 if msg: print msg
87 sys.exit(code)
90 def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
91 exclude=0, single=0, randomize=0, fromfile=None, findleaks=0,
92 use_resources=None):
93 """Execute a test suite.
95 This also parses command-line options and modifies its behavior
96 accordingly.
98 tests -- a list of strings containing test names (optional)
99 testdir -- the directory in which to look for tests (optional)
101 Users other than the Python test suite will certainly want to
102 specify testdir; if it's omitted, the directory containing the
103 Python test suite is searched for.
105 If the tests argument is omitted, the tests listed on the
106 command-line will be used. If that's empty, too, then all *.py
107 files beginning with test_ will be used.
109 The other default arguments (verbose, quiet, generate, exclude,
110 single, randomize, findleaks, and use_resources) allow programmers
111 calling main() directly to set the values that would normally be
112 set by flags on the command line.
116 test_support.record_original_stdout(sys.stdout)
117 try:
118 opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsrf:lu:t:',
119 ['help', 'verbose', 'quiet', 'generate',
120 'exclude', 'single', 'random', 'fromfile',
121 'findleaks', 'use=', 'threshold='])
122 except getopt.error, msg:
123 usage(2, msg)
125 # Defaults
126 if use_resources is None:
127 use_resources = []
128 for o, a in opts:
129 if o in ('-h', '--help'):
130 usage(0)
131 elif o in ('-v', '--verbose'):
132 verbose += 1
133 elif o in ('-q', '--quiet'):
134 quiet = 1;
135 verbose = 0
136 elif o in ('-g', '--generate'):
137 generate = 1
138 elif o in ('-x', '--exclude'):
139 exclude = 1
140 elif o in ('-s', '--single'):
141 single = 1
142 elif o in ('-r', '--randomize'):
143 randomize = 1
144 elif o in ('-f', '--fromfile'):
145 fromfile = a
146 elif o in ('-l', '--findleaks'):
147 findleaks = 1
148 elif o in ('-t', '--threshold'):
149 import gc
150 gc.set_threshold(int(a))
151 elif o in ('-u', '--use'):
152 u = [x.lower() for x in a.split(',')]
153 for r in u:
154 if r == 'all':
155 use_resources = RESOURCE_NAMES
156 break
157 if r not in RESOURCE_NAMES:
158 usage(1, 'Invalid -u/--use option: ' + a)
159 if r not in use_resources:
160 use_resources.append(r)
161 if generate and verbose:
162 usage(2, "-g and -v don't go together!")
163 if single and fromfile:
164 usage(2, "-s and -f don't go together!")
166 good = []
167 bad = []
168 skipped = []
170 if findleaks:
171 try:
172 import gc
173 except ImportError:
174 print 'No GC available, disabling findleaks.'
175 findleaks = 0
176 else:
177 # Uncomment the line below to report garbage that is not
178 # freeable by reference counting alone. By default only
179 # garbage that is not collectable by the GC is reported.
180 #gc.set_debug(gc.DEBUG_SAVEALL)
181 found_garbage = []
183 if single:
184 from tempfile import gettempdir
185 filename = os.path.join(gettempdir(), 'pynexttest')
186 try:
187 fp = open(filename, 'r')
188 next = fp.read().strip()
189 tests = [next]
190 fp.close()
191 except IOError:
192 pass
194 if fromfile:
195 tests = []
196 fp = open(fromfile)
197 for line in fp:
198 guts = line.split() # assuming no test has whitespace in its name
199 if guts and not guts[0].startswith('#'):
200 tests.extend(guts)
201 fp.close()
203 # Strip .py extensions.
204 if args:
205 args = map(removepy, args)
206 if tests:
207 tests = map(removepy, tests)
209 stdtests = STDTESTS[:]
210 nottests = NOTTESTS[:]
211 if exclude:
212 for arg in args:
213 if arg in stdtests:
214 stdtests.remove(arg)
215 nottests[:0] = args
216 args = []
217 tests = tests or args or findtests(testdir, stdtests, nottests)
218 if single:
219 tests = tests[:1]
220 if randomize:
221 random.shuffle(tests)
222 test_support.verbose = verbose # Tell tests to be moderately quiet
223 test_support.use_resources = use_resources
224 save_modules = sys.modules.keys()
225 for test in tests:
226 if not quiet:
227 print test
228 sys.stdout.flush()
229 ok = runtest(test, generate, verbose, quiet, testdir)
230 if ok > 0:
231 good.append(test)
232 elif ok == 0:
233 bad.append(test)
234 else:
235 skipped.append(test)
236 if findleaks:
237 gc.collect()
238 if gc.garbage:
239 print "Warning: test created", len(gc.garbage),
240 print "uncollectable object(s)."
241 # move the uncollectable objects somewhere so we don't see
242 # them again
243 found_garbage.extend(gc.garbage)
244 del gc.garbage[:]
245 # Unload the newly imported modules (best effort finalization)
246 for module in sys.modules.keys():
247 if module not in save_modules and module.startswith("test."):
248 test_support.unload(module)
250 # The lists won't be sorted if running with -r
251 good.sort()
252 bad.sort()
253 skipped.sort()
255 if good and not quiet:
256 if not bad and not skipped and len(good) > 1:
257 print "All",
258 print count(len(good), "test"), "OK."
259 if verbose:
260 print "CAUTION: stdout isn't compared in verbose mode:"
261 print "a test that passes in verbose mode may fail without it."
262 if bad:
263 print count(len(bad), "test"), "failed:"
264 printlist(bad)
265 if skipped and not quiet:
266 print count(len(skipped), "test"), "skipped:"
267 printlist(skipped)
269 e = _ExpectedSkips()
270 plat = sys.platform
271 if e.isvalid():
272 surprise = Set(skipped) - e.getexpected()
273 if surprise:
274 print count(len(surprise), "skip"), \
275 "unexpected on", plat + ":"
276 printlist(surprise)
277 else:
278 print "Those skips are all expected on", plat + "."
279 else:
280 print "Ask someone to teach regrtest.py about which tests are"
281 print "expected to get skipped on", plat + "."
283 if single:
284 alltests = findtests(testdir, stdtests, nottests)
285 for i in range(len(alltests)):
286 if tests[0] == alltests[i]:
287 if i == len(alltests) - 1:
288 os.unlink(filename)
289 else:
290 fp = open(filename, 'w')
291 fp.write(alltests[i+1] + '\n')
292 fp.close()
293 break
294 else:
295 os.unlink(filename)
297 sys.exit(len(bad) > 0)
300 STDTESTS = [
301 'test_grammar',
302 'test_opcodes',
303 'test_operations',
304 'test_builtin',
305 'test_exceptions',
306 'test_types',
309 NOTTESTS = [
310 'test_support',
311 'test_b1',
312 'test_b2',
313 'test_future1',
314 'test_future2',
315 'test_future3',
318 def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
319 """Return a list of all applicable test modules."""
320 if not testdir: testdir = findtestdir()
321 names = os.listdir(testdir)
322 tests = []
323 for name in names:
324 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
325 modname = name[:-3]
326 if modname not in stdtests and modname not in nottests:
327 tests.append(modname)
328 tests.sort()
329 return stdtests + tests
331 def runtest(test, generate, verbose, quiet, testdir = None):
332 """Run a single test.
333 test -- the name of the test
334 generate -- if true, generate output, instead of running the test
335 and comparing it to a previously created output file
336 verbose -- if true, print more messages
337 quiet -- if true, don't print 'skipped' messages (probably redundant)
338 testdir -- test directory
340 test_support.unload(test)
341 if not testdir: testdir = findtestdir()
342 outputdir = os.path.join(testdir, "output")
343 outputfile = os.path.join(outputdir, test)
344 if verbose:
345 cfp = None
346 else:
347 cfp = StringIO.StringIO()
348 try:
349 save_stdout = sys.stdout
350 try:
351 if cfp:
352 sys.stdout = cfp
353 print test # Output file starts with test name
354 if test.startswith('test.'):
355 abstest = test
356 else:
357 # Always import it from the test package
358 abstest = 'test.' + test
359 the_package = __import__(abstest, globals(), locals(), [])
360 the_module = getattr(the_package, test)
361 # Most tests run to completion simply as a side-effect of
362 # being imported. For the benefit of tests that can't run
363 # that way (like test_threaded_import), explicitly invoke
364 # their test_main() function (if it exists).
365 indirect_test = getattr(the_module, "test_main", None)
366 if indirect_test is not None:
367 indirect_test()
368 finally:
369 sys.stdout = save_stdout
370 except (ImportError, test_support.TestSkipped), msg:
371 if not quiet:
372 print "test", test, "skipped --", msg
373 sys.stdout.flush()
374 return -1
375 except KeyboardInterrupt:
376 raise
377 except test_support.TestFailed, msg:
378 print "test", test, "failed --", msg
379 sys.stdout.flush()
380 return 0
381 except:
382 type, value = sys.exc_info()[:2]
383 print "test", test, "crashed --", str(type) + ":", value
384 sys.stdout.flush()
385 if verbose:
386 traceback.print_exc(file=sys.stdout)
387 sys.stdout.flush()
388 return 0
389 else:
390 if not cfp:
391 return 1
392 output = cfp.getvalue()
393 if generate:
394 if output == test + "\n":
395 if os.path.exists(outputfile):
396 # Write it since it already exists (and the contents
397 # may have changed), but let the user know it isn't
398 # needed:
399 print "output file", outputfile, \
400 "is no longer needed; consider removing it"
401 else:
402 # We don't need it, so don't create it.
403 return 1
404 fp = open(outputfile, "w")
405 fp.write(output)
406 fp.close()
407 return 1
408 if os.path.exists(outputfile):
409 fp = open(outputfile, "r")
410 expected = fp.read()
411 fp.close()
412 else:
413 expected = test + "\n"
414 if output == expected:
415 return 1
416 print "test", test, "produced unexpected output:"
417 sys.stdout.flush()
418 reportdiff(expected, output)
419 sys.stdout.flush()
420 return 0
422 def reportdiff(expected, output):
423 import difflib
424 print "*" * 70
425 a = expected.splitlines(1)
426 b = output.splitlines(1)
427 sm = difflib.SequenceMatcher(a=a, b=b)
428 tuples = sm.get_opcodes()
430 def pair(x0, x1):
431 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
432 x0 += 1
433 if x0 >= x1:
434 return "line " + str(x0)
435 else:
436 return "lines %d-%d" % (x0, x1)
438 for op, a0, a1, b0, b1 in tuples:
439 if op == 'equal':
440 pass
442 elif op == 'delete':
443 print "***", pair(a0, a1), "of expected output missing:"
444 for line in a[a0:a1]:
445 print "-", line,
447 elif op == 'replace':
448 print "*** mismatch between", pair(a0, a1), "of expected", \
449 "output and", pair(b0, b1), "of actual output:"
450 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
451 print line,
453 elif op == 'insert':
454 print "***", pair(b0, b1), "of actual output doesn't appear", \
455 "in expected output after line", str(a1)+":"
456 for line in b[b0:b1]:
457 print "+", line,
459 else:
460 print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
462 print "*" * 70
464 def findtestdir():
465 if __name__ == '__main__':
466 file = sys.argv[0]
467 else:
468 file = __file__
469 testdir = os.path.dirname(file) or os.curdir
470 return testdir
472 def removepy(name):
473 if name.endswith(os.extsep + "py"):
474 name = name[:-3]
475 return name
477 def count(n, word):
478 if n == 1:
479 return "%d %s" % (n, word)
480 else:
481 return "%d %ss" % (n, word)
483 def printlist(x, width=70, indent=4):
484 """Print the elements of iterable x to stdout.
486 Optional arg width (default 70) is the maximum line length.
487 Optional arg indent (default 4) is the number of blanks with which to
488 begin each line.
491 from textwrap import fill
492 blanks = ' ' * indent
493 print fill(' '.join(map(str, x)), width,
494 initial_indent=blanks, subsequent_indent=blanks)
496 # Map sys.platform to a string containing the basenames of tests
497 # expected to be skipped on that platform.
499 # Special cases:
500 # test_pep277
501 # The _ExpectedSkips constructor adds this to the set of expected
502 # skips if not os.path.supports_unicode_filenames.
504 _expectations = {
505 'win32':
507 test_al
508 test_cd
509 test_cl
510 test_commands
511 test_crypt
512 test_curses
513 test_dbm
514 test_dl
515 test_email_codecs
516 test_fcntl
517 test_fork1
518 test_gdbm
519 test_gl
520 test_grp
521 test_imgfile
522 test_largefile
523 test_linuxaudiodev
524 test_mhlib
525 test_mpz
526 test_nis
527 test_openpty
528 test_poll
529 test_pty
530 test_pwd
531 test_resource
532 test_signal
533 test_socket_ssl
534 test_socketserver
535 test_sunaudiodev
536 test_timing
537 """,
538 'linux2':
540 test_al
541 test_cd
542 test_cl
543 test_curses
544 test_dl
545 test_email_codecs
546 test_gl
547 test_imgfile
548 test_largefile
549 test_nis
550 test_ntpath
551 test_socket_ssl
552 test_socketserver
553 test_sunaudiodev
554 test_unicode_file
555 test_winreg
556 test_winsound
557 """,
558 'mac':
560 test_al
561 test_bsddb
562 test_cd
563 test_cl
564 test_commands
565 test_crypt
566 test_curses
567 test_dbm
568 test_dl
569 test_fcntl
570 test_fork1
571 test_gl
572 test_grp
573 test_imgfile
574 test_largefile
575 test_linuxaudiodev
576 test_locale
577 test_mmap
578 test_nis
579 test_ntpath
580 test_openpty
581 test_poll
582 test_popen2
583 test_pty
584 test_pwd
585 test_signal
586 test_socket_ssl
587 test_socketserver
588 test_sunaudiodev
589 test_sundry
590 test_timing
591 test_unicode_file
592 test_winreg
593 test_winsound
594 """,
595 'unixware7':
597 test_al
598 test_bsddb
599 test_cd
600 test_cl
601 test_dl
602 test_gl
603 test_imgfile
604 test_largefile
605 test_linuxaudiodev
606 test_minidom
607 test_nis
608 test_ntpath
609 test_openpty
610 test_pyexpat
611 test_sax
612 test_socketserver
613 test_sunaudiodev
614 test_sundry
615 test_unicode_file
616 test_winreg
617 test_winsound
618 """,
619 'openunix8':
621 test_al
622 test_bsddb
623 test_cd
624 test_cl
625 test_dl
626 test_gl
627 test_imgfile
628 test_largefile
629 test_linuxaudiodev
630 test_minidom
631 test_nis
632 test_ntpath
633 test_openpty
634 test_pyexpat
635 test_sax
636 test_socketserver
637 test_sunaudiodev
638 test_sundry
639 test_unicode_file
640 test_winreg
641 test_winsound
642 """,
643 'sco_sv3':
645 test_al
646 test_asynchat
647 test_bsddb
648 test_cd
649 test_cl
650 test_dl
651 test_fork1
652 test_gettext
653 test_gl
654 test_imgfile
655 test_largefile
656 test_linuxaudiodev
657 test_locale
658 test_minidom
659 test_nis
660 test_ntpath
661 test_openpty
662 test_pyexpat
663 test_queue
664 test_sax
665 test_socketserver
666 test_sunaudiodev
667 test_sundry
668 test_thread
669 test_threaded_import
670 test_threadedtempfile
671 test_threading
672 test_unicode_file
673 test_winreg
674 test_winsound
675 """,
676 'riscos':
678 test_al
679 test_asynchat
680 test_bsddb
681 test_cd
682 test_cl
683 test_commands
684 test_crypt
685 test_dbm
686 test_dl
687 test_fcntl
688 test_fork1
689 test_gdbm
690 test_gl
691 test_grp
692 test_imgfile
693 test_largefile
694 test_linuxaudiodev
695 test_locale
696 test_mmap
697 test_nis
698 test_ntpath
699 test_openpty
700 test_poll
701 test_popen2
702 test_pty
703 test_pwd
704 test_socket_ssl
705 test_socketserver
706 test_strop
707 test_sunaudiodev
708 test_sundry
709 test_thread
710 test_threaded_import
711 test_threadedtempfile
712 test_threading
713 test_timing
714 test_unicode_file
715 test_winreg
716 test_winsound
717 """,
718 'darwin':
720 test_al
721 test_cd
722 test_cl
723 test_curses
724 test_dl
725 test_gdbm
726 test_gl
727 test_imgfile
728 test_largefile
729 test_linuxaudiodev
730 test_minidom
731 test_nis
732 test_ntpath
733 test_poll
734 test_socket_ssl
735 test_socketserver
736 test_sunaudiodev
737 test_unicode_file
738 test_winreg
739 test_winsound
740 """,
741 'sunos5':
743 test_al
744 test_bsddb
745 test_cd
746 test_cl
747 test_curses
748 test_dbm
749 test_email_codecs
750 test_gdbm
751 test_gl
752 test_gzip
753 test_imgfile
754 test_linuxaudiodev
755 test_mpz
756 test_openpty
757 test_socket_ssl
758 test_socketserver
759 test_winreg
760 test_winsound
761 test_zipfile
762 test_zlib
763 """,
764 'hp-ux11':
766 test_al
767 test_bsddb
768 test_cd
769 test_cl
770 test_curses
771 test_dl
772 test_gdbm
773 test_gl
774 test_gzip
775 test_imgfile
776 test_largefile
777 test_linuxaudiodev
778 test_locale
779 test_minidom
780 test_nis
781 test_ntpath
782 test_openpty
783 test_pyexpat
784 test_sax
785 test_socket_ssl
786 test_socketserver
787 test_sunaudiodev
788 test_unicode_file
789 test_winreg
790 test_winsound
791 test_zipfile
792 test_zlib
793 """,
794 'atheos':
796 test_al
797 test_cd
798 test_cl
799 test_curses
800 test_dl
801 test_email_codecs
802 test_gdbm
803 test_gl
804 test_imgfile
805 test_largefile
806 test_linuxaudiodev
807 test_locale
808 test_mhlib
809 test_mmap
810 test_mpz
811 test_nis
812 test_poll
813 test_popen2
814 test_resource
815 test_socket_ssl
816 test_socketserver
817 test_sunaudiodev
818 test_unicode_file
819 test_winreg
820 test_winsound
821 """,
824 class _ExpectedSkips:
825 def __init__(self):
826 import os.path
827 self.valid = False
828 if sys.platform in _expectations:
829 s = _expectations[sys.platform]
830 self.expected = Set(s.split())
831 if not os.path.supports_unicode_filenames:
832 self.expected.add('test_pep277')
833 self.valid = True
835 def isvalid(self):
836 "Return true iff _ExpectedSkips knows about the current platform."
837 return self.valid
839 def getexpected(self):
840 """Return set of test names we expect to skip on current platform.
842 self.isvalid() must be true.
845 assert self.isvalid()
846 return self.expected
848 if __name__ == '__main__':
849 # Remove regrtest.py's own directory from the module search path. This
850 # prevents relative imports from working, and relative imports will screw
851 # up the testing framework. E.g. if both test.test_support and
852 # test_support are imported, they will not contain the same globals, and
853 # much of the testing framework relies on the globals in the
854 # test.test_support module.
855 mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
856 i = pathlen = len(sys.path)
857 while i >= 0:
858 i -= 1
859 if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
860 del sys.path[i]
861 if len(sys.path) == pathlen:
862 print 'Could not find %r in sys.path to remove it' % mydir
863 main()