/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):
5698.1.1 by Jelmer Vernooij
Add bzrlib.workingtree_2 to the list of forbidden modules for 'bzr st' in a 2a tree.
96
        # 'st' in a default format working tree shouldn't need many modules
5017.2.2 by Martin Pool
Add import tariff tests
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'], [
5757.8.2 by Jelmer Vernooij
Avoid annotate import during 'bzr st'.
99
            'bzrlib.annotate',
5745.2.2 by Jelmer Vernooij
Add atomicfile to test import tariff blacklist.
100
            'bzrlib.atomicfile',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
101
            'bzrlib.bugtracker',
5018.1.11 by Martin Pool
Check bundle commands are not loaded for 'bzr st' invocation
102
            'bzrlib.bundle.commands',
5127.1.1 by Martin Pool
version-info is lazily loaded
103
            'bzrlib.cmd_version_info',
5671.2.2 by Jelmer Vernooij
Remove unused serializer class.
104
            'bzrlib.externalcommand',
5745.3.2 by Jelmer Vernooij
Add filters to import tariff blacklist.
105
            'bzrlib.filters',
5749.1.1 by Jelmer Vernooij
Remove bzrlib.foreign from the test_import_tariff blacklist.
106
            # foreign branch plugins import the foreign_vcs_registry from 
107
            # bzrlib.foreign so it can't be blacklisted
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
108
            'bzrlib.gpg',
109
            'bzrlib.info',
5757.8.1 by Jelmer Vernooij
Avoid bzrlib.knit imports when using groupcompress repositories.
110
            'bzrlib.knit',
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.
111
            'bzrlib.merge3',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
112
            'bzrlib.merge_directive',
113
            'bzrlib.msgeditor',
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.
114
            'bzrlib.patiencediff',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
115
            'bzrlib.remote',
5757.8.3 by Jelmer Vernooij
Add knitrepo to blacklist.
116
            'bzrlib.repofmt.knitrepo',
5757.1.4 by Jelmer Vernooij
Add bzrlib.repofmt.knitpack_repo to the blacklist in test_import_tariff.
117
            'bzrlib.repofmt.knitpack_repo',
5745.3.3 by Jelmer Vernooij
Add rules to blacklist.
118
            'bzrlib.rules',
5127.1.4 by Martin Pool
Lazy-load sign-my-commits
119
            'bzrlib.sign_my_commits',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
120
            'bzrlib.smart',
5712.3.15 by Jelmer Vernooij
Remove unused register format functions.
121
            'bzrlib.smart.client',
5757.8.5 by Jelmer Vernooij
Add smart medium/server to the blacklist.
122
            'bzrlib.smart.medium',
123
            'bzrlib.smart.server',
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.
124
            'bzrlib.transform',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
125
            'bzrlib.version_info_formats.format_rio',
5582.10.91 by Jelmer Vernooij
Fix some tests.
126
            'bzrlib.plugins.weave_fmt.branch',
127
            'bzrlib.plugins.weave_fmt.bzrdir',
128
            'bzrlib.plugins.weave_fmt.repository',
129
            'bzrlib.plugins.weave_fmt.workingtree',
5718.2.1 by Jelmer Vernooij
Some more post-weave cleanups.
130
            'bzrlib.weave',
131
            'bzrlib.weavefile',
5671.2.2 by Jelmer Vernooij
Remove unused serializer class.
132
            'bzrlib.xml4',
133
            'bzrlib.xml5',
134
            'bzrlib.xml6',
135
            'bzrlib.xml7',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
136
            'getpass',
5271.1.1 by Martin Pool
Test that Kerberos is no longer loaded.
137
            'kerberos',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
138
            'smtplib',
139
            'tarfile',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
140
            'tempfile',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
141
            ])
5127.1.2 by Martin Pool
Lazy-load conflict commands
142
        # TODO: similar test for repository-only operations, checking we avoid
143
        # loading wt-specific stuff
144
        #
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
145
        # See https://bugs.launchpad.net/bzr/+bug/553017
5510.1.1 by Martin von Gagern
Ensure 'bzr help commands' doesn't import testtools.
146
147
    def test_help_commands(self):
148
        # See https://bugs.launchpad.net/bzr/+bug/663773
149
        self.run_command_check_imports(['help', 'commands'], [
150
            'testtools',
151
            ])