~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 15:55:48 UTC
  • mto: (4985.1.4 add-attr-cleanup)
  • mto: This revision was merged to the branch mainline in revision 4988.
  • Revision ID: v.ladeuil+lp@free.fr-20100125155548-0l352pujvt5bzl5e
Deploy addAttrCleanup on the whole test suite.

Several use case worth mentioning:

- setting a module or any other object attribute is the majority
by far. In some cases the setting itself is deferred but most of
the time we want to set at the same time we add the cleanup.

- there multiple occurrences of protecting hooks or ui factory
which are now useless (the test framework takes care of that now),

- there was some lambda uses that can now be avoided.

That first cleanup already simplifies things a lot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
855
855
            debug.debug_flags.clear()
856
856
        if 'disable_lock_checks' not in selftest_debug_flags:
857
857
            debug.debug_flags.add('strict_locks')
 
858
        # XXX: needs more thinking
858
859
        self.addCleanup(self._restore_debug_flags)
859
860
 
860
861
    def _clear_hooks(self):
881
882
 
882
883
    def _silenceUI(self):
883
884
        """Turn off UI for duration of test"""
 
885
        self.addAttrCleanup(ui, 'ui_factory')
884
886
        # 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
887
        ui.ui_factory = ui.SilentUIFactory()
889
 
        self.addCleanup(_restore)
890
888
 
891
889
    def _check_locks(self):
892
890
        """Check that all lock take/release actions have been paired."""
921
919
            self._lock_check_thorough = False
922
920
        else:
923
921
            self._lock_check_thorough = True
924
 
            
 
922
 
925
923
        self.addCleanup(self._check_locks)
926
924
        _mod_lock.Lock.hooks.install_named_hook('lock_acquired',
927
925
                                                self._lock_acquired, None)
2047
2045
 
2048
2046
        Tests that expect to provoke LockContention errors should call this.
2049
2047
        """
2050
 
        orig_timeout = bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS
2051
 
        def resetTimeout():
2052
 
            bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = orig_timeout
2053
 
        self.addCleanup(resetTimeout)
 
2048
        self.addAttrCleanup(bzrlib.lockdir, '_DEFAULT_TIMEOUT_SECONDS')
2054
2049
        bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 0
2055
2050
 
2056
2051
    def make_utf8_encoded_stringio(self, encoding_type=None):
2071
2066
        request_handlers = request.request_handlers
2072
2067
        orig_method = request_handlers.get(verb)
2073
2068
        request_handlers.remove(verb)
2074
 
        def restoreVerb():
2075
 
            request_handlers.register(verb, orig_method)
2076
 
        self.addCleanup(restoreVerb)
 
2069
        self.addCleanup(request_handlers.register, verb, orig_method)
2077
2070
 
2078
2071
 
2079
2072
class CapturedCall(object):
2386
2379
    def setUp(self):
2387
2380
        super(TestCaseWithMemoryTransport, self).setUp()
2388
2381
        self._make_test_root()
2389
 
        _currentdir = os.getcwdu()
2390
 
        def _leaveDirectory():
2391
 
            os.chdir(_currentdir)
2392
 
        self.addCleanup(_leaveDirectory)
 
2382
        self.addCleanup(os.chdir, os.getcwdu())
2393
2383
        self.makeAndChdirToTestDir()
2394
2384
        self.overrideEnvironmentForTesting()
2395
2385
        self.__readonly_server = None