~bzr/ubuntu/lucid/bzr/beta-ppa

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_import_tariff.py

  • Committer: Martin Pool
  • Date: 2010-08-18 04:26:39 UTC
  • mfrom: (129.1.8 packaging-karmic)
  • Revision ID: mbp@sourcefrog.net-20100818042639-mjoxtngyjwiu05fo
* PPA rebuild for lucid.
* PPA rebuild for karmic.
* PPA rebuild onto jaunty.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
 
 
18
"""Tests for how many modules are loaded in executing various commands."""
 
19
 
 
20
from testtools import content
 
21
 
 
22
from bzrlib.plugin import (
 
23
    are_plugins_disabled,
 
24
    )
 
25
 
 
26
from bzrlib.tests import (
 
27
    TestCaseWithTransport,
 
28
    )
 
29
 
 
30
 
 
31
class TestImportTariffs(TestCaseWithTransport):
 
32
 
 
33
    """Check how many modules are loaded for some representative scenarios.
 
34
 
 
35
    See the Testing Guide in the developer documentation for more explanation.
 
36
    """
 
37
 
 
38
    def run_command_check_imports(self, args, forbidden_imports):
 
39
        """Run bzr ARGS in a subprocess and check its imports.
 
40
 
 
41
        This is fairly expensive because we start a subprocess, so we aim to
 
42
        cover representative rather than exhaustive cases.
 
43
 
 
44
        :param forbidden_imports: List of fully-qualified Python module names
 
45
            that should not be loaded while running this command.
 
46
        """
 
47
        # We use PYTHON_VERBOSE rather than --profile-importts because in
 
48
        # experimentation the profile-imports output seems to not always show
 
49
        # the modules you'd expect; this can be debugged but python -v seems
 
50
        # more likely to always show everything.  And we use the environment
 
51
        # variable rather than 'python -v' in the hope it will work even if
 
52
        # bzr is frozen and python is not explicitly specified. -- mbp 20100208
 
53
        #
 
54
        # Normally we want test isolation from the real $HOME but here we
 
55
        # explicitly do want to test against things installed there, therefore
 
56
        # we pass it through.
 
57
        env_changes = dict(PYTHONVERBOSE='1')
 
58
        for name in ['BZR_HOME', 'BZR_PLUGIN_PATH',
 
59
                     'BZR_DISABLE_PLUGINS', 'BZR_PLUGINS_AT',
 
60
                     'HOME',]:
 
61
            env_changes[name] = self._old_env.get(name)
 
62
        out, err = self.run_bzr_subprocess(args,
 
63
            allow_plugins=(not are_plugins_disabled()),
 
64
            env_changes=env_changes)
 
65
 
 
66
        self.addDetail('subprocess_stderr',
 
67
            content.Content(content.ContentType("text", "plain"),
 
68
                lambda:[err]))
 
69
 
 
70
        bad_modules = []
 
71
        for module_name in forbidden_imports:
 
72
            if err.find("\nimport %s " % module_name) != -1:
 
73
                bad_modules.append(module_name)
 
74
 
 
75
        if bad_modules:
 
76
            self.fail("command %r loaded forbidden modules %r"
 
77
                % (args, bad_modules))
 
78
        return out, err
 
79
 
 
80
    def test_import_tariffs_working(self):
 
81
        # check some guaranteed-true and false imports to be sure we're
 
82
        # measuring correctly
 
83
        self.make_branch_and_tree('.')
 
84
        self.run_command_check_imports(['st'],
 
85
            ['nonexistentmodulename', 'anothernonexistentmodule'])
 
86
        self.assertRaises(AssertionError,
 
87
            self.run_command_check_imports,
 
88
            ['st'],
 
89
            ['bzrlib.tree'])
 
90
 
 
91
    def test_simple_local(self):
 
92
        # 'st' in a working tree shouldn't need many modules
 
93
        self.make_branch_and_tree('.')
 
94
        self.run_command_check_imports(['st'], [
 
95
            'bzrlib.bundle.commands',
 
96
            'bzrlib.cmd_version_info',
 
97
            'bzrlib.foreign',
 
98
            'bzrlib.merge3',
 
99
            'bzrlib.patiencediff',
 
100
            'bzrlib.remote',
 
101
            'bzrlib.sign_my_commits',
 
102
            'bzrlib.smart',
 
103
            'bzrlib.transform',
 
104
            'kerberos',
 
105
            'smtplib',
 
106
            'tarfile',
 
107
            ])
 
108
        # TODO: similar test for repository-only operations, checking we avoid
 
109
        # loading wt-specific stuff
 
110
        #
 
111
        # See https://bugs.launchpad.net/bzr/+bug/553017