1
# Copyright (C) 2010 Canonical Ltd
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.
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.
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
18
"""Tests for how many modules are loaded in executing various commands."""
20
from testtools import content
22
from bzrlib.plugin import (
26
from bzrlib.tests import (
27
TestCaseWithTransport,
31
class TestImportTariffs(TestCaseWithTransport):
33
"""Check how many modules are loaded for some representative scenarios.
35
See the Testing Guide in the developer documentation for more explanation.
38
def run_command_check_imports(self, args, forbidden_imports):
39
"""Run bzr ARGS in a subprocess and check its imports.
41
This is fairly expensive because we start a subprocess, so we aim to
42
cover representative rather than exhaustive cases.
44
:param forbidden_imports: List of fully-qualified Python module names
45
that should not be loaded while running this command.
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
54
# Normally we want test isolation from the real $HOME but here we
55
# explicitly do want to test against things installed there, therefore
57
env_changes = dict(PYTHONVERBOSE='1')
58
for name in ['BZR_HOME', 'BZR_PLUGIN_PATH', 'HOME',]:
59
env_changes[name] = self._old_env.get(name)
60
out, err = self.run_bzr_subprocess(args,
61
allow_plugins=(not are_plugins_disabled()),
62
env_changes=env_changes)
64
self.addDetail('subprocess_stderr',
65
content.Content(content.ContentType("text", "plain"),
69
for module_name in forbidden_imports:
70
if err.find("\nimport %s " % module_name) != -1:
71
bad_modules.append(module_name)
74
self.fail("command %r loaded forbidden modules %r"
75
% (args, bad_modules))
78
def test_import_tariffs_working(self):
79
# check some guaranteed-true and false imports to be sure we're
81
self.make_branch_and_tree('.')
82
self.run_command_check_imports(['st'],
83
['nonexistentmodulename', 'anothernonexistentmodule'])
84
self.assertRaises(AssertionError,
85
self.run_command_check_imports,
89
def test_simple_local(self):
90
# 'st' in a working tree shouldn't need many modules
91
self.make_branch_and_tree('.')
92
self.run_command_check_imports(['st'], [
93
'bzrlib.bundle.commands',
94
'bzrlib.cmd_version_info',
97
'bzrlib.sign_my_commits',
102
# TODO: similar test for repository-only operations, checking we avoid
103
# loading wt-specific stuff
105
# See https://bugs.edge.launchpad.net/bzr/+bug/553017