~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2011-11-28 15:18:59 UTC
  • mto: This revision was merged to the branch mainline in revision 6317.
  • Revision ID: jelmer@samba.org-20111128151859-dw4nua1xiobilw4r
Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Testing framework extensions"""
18
18
 
19
 
from __future__ import absolute_import
20
 
 
21
19
# NOTE: Some classes in here use camelCaseNaming() rather than
22
20
# underscore_naming().  That's for consistency with unittest; it's not the
23
21
# general style of bzrlib.  Please continue that consistency when adding e.g.
38
36
import random
39
37
import re
40
38
import shlex
41
 
import site
42
39
import stat
43
40
import subprocess
44
41
import sys
97
94
    deprecated_in,
98
95
    )
99
96
from bzrlib.tests import (
100
 
    fixtures,
101
97
    test_server,
102
98
    TestUtil,
103
99
    treeshape,
247
243
    different types of display.
248
244
 
249
245
    When a test finishes, in whatever way, it calls one of the addSuccess,
250
 
    addFailure or addError methods.  These in turn may redirect to a more
 
246
    addFailure or addError classes.  These in turn may redirect to a more
251
247
    specific case for the special test results supported by our extended
252
248
    tests.
253
249
 
999
995
 
1000
996
    def setUp(self):
1001
997
        super(TestCase, self).setUp()
1002
 
 
1003
 
        timeout = config.GlobalStack().get('selftest.timeout')
1004
 
        if timeout:
1005
 
            timeout_fixture = fixtures.TimeoutFixture(timeout)
1006
 
            timeout_fixture.setUp()
1007
 
            self.addCleanup(timeout_fixture.cleanUp)
1008
 
 
1009
998
        for feature in getattr(self, '_test_needs_features', []):
1010
999
            self.requireFeature(feature)
1011
1000
        self._cleanEnvironment()
1012
 
 
1013
1001
        if bzrlib.global_state is not None:
1014
1002
            self.overrideAttr(bzrlib.global_state, 'cmdline_overrides',
1015
1003
                              config.CommandLineStore())
1016
 
 
1017
1004
        self._silenceUI()
1018
1005
        self._startLogFile()
1019
1006
        self._benchcalls = []
1992
1979
 
1993
1980
        self.log('run bzr: %r', args)
1994
1981
        # FIXME: don't call into logging here
1995
 
        handler = trace.EncodedStreamHandler(stderr, errors="replace",
1996
 
            level=logging.INFO)
 
1982
        handler = logging.StreamHandler(stderr)
 
1983
        handler.setLevel(logging.INFO)
1997
1984
        logger = logging.getLogger('')
1998
1985
        logger.addHandler(handler)
1999
1986
        old_ui_factory = ui.ui_factory
2187
2174
 
2188
2175
        if env_changes is None:
2189
2176
            env_changes = {}
2190
 
        # Because $HOME is set to a tempdir for the context of a test, modules
2191
 
        # installed in the user dir will not be found unless $PYTHONUSERBASE
2192
 
        # gets set to the computed directory of this parent process.
2193
 
        if site.USER_BASE is not None:
2194
 
            env_changes["PYTHONUSERBASE"] = site.USER_BASE
2195
2177
        old_env = {}
2196
2178
 
2197
2179
        def cleanup_environment():
2388
2370
        from bzrlib.smart import request
2389
2371
        request_handlers = request.request_handlers
2390
2372
        orig_method = request_handlers.get(verb)
2391
 
        orig_info = request_handlers.get_info(verb)
2392
2373
        request_handlers.remove(verb)
2393
 
        self.addCleanup(request_handlers.register, verb, orig_method,
2394
 
            info=orig_info)
 
2374
        self.addCleanup(request_handlers.register, verb, orig_method)
2395
2375
 
2396
2376
 
2397
2377
class CapturedCall(object):
2658
2638
        self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
2659
2639
        self.permit_dir(self.test_dir)
2660
2640
 
2661
 
    def make_branch(self, relpath, format=None, name=None):
 
2641
    def make_branch(self, relpath, format=None):
2662
2642
        """Create a branch on the transport at relpath."""
2663
2643
        repo = self.make_repository(relpath, format=format)
2664
 
        return repo.bzrdir.create_branch(append_revisions_only=False,
2665
 
                                         name=name)
 
2644
        return repo.bzrdir.create_branch(append_revisions_only=False)
2666
2645
 
2667
2646
    def get_default_format(self):
2668
2647
        return 'default'
2735
2714
 
2736
2715
    def setUp(self):
2737
2716
        super(TestCaseWithMemoryTransport, self).setUp()
2738
 
 
2739
 
        def _add_disconnect_cleanup(transport):
2740
 
            """Schedule disconnection of given transport at test cleanup
2741
 
 
2742
 
            This needs to happen for all connected transports or leaks occur.
2743
 
 
2744
 
            Note reconnections may mean we call disconnect multiple times per
2745
 
            transport which is suboptimal but seems harmless.
2746
 
            """
2747
 
            self.addCleanup(transport.disconnect)
2748
 
 
2749
 
        _mod_transport.Transport.hooks.install_named_hook('post_connect',
2750
 
            _add_disconnect_cleanup, None)
2751
 
 
 
2717
        # Ensure that ConnectedTransport doesn't leak sockets
 
2718
        def get_transport_from_url_with_cleanup(*args, **kwargs):
 
2719
            t = orig_get_transport_from_url(*args, **kwargs)
 
2720
            if isinstance(t, _mod_transport.ConnectedTransport):
 
2721
                self.addCleanup(t.disconnect)
 
2722
            return t
 
2723
 
 
2724
        orig_get_transport_from_url = self.overrideAttr(
 
2725
            _mod_transport, 'get_transport_from_url',
 
2726
            get_transport_from_url_with_cleanup)
2752
2727
        self._make_test_root()
2753
2728
        self.addCleanup(os.chdir, os.getcwdu())
2754
2729
        self.makeAndChdirToTestDir()
2760
2735
    def setup_smart_server_with_call_log(self):
2761
2736
        """Sets up a smart server as the transport server with a call log."""
2762
2737
        self.transport_server = test_server.SmartTCPServer_for_testing
2763
 
        self.hpss_connections = []
2764
2738
        self.hpss_calls = []
2765
2739
        import traceback
2766
2740
        # Skip the current stack down to the caller of
2769
2743
        def capture_hpss_call(params):
2770
2744
            self.hpss_calls.append(
2771
2745
                CapturedCall(params, prefix_length))
2772
 
        def capture_connect(transport):
2773
 
            self.hpss_connections.append(transport)
2774
2746
        client._SmartClient.hooks.install_named_hook(
2775
2747
            'call', capture_hpss_call, None)
2776
 
        _mod_transport.Transport.hooks.install_named_hook(
2777
 
            'post_connect', capture_connect, None)
2778
2748
 
2779
2749
    def reset_smart_call_log(self):
2780
2750
        self.hpss_calls = []
2781
 
        self.hpss_connections = []
2782
2751
 
2783
2752
 
2784
2753
class TestCaseInTempDir(TestCaseWithMemoryTransport):
4003
3972
        'bzrlib.tests.test_http',
4004
3973
        'bzrlib.tests.test_http_response',
4005
3974
        'bzrlib.tests.test_https_ca_bundle',
4006
 
        'bzrlib.tests.test_https_urllib',
4007
3975
        'bzrlib.tests.test_i18n',
4008
3976
        'bzrlib.tests.test_identitymap',
4009
3977
        'bzrlib.tests.test_ignores',
4058
4026
        'bzrlib.tests.test_revisiontree',
4059
4027
        'bzrlib.tests.test_rio',
4060
4028
        'bzrlib.tests.test_rules',
4061
 
        'bzrlib.tests.test_url_policy_open',
4062
4029
        'bzrlib.tests.test_sampler',
4063
4030
        'bzrlib.tests.test_scenarios',
4064
4031
        'bzrlib.tests.test_script',
4108
4075
        'bzrlib.tests.test_version',
4109
4076
        'bzrlib.tests.test_version_info',
4110
4077
        'bzrlib.tests.test_versionedfile',
4111
 
        'bzrlib.tests.test_vf_search',
4112
4078
        'bzrlib.tests.test_weave',
4113
4079
        'bzrlib.tests.test_whitebox',
4114
4080
        'bzrlib.tests.test_win32utils',