blob: aadd1702ff6562c08bdc3c51d74ef0b1854f261f [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:
96 fake_stdout = Compare(filename)
Guido van Rossum85f18201992-11-27 22:53:50 +000097 try:
Guido van Rossumdbfed711996-12-11 16:54:54 +000098 sys.stdout = fake_stdout
Barry Warsaw272c00b1996-12-10 19:51:10 +000099 print t
100 unload(t)
101 try:
102 __import__(t, globals(), locals())
103 except ImportError, msg:
104 if warn:
105 sys.stderr.write(msg+': Un-installed'
106 ' optional module?\n')
Guido van Rossum85f18201992-11-27 22:53:50 +0000107 finally:
108 sys.stdout = real_stdout
Guido van Rossumdbfed711996-12-11 16:54:54 +0000109 fake_stdout.close()
Barry Warsaw272c00b1996-12-10 19:51:10 +0000110
111
112
113def main():
114 global generate
115 global warn
116 try:
117 opts, args = getopt.getopt(
118 sys.argv[1:], 'ghw',
119 ['generate', 'help', 'warn'])
120 except getopt.error, msg:
121 print msg
122 usage(1)
123 for opt, val in opts:
124 if opt in ['-h', '--help']:
125 usage(0)
126 elif opt in ['-g', '--generate']:
127 generate = 1
128 elif opt in ['-w', '--warn']:
129 warn = 1
130
131 # find the output directory
Barry Warsawe4a252e1996-12-10 23:19:14 +0000132 outdir = findfile('output')
Barry Warsaw272c00b1996-12-10 19:51:10 +0000133 if args:
134 tests = args
135 else:
136 import testall
137 tests = testall.tests
138 for test in tests:
139 try:
140 do_one_test(test, outdir)
141 except TestFailed, msg:
142 print 'Failure of test:', test
143 traceback.print_exc()
Guido van Rossum85f18201992-11-27 22:53:50 +0000144 print 'All tests OK.'
145
146main()