blob: 060fd8209c2d6595239db064966e962a6f6ddc2d [file] [log] [blame]
Barry Warsaw272c00b1996-12-10 19:51:10 +00001"""
2Automatic Python regression test.
3
4The list of individual tests is contained in the `testall' module.
5These test some (but not all) essential parts of the Python
6interpreter and built-in modules. When a test fails, an exception is
7raised and testing halts. When a test succeeds, it can produce quite
8a lot of output, which is compared against the output from a previous
9run. If a difference is noticed it raises an exception; if all is
10well, it prints nothing except 'All tests OK.' at the very end.
11
12The output from a previous run is supposed to be contained in separate
Barry Warsawe4a252e1996-12-10 23:19:14 +000013files (one per test) in the `output' subdirectory somewhere on the
Barry Warsaw272c00b1996-12-10 19:51:10 +000014search path for modules (sys.path, initialized from $PYTHONPATH plus
15some default places).
16
17Of course, if the normal output of the tests is changed because the
18tests have been changed (rather than a test producing the wrong
19output), 'autotest' will fail as well. In this case, run 'autotest'
20with the -g option.
21
22Usage:
23
24 %s [-g] [-w] [-h] [test1 [test2 ...]]
25
26Options:
27
28 -g, --generate : generate the output files instead of verifying
29 the results
30
31 -w, --warn : warn about un-importable tests
32
33 -h, --help : print this message
34
35If individual tests are provided on the command line, only those tests
36will be performed or generated. Otherwise, all tests (as contained in
37testall.py) will be performed.
38
39"""
Guido van Rossum85f18201992-11-27 22:53:50 +000040
41import os
42import sys
Barry Warsaw272c00b1996-12-10 19:51:10 +000043import getopt
44import traceback
45from test_support import *
Guido van Rossum85f18201992-11-27 22:53:50 +000046
Barry Warsaw272c00b1996-12-10 19:51:10 +000047# Exception raised when the test failed (not the same as in test_support)
48TestFailed = 'autotest.TestFailed'
Barry Warsawcb17a461996-12-12 22:34:26 +000049TestMissing = 'autotest.TestMissing'
Barry Warsaw272c00b1996-12-10 19:51:10 +000050
51# defaults
52generate = 0
53warn = 0
54
55
56
Guido van Rossum85f18201992-11-27 22:53:50 +000057# Function to find a file somewhere on sys.path
58def findfile(filename):
59 for dirname in sys.path:
60 fullname = os.path.join(dirname, filename)
61 if os.path.exists(fullname):
62 return fullname
63 return filename # Will cause exception later
64
Guido van Rossum85f18201992-11-27 22:53:50 +000065
Barry Warsaw272c00b1996-12-10 19:51:10 +000066
Guido van Rossum85f18201992-11-27 22:53:50 +000067# Class substituted for sys.stdout, to compare it with the given file
68class Compare:
Guido van Rossum7bc817d1993-12-17 15:25:27 +000069 def __init__(self, filename):
Guido van Rossum85f18201992-11-27 22:53:50 +000070 self.fp = open(filename, 'r')
Guido van Rossum85f18201992-11-27 22:53:50 +000071 def write(self, data):
72 expected = self.fp.read(len(data))
73 if data <> expected:
74 raise TestFailed, \
75 'Writing: '+`data`+', expected: '+`expected`
76 def close(self):
Barry Warsaw272c00b1996-12-10 19:51:10 +000077 leftover = self.fp.read()
78 if leftover:
79 raise TestFailed, 'Unread: '+`leftover`
Guido van Rossum85f18201992-11-27 22:53:50 +000080 self.fp.close()
81
Barry Warsaw272c00b1996-12-10 19:51:10 +000082
Guido van Rossum85f18201992-11-27 22:53:50 +000083# The main program
Barry Warsaw272c00b1996-12-10 19:51:10 +000084def usage(status):
85 print __doc__ % sys.argv[0]
86 sys.exit(status)
87
88
89
90def do_one_test(t, outdir):
91 filename = os.path.join(outdir, t)
Guido van Rossum85f18201992-11-27 22:53:50 +000092 real_stdout = sys.stdout
Guido van Rossumdbfed711996-12-11 16:54:54 +000093 if generate:
94 print 'Generating:', filename
95 fake_stdout = open(filename, 'w')
96 else:
Barry Warsaw1c92eba1996-12-12 22:21:10 +000097 try:
98 fake_stdout = Compare(filename)
99 except IOError:
Barry Warsawcb17a461996-12-12 22:34:26 +0000100 raise TestMissing
Guido van Rossum85f18201992-11-27 22:53:50 +0000101 try:
Guido van Rossumdbfed711996-12-11 16:54:54 +0000102 sys.stdout = fake_stdout
Barry Warsaw272c00b1996-12-10 19:51:10 +0000103 print t
104 unload(t)
105 try:
106 __import__(t, globals(), locals())
107 except ImportError, msg:
108 if warn:
109 sys.stderr.write(msg+': Un-installed'
110 ' optional module?\n')
Guido van Rossum85f18201992-11-27 22:53:50 +0000111 finally:
112 sys.stdout = real_stdout
Guido van Rossumdbfed711996-12-11 16:54:54 +0000113 fake_stdout.close()
Barry Warsaw272c00b1996-12-10 19:51:10 +0000114
115
116
117def main():
118 global generate
119 global warn
120 try:
121 opts, args = getopt.getopt(
122 sys.argv[1:], 'ghw',
123 ['generate', 'help', 'warn'])
124 except getopt.error, msg:
125 print msg
126 usage(1)
127 for opt, val in opts:
128 if opt in ['-h', '--help']:
129 usage(0)
130 elif opt in ['-g', '--generate']:
131 generate = 1
132 elif opt in ['-w', '--warn']:
133 warn = 1
134
135 # find the output directory
Barry Warsawe4a252e1996-12-10 23:19:14 +0000136 outdir = findfile('output')
Barry Warsaw272c00b1996-12-10 19:51:10 +0000137 if args:
138 tests = args
139 else:
140 import testall
141 tests = testall.tests
Barry Warsawcb17a461996-12-12 22:34:26 +0000142 failed = []
143 missing = []
Barry Warsaw272c00b1996-12-10 19:51:10 +0000144 for test in tests:
145 try:
146 do_one_test(test, outdir)
147 except TestFailed, msg:
Barry Warsaw272c00b1996-12-10 19:51:10 +0000148 traceback.print_exc()
Barry Warsawcb17a461996-12-12 22:34:26 +0000149 failed.append(test)
150 except TestMissing:
151 missing.append(test)
152 print '**********\n* Report *\n**********'
153 if not failed and not missing:
154 print 'All tests OK.'
155 if failed:
156 print 'Failed tests:'
157 for t in failed:
158 print ' ', t
159 if missing:
160 print 'Missing tests:'
161 for t in missing:
162 print ' ', t
163
Guido van Rossum85f18201992-11-27 22:53:50 +0000164main()