blob: d6c50810c1a9f1e918b9e99ac8dad04b75bbcdb1 [file] [log] [blame]
Guido van Rossum152494a1996-12-20 03:12:20 +00001#! /usr/bin/env python
2
3"""Regression test.
4
5This will find all modules whose name is "test_*" in the test
6directory, and run them. Various command line options provide
7additional facilities.
8
9Command line options:
10
11-v: verbose -- print the name name of each test as it is being run
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
16If non-option arguments are present, they are names for tests to run,
17unless -x is given, in which case they are names for tests not to run.
18If no test names are given, all tests are run.
Guido van Rossumf58ed251997-03-07 21:04:33 +000019
20If -v is given *twice*, the tests themselves are run in verbose mode.
21This is incompatible with -g and does not compare test output files.
Guido van Rossum152494a1996-12-20 03:12:20 +000022"""
23
24import sys
25import string
26import os
27import getopt
28
29import test_support
30
31def main():
32 try:
33 opts, args = getopt.getopt(sys.argv[1:], 'vgqx')
34 except getopt.error, msg:
35 print msg
36 print __doc__
37 sys.exit(2)
38 verbose = 0
39 quiet = 0
40 generate = 0
41 exclude = 0
42 for o, a in opts:
Guido van Rossumf58ed251997-03-07 21:04:33 +000043 if o == '-v': verbose = verbose+1
Guido van Rossum152494a1996-12-20 03:12:20 +000044 if o == '-q': quiet = 1
45 if o == '-g': generate = 1
46 if o == '-x': exclude = 1
Guido van Rossumf58ed251997-03-07 21:04:33 +000047 if generate and verbose>1:
48 print "-g and more than one -v don't go together!"
49 sys.exit(2)
Guido van Rossum152494a1996-12-20 03:12:20 +000050 good = []
51 bad = []
52 skipped = []
53 if exclude:
54 nottests[:0] = args
55 args = []
56 tests = args or findtests()
Guido van Rossumf58ed251997-03-07 21:04:33 +000057 test_support.verbose = verbose>1 # Tell tests to be moderately quiet
Guido van Rossum152494a1996-12-20 03:12:20 +000058 for test in tests:
59 if verbose:
60 print test
Guido van Rossumf58ed251997-03-07 21:04:33 +000061 ok = runtest(test, generate, verbose>1)
Guido van Rossum152494a1996-12-20 03:12:20 +000062 if ok > 0:
63 good.append(test)
64 elif ok == 0:
65 bad.append(test)
66 else:
67 if not quiet:
68 print "test", test,
69 print "skipped -- an optional feature could not be imported"
70 skipped.append(test)
71 if good and not quiet:
72 if not bad and not skipped and len(good) > 1:
73 print "All",
74 print count(len(good), "test"), "OK."
75 if bad:
76 print count(len(bad), "test"), "failed:",
77 print string.join(bad)
78 if skipped and not quiet:
79 print count(len(skipped), "test"), "skipped:",
80 print string.join(skipped)
81 sys.exit(len(bad) > 0)
82
83stdtests = [
84 'test_grammar',
85 'test_opcodes',
86 'test_operations',
87 'test_builtin',
88 'test_exceptions',
89 'test_types',
90 ]
91
92nottests = [
93 'test_support',
94 'test_b1',
95 'test_b2',
96 ]
97
98def findtests():
99 """Return a list of all applicable test modules."""
100 testdir = findtestdir()
101 names = os.listdir(testdir)
102 tests = []
103 for name in names:
104 if name[:5] == "test_" and name[-3:] == ".py":
105 modname = name[:-3]
106 if modname not in stdtests and modname not in nottests:
107 tests.append(modname)
108 tests.sort()
109 return stdtests + tests
110
Guido van Rossumf58ed251997-03-07 21:04:33 +0000111def runtest(test, generate, verbose2):
Guido van Rossum152494a1996-12-20 03:12:20 +0000112 test_support.unload(test)
113 testdir = findtestdir()
114 outputdir = os.path.join(testdir, "output")
115 outputfile = os.path.join(outputdir, test)
116 try:
117 if generate:
118 cfp = open(outputfile, "w")
Guido van Rossumf58ed251997-03-07 21:04:33 +0000119 elif verbose2:
120 cfp = sys.stdout
Guido van Rossum152494a1996-12-20 03:12:20 +0000121 else:
122 cfp = Compare(outputfile)
123 except IOError:
124 cfp = None
125 print "Warning: can't open", outputfile
126 try:
127 save_stdout = sys.stdout
128 try:
129 if cfp:
130 sys.stdout = cfp
131 print test # Output file starts with test name
132 __import__(test)
133 finally:
134 sys.stdout = save_stdout
135 except ImportError, msg:
136 return -1
137 except test_support.TestFailed, msg:
138 print "test", test, "failed --", msg
139 return 0
140 else:
141 return 1
142
143def findtestdir():
144 if __name__ == '__main__':
145 file = sys.argv[0]
146 else:
147 file = __file__
148 testdir = os.path.dirname(file) or os.curdir
149 return testdir
150
151def count(n, word):
152 if n == 1:
153 return "%d %s" % (n, word)
154 else:
155 return "%d %ss" % (n, word)
156
157class Compare:
158
159 def __init__(self, filename):
160 self.fp = open(filename, 'r')
161
162 def write(self, data):
163 expected = self.fp.read(len(data))
164 if data <> expected:
165 raise test_support.TestFailed, \
166 'Writing: '+`data`+', expected: '+`expected`
167
168 def close(self):
169 leftover = self.fp.read()
170 if leftover:
171 raise test_support.TestFailed, 'Unread: '+`leftover`
172 self.fp.close()
173
174 def isatty(self):
175 return 0
176
177if __name__ == '__main__':
178 main()