home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / regrtest.py < prev    next >
Text File  |  1997-10-20  |  5KB  |  194 lines

  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.  
  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.  
  16. If non-option arguments are present, they are names for tests to run,
  17. unless -x is given, in which case they are names for tests not to run.
  18. If no test names are given, all tests are run.
  19.  
  20. -v is incompatible with -g and does not compare test output files.
  21. """
  22.  
  23. import sys
  24. import string
  25. import os
  26. import getopt
  27. import traceback
  28.  
  29. import test_support
  30.  
  31. def main():
  32.     try:
  33.     opts, args = getopt.getopt(sys.argv[1:], 'vgqx')
  34.     except getopt.error, msg:
  35.     print msg
  36.     print __doc__
  37.     return 2
  38.     verbose = 0
  39.     quiet = 0
  40.     generate = 0
  41.     exclude = 0
  42.     for o, a in opts:
  43.     if o == '-v': verbose = verbose+1
  44.     if o == '-q': quiet = 1; verbose = 0
  45.     if o == '-g': generate = 1
  46.     if o == '-x': exclude = 1
  47.     if generate and verbose:
  48.     print "-g and -v don't go together!"
  49.     return 2
  50.     good = []
  51.     bad = []
  52.     skipped = []
  53.     for i in range(len(args)):
  54.     # Strip trailing ".py" from arguments
  55.     if args[i][-3:] == '.py':
  56.         args[i] = args[i][:-3]
  57.     if exclude:
  58.     nottests[:0] = args
  59.     args = []
  60.     tests = args or findtests()
  61.     test_support.verbose = verbose    # Tell tests to be moderately quiet
  62.     for test in tests:
  63.     if not quiet:
  64.         print test
  65.     ok = runtest(test, generate, verbose)
  66.     if ok > 0:
  67.         good.append(test)
  68.     elif ok == 0:
  69.         bad.append(test)
  70.     else:
  71.         if not quiet:
  72.         print "test", test,
  73.         print "skipped -- an optional feature could not be imported"
  74.         skipped.append(test)
  75.     if good and not quiet:
  76.     if not bad and not skipped and len(good) > 1:
  77.         print "All",
  78.     print count(len(good), "test"), "OK."
  79.     if bad:
  80.     print count(len(bad), "test"), "failed:",
  81.     print string.join(bad)
  82.     if skipped and not quiet:
  83.     print count(len(skipped), "test"), "skipped:",
  84.     print string.join(skipped)
  85.     return len(bad) > 0
  86.  
  87. stdtests = [
  88.     'test_grammar',
  89.     'test_opcodes',
  90.     'test_operations',
  91.     'test_builtin',
  92.     'test_exceptions',
  93.     'test_types',
  94.    ]
  95.  
  96. nottests = [
  97.     'test_support',
  98.     'test_b1',
  99.     'test_b2',
  100.     ]
  101.  
  102. def findtests():
  103.     """Return a list of all applicable test modules."""
  104.     testdir = findtestdir()
  105.     names = os.listdir(testdir)
  106.     tests = []
  107.     for name in names:
  108.     if name[:5] == "test_" and name[-3:] == ".py":
  109.         modname = name[:-3]
  110.         if modname not in stdtests and modname not in nottests:
  111.         tests.append(modname)
  112.     tests.sort()
  113.     return stdtests + tests
  114.  
  115. def runtest(test, generate, verbose):
  116.     test_support.unload(test)
  117.     testdir = findtestdir()
  118.     outputdir = os.path.join(testdir, "output")
  119.     outputfile = os.path.join(outputdir, test)
  120.     try:
  121.     if generate:
  122.         cfp = open(outputfile, "w")
  123.     elif verbose:
  124.         cfp = sys.stdout
  125.     else:
  126.         cfp = Compare(outputfile)
  127.     except IOError:
  128.     cfp = None
  129.     print "Warning: can't open", outputfile
  130.     try:
  131.     save_stdout = sys.stdout
  132.     try:
  133.         if cfp:
  134.         sys.stdout = cfp
  135.         print test        # Output file starts with test name
  136.         __import__(test, globals(), locals(), [])
  137.     finally:
  138.         sys.stdout = save_stdout
  139.     except ImportError, msg:
  140.     return -1
  141.     except KeyboardInterrupt, v:
  142.     raise KeyboardInterrupt, v, sys.exc_info()[2]
  143.     except test_support.TestFailed, msg:
  144.     print "test", test, "failed --", msg
  145.     return 0
  146.     except:
  147.     type, value = sys.exc_info()[:2]
  148.     print "test", test, "crashed --", type, ":", value
  149.     if verbose:
  150.         traceback.print_exc(file=sys.stdout)
  151.     return 0
  152.     else:
  153.     return 1
  154.  
  155. def findtestdir():
  156.     if __name__ == '__main__':
  157.     file = sys.argv[0]
  158.     else:
  159.     file = __file__
  160.     testdir = os.path.dirname(file) or os.curdir
  161.     return testdir
  162.  
  163. def count(n, word):
  164.     if n == 1:
  165.     return "%d %s" % (n, word)
  166.     else:
  167.     return "%d %ss" % (n, word)
  168.  
  169. class Compare:
  170.  
  171.     def __init__(self, filename):
  172.     self.fp = open(filename, 'r')
  173.  
  174.     def write(self, data):
  175.     expected = self.fp.read(len(data))
  176.     if data <> expected:
  177.         raise test_support.TestFailed, \
  178.             'Writing: '+`data`+', expected: '+`expected`
  179.  
  180.     def flush(self):
  181.     pass
  182.  
  183.     def close(self):
  184.     leftover = self.fp.read()
  185.     if leftover:
  186.         raise test_support.TestFailed, 'Unread: '+`leftover`
  187.     self.fp.close()
  188.  
  189.     def isatty(self):
  190.     return 0
  191.  
  192. if __name__ == '__main__':
  193.     sys.exit(main())
  194.