~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-01-26 10:49:57 UTC
  • mfrom: (4987.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100126104957-dmtqnc0pckuruyla
(vila,
        jam) Implement TestCase.overrideAttr to simplify tests setUp/cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
126
126
 
127
127
default_transport = LocalURLServer
128
128
 
 
129
 
 
130
_unitialized_attr = object()
 
131
"""A sentinel needed to act as a default value in a method signature."""
 
132
 
 
133
 
129
134
# Subunit result codes, defined here to prevent a hard dependency on subunit.
130
135
SUBUNIT_SEEK_SET = 0
131
136
SUBUNIT_SEEK_CUR = 1
850
855
        Tests that want to use debug flags can just set them in the
851
856
        debug_flags set during setup/teardown.
852
857
        """
853
 
        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))
854
860
        if 'allow_debug' not in selftest_debug_flags:
855
861
            debug.debug_flags.clear()
856
862
        if 'disable_lock_checks' not in selftest_debug_flags:
857
863
            debug.debug_flags.add('strict_locks')
858
 
        self.addCleanup(self._restore_debug_flags)
859
864
 
860
865
    def _clear_hooks(self):
861
866
        # prevent hooks affecting tests
882
887
    def _silenceUI(self):
883
888
        """Turn off UI for duration of test"""
884
889
        # by default the UI is off; tests can turn it on if they want it.
885
 
        saved = ui.ui_factory
886
 
        def _restore():
887
 
            ui.ui_factory = saved
888
 
        ui.ui_factory = ui.SilentUIFactory()
889
 
        self.addCleanup(_restore)
 
890
        self.overrideAttr(ui, 'ui_factory', ui.SilentUIFactory())
890
891
 
891
892
    def _check_locks(self):
892
893
        """Check that all lock take/release actions have been paired."""
921
922
            self._lock_check_thorough = False
922
923
        else:
923
924
            self._lock_check_thorough = True
924
 
            
 
925
 
925
926
        self.addCleanup(self._check_locks)
926
927
        _mod_lock.Lock.hooks.install_named_hook('lock_acquired',
927
928
                                                self._lock_acquired, None)
1479
1480
        """
1480
1481
        self._cleanups.append((callable, args, kwargs))
1481
1482
 
 
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.
 
1492
 
 
1493
        :returns: The actual attr value.
 
1494
        """
 
1495
        value = getattr(obj, attr_name)
 
1496
        # The actual value is captured by the call below
 
1497
        self.addCleanup(setattr, obj, attr_name, value)
 
1498
        if new is not _unitialized_attr:
 
1499
            setattr(obj, attr_name, new)
 
1500
        return value
 
1501
 
1482
1502
    def _cleanEnvironment(self):
1483
1503
        new_env = {
1484
1504
            'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.
1530
1550
        """Set an environment variable, and reset it when finished."""
1531
1551
        self.__old_env[name] = osutils.set_or_unset_env(name, newvalue)
1532
1552
 
1533
 
    def _restore_debug_flags(self):
1534
 
        debug.debug_flags.clear()
1535
 
        debug.debug_flags.update(self._preserved_debug_flags)
1536
 
 
1537
1553
    def _restoreEnvironment(self):
1538
1554
        for name, value in self.__old_env.iteritems():
1539
1555
            osutils.set_or_unset_env(name, value)
2037
2053
 
2038
2054
        Tests that expect to provoke LockContention errors should call this.
2039
2055
        """
2040
 
        orig_timeout = bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS
2041
 
        def resetTimeout():
2042
 
            bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = orig_timeout
2043
 
        self.addCleanup(resetTimeout)
2044
 
        bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 0
 
2056
        self.overrideAttr(bzrlib.lockdir, '_DEFAULT_TIMEOUT_SECONDS', 0)
2045
2057
 
2046
2058
    def make_utf8_encoded_stringio(self, encoding_type=None):
2047
2059
        """Return a StringIOWrapper instance, that will encode Unicode
2061
2073
        request_handlers = request.request_handlers
2062
2074
        orig_method = request_handlers.get(verb)
2063
2075
        request_handlers.remove(verb)
2064
 
        def restoreVerb():
2065
 
            request_handlers.register(verb, orig_method)
2066
 
        self.addCleanup(restoreVerb)
 
2076
        self.addCleanup(request_handlers.register, verb, orig_method)
2067
2077
 
2068
2078
 
2069
2079
class CapturedCall(object):
2376
2386
    def setUp(self):
2377
2387
        super(TestCaseWithMemoryTransport, self).setUp()
2378
2388
        self._make_test_root()
2379
 
        _currentdir = os.getcwdu()
2380
 
        def _leaveDirectory():
2381
 
            os.chdir(_currentdir)
2382
 
        self.addCleanup(_leaveDirectory)
 
2389
        self.addCleanup(os.chdir, os.getcwdu())
2383
2390
        self.makeAndChdirToTestDir()
2384
2391
        self.overrideEnvironmentForTesting()
2385
2392
        self.__readonly_server = None