[Python-checkins] CVS: python/dist/src/Lib/test regrtest.py,1.51,1.52 test_support.py,1.32,1.33

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 21 Sep 2001 13:31:54 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv7621

Modified Files:
	regrtest.py test_support.py 
Log Message:
Change the way unexpected output is reported: rather than stopping at
the first difference, let the test run till completion, then gather
all the output and compare it to the expected output using difflib.

XXX Still to do: produce diff output that only shows the sections that
differ; currently it produces ndiff-style output because that's the
easiest to produce with difflib, but this becomes a liability when the
output is voluminous and there are only a few differences.



Index: regrtest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/regrtest.py,v
retrieving revision 1.51
retrieving revision 1.52
diff -C2 -d -r1.51 -r1.52
*** regrtest.py	2001/09/18 20:34:19	1.51
--- regrtest.py	2001/09/21 20:31:52	1.52
***************
*** 283,296 ****
      outputdir = os.path.join(testdir, "output")
      outputfile = os.path.join(outputdir, test)
!     try:
!         if generate:
!             cfp = StringIO.StringIO()
!         elif verbose:
!             cfp = sys.stdout
!         else:
!             cfp = Compare(outputfile, sys.stdout)
!     except IOError:
          cfp = None
!         print "Warning: can't open", outputfile
      try:
          save_stdout = sys.stdout
--- 283,290 ----
      outputdir = os.path.join(testdir, "output")
      outputfile = os.path.join(outputdir, test)
!     if verbose or generate:
          cfp = None
!     else:
!         cfp =  StringIO.StringIO()
      try:
          save_stdout = sys.stdout
***************
*** 307,314 ****
              if indirect_test is not None:
                  indirect_test()
-             if cfp and not (generate or verbose):
-                 cfp.close()
          finally:
              sys.stdout = save_stdout
      except (ImportError, test_support.TestSkipped), msg:
          if not quiet:
--- 301,313 ----
              if indirect_test is not None:
                  indirect_test()
          finally:
              sys.stdout = save_stdout
+             if cfp and test_support.output_comparison_denied():
+                 output = cfp.getvalue()
+                 cfp = None
+                 s = test + "\n"
+                 if output.startswith(s):
+                     output = output[len(s):]
+                 sys.stdout.write(output)
      except (ImportError, test_support.TestSkipped), msg:
          if not quiet:
***************
*** 327,332 ****
          return 0
      else:
          if generate:
-             output = cfp.getvalue()
              if output == test + "\n":
                  if os.path.exists(outputfile):
--- 326,333 ----
          return 0
      else:
+         if not cfp:
+             return 1
+         output = cfp.getvalue()
          if generate:
              if output == test + "\n":
                  if os.path.exists(outputfile):
***************
*** 334,349 ****
                      # may have changed), but let the user know it isn't
                      # needed:
-                     fp = open(outputfile, "w")
-                     fp.write(output)
-                     fp.close()
                      print "output file", outputfile, \
                            "is no longer needed; consider removing it"
!                 # else:
!                 #     We don't need it, so don't create it.
!             else:
!                 fp = open(outputfile, "w")
!                 fp.write(output)
!                 fp.close()
!         return 1
  
  def findtestdir():
--- 335,367 ----
                      # may have changed), but let the user know it isn't
                      # needed:
                      print "output file", outputfile, \
                            "is no longer needed; consider removing it"
!                 else:
!                     # We don't need it, so don't create it.
!                     return 1
!             fp = open(outputfile, "w")
!             fp.write(output)
!             fp.close()
!             return 1
!         if os.path.exists(outputfile):
!             fp = open(outputfile, "r")
!             expected = fp.read()
!             fp.close()
!         else:
!             expected = test + "\n"
!         if output == expected:
!             return 1
!         print "test", test, "produced unexpected output:"
!         reportdiff(expected, output)
!         return 0
! 
! def reportdiff(expected, output):
!     print "*" * 70
!     import difflib
!     a = expected.splitlines(1)
!     b = output.splitlines(1)
!     diff = difflib.ndiff(a, b)
!     print ''.join(diff),
!     print "*" * 70
  
  def findtestdir():
***************
*** 384,443 ****
      if len(line) > indent:
          print line
- 
- class Compare:
-     def __init__(self, filename, origstdout):
-         self.origstdout = origstdout
-         if os.path.exists(filename):
-             self.fp = open(filename, 'r')
-         else:
-             self.fp = StringIO.StringIO(
-                 os.path.basename(filename) + "\n")
-         self.stuffthatmatched = []
- 
-     def write(self, data):
-         if test_support.suppress_output_comparison():
-             self.origstdout.write(data)
-             return
-         expected = self.fp.read(len(data))
-         if data == expected:
-             self.stuffthatmatched.append(expected)
-         else:
-             # This Compare instance is spoofing stdout, so we need to write
-             # to stderr instead.
-             from sys import stderr as e
-             print >> e, "The actual stdout doesn't match the expected stdout."
-             if self.stuffthatmatched:
-                 print >> e, "This much did match (between asterisk lines):"
-                 print >> e, "*" * 70
-                 good = "".join(self.stuffthatmatched)
-                 e.write(good)
-                 if not good.endswith("\n"):
-                     e.write("\n")
-                 print >> e, "*" * 70
-                 print >> e, "Then ..."
-             else:
-                 print >> e, "The first write to stdout clashed:"
-             # Note that the prompts are the same length in next two lines.
-             # This is so what we expected and what we got line up.
-             print >> e, "We expected (repr):", `expected`
-             print >> e, "But instead we got:", `data`
-             raise test_support.TestFailed('Writing: ' + `data`+
-                                           ', expected: ' + `expected`)
- 
-     def writelines(self, listoflines):
-         map(self.write, listoflines)
- 
-     def flush(self):
-         pass
- 
-     def close(self):
-         leftover = self.fp.read()
-         if leftover:
-             raise test_support.TestFailed('Tail of expected stdout unseen: ' +
-                                           `leftover`)
-         self.fp.close()
- 
-     def isatty(self):
-         return 0
  
  class _Set:
--- 402,405 ----

Index: test_support.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_support.py,v
retrieving revision 1.32
retrieving revision 1.33
diff -C2 -d -r1.32 -r1.33
*** test_support.py	2001/09/20 06:31:22	1.32
--- test_support.py	2001/09/21 20:31:52	1.33
***************
*** 24,29 ****
  # _output_comparison controls whether regrtest will try to compare stdout
  # with an expected-output file.  For straight regrtests, it should.
! # The doctest driver should set_output_comparison(0) for the duration, and
! # restore the old value when it's done.
  # Note that this control is in addition to verbose mode:  output will be
  # compared if and only if _output_comparison is true and verbose mode is
--- 24,28 ----
  # _output_comparison controls whether regrtest will try to compare stdout
  # with an expected-output file.  For straight regrtests, it should.
! # The doctest driver resets this flag by calling deny_output_comparison().
  # Note that this control is in addition to verbose mode:  output will be
  # compared if and only if _output_comparison is true and verbose mode is
***************
*** 31,44 ****
  _output_comparison = 1
  
! def set_output_comparison(newvalue):
      global _output_comparison
!     oldvalue = _output_comparison
!     _output_comparison = newvalue
!     return oldvalue
  
  # regrtest's interface to _output_comparison.
! def suppress_output_comparison():
!     return not _output_comparison
! 
  
  def unload(name):
--- 30,43 ----
  _output_comparison = 1
  
! def deny_output_comparison():
      global _output_comparison
!     _output_comparison = 0
  
  # regrtest's interface to _output_comparison.
! def output_comparison_denied():
!     global _output_comparison
!     denied = not _output_comparison
!     _output_comparison = 1
!     return denied
  
  def unload(name):
***************
*** 200,208 ****
          verbosity = None
  
!     oldvalue = set_output_comparison(0)
!     try:
!         f, t = doctest.testmod(module, verbose=verbosity)
!         if f:
!             raise TestFailed("%d of %d doctests failed" % (f, t))
!     finally:
!         set_output_comparison(oldvalue)
--- 199,204 ----
          verbosity = None
  
!     deny_output_comparison()
!     f, t = doctest.testmod(module, verbose=verbosity)
!     if f:
!         raise TestFailed("%d of %d doctests failed" % (f, t))