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
# We use PYTHON_VERBOSE rather than --profile-importts because in
40
# experimentation the profile-imports output seems to not always show
41
# the modules you'd expect; this can be debugged but python -v seems
42
# more likely to always show everything. And we use the environment
43
# variable rather than 'python -v' in the hope it will work even if
44
# bzr is frozen and python is not explicitly specified. -- mbp 20100208
46
# Normally we want test isolation from the real $HOME but here we
47
# explicitly do want to test against things installed there, therefore
49
env_changes = dict(PYTHONVERBOSE='1')
50
for name in ['BZR_HOME', 'BZR_PLUGIN_PATH', 'HOME',]:
51
env_changes[name] = self._old_env.get(name)
52
out, err = self.run_bzr_subprocess(args,
53
allow_plugins=(not are_plugins_disabled()),
54
env_changes=env_changes)
56
self.addDetail('subprocess_stderr',
57
content.Content(content.ContentType("text", "plain"),
61
for module_name in forbidden_imports:
62
if err.find("\nimport %s " % module_name) != -1:
63
bad_modules.append(module_name)
66
self.fail("command %r loaded forbidden modules %r"
67
% (args, bad_modules))
70
def test_import_tariffs_working(self):
71
# check some guaranteed-true and false imports to be sure we're
73
self.make_branch_and_tree('.')
74
self.run_command_check_imports(['st'],
75
['nonexistentmodulename', 'anothernonexistentmodule'])
76
self.assertRaises(AssertionError,
77
self.run_command_check_imports,
81
def test_simple_local(self):
82
# 'st' in a working tree shouldn't need many modules
83
self.make_branch_and_tree('.')
84
self.run_command_check_imports(['st'], [