blob: 0506fa2fd370bd1639adb75bc1db42891463fd68 [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'
49
50# defaults
51generate = 0
52warn = 0
53
54
55
Guido van Rossum85f18201992-11-27 22:53:50 +000056# Function to find a file somewhere on sys.path
57def findfile(filename):
58 for dirname in sys.path:
59 fullname = os.path.join(dirname, filename)
60 if os.path.exists(fullname):
61 return fullname
62 return filename # Will cause exception later
63
Guido van Rossum85f18201992-11-27 22:53:50 +000064
Barry Warsaw272c00b1996-12-10 19:51:10 +000065
Guido van Rossum85f18201992-11-27 22:53:50 +000066# Class substituted for sys.stdout, to compare it with the given file
67class Compare:
Guido van Rossum7bc817d1993-12-17 15:25:27 +000068 def __init__(self, filename):
Guido van Rossum85f18201992-11-27 22:53:50 +000069 self.fp = open(filename, 'r')
Guido van Rossum85f18201992-11-27 22:53:50 +000070 def write(self, data):
71 expected = self.fp.read(len(data))
72 if data <> expected:
73 raise TestFailed, \
74 'Writing: '+`data`+', expected: '+`expected`
75 def close(self):
Barry Warsaw272c00b1996-12-10 19:51:10 +000076 leftover = self.fp.read()
77 if leftover:
78 raise TestFailed, 'Unread: '+`leftover`
Guido van Rossum85f18201992-11-27 22:53:50 +000079 self.fp.close()
80
Barry Warsaw272c00b1996-12-10 19:51:10 +000081
Guido van Rossum85f18201992-11-27 22:53:50 +000082# The main program
Barry Warsaw272c00b1996-12-10 19:51:10 +000083def usage(status):
84 print __doc__ % sys.argv[0]
85 sys.exit(status)
86
87
88
89def do_one_test(t, outdir):
90 filename = os.path.join(outdir, t)
Guido van Rossum85f18201992-11-27 22:53:50 +000091 real_stdout = sys.stdout
Guido van Rossumdbfed711996-12-11 16:54:54 +000092 if generate:
93 print 'Generating:', filename
94 fake_stdout = open(filename, 'w')
95 else:
Barry Warsaw1c92eba1996-12-12 22:21:10 +000096 try:
97 fake_stdout = Compare(filename)
98 except IOError:
99 print 'Could not find output file for test:', t
100 return
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
142 for test in tests:
143 try:
144 do_one_test(test, outdir)
145 except TestFailed, msg:
146 print 'Failure of test:', test
147 traceback.print_exc()
Guido van Rossum85f18201992-11-27 22:53:50 +0000148 print 'All tests OK.'
149
150main()