Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Regression test. |
| 4 | |
| 5 | This will find all modules whose name is "test_*" in the test |
| 6 | directory, and run them. Various command line options provide |
| 7 | additional facilities. |
| 8 | |
| 9 | Command line options: |
| 10 | |
Barry Warsaw | a873b03 | 2000-08-03 15:50:37 +0000 | [diff] [blame] | 11 | -v: verbose -- run tests in verbose mode with output to stdout |
| 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 | -s: single -- run only a single test (see below) |
| 16 | -r: random -- randomize test execution order |
Neil Schemenauer | 8a00abc | 2000-10-13 01:32:42 +0000 | [diff] [blame] | 17 | -l: findleaks -- if GC is available detect tests that leak memory |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 18 | --have-resources -- run tests that require large resources (time/space) |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 19 | |
| 20 | If non-option arguments are present, they are names for tests to run, |
| 21 | unless -x is given, in which case they are names for tests not to run. |
| 22 | If no test names are given, all tests are run. |
Guido van Rossum | f58ed25 | 1997-03-07 21:04:33 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | a412220 | 1997-08-18 20:08:24 +0000 | [diff] [blame] | 24 | -v is incompatible with -g and does not compare test output files. |
Barry Warsaw | e11e3de | 1999-01-28 19:51:51 +0000 | [diff] [blame] | 25 | |
Barry Warsaw | 22e4182 | 2001-02-23 18:31:40 +0000 | [diff] [blame] | 26 | -s means to run only a single test and exit. This is useful when doing memory |
| 27 | analysis on the Python interpreter (which tend to consume to many resources to |
| 28 | run the full regression test non-stop). The file /tmp/pynexttest is read to |
| 29 | find the next test to run. If this file is missing, the first test_*.py file |
| 30 | in testdir or on the command line is used. (actually tempfile.gettempdir() is |
| 31 | used instead of /tmp). |
Barry Warsaw | e11e3de | 1999-01-28 19:51:51 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 33 | """ |
| 34 | |
| 35 | import sys |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 36 | import os |
| 37 | import getopt |
Guido van Rossum | 9e48b27 | 1997-07-16 01:56:13 +0000 | [diff] [blame] | 38 | import traceback |
Skip Montanaro | ab1c791 | 2000-06-30 16:39:27 +0000 | [diff] [blame] | 39 | import random |
Fred Drake | ae1bb17 | 2001-05-21 21:08:12 +0000 | [diff] [blame] | 40 | import StringIO |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 41 | |
| 42 | import test_support |
| 43 | |
Skip Montanaro | ab1c791 | 2000-06-30 16:39:27 +0000 | [diff] [blame] | 44 | def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0, |
Neil Schemenauer | d569f23 | 2000-09-22 15:29:28 +0000 | [diff] [blame] | 45 | exclude=0, single=0, randomize=0, findleaks=0, |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 46 | use_large_resources=0): |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 47 | """Execute a test suite. |
| 48 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 49 | This also parses command-line options and modifies its behavior |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 50 | accordingly. |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 51 | |
| 52 | tests -- a list of strings containing test names (optional) |
| 53 | testdir -- the directory in which to look for tests (optional) |
| 54 | |
| 55 | Users other than the Python test suite will certainly want to |
| 56 | specify testdir; if it's omitted, the directory containing the |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 57 | Python test suite is searched for. |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 58 | |
| 59 | If the tests argument is omitted, the tests listed on the |
| 60 | command-line will be used. If that's empty, too, then all *.py |
| 61 | files beginning with test_ will be used. |
Skip Montanaro | ab1c791 | 2000-06-30 16:39:27 +0000 | [diff] [blame] | 62 | |
Barry Warsaw | a873b03 | 2000-08-03 15:50:37 +0000 | [diff] [blame] | 63 | The other seven default arguments (verbose, quiet, generate, exclude, |
Neil Schemenauer | d569f23 | 2000-09-22 15:29:28 +0000 | [diff] [blame] | 64 | single, randomize, and findleaks) allow programmers calling main() |
Barry Warsaw | a873b03 | 2000-08-03 15:50:37 +0000 | [diff] [blame] | 65 | directly to set the values that would normally be set by flags on the |
| 66 | command line. |
| 67 | |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 68 | """ |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 70 | try: |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 71 | opts, args = getopt.getopt(sys.argv[1:], 'vgqxsrl', ['have-resources']) |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 72 | except getopt.error, msg: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 73 | print msg |
| 74 | print __doc__ |
| 75 | return 2 |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 76 | for o, a in opts: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 77 | if o == '-v': verbose = verbose+1 |
| 78 | if o == '-q': quiet = 1; verbose = 0 |
| 79 | if o == '-g': generate = 1 |
| 80 | if o == '-x': exclude = 1 |
Barry Warsaw | e11e3de | 1999-01-28 19:51:51 +0000 | [diff] [blame] | 81 | if o == '-s': single = 1 |
Skip Montanaro | ab1c791 | 2000-06-30 16:39:27 +0000 | [diff] [blame] | 82 | if o == '-r': randomize = 1 |
Neil Schemenauer | d569f23 | 2000-09-22 15:29:28 +0000 | [diff] [blame] | 83 | if o == '-l': findleaks = 1 |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 84 | if o == '--have-resources': use_large_resources = 1 |
Guido van Rossum | a412220 | 1997-08-18 20:08:24 +0000 | [diff] [blame] | 85 | if generate and verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 86 | print "-g and -v don't go together!" |
| 87 | return 2 |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 88 | good = [] |
| 89 | bad = [] |
| 90 | skipped = [] |
Barry Warsaw | e11e3de | 1999-01-28 19:51:51 +0000 | [diff] [blame] | 91 | |
Neil Schemenauer | d569f23 | 2000-09-22 15:29:28 +0000 | [diff] [blame] | 92 | if findleaks: |
Barry Warsaw | a873b03 | 2000-08-03 15:50:37 +0000 | [diff] [blame] | 93 | try: |
| 94 | import gc |
| 95 | except ImportError: |
Neil Schemenauer | 8a00abc | 2000-10-13 01:32:42 +0000 | [diff] [blame] | 96 | print 'No GC available, disabling findleaks.' |
Neil Schemenauer | d569f23 | 2000-09-22 15:29:28 +0000 | [diff] [blame] | 97 | findleaks = 0 |
Barry Warsaw | a873b03 | 2000-08-03 15:50:37 +0000 | [diff] [blame] | 98 | else: |
Neil Schemenauer | 8a00abc | 2000-10-13 01:32:42 +0000 | [diff] [blame] | 99 | # Uncomment the line below to report garbage that is not |
| 100 | # freeable by reference counting alone. By default only |
| 101 | # garbage that is not collectable by the GC is reported. |
| 102 | #gc.set_debug(gc.DEBUG_SAVEALL) |
Neil Schemenauer | d569f23 | 2000-09-22 15:29:28 +0000 | [diff] [blame] | 103 | found_garbage = [] |
Barry Warsaw | a873b03 | 2000-08-03 15:50:37 +0000 | [diff] [blame] | 104 | |
Barry Warsaw | e11e3de | 1999-01-28 19:51:51 +0000 | [diff] [blame] | 105 | if single: |
| 106 | from tempfile import gettempdir |
| 107 | filename = os.path.join(gettempdir(), 'pynexttest') |
| 108 | try: |
| 109 | fp = open(filename, 'r') |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 110 | next = fp.read().strip() |
Barry Warsaw | e11e3de | 1999-01-28 19:51:51 +0000 | [diff] [blame] | 111 | tests = [next] |
| 112 | fp.close() |
| 113 | except IOError: |
| 114 | pass |
Guido van Rossum | a412220 | 1997-08-18 20:08:24 +0000 | [diff] [blame] | 115 | for i in range(len(args)): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 116 | # Strip trailing ".py" from arguments |
| 117 | if args[i][-3:] == '.py': |
| 118 | args[i] = args[i][:-3] |
Guido van Rossum | 6c74fea | 1998-08-25 12:29:08 +0000 | [diff] [blame] | 119 | stdtests = STDTESTS[:] |
| 120 | nottests = NOTTESTS[:] |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 121 | if exclude: |
Guido van Rossum | 6c74fea | 1998-08-25 12:29:08 +0000 | [diff] [blame] | 122 | for arg in args: |
| 123 | if arg in stdtests: |
| 124 | stdtests.remove(arg) |
| 125 | nottests[:0] = args |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 126 | args = [] |
Guido van Rossum | 747e1ca | 1998-08-24 13:48:36 +0000 | [diff] [blame] | 127 | tests = tests or args or findtests(testdir, stdtests, nottests) |
Barry Warsaw | e11e3de | 1999-01-28 19:51:51 +0000 | [diff] [blame] | 128 | if single: |
| 129 | tests = tests[:1] |
Skip Montanaro | ab1c791 | 2000-06-30 16:39:27 +0000 | [diff] [blame] | 130 | if randomize: |
| 131 | random.shuffle(tests) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 132 | test_support.verbose = verbose # Tell tests to be moderately quiet |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 133 | test_support.use_large_resources = use_large_resources |
Guido van Rossum | 5796d26 | 2000-04-21 21:35:06 +0000 | [diff] [blame] | 134 | save_modules = sys.modules.keys() |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 135 | for test in tests: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 136 | if not quiet: |
| 137 | print test |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 138 | ok = runtest(test, generate, verbose, quiet, testdir) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 139 | if ok > 0: |
| 140 | good.append(test) |
| 141 | elif ok == 0: |
| 142 | bad.append(test) |
| 143 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 144 | skipped.append(test) |
Neil Schemenauer | d569f23 | 2000-09-22 15:29:28 +0000 | [diff] [blame] | 145 | if findleaks: |
| 146 | gc.collect() |
| 147 | if gc.garbage: |
Neil Schemenauer | 8a00abc | 2000-10-13 01:32:42 +0000 | [diff] [blame] | 148 | print "Warning: test created", len(gc.garbage), |
| 149 | print "uncollectable object(s)." |
| 150 | # move the uncollectable objects somewhere so we don't see |
| 151 | # them again |
Neil Schemenauer | d569f23 | 2000-09-22 15:29:28 +0000 | [diff] [blame] | 152 | found_garbage.extend(gc.garbage) |
| 153 | del gc.garbage[:] |
Guido van Rossum | 5796d26 | 2000-04-21 21:35:06 +0000 | [diff] [blame] | 154 | # Unload the newly imported modules (best effort finalization) |
| 155 | for module in sys.modules.keys(): |
Guido van Rossum | 5193114 | 2000-05-05 14:27:39 +0000 | [diff] [blame] | 156 | if module not in save_modules and module.startswith("test."): |
Guido van Rossum | 5796d26 | 2000-04-21 21:35:06 +0000 | [diff] [blame] | 157 | test_support.unload(module) |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 158 | if good and not quiet: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 159 | if not bad and not skipped and len(good) > 1: |
| 160 | print "All", |
| 161 | print count(len(good), "test"), "OK." |
Tim Peters | 1a4d77b | 2000-12-30 22:21:22 +0000 | [diff] [blame] | 162 | if verbose: |
| 163 | print "CAUTION: stdout isn't compared in verbose mode: a test" |
| 164 | print "that passes in verbose mode may fail without it." |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 165 | if bad: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 166 | print count(len(bad), "test"), "failed:", |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 167 | print " ".join(bad) |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 168 | if skipped and not quiet: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 169 | print count(len(skipped), "test"), "skipped:", |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 170 | print " ".join(skipped) |
Barry Warsaw | e11e3de | 1999-01-28 19:51:51 +0000 | [diff] [blame] | 171 | |
| 172 | if single: |
| 173 | alltests = findtests(testdir, stdtests, nottests) |
| 174 | for i in range(len(alltests)): |
| 175 | if tests[0] == alltests[i]: |
| 176 | if i == len(alltests) - 1: |
| 177 | os.unlink(filename) |
| 178 | else: |
| 179 | fp = open(filename, 'w') |
| 180 | fp.write(alltests[i+1] + '\n') |
| 181 | fp.close() |
| 182 | break |
| 183 | else: |
| 184 | os.unlink(filename) |
| 185 | |
Guido van Rossum | e838701 | 1997-08-14 19:40:34 +0000 | [diff] [blame] | 186 | return len(bad) > 0 |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 188 | STDTESTS = [ |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 189 | 'test_grammar', |
| 190 | 'test_opcodes', |
| 191 | 'test_operations', |
| 192 | 'test_builtin', |
| 193 | 'test_exceptions', |
| 194 | 'test_types', |
| 195 | ] |
| 196 | |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 197 | NOTTESTS = [ |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 198 | 'test_support', |
| 199 | 'test_b1', |
| 200 | 'test_b2', |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 201 | 'test_future1', |
| 202 | 'test_future2', |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 203 | ] |
| 204 | |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 205 | def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS): |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 206 | """Return a list of all applicable test modules.""" |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 207 | if not testdir: testdir = findtestdir() |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 208 | names = os.listdir(testdir) |
| 209 | tests = [] |
| 210 | for name in names: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 211 | if name[:5] == "test_" and name[-3:] == ".py": |
| 212 | modname = name[:-3] |
| 213 | if modname not in stdtests and modname not in nottests: |
| 214 | tests.append(modname) |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 215 | tests.sort() |
| 216 | return stdtests + tests |
| 217 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 218 | def runtest(test, generate, verbose, quiet, testdir = None): |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 219 | """Run a single test. |
| 220 | test -- the name of the test |
| 221 | generate -- if true, generate output, instead of running the test |
| 222 | and comparing it to a previously created output file |
| 223 | verbose -- if true, print more messages |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 224 | quiet -- if true, don't print 'skipped' messages (probably redundant) |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 225 | testdir -- test directory |
| 226 | """ |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 227 | test_support.unload(test) |
Guido van Rossum | 6fd83b7 | 1998-08-01 17:04:08 +0000 | [diff] [blame] | 228 | if not testdir: testdir = findtestdir() |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 229 | outputdir = os.path.join(testdir, "output") |
| 230 | outputfile = os.path.join(outputdir, test) |
| 231 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 232 | if generate: |
| 233 | cfp = open(outputfile, "w") |
| 234 | elif verbose: |
| 235 | cfp = sys.stdout |
| 236 | else: |
| 237 | cfp = Compare(outputfile) |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 238 | except IOError: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 239 | cfp = None |
| 240 | print "Warning: can't open", outputfile |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 241 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 242 | save_stdout = sys.stdout |
| 243 | try: |
| 244 | if cfp: |
| 245 | sys.stdout = cfp |
| 246 | print test # Output file starts with test name |
Tim Peters | d974221 | 2001-05-22 18:28:25 +0000 | [diff] [blame^] | 247 | the_module = __import__(test, globals(), locals(), []) |
| 248 | # Most tests run to completion simply as a side-effect of |
| 249 | # being imported. For the benefit of tests that can't run |
| 250 | # that way (like test_threaded_import), explicitly invoke |
| 251 | # their test_main() function (if it exists). |
| 252 | indirect_test = getattr(the_module, "test_main", None) |
| 253 | if indirect_test is not None: |
| 254 | indirect_test() |
Jeremy Hylton | fff9e20 | 2000-07-11 15:15:31 +0000 | [diff] [blame] | 255 | if cfp and not (generate or verbose): |
| 256 | cfp.close() |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 257 | finally: |
| 258 | sys.stdout = save_stdout |
Thomas Wouters | 3af826e | 2000-08-04 13:17:51 +0000 | [diff] [blame] | 259 | except (ImportError, test_support.TestSkipped), msg: |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 260 | if not quiet: |
| 261 | print "test", test, |
| 262 | print "skipped -- ", msg |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 263 | return -1 |
Fred Drake | fe5c22a | 2000-08-18 16:04:05 +0000 | [diff] [blame] | 264 | except KeyboardInterrupt: |
| 265 | raise |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 266 | except test_support.TestFailed, msg: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 267 | print "test", test, "failed --", msg |
| 268 | return 0 |
Guido van Rossum | 9e48b27 | 1997-07-16 01:56:13 +0000 | [diff] [blame] | 269 | except: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 270 | type, value = sys.exc_info()[:2] |
Fred Drake | 27c4b39 | 2000-08-23 20:34:40 +0000 | [diff] [blame] | 271 | print "test", test, "crashed --", str(type) + ":", value |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 272 | if verbose: |
| 273 | traceback.print_exc(file=sys.stdout) |
| 274 | return 0 |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 275 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 276 | return 1 |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 277 | |
| 278 | def findtestdir(): |
| 279 | if __name__ == '__main__': |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 280 | file = sys.argv[0] |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 281 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 282 | file = __file__ |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 283 | testdir = os.path.dirname(file) or os.curdir |
| 284 | return testdir |
| 285 | |
| 286 | def count(n, word): |
| 287 | if n == 1: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 288 | return "%d %s" % (n, word) |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 289 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 290 | return "%d %ss" % (n, word) |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 291 | |
| 292 | class Compare: |
| 293 | |
| 294 | def __init__(self, filename): |
Fred Drake | ae1bb17 | 2001-05-21 21:08:12 +0000 | [diff] [blame] | 295 | if os.path.exists(filename): |
| 296 | self.fp = open(filename, 'r') |
| 297 | else: |
| 298 | self.fp = StringIO.StringIO( |
| 299 | os.path.basename(filename) + "\n") |
Tim Peters | 1a4d77b | 2000-12-30 22:21:22 +0000 | [diff] [blame] | 300 | self.stuffthatmatched = [] |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 301 | |
| 302 | def write(self, data): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 303 | expected = self.fp.read(len(data)) |
Tim Peters | 1a4d77b | 2000-12-30 22:21:22 +0000 | [diff] [blame] | 304 | if data == expected: |
| 305 | self.stuffthatmatched.append(expected) |
| 306 | else: |
| 307 | # This Compare instance is spoofing stdout, so we need to write |
| 308 | # to stderr instead. |
| 309 | from sys import stderr as e |
| 310 | print >> e, "The actual stdout doesn't match the expected stdout." |
| 311 | if self.stuffthatmatched: |
| 312 | print >> e, "This much did match (between asterisk lines):" |
| 313 | print >> e, "*" * 70 |
| 314 | good = "".join(self.stuffthatmatched) |
| 315 | e.write(good) |
| 316 | if not good.endswith("\n"): |
| 317 | e.write("\n") |
| 318 | print >> e, "*" * 70 |
| 319 | print >> e, "Then ..." |
| 320 | else: |
| 321 | print >> e, "The first write to stdout clashed:" |
| 322 | # Note that the prompts are the same length in next two lines. |
| 323 | # This is so what we expected and what we got line up. |
| 324 | print >> e, "We expected (repr):", `expected` |
| 325 | print >> e, "But instead we got:", `data` |
| 326 | raise test_support.TestFailed('Writing: ' + `data`+ |
| 327 | ', expected: ' + `expected`) |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 328 | |
Guido van Rossum | e87ed5f | 1998-04-23 13:33:21 +0000 | [diff] [blame] | 329 | def writelines(self, listoflines): |
| 330 | map(self.write, listoflines) |
| 331 | |
Guido van Rossum | 75fce30 | 1997-07-17 14:51:37 +0000 | [diff] [blame] | 332 | def flush(self): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 333 | pass |
Guido van Rossum | 75fce30 | 1997-07-17 14:51:37 +0000 | [diff] [blame] | 334 | |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 335 | def close(self): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 336 | leftover = self.fp.read() |
| 337 | if leftover: |
Tim Peters | 1a4d77b | 2000-12-30 22:21:22 +0000 | [diff] [blame] | 338 | raise test_support.TestFailed('Tail of expected stdout unseen: ' + |
| 339 | `leftover`) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 340 | self.fp.close() |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 341 | |
| 342 | def isatty(self): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 343 | return 0 |
Guido van Rossum | 152494a | 1996-12-20 03:12:20 +0000 | [diff] [blame] | 344 | |
| 345 | if __name__ == '__main__': |
Guido van Rossum | e838701 | 1997-08-14 19:40:34 +0000 | [diff] [blame] | 346 | sys.exit(main()) |