Rev 4978: Implement test.addAttrCleanup. in file:///home/vila/src/bzr/bugs/476293-log-check-ancestor/

Vincent Ladeuil v.ladeuil+lp at free.fr
Sat Jan 23 10:27:37 GMT 2010


At file:///home/vila/src/bzr/bugs/476293-log-check-ancestor/

------------------------------------------------------------
revno: 4978
revision-id: v.ladeuil+lp at free.fr-20100123102737-ei7i2abkdj53yile
parent: v.ladeuil+lp at free.fr-20100122143237-9mix42wxn7rabrru
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: cleanup
timestamp: Sat 2010-01-23 11:27:37 +0100
message:
  Implement test.addAttrCleanup.
  
  * bzrlib/tests/test_selftest.py:
  (TestRunner.test_addAttrCleanup): Ensures that the attribute value
  is restored properly.
  
  * bzrlib/tests/__init__.py:
  (TestCase.addAttrCleanup): Helper to restore attributes modified
  by tests.
-------------- next part --------------
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2010-01-20 16:31:24 +0000
+++ b/bzrlib/tests/__init__.py	2010-01-23 10:27:37 +0000
@@ -1479,6 +1479,16 @@
         """
         self._cleanups.append((callable, args, kwargs))
 
+    def addAttrCleanup(self, obj, attr_name):
+        """Add a cleanup which restores the attribute to its original value.
+
+        :returns: The actual attr value.
+        """
+        value = getattr(obj, attr_name)
+        # The actual value is captured by the call below
+        self.addCleanup(setattr, obj, attr_name, value)
+        return value
+
     def _cleanEnvironment(self):
         new_env = {
             'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2010-01-17 21:48:39 +0000
+++ b/bzrlib/tests/test_selftest.py	2010-01-23 10:27:37 +0000
@@ -1252,6 +1252,26 @@
         result = self.run_test_runner(runner, test)
         self.assertLength(1, calls)
 
+    def test_addAttrCleanup(self):
+        self.test_attr = 'original' # Define a test attribute
+        obj = self # Make 'obj' visible to the embedded test
+        class Test(tests.TestCase):
+
+            def setUp(self):
+                tests.TestCase.setUp(self)
+                self.orig = self.addAttrCleanup(obj, 'test_attr')
+                obj.test_attr = 'modified'
+
+            def test_value(self):
+                self.assertEqual('original', self.orig)
+                self.assertEqual('modified', obj.test_attr)
+
+        runner = tests.TextTestRunner(stream=self._log_file)
+        test = Test('test_value')
+        result = self.run_test_runner(runner, test)
+        self.assertTrue(result.wasSuccessful())
+        self.assertEqual('original', obj.test_attr)
+
 
 class SampleTestCase(tests.TestCase):
 



More information about the bazaar-commits mailing list