Rev 2561: Make debug.debug_flags be isolated for all tests. in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Thu Jun 28 09:29:05 BST 2007


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 2561
revision-id: pqm at pqm.ubuntu.com-20070628082903-b21gad45bimzvmgu
parent: pqm at pqm.ubuntu.com-20070628070827-h5s313dg5tnag9vj
parent: robertc at robertcollins.net-20070628080025-nc7i3aax1hz39dkw
committer: Canonical.com Patch Queue Manager<pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Thu 2007-06-28 09:29:03 +0100
message:
  Make debug.debug_flags be isolated for all tests.
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_selftest.py  test_selftest.py-20051202044319-c110a115d8c0456a
    ------------------------------------------------------------
    revno: 2560.1.3
    merged: robertc at robertcollins.net-20070628080025-nc7i3aax1hz39dkw
    parent: robertc at robertcollins.net-20070628075053-uzqfrh5jvizm2fzf
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: debug-test-isolation
    timestamp: Thu 2007-06-28 18:00:25 +1000
    message:
      Allow 'from debug import debug_flags'
    ------------------------------------------------------------
    revno: 2560.1.2
    merged: robertc at robertcollins.net-20070628075053-uzqfrh5jvizm2fzf
    parent: robertc at robertcollins.net-20070628074823-cj7pcabpm2jrdi7m
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: debug-test-isolation
    timestamp: Thu 2007-06-28 17:50:53 +1000
    message:
      Remember NEWS
    ------------------------------------------------------------
    revno: 2560.1.1
    merged: robertc at robertcollins.net-20070628074823-cj7pcabpm2jrdi7m
    parent: pqm at pqm.ubuntu.com-20070628070827-h5s313dg5tnag9vj
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: debug-test-isolation
    timestamp: Thu 2007-06-28 17:48:23 +1000
    message:
      Make debug.debug_flags be isolated for all tests.
=== modified file 'NEWS'
--- a/NEWS	2007-06-28 07:08:27 +0000
+++ b/NEWS	2007-06-28 07:50:53 +0000
@@ -73,6 +73,9 @@
       either a list of parameters, a string containing the command line, or
       (deprecated) varargs parameters.  (Martin Pool)
 
+    * The base TestCase now isolates tests from -D parameters by clearing
+      ``debug.debug_flags`` and restores it afterwards. (Robert Collins)
+
   BUGFIXES:
 
     * Work around python-2.4.1 inhability to correctly parse the

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-06-28 07:08:27 +0000
+++ b/bzrlib/tests/__init__.py	2007-06-28 08:00:25 +0000
@@ -752,6 +752,17 @@
         self._benchcalls = []
         self._benchtime = None
         self._clear_hooks()
+        self._clear_debug_flags()
+
+    def _clear_debug_flags(self):
+        """Prevent externally set debug flags affecting tests.
+        
+        Tests that want to use debug flags can just set them in the
+        debug_flags set during setup/teardown.
+        """
+        self._preserved_debug_flags = set(debug.debug_flags)
+        debug.debug_flags.clear()
+        self.addCleanup(self._restore_debug_flags)
 
     def _clear_hooks(self):
         # prevent hooks affecting tests
@@ -765,9 +776,6 @@
         # reset all hooks to an empty instance of the appropriate type
         bzrlib.branch.Branch.hooks = bzrlib.branch.BranchHooks()
         bzrlib.smart.server.SmartTCPServer.hooks = bzrlib.smart.server.SmartServerHooks()
-        # FIXME: Rather than constructing new objects like this, how about
-        # having save() and clear() methods on the base Hook class? mbp
-        # 20070416
 
     def _silenceUI(self):
         """Turn off UI for duration of test"""
@@ -1108,6 +1116,10 @@
         """Set an environment variable, and reset it when finished."""
         self.__old_env[name] = osutils.set_or_unset_env(name, newvalue)
 
+    def _restore_debug_flags(self):
+        debug.debug_flags.clear()
+        debug.debug_flags.update(self._preserved_debug_flags)
+
     def _restoreEnvironment(self):
         for name, value in self.__old_env.iteritems():
             osutils.set_or_unset_env(name, value)
@@ -1269,15 +1281,10 @@
             os.chdir(working_dir)
 
         try:
-            saved_debug_flags = frozenset(debug.debug_flags)
-            debug.debug_flags.clear()
-            try:
-                result = self.apply_redirected(ui.ui_factory.stdin,
-                    stdout, stderr,
-                    bzrlib.commands.run_bzr_catch_errors,
-                    args)
-            finally:
-                debug.debug_flags.update(saved_debug_flags)
+            result = self.apply_redirected(ui.ui_factory.stdin,
+                stdout, stderr,
+                bzrlib.commands.run_bzr_catch_errors,
+                args)
         finally:
             logger.removeHandler(handler)
             ui.ui_factory = old_ui_factory

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2007-06-18 13:55:14 +0000
+++ b/bzrlib/tests/test_selftest.py	2007-06-28 07:48:23 +0000
@@ -1128,6 +1128,13 @@
 class TestTestCase(TestCase):
     """Tests that test the core bzrlib TestCase."""
 
+    def test_debug_flags_sanitised(self):
+        """The bzrlib debug flags should be sanitised by setUp."""
+        # we could set something and run a test that will check
+        # it gets santised, but this is probably sufficient for now:
+        # if someone runs the test with -Dsomething it will error.
+        self.assertEqual(set(), bzrlib.debug.debug_flags)
+
     def inner_test(self):
         # the inner child test
         note("inner_test")




More information about the bazaar-commits mailing list