Rev 2831: If TestCase.run_bzr hits an internal exception, don't catch it but rather propagate up into the test suite in http://sourcefrog.net/bzr/test-traceback

Martin Pool mbp at sourcefrog.net
Tue Sep 18 06:35:32 BST 2007


At http://sourcefrog.net/bzr/test-traceback

------------------------------------------------------------
revno: 2831
revision-id: mbp at sourcefrog.net-20070918053531-hwrmyg05y3az3xcx
parent: pqm at pqm.ubuntu.com-20070918034007-n72x452efuovdelm
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: test-traceback
timestamp: Tue 2007-09-18 15:35:31 +1000
message:
  If TestCase.run_bzr hits an internal exception, don't catch it but rather propagate up into the test suite
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/commands.py             bzr.py-20050309040720-d10f4714595cf8c3
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_selftest.py  test_selftest.py-20051202044319-c110a115d8c0456a
  bzrlib/trace.py                trace.py-20050309040759-c8ed824bdcd4748a
=== modified file 'NEWS'
--- a/NEWS	2007-09-18 01:29:59 +0000
+++ b/NEWS	2007-09-18 05:35:31 +0000
@@ -65,6 +65,12 @@
 
   TESTING:
 
+   * When running bzr commands within the test suite, internal exceptions are
+     not caught and reported in the usual way, but rather allowed to propagate
+     up and be visible to the test suite.  A new API ``run_bzr_catch_user_errors``
+     makes this behavior available to other users.
+     (Martin Pool)
+
 
 bzr 0.91rc2 2007-09-11
 ======================

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2007-09-10 02:40:07 +0000
+++ b/bzrlib/commands.py	2007-09-18 05:35:31 +0000
@@ -811,6 +811,23 @@
         return 3
 
 
+def run_bzr_catch_user_errors(argv):
+    """Run bzr and report user errors, but let internal errors propagate.
+
+    This is used for the test suite, and might be useful for other programs
+    that want to wrap the commandline interface.
+    """
+    try:
+        return run_bzr(argv)
+    except Exception, e:
+        if (isinstance(e, (OSError, IOError))
+            or not getattr(e, 'internal_error', True)):
+            trace.report_exception(sys.exc_info(), sys.stderr)
+            return 3
+        else:
+            raise
+
+
 class HelpCommandIndex(object):
     """A index for bzr help that returns commands."""
 

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-09-13 01:54:49 +0000
+++ b/bzrlib/tests/__init__.py	2007-09-18 05:35:31 +0000
@@ -1351,7 +1351,7 @@
         try:
             result = self.apply_redirected(ui.ui_factory.stdin,
                 stdout, stderr,
-                bzrlib.commands.run_bzr_catch_errors,
+                bzrlib.commands.run_bzr_catch_user_errors,
                 args)
         finally:
             logger.removeHandler(handler)

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2007-09-03 04:35:49 +0000
+++ b/bzrlib/tests/test_selftest.py	2007-09-18 05:35:31 +0000
@@ -1658,3 +1658,29 @@
             self.check_inventory_shape(tree.inventory, files)
         finally:
             tree.unlock()
+
+
+class TestBlackboxSupport(TestCase):
+    """Tests for testsuite blackbox features."""
+
+    def test_run_bzr_failure_not_caught(self):
+        # When we run bzr in blackbox mode, we want any unexpected errors to
+        # propagate up to the test suite so that it can show the error in the
+        # usual way, and we won't get a double traceback.
+        e = self.assertRaises(
+            AssertionError,
+            self.run_bzr, ['assert-fail'])
+        # make sure we got the real thing, not an error from somewhere else in
+        # the test framework
+        self.assertEquals('always fails', str(e))
+        # check that there's no traceback in the test log
+        self.assertNotContainsRe(self._get_log(keep_log_file=True),
+            r'Traceback')
+
+    def test_run_bzr_user_error_caught(self):
+        # Running bzr in blackbox mode, normal/expected/user errors should be
+        # caught in the regular way and turned into an error message plus exit
+        # code.
+        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
+        self.assertEqual(out, '')
+        self.assertEqual(err, 'bzr: ERROR: Not a branch: "/nonexistantpath/".\n')

=== modified file 'bzrlib/trace.py'
--- a/bzrlib/trace.py	2007-08-31 02:00:37 +0000
+++ b/bzrlib/trace.py	2007-09-18 05:35:31 +0000
@@ -308,6 +308,8 @@
 
 
 def report_exception(exc_info, err_file):
+    """Report an exception to err_file (typically stderr) and to .bzr.log.
+    """
     exc_type, exc_object, exc_tb = exc_info
     # Log the full traceback to ~/.bzr.log
     log_exception_quietly()




More information about the bazaar-commits mailing list