Rev 4986: Implement TestCase.addAttrCleanup in file:///home/vila/src/bzr/bugs/add-attr-cleanup/

Vincent Ladeuil v.ladeuil+lp at free.fr
Sat Jan 23 17:20:02 GMT 2010


At file:///home/vila/src/bzr/bugs/add-attr-cleanup/

------------------------------------------------------------
revno: 4986 [merge]
revision-id: v.ladeuil+lp at free.fr-20100123172001-ujlk1bobwau2vcxx
parent: pqm at pqm.ubuntu.com-20100123121233-yse96ztm8u0l3ljn
parent: v.ladeuil+lp at free.fr-20100123102737-ei7i2abkdj53yile
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: add-attr-cleanup
timestamp: Sat 2010-01-23 18:20:01 +0100
message:
  Implement TestCase.addAttrCleanup
modified:
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_selftest.py  test_selftest.py-20051202044319-c110a115d8c0456a
-------------- 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