[Checkins] SVN: zope.testrunner/trunk/ Fix --post-mortem (aka -D, --pdb) when a StartUpFailure occurs.

Marius Gedminas cvs-admin at zope.org
Fri Feb 8 13:14:45 UTC 2013


Log message for revision 129222:
  Fix --post-mortem (aka -D, --pdb) when a StartUpFailure occurs.
  
  Fixes https://bugs.launchpad.net/zope.testrunner/+bug/1119363
  
  

Changed:
  U   zope.testrunner/trunk/CHANGES.txt
  U   zope.testrunner/trunk/setup.py
  U   zope.testrunner/trunk/src/zope/testrunner/find.py
  A   zope.testrunner/trunk/src/zope/testrunner/testrunner-debugging-import-failure.test
  U   zope.testrunner/trunk/src/zope/testrunner/tests.py

-=-
Modified: zope.testrunner/trunk/CHANGES.txt
===================================================================
--- zope.testrunner/trunk/CHANGES.txt	2013-02-08 10:52:31 UTC (rev 129221)
+++ zope.testrunner/trunk/CHANGES.txt	2013-02-08 13:14:44 UTC (rev 129222)
@@ -21,7 +21,10 @@
 - Use a temporary coverage directory in the test suite so that tests
   executed by detox in parallel don't conflict.
 
+* Fix --post-mortem (aka -D, --pdb) when a test module cannot be imported
+  or is invalid (LP #1119363).
 
+
 4.1.0 (2013-02-07)
 ==================
 

Modified: zope.testrunner/trunk/setup.py
===================================================================
--- zope.testrunner/trunk/setup.py	2013-02-08 10:52:31 UTC (rev 129221)
+++ zope.testrunner/trunk/setup.py	2013-02-08 13:14:44 UTC (rev 129222)
@@ -37,6 +37,7 @@
                      'src/zope/testrunner/testrunner-coverage-win32.txt',
                      'src/zope/testrunner/testrunner-coverage.txt',
                      'src/zope/testrunner/testrunner-debugging-layer-setup.test',
+                     'src/zope/testrunner/testrunner-debugging-import-failure.test',
                      'src/zope/testrunner/testrunner-debugging.txt',
                      'src/zope/testrunner/testrunner-discovery',
                      'src/zope/testrunner/testrunner-edge-cases.txt',

Modified: zope.testrunner/trunk/src/zope/testrunner/find.py
===================================================================
--- zope.testrunner/trunk/src/zope/testrunner/find.py	2013-02-08 10:52:31 UTC (rev 129221)
+++ zope.testrunner/trunk/src/zope/testrunner/find.py	2013-02-08 13:14:44 UTC (rev 129222)
@@ -192,11 +192,12 @@
                 except KeyboardInterrupt:
                     raise
                 except:
+                    exc_info = sys.exc_info()
+                    if not options.post_mortem:
+                        # Skip a couple of frames
+                        exc_info = exc_info[:2] + (exc_info[2].tb_next.tb_next,)
                     suite = StartUpFailure(
-                        options, module_name,
-                        sys.exc_info()[:2]
-                        + (sys.exc_info()[2].tb_next.tb_next,),
-                        )
+                        options, module_name, exc_info)
                 else:
                     try:
                         if hasattr(module, options.suite_name):
@@ -218,8 +219,12 @@
                     except KeyboardInterrupt:
                         raise
                     except:
+                        exc_info = sys.exc_info()
+                        if not options.post_mortem:
+                            # Suppress traceback
+                            exc_info = exc_info[:2] + (None,)
                         suite = StartUpFailure(
-                            options, module_name, sys.exc_info()[:2]+(None,))
+                            options, module_name, exc_info)
 
                 yield suite
                 break

Added: zope.testrunner/trunk/src/zope/testrunner/testrunner-debugging-import-failure.test
===================================================================
--- zope.testrunner/trunk/src/zope/testrunner/testrunner-debugging-import-failure.test	                        (rev 0)
+++ zope.testrunner/trunk/src/zope/testrunner/testrunner-debugging-import-failure.test	2013-02-08 13:14:44 UTC (rev 129222)
@@ -0,0 +1,64 @@
+Regression tests for https://bugs.launchpad.net/zope.testrunner/+bug/1119363
+
+Post-mortem debugging also works when there is an import failure.
+
+    >>> import os, shutil, sys, tempfile
+    >>> tdir = tempfile.mkdtemp()
+    >>> dir = os.path.join(tdir, 'TESTS-DIR')
+    >>> os.mkdir(dir)
+    >>> written = open(os.path.join(dir, 'tests.py'), 'w').write(
+    ... '''
+    ... impot doctest
+    ... ''')
+
+    >>> class Input:
+    ...     def __init__(self, src):
+    ...         self.lines = src.split('\n')
+    ...     def readline(self):
+    ...         line = self.lines.pop(0)
+    ...         print line
+    ...         return line+'\n'
+
+    >>> real_stdin = sys.stdin
+    >>> sys.stdin = Input('c')
+
+    >>> sys.argv = [testrunner_script]
+    >>> import zope.testrunner
+    >>> try:
+    ...     zope.testrunner.run_internal(['--path', dir, '-D'])
+    ... except zope.testrunner.interfaces.EndRun: print 'EndRun raised'
+    ... finally: sys.stdin = real_stdin
+    ... # doctest: +ELLIPSIS +REPORT_NDIFF
+    exceptions.SyntaxError:
+    invalid syntax (tests.py, line 2)
+    > ...find.py(399)import_name()
+    -> __import__(name)
+    (Pdb) c
+    EndRun raised
+
+Post-mortem debugging also works when the test suite is invalid:
+
+    >>> sys.stdin = Input('c')
+
+    >>> written = open(os.path.join(dir, 'tests2.py'), 'w').write(
+    ... '''
+    ... def test_suite():
+    ...     return None
+    ... ''')
+
+    >>> import sys
+    >>> try:
+    ...     zope.testrunner.run_internal(
+    ...       ['--path', dir, '-Dvv', '--tests-pattern', 'tests2'])
+    ... except zope.testrunner.interfaces.EndRun: print 'EndRun raised'
+    ... finally: sys.stdin = real_stdin
+    ... # doctest: +ELLIPSIS +REPORT_NDIFF
+    exceptions.TypeError:
+    Invalid test_suite, None, in tests2
+    > ...find.py(217)find_suites()
+    -> % (suite, module_name)
+    (Pdb) c
+    EndRun raised
+
+    >>> shutil.rmtree(tdir)
+

Modified: zope.testrunner/trunk/src/zope/testrunner/tests.py
===================================================================
--- zope.testrunner/trunk/src/zope/testrunner/tests.py	2013-02-08 10:52:31 UTC (rev 129221)
+++ zope.testrunner/trunk/src/zope/testrunner/tests.py	2013-02-08 13:14:44 UTC (rev 129222)
@@ -90,6 +90,10 @@
         #(re.compile('^> [^\n]+->None$', re.M), '> ...->None'),
         (re.compile('import pdb; pdb'), 'Pdb()'), # Py 2.3
 
+        # Python 3 exceptions are from the builtins module
+        (re.compile(r'builtins\.(SyntaxError|TypeError)'),
+         r'exceptions.\1'),
+
         # Python 3.3 has better exception messages
         (re.compile("ImportError: No module named '(?:[^']*[.])?([^'.]*)'"),
          r'ImportError: No module named \1'),
@@ -145,6 +149,10 @@
          r''),
         (re.compile('import pdb; pdb'), 'Pdb()'), # Py 2.3
 
+        # Python 3 exceptions are from the builtins module
+        (re.compile(r'builtins\.(SyntaxError|TypeError)'),
+         r'exceptions.\1'),
+
         # Python 3.3 has better exception messages
         (re.compile("ImportError: No module named '(?:[^']*[.])?([^'.]*)'"),
          r'ImportError: No module named \1'),
@@ -175,6 +183,7 @@
         'testrunner-arguments.txt',
         'testrunner-coverage.txt',
         'testrunner-debugging-layer-setup.test',
+        'testrunner-debugging-import-failure.test',
         'testrunner-debugging.txt',
         'testrunner-edge-cases.txt',
         'testrunner-errors.txt',



More information about the checkins mailing list