~ubuntu-branches/debian/sid/bzr/sid-201308041036

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_import_tariff.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2011-06-17 01:15:56 UTC
  • mfrom: (1.5.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20110617011556-mi7z6c6e2fumivln
Tags: 2.4.0~beta4-1
* New upstream release.
 + Using "bzr blame" no longer requires user to set identity.  LP: #667408
* Bump standards version to 3.9.2 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Tests for how many modules are loaded in executing various commands."""
19
19
 
20
20
import os
 
21
 
21
22
from testtools import content
22
23
 
 
24
from bzrlib import (
 
25
    plugins as _mod_plugins,
 
26
    trace,
 
27
    )
23
28
from bzrlib.bzrdir import BzrDir
24
29
from bzrlib.smart import medium
25
30
from bzrlib.transport import remote
48
53
    ]
49
54
 
50
55
 
51
 
class TestImportTariffs(TestCaseWithTransport):
 
56
class ImportTariffTestCase(TestCaseWithTransport):
52
57
    """Check how many modules are loaded for some representative scenarios.
53
58
 
54
59
    See the Testing Guide in the developer documentation for more explanation.
 
60
 
 
61
 
 
62
    We must respect the setup used by the selftest command regarding
 
63
    plugins. This allows the user to control which plugins are in effect while
 
64
    running these tests and respect the import policies defined here.
 
65
 
 
66
    When failures are encountered for a given plugin, they can generally be
 
67
    addressed by using lazy import or lazy hook registration.
55
68
    """
56
69
 
57
70
    def setUp(self):
58
 
        # Preserve some env vars as we want to escape the isolation for them
59
71
        self.preserved_env_vars = {}
60
 
        for name in ('BZR_HOME', 'BZR_PLUGIN_PATH', 'BZR_DISABLE_PLUGINS',
61
 
                     'BZR_PLUGINS_AT', 'HOME'):
 
72
        for name in ('BZR_PLUGIN_PATH', 'BZR_DISABLE_PLUGINS', 'BZR_PLUGINS_AT'
 
73
                     ):
62
74
            self.preserved_env_vars[name] = os.environ.get(name)
63
 
        super(TestImportTariffs, self).setUp()
 
75
        super(ImportTariffTestCase, self).setUp()
64
76
 
65
 
    def start_bzr_subprocess_with_import_check(self, args):
 
77
    def start_bzr_subprocess_with_import_check(self, args, stderr_file=None):
66
78
        """Run a bzr process and capture the imports.
67
79
 
68
80
        This is fairly expensive because we start a subprocess, so we aim to
69
81
        cover representative rather than exhaustive cases.
70
82
        """
71
 
        # We use PYTHON_VERBOSE rather than --profile-importts because in
 
83
        # We use PYTHON_VERBOSE rather than --profile-imports because in
72
84
        # experimentation the profile-imports output seems to not always show
73
85
        # the modules you'd expect; this can be debugged but python -v seems
74
86
        # more likely to always show everything.  And we use the environment
75
87
        # variable rather than 'python -v' in the hope it will work even if
76
88
        # bzr is frozen and python is not explicitly specified. -- mbp 20100208
77
 
 
78
 
        # Normally we want test isolation from the real $HOME but here we
79
 
        # explicitly do want to test against things installed there, therefore
80
 
        # we pass it through.
81
89
        env_changes = dict(PYTHONVERBOSE='1', **self.preserved_env_vars)
82
 
        return self.start_bzr_subprocess(args, env_changes=env_changes,
83
 
            allow_plugins=(not are_plugins_disabled()))
 
90
        trace.mutter('Setting env for bzr subprocess: %r', env_changes)
 
91
        kwargs = dict(env_changes=env_changes,
 
92
                      allow_plugins=(not are_plugins_disabled()))
 
93
        if stderr_file:
 
94
            # We don't want to update the whole call chain so we insert stderr
 
95
            # *iff* we need to
 
96
            kwargs['stderr'] = stderr_file
 
97
        return self.start_bzr_subprocess(args, **kwargs)
84
98
 
85
99
    def check_forbidden_modules(self, err, forbidden_imports):
86
100
        """Check for forbidden modules in stderr.
127
141
        self.finish_bzr_subprocess_with_import_check(process, args,
128
142
            forbidden_imports)
129
143
 
 
144
 
 
145
class TestImportTariffs(ImportTariffTestCase):
 
146
    """Basic import tariff tests for some common bzr commands"""
 
147
 
130
148
    def test_import_tariffs_working(self):
131
149
        # check some guaranteed-true and false imports to be sure we're
132
150
        # measuring correctly
187
205
    def test_simple_serve(self):
188
206
        # 'serve' in a default format working tree shouldn't need many modules
189
207
        tree = self.make_branch_and_tree('.')
 
208
        # Capture the bzr serve process' stderr in a file to avoid deadlocks
 
209
        # while the smart client interacts with it.
 
210
        stderr_file = open('bzr-serve.stderr', 'w')
190
211
        process = self.start_bzr_subprocess_with_import_check(['serve',
191
 
            '--inet', '-d', tree.basedir])
 
212
            '--inet', '-d', tree.basedir], stderr_file=stderr_file)
192
213
        url = 'bzr://localhost/'
193
214
        self.permit_url(url)
194
215
        client_medium = medium.SmartSimplePipesClientMedium(
200
221
        process.stdin = None
201
222
        (out, err) = self.finish_bzr_subprocess(process,
202
223
            universal_newlines=False)
 
224
        stderr_file.close()
 
225
        with open('bzr-serve.stderr', 'r') as stderr_file:
 
226
            err = stderr_file.read()
203
227
        self.check_forbidden_modules(err,
204
228
            ['bzrlib.annotate',
205
229
            'bzrlib.atomicfile',