blob: 67910344ceb595914bb54497c8e0a4cc050f34c6 [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')
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000111 try:
112 fake_stdout.close()
113 except TestFailed:
114 pass
115 fake_stdout = None
Guido van Rossum85f18201992-11-27 22:53:50 +0000116 finally:
117 sys.stdout = real_stdout
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000118 if fake_stdout:
119 fake_stdout.close()
Barry Warsaw272c00b1996-12-10 19:51:10 +0000120
121
122
123def main():
124 global generate
125 global warn
126 try:
127 opts, args = getopt.getopt(
128 sys.argv[1:], 'ghw',
129 ['generate', 'help', 'warn'])
130 except getopt.error, msg:
131 print msg
132 usage(1)
133 for opt, val in opts:
134 if opt in ['-h', '--help']:
135 usage(0)
136 elif opt in ['-g', '--generate']:
137 generate = 1
138 elif opt in ['-w', '--warn']:
139 warn = 1
140
141 # find the output directory
Barry Warsawe4a252e1996-12-10 23:19:14 +0000142 outdir = findfile('output')
Barry Warsaw272c00b1996-12-10 19:51:10 +0000143 if args:
144 tests = args
145 else:
146 import testall
147 tests = testall.tests
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000148 failed = 0
Barry Warsaw272c00b1996-12-10 19:51:10 +0000149 for test in tests:
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000150 print 'testing:', test
Barry Warsaw272c00b1996-12-10 19:51:10 +0000151 try:
152 do_one_test(test, outdir)
153 except TestFailed, msg:
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000154 print 'test', test, 'failed'
155 failed = failed + 1
156 if not failed:
Barry Warsawcb17a461996-12-12 22:34:26 +0000157 print 'All tests OK.'
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000158 else:
159 print failed, 'tests failed'
Barry Warsawcb17a461996-12-12 22:34:26 +0000160
Guido van Rossum85f18201992-11-27 22:53:50 +0000161main()