/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2010, 2011 Canonical Ltd
5017.2.2 by Martin Pool
Add import tariff tests
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
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
20
import os
5017.2.2 by Martin Pool
Add import tariff tests
21
from testtools import content
22
23
from bzrlib.plugin import (
24
    are_plugins_disabled,
25
    )
26
27
from bzrlib.tests import (
28
    TestCaseWithTransport,
29
    )
30
31
32
class TestImportTariffs(TestCaseWithTransport):
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
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
38
    def setUp(self):
39
        # Preserve some env vars as we want to escape the isolation for them
40
        self.preserved_env_vars = {}
41
        for name in ('BZR_HOME', 'BZR_PLUGIN_PATH', 'BZR_DISABLE_PLUGINS',
42
                     'BZR_PLUGINS_AT', 'HOME'):
43
            self.preserved_env_vars[name] = os.environ.get(name)
44
        super(TestImportTariffs, self).setUp()
45
5017.2.2 by Martin Pool
Add import tariff tests
46
    def run_command_check_imports(self, args, forbidden_imports):
5018.1.8 by Martin Pool
doc
47
        """Run bzr ARGS in a subprocess and check its imports.
48
49
        This is fairly expensive because we start a subprocess, so we aim to
50
        cover representative rather than exhaustive cases.
51
52
        :param forbidden_imports: List of fully-qualified Python module names
53
            that should not be loaded while running this command.
54
        """
5017.2.2 by Martin Pool
Add import tariff tests
55
        # We use PYTHON_VERBOSE rather than --profile-importts because in
56
        # experimentation the profile-imports output seems to not always show
57
        # the modules you'd expect; this can be debugged but python -v seems
58
        # more likely to always show everything.  And we use the environment
59
        # variable rather than 'python -v' in the hope it will work even if
60
        # bzr is frozen and python is not explicitly specified. -- mbp 20100208
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
61
5017.2.2 by Martin Pool
Add import tariff tests
62
        # Normally we want test isolation from the real $HOME but here we
63
        # explicitly do want to test against things installed there, therefore
64
        # we pass it through.
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
65
        env_changes = dict(PYTHONVERBOSE='1', **self.preserved_env_vars)
5017.2.2 by Martin Pool
Add import tariff tests
66
        out, err = self.run_bzr_subprocess(args,
67
            allow_plugins=(not are_plugins_disabled()),
68
            env_changes=env_changes)
69
5304.1.1 by Vincent Ladeuil
Pass BZR_PLUGINS_AT and BZR_DISABLE_PLINGS to the subprocess fpr test_import_tariff
70
        self.addDetail('subprocess_stderr',
5017.2.2 by Martin Pool
Add import tariff tests
71
            content.Content(content.ContentType("text", "plain"),
72
                lambda:[err]))
73
74
        bad_modules = []
75
        for module_name in forbidden_imports:
76
            if err.find("\nimport %s " % module_name) != -1:
77
                bad_modules.append(module_name)
78
79
        if bad_modules:
5304.1.1 by Vincent Ladeuil
Pass BZR_PLUGINS_AT and BZR_DISABLE_PLINGS to the subprocess fpr test_import_tariff
80
            self.fail("command %r loaded forbidden modules %r"
5017.2.2 by Martin Pool
Add import tariff tests
81
                % (args, bad_modules))
82
        return out, err
83
84
    def test_import_tariffs_working(self):
85
        # check some guaranteed-true and false imports to be sure we're
86
        # measuring correctly
87
        self.make_branch_and_tree('.')
88
        self.run_command_check_imports(['st'],
89
            ['nonexistentmodulename', 'anothernonexistentmodule'])
90
        self.assertRaises(AssertionError,
91
            self.run_command_check_imports,
92
            ['st'],
93
            ['bzrlib.tree'])
94
95
    def test_simple_local(self):
96
        # 'st' in a working tree shouldn't need many modules
97
        self.make_branch_and_tree('.')
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
98
        self.run_command_check_imports(['st'], [
5018.1.11 by Martin Pool
Check bundle commands are not loaded for 'bzr st' invocation
99
            'bzrlib.bundle.commands',
5127.1.1 by Martin Pool
version-info is lazily loaded
100
            'bzrlib.cmd_version_info',
5127.1.3 by Martin Pool
lazy-load dpush
101
            'bzrlib.foreign',
5279.1.1 by Andrew Bennetts
lazy_import most things in merge.py; add a few representative modules to the import tariff tests; tweak a couple of other modules so that patiencediff is not necessarily imported; remove a bunch of unused imports from test_knit.py.
102
            'bzrlib.merge3',
103
            'bzrlib.patiencediff',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
104
            'bzrlib.remote',
5127.1.4 by Martin Pool
Lazy-load sign-my-commits
105
            'bzrlib.sign_my_commits',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
106
            'bzrlib.smart',
5279.1.1 by Andrew Bennetts
lazy_import most things in merge.py; add a few representative modules to the import tariff tests; tweak a couple of other modules so that patiencediff is not necessarily imported; remove a bunch of unused imports from test_knit.py.
107
            'bzrlib.transform',
5271.1.1 by Martin Pool
Test that Kerberos is no longer loaded.
108
            'kerberos',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
109
            'smtplib',
110
            'tarfile',
111
            ])
5127.1.2 by Martin Pool
Lazy-load conflict commands
112
        # TODO: similar test for repository-only operations, checking we avoid
113
        # loading wt-specific stuff
114
        #
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
115
        # See https://bugs.launchpad.net/bzr/+bug/553017
5510.1.1 by Martin von Gagern
Ensure 'bzr help commands' doesn't import testtools.
116
117
    def test_help_commands(self):
118
        # See https://bugs.launchpad.net/bzr/+bug/663773
119
        self.run_command_check_imports(['help', 'commands'], [
120
            'testtools',
121
            ])