~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Vincent Ladeuil
  • Date: 2010-01-25 17:48:22 UTC
  • mto: (4987.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4988.
  • Revision ID: v.ladeuil+lp@free.fr-20100125174822-nce4l19sbwx83jvq
Deploying the new overrideAttr facility further reduces the complexity
and make the code clearer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
855
855
        Tests that want to use debug flags can just set them in the
856
856
        debug_flags set during setup/teardown.
857
857
        """
858
 
        self._preserved_debug_flags = set(debug.debug_flags)
 
858
        # Start with a copy of the current debug flags we can safely modify.
 
859
        self.overrideAttr(debug, 'debug_flags', set(debug.debug_flags))
859
860
        if 'allow_debug' not in selftest_debug_flags:
860
861
            debug.debug_flags.clear()
861
862
        if 'disable_lock_checks' not in selftest_debug_flags:
862
863
            debug.debug_flags.add('strict_locks')
863
 
        # XXX: needs more thinking
864
 
        self.addCleanup(self._restore_debug_flags)
865
864
 
866
865
    def _clear_hooks(self):
867
866
        # prevent hooks affecting tests
887
886
 
888
887
    def _silenceUI(self):
889
888
        """Turn off UI for duration of test"""
890
 
        self.addAttrCleanup(ui, 'ui_factory')
891
889
        # by default the UI is off; tests can turn it on if they want it.
892
 
        ui.ui_factory = ui.SilentUIFactory()
 
890
        self.overrideAttr(ui, 'ui_factory', ui.SilentUIFactory())
893
891
 
894
892
    def _check_locks(self):
895
893
        """Check that all lock take/release actions have been paired."""
1482
1480
        """
1483
1481
        self._cleanups.append((callable, args, kwargs))
1484
1482
 
1485
 
    def overrideAttr(test, obj, attr_name, new=_unitialized_attr):
1486
 
        """Add a cleanup which restores the attribute to its original value.
 
1483
    def overrideAttr(self, obj, attr_name, new=_unitialized_attr):
 
1484
        """Overrides an object attribute restoring it after the test.
 
1485
 
 
1486
        :param obj: The object that will be mutated.
 
1487
 
 
1488
        :param attr_name: The attribute name we want to preserve/override in
 
1489
            the object.
 
1490
 
 
1491
        :param new: The optional value we want to set the attribute to.
1487
1492
 
1488
1493
        :returns: The actual attr value.
1489
1494
        """
1545
1550
        """Set an environment variable, and reset it when finished."""
1546
1551
        self.__old_env[name] = osutils.set_or_unset_env(name, newvalue)
1547
1552
 
1548
 
    def _restore_debug_flags(self):
1549
 
        debug.debug_flags.clear()
1550
 
        debug.debug_flags.update(self._preserved_debug_flags)
1551
 
 
1552
1553
    def _restoreEnvironment(self):
1553
1554
        for name, value in self.__old_env.iteritems():
1554
1555
            osutils.set_or_unset_env(name, value)
2052
2053
 
2053
2054
        Tests that expect to provoke LockContention errors should call this.
2054
2055
        """
2055
 
        self.addAttrCleanup(bzrlib.lockdir, '_DEFAULT_TIMEOUT_SECONDS')
2056
 
        bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 0
 
2056
        self.overrideAttr(bzrlib.lockdir, '_DEFAULT_TIMEOUT_SECONDS', 0)
2057
2057
 
2058
2058
    def make_utf8_encoded_stringio(self, encoding_type=None):
2059
2059
        """Return a StringIOWrapper instance, that will encode Unicode