/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/tests/test_plugins.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005-2012, 2016 Canonical Ltd
2
 
# Copyright (C) 2017-2018 Breezy developers
3
2
#
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
17
16
 
18
17
"""Tests for plugins"""
19
18
 
20
 
import importlib
21
 
from io import StringIO
 
19
# XXX: There are no plugin tests at the moment because the plugin module
 
20
# affects the global state of the process.  See brzlib/plugins.py for more
 
21
# comments.
 
22
 
 
23
from cStringIO import StringIO
22
24
import logging
23
25
import os
24
26
import sys
25
 
import types
26
27
 
27
 
import breezy
28
 
from .. import (
 
28
import brzlib
 
29
from brzlib import (
 
30
    errors,
29
31
    osutils,
30
32
    plugin,
 
33
    plugins,
31
34
    tests,
 
35
    trace,
32
36
    )
33
 
from ..tests.features import pkg_resources_feature
34
37
 
35
38
 
36
39
# TODO: Write a test for plugin decoration of commands.
37
40
 
38
 
invalidate_caches = getattr(importlib, "invalidate_caches", lambda: None)
39
 
 
40
 
 
41
41
class BaseTestPlugins(tests.TestCaseInTempDir):
42
 
    """TestCase that isolates plugin imports and cleans up on completion."""
43
 
 
44
 
    def setUp(self):
45
 
        super(BaseTestPlugins, self).setUp()
46
 
        self.module_name = "breezy.testingplugins"
47
 
        self.module_prefix = self.module_name + "."
48
 
        self.module = types.ModuleType(self.module_name)
49
 
 
50
 
        self.overrideAttr(plugin, "_MODULE_PREFIX", self.module_prefix)
51
 
        self.overrideAttr(breezy, "testingplugins", self.module)
52
 
 
53
 
        sys.modules[self.module_name] = self.module
54
 
        self.addCleanup(self._unregister_all)
55
 
        self.addCleanup(self._unregister_finder)
56
 
 
57
 
        invalidate_caches()
58
 
 
59
 
    def reset(self):
60
 
        """Remove all global testing state and clean up module."""
61
 
        # GZ 2017-06-02: Ideally don't do this, write new test or generate
62
 
        # bytecode by other mechanism.
63
 
        self.log("resetting plugin testing context")
64
 
        self._unregister_all()
65
 
        self._unregister_finder()
66
 
        sys.modules[self.module_name] = self.module
67
 
        for name in list(self.module.__dict__):
68
 
            if name[:2] != '__':
69
 
                delattr(self.module, name)
70
 
        invalidate_caches()
71
 
        self.plugins = None
72
 
 
73
 
    def update_module_paths(self, paths):
74
 
        paths = plugin.extend_path(paths, self.module_name)
75
 
        self.module.__path__ = paths
76
 
        self.log("using %r", paths)
77
 
        return paths
78
 
 
79
 
    def load_with_paths(self, paths):
80
 
        self.log("loading plugins!")
81
 
        plugin.load_plugins(self.update_module_paths(paths), state=self)
82
42
 
83
43
    def create_plugin(self, name, source=None, dir='.', file_name=None):
84
44
        if source is None:
89
49
            file_name = name + '.py'
90
50
        # 'source' must not fail to load
91
51
        path = osutils.pathjoin(dir, file_name)
92
 
        with open(path, 'w') as f:
 
52
        f = open(path, 'w')
 
53
        self.addCleanup(os.unlink, path)
 
54
        try:
93
55
            f.write(source + '\n')
 
56
        finally:
 
57
            f.close()
94
58
 
95
59
    def create_plugin_package(self, name, dir=None, source=None):
96
60
        if dir is None:
101
65
dir_source = '%s'
102
66
''' % (name, dir)
103
67
        os.makedirs(dir)
 
68
        def cleanup():
 
69
            # Workaround lazy import random? madness
 
70
            osutils.rmtree(dir)
 
71
        self.addCleanup(cleanup)
104
72
        self.create_plugin(name, source, dir,
105
73
                           file_name='__init__.py')
106
74
 
107
 
    def promote_cache(self, directory):
108
 
        """Move bytecode files out of __pycache__ in given directory."""
109
 
        cache_dir = os.path.join(directory, '__pycache__')
110
 
        if os.path.isdir(cache_dir):
111
 
            for name in os.listdir(cache_dir):
112
 
                magicless_name = '.'.join(name.split('.')[0::name.count('.')])
113
 
                rel = osutils.relpath(self.test_dir, cache_dir)
114
 
                self.log("moving %s in %s to %s", name, rel, magicless_name)
115
 
                os.rename(os.path.join(cache_dir, name),
116
 
                          os.path.join(directory, magicless_name))
117
 
 
118
 
    def _unregister_finder(self):
119
 
        """Removes any test copies of _PluginsAtFinder from sys.meta_path."""
120
 
        idx = len(sys.meta_path)
121
 
        while idx:
122
 
            idx -= 1
123
 
            finder = sys.meta_path[idx]
124
 
            if getattr(finder, "prefix", "") == self.module_prefix:
125
 
                self.log("removed %r from sys.meta_path", finder)
126
 
                sys.meta_path.pop(idx)
127
 
 
128
 
    def _unregister_all(self):
129
 
        """Remove all plugins in the test namespace from sys.modules."""
130
 
        for name in list(sys.modules):
131
 
            if name.startswith(self.module_prefix) or name == self.module_name:
132
 
                self.log("removed %s from sys.modules", name)
133
 
                del sys.modules[name]
134
 
 
135
 
    def assertPluginModules(self, plugin_dict):
136
 
        self.assertEqual(
137
 
            dict((k[len(self.module_prefix):], sys.modules[k])
138
 
                 for k in sys.modules if k.startswith(self.module_prefix)),
139
 
            plugin_dict)
 
75
    def _unregister_plugin(self, name):
 
76
        """Remove the plugin from sys.modules and the brzlib namespace."""
 
77
        py_name = 'brzlib.plugins.%s' % name
 
78
        if py_name in sys.modules:
 
79
            del sys.modules[py_name]
 
80
        if getattr(brzlib.plugins, name, None) is not None:
 
81
            delattr(brzlib.plugins, name)
 
82
 
 
83
    def _unregister_plugin_submodule(self, plugin_name, submodule_name):
 
84
        """Remove the submodule from sys.modules and the brzlib namespace."""
 
85
        py_name = 'brzlib.plugins.%s.%s' % (plugin_name, submodule_name)
 
86
        if py_name in sys.modules:
 
87
            del sys.modules[py_name]
 
88
        plugin = getattr(brzlib.plugins, plugin_name, None)
 
89
        if plugin is not None:
 
90
            if getattr(plugin, submodule_name, None) is not None:
 
91
                delattr(plugin, submodule_name)
140
92
 
141
93
    def assertPluginUnknown(self, name):
142
 
        self.assertTrue(getattr(self.module, name, None) is None)
143
 
        self.assertFalse(self.module_prefix + name in sys.modules)
 
94
        self.assertFalse(getattr(brzlib.plugins, name, None) is not None)
 
95
        self.assertFalse('brzlib.plugins.%s' % name in sys.modules)
144
96
 
145
97
    def assertPluginKnown(self, name):
146
 
        self.assertTrue(getattr(self.module, name, None) is not None)
147
 
        self.assertTrue(self.module_prefix + name in sys.modules)
 
98
        self.assertTrue(getattr(brzlib.plugins, name, None) is not None)
 
99
        self.assertTrue('brzlib.plugins.%s' % name in sys.modules)
148
100
 
149
101
 
150
102
class TestLoadingPlugins(BaseTestPlugins):
161
113
        # set a place for the plugins to record their loading, and at the same
162
114
        # time validate that the location the plugins should record to is
163
115
        # valid and correct.
164
 
        self.__class__.activeattributes[tempattribute] = []
 
116
        self.__class__.activeattributes [tempattribute] = []
165
117
        self.assertTrue(tempattribute in self.activeattributes)
166
118
        # create two plugin directories
167
119
        os.mkdir('first')
168
120
        os.mkdir('second')
169
121
        # write a plugin that will record when its loaded in the
170
122
        # tempattribute list.
171
 
        template = ("from breezy.tests.test_plugins import TestLoadingPlugins\n"
 
123
        template = ("from brzlib.tests.test_plugins import TestLoadingPlugins\n"
172
124
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
173
125
 
174
 
        with open(os.path.join('first', 'plugin.py'), 'w') as outfile:
 
126
        outfile = open(os.path.join('first', 'plugin.py'), 'w')
 
127
        try:
175
128
            outfile.write(template % (tempattribute, 'first'))
176
129
            outfile.write('\n')
 
130
        finally:
 
131
            outfile.close()
177
132
 
178
 
        with open(os.path.join('second', 'plugin.py'), 'w') as outfile:
 
133
        outfile = open(os.path.join('second', 'plugin.py'), 'w')
 
134
        try:
179
135
            outfile.write(template % (tempattribute, 'second'))
180
136
            outfile.write('\n')
 
137
        finally:
 
138
            outfile.close()
181
139
 
182
140
        try:
183
 
            self.load_with_paths(['first', 'second'])
 
141
            brzlib.plugin.load_from_path(['first', 'second'])
184
142
            self.assertEqual(['first'], self.activeattributes[tempattribute])
185
143
        finally:
 
144
            # remove the plugin 'plugin'
186
145
            del self.activeattributes[tempattribute]
 
146
            self._unregister_plugin('plugin')
 
147
        self.assertPluginUnknown('plugin')
187
148
 
188
149
    def test_plugins_from_different_dirs_can_demand_load(self):
189
 
        self.assertFalse('breezy.plugins.pluginone' in sys.modules)
190
 
        self.assertFalse('breezy.plugins.plugintwo' in sys.modules)
 
150
        self.assertFalse('brzlib.plugins.pluginone' in sys.modules)
 
151
        self.assertFalse('brzlib.plugins.plugintwo' in sys.modules)
191
152
        # This test tests that having two plugins in different
192
153
        # directories with different names allows them both to be loaded, when
193
154
        # we do a direct import statement.
198
159
        # set a place for the plugins to record their loading, and at the same
199
160
        # time validate that the location the plugins should record to is
200
161
        # valid and correct.
201
 
        breezy.tests.test_plugins.TestLoadingPlugins.activeattributes[tempattribute] = [
202
 
            ]
 
162
        brzlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
 
163
            [tempattribute] = []
203
164
        self.assertTrue(tempattribute in self.activeattributes)
204
165
        # create two plugin directories
205
166
        os.mkdir('first')
206
167
        os.mkdir('second')
207
168
        # write plugins that will record when they are loaded in the
208
169
        # tempattribute list.
209
 
        template = ("from breezy.tests.test_plugins import TestLoadingPlugins\n"
 
170
        template = ("from brzlib.tests.test_plugins import TestLoadingPlugins\n"
210
171
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
211
172
 
212
 
        with open(os.path.join('first', 'pluginone.py'), 'w') as outfile:
 
173
        outfile = open(os.path.join('first', 'pluginone.py'), 'w')
 
174
        try:
213
175
            outfile.write(template % (tempattribute, 'first'))
214
176
            outfile.write('\n')
 
177
        finally:
 
178
            outfile.close()
215
179
 
216
 
        with open(os.path.join('second', 'plugintwo.py'), 'w') as outfile:
 
180
        outfile = open(os.path.join('second', 'plugintwo.py'), 'w')
 
181
        try:
217
182
            outfile.write(template % (tempattribute, 'second'))
218
183
            outfile.write('\n')
 
184
        finally:
 
185
            outfile.close()
219
186
 
 
187
        oldpath = brzlib.plugins.__path__
220
188
        try:
221
 
            self.assertPluginUnknown('pluginone')
222
 
            self.assertPluginUnknown('plugintwo')
223
 
            self.update_module_paths(['first', 'second'])
224
 
            exec("import %spluginone" % self.module_prefix)
 
189
            self.assertFalse('brzlib.plugins.pluginone' in sys.modules)
 
190
            self.assertFalse('brzlib.plugins.plugintwo' in sys.modules)
 
191
            brzlib.plugins.__path__ = ['first', 'second']
 
192
            exec "import brzlib.plugins.pluginone"
225
193
            self.assertEqual(['first'], self.activeattributes[tempattribute])
226
 
            exec("import %splugintwo" % self.module_prefix)
 
194
            exec "import brzlib.plugins.plugintwo"
227
195
            self.assertEqual(['first', 'second'],
228
 
                             self.activeattributes[tempattribute])
 
196
                self.activeattributes[tempattribute])
229
197
        finally:
 
198
            # remove the plugin 'plugin'
230
199
            del self.activeattributes[tempattribute]
 
200
            self._unregister_plugin('pluginone')
 
201
            self._unregister_plugin('plugintwo')
 
202
        self.assertPluginUnknown('pluginone')
 
203
        self.assertPluginUnknown('plugintwo')
231
204
 
232
205
    def test_plugins_can_load_from_directory_with_trailing_slash(self):
233
206
        # This test tests that a plugin can load from a directory when the
239
212
        # set a place for the plugin to record its loading, and at the same
240
213
        # time validate that the location the plugin should record to is
241
214
        # valid and correct.
242
 
        breezy.tests.test_plugins.TestLoadingPlugins.activeattributes[tempattribute] = [
243
 
            ]
 
215
        brzlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
 
216
            [tempattribute] = []
244
217
        self.assertTrue(tempattribute in self.activeattributes)
245
218
        # create a directory for the plugin
246
219
        os.mkdir('plugin_test')
247
220
        # write a plugin that will record when its loaded in the
248
221
        # tempattribute list.
249
 
        template = ("from breezy.tests.test_plugins import TestLoadingPlugins\n"
 
222
        template = ("from brzlib.tests.test_plugins import TestLoadingPlugins\n"
250
223
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
251
224
 
252
 
        with open(os.path.join('plugin_test', 'ts_plugin.py'), 'w') as outfile:
 
225
        outfile = open(os.path.join('plugin_test', 'ts_plugin.py'), 'w')
 
226
        try:
253
227
            outfile.write(template % (tempattribute, 'plugin'))
254
228
            outfile.write('\n')
 
229
        finally:
 
230
            outfile.close()
255
231
 
256
232
        try:
257
 
            self.load_with_paths(['plugin_test' + os.sep])
 
233
            brzlib.plugin.load_from_path(['plugin_test'+os.sep])
258
234
            self.assertEqual(['plugin'], self.activeattributes[tempattribute])
259
 
            self.assertPluginKnown('ts_plugin')
260
235
        finally:
261
236
            del self.activeattributes[tempattribute]
 
237
            self._unregister_plugin('ts_plugin')
 
238
        self.assertPluginUnknown('ts_plugin')
262
239
 
263
240
    def load_and_capture(self, name):
264
241
        """Load plugins from '.' capturing the output.
270
247
        stream = StringIO()
271
248
        try:
272
249
            handler = logging.StreamHandler(stream)
273
 
            log = logging.getLogger('brz')
 
250
            log = logging.getLogger('bzr')
274
251
            log.addHandler(handler)
275
252
            try:
276
 
                self.load_with_paths(['.'])
 
253
                try:
 
254
                    brzlib.plugin.load_from_path(['.'])
 
255
                finally:
 
256
                    if 'brzlib.plugins.%s' % name in sys.modules:
 
257
                        del sys.modules['brzlib.plugins.%s' % name]
 
258
                    if getattr(brzlib.plugins, name, None):
 
259
                        delattr(brzlib.plugins, name)
277
260
            finally:
278
261
                # Stop capturing output
279
262
                handler.flush()
285
268
 
286
269
    def test_plugin_with_bad_api_version_reports(self):
287
270
        """Try loading a plugin that requests an unsupported api.
288
 
 
 
271
        
289
272
        Observe that it records the problem but doesn't complain on stderr.
290
273
 
291
274
        See https://bugs.launchpad.net/bzr/+bug/704195
292
275
        """
 
276
        self.overrideAttr(plugin, 'plugin_warnings', {})
293
277
        name = 'wants100.py'
294
 
        with open(name, 'w') as f:
295
 
            f.write("import breezy\n"
296
 
                    "from breezy.errors import IncompatibleVersion\n"
297
 
                    "raise IncompatibleVersion(breezy, [(1, 0, 0)], (0, 0, 5))\n")
 
278
        f = file(name, 'w')
 
279
        try:
 
280
            f.write("import brzlib.api\n"
 
281
                "brzlib.api.require_any_api(brzlib, [(1, 0, 0)])\n")
 
282
        finally:
 
283
            f.close()
298
284
        log = self.load_and_capture(name)
299
285
        self.assertNotContainsRe(log,
300
 
                                 r"It supports breezy version")
301
 
        self.assertEqual({'wants100'}, self.plugin_warnings.keys())
 
286
            r"It requested API version")
 
287
        self.assertEqual(
 
288
            ['wants100'],
 
289
            plugin.plugin_warnings.keys())
302
290
        self.assertContainsRe(
303
 
            self.plugin_warnings['wants100'][0],
304
 
            r"It supports breezy version")
 
291
            plugin.plugin_warnings['wants100'][0],
 
292
            r"It requested API version")
305
293
 
306
294
    def test_plugin_with_bad_name_does_not_load(self):
307
295
        # The file name here invalid for a python module.
308
 
        name = 'brz-bad plugin-name..py'
309
 
        open(name, 'w').close()
 
296
        name = 'bzr-bad plugin-name..py'
 
297
        file(name, 'w').close()
310
298
        log = self.load_and_capture(name)
311
299
        self.assertContainsRe(log,
312
 
                              r"Unable to load 'brz-bad plugin-name\.' in '\.' as a plugin "
313
 
                              "because the file path isn't a valid module name; try renaming "
314
 
                              "it to 'bad_plugin_name_'\\.")
 
300
            r"Unable to load 'bzr-bad plugin-name\.' in '\.' as a plugin "
 
301
            "because the file path isn't a valid module name; try renaming "
 
302
            "it to 'bad_plugin_name_'\.")
315
303
 
316
304
 
317
305
class TestPlugins(BaseTestPlugins):
318
306
 
319
307
    def setup_plugin(self, source=""):
320
 
        # This test tests a new plugin appears in breezy.plugin.plugins().
 
308
        # This test tests a new plugin appears in brzlib.plugin.plugins().
321
309
        # check the plugin is not loaded already
322
310
        self.assertPluginUnknown('plugin')
323
311
        # write a plugin that _cannot_ fail to load.
324
 
        with open('plugin.py', 'w') as f:
325
 
            f.write(source + '\n')
326
 
        self.load_with_paths(['.'])
327
 
 
328
 
    def test_plugin_loaded(self):
329
 
        self.assertPluginUnknown('plugin')
330
 
        self.assertIs(None, breezy.plugin.get_loaded_plugin('plugin'))
331
 
        self.setup_plugin()
332
 
        p = breezy.plugin.get_loaded_plugin('plugin')
333
 
        self.assertIsInstance(p, breezy.plugin.PlugIn)
334
 
        self.assertIs(p.module, sys.modules[self.module_prefix + 'plugin'])
335
 
 
336
 
    def test_plugin_loaded_disabled(self):
337
 
        self.assertPluginUnknown('plugin')
338
 
        self.overrideEnv('BRZ_DISABLE_PLUGINS', 'plugin')
339
 
        self.setup_plugin()
340
 
        self.assertIs(None, breezy.plugin.get_loaded_plugin('plugin'))
 
312
        with file('plugin.py', 'w') as f: f.write(source + '\n')
 
313
        self.addCleanup(self.teardown_plugin)
 
314
        plugin.load_from_path(['.'])
 
315
 
 
316
    def teardown_plugin(self):
 
317
        self._unregister_plugin('plugin')
 
318
        self.assertPluginUnknown('plugin')
341
319
 
342
320
    def test_plugin_appears_in_plugins(self):
343
321
        self.setup_plugin()
344
322
        self.assertPluginKnown('plugin')
345
 
        p = self.plugins['plugin']
346
 
        self.assertIsInstance(p, breezy.plugin.PlugIn)
347
 
        self.assertIs(p.module, sys.modules[self.module_prefix + 'plugin'])
 
323
        p = plugin.plugins()['plugin']
 
324
        self.assertIsInstance(p, brzlib.plugin.PlugIn)
 
325
        self.assertEqual(p.module, plugins.plugin)
348
326
 
349
327
    def test_trivial_plugin_get_path(self):
350
328
        self.setup_plugin()
351
 
        p = self.plugins['plugin']
 
329
        p = plugin.plugins()['plugin']
352
330
        plugin_path = self.test_dir + '/plugin.py'
353
331
        self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
354
332
 
355
333
    def test_plugin_get_path_py_not_pyc(self):
356
334
        # first import creates plugin.pyc
357
335
        self.setup_plugin()
358
 
        self.promote_cache(self.test_dir)
359
 
        self.reset()
360
 
        self.load_with_paths(['.'])  # import plugin.pyc
 
336
        self.teardown_plugin()
 
337
        plugin.load_from_path(['.']) # import plugin.pyc
361
338
        p = plugin.plugins()['plugin']
362
339
        plugin_path = self.test_dir + '/plugin.py'
363
340
        self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
365
342
    def test_plugin_get_path_pyc_only(self):
366
343
        # first import creates plugin.pyc (or plugin.pyo depending on __debug__)
367
344
        self.setup_plugin()
 
345
        self.teardown_plugin()
368
346
        os.unlink(self.test_dir + '/plugin.py')
369
 
        self.promote_cache(self.test_dir)
370
 
        self.reset()
371
 
        self.load_with_paths(['.'])  # import plugin.pyc (or .pyo)
 
347
        plugin.load_from_path(['.']) # import plugin.pyc (or .pyo)
372
348
        p = plugin.plugins()['plugin']
373
 
        plugin_path = self.test_dir + '/plugin' + plugin.COMPILED_EXT
 
349
        if __debug__:
 
350
            plugin_path = self.test_dir + '/plugin.pyc'
 
351
        else:
 
352
            plugin_path = self.test_dir + '/plugin.pyo'
374
353
        self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
375
354
 
376
355
    def test_no_test_suite_gives_None_for_test_suite(self):
392
371
 
393
372
    def test_load_plugin_tests_gives_load_plugin_tests_result(self):
394
373
        source = """
395
 
def load_tests(loader, standard_tests, pattern):
 
374
def load_tests(standard_tests, module, loader):
396
375
    return 'foo'"""
397
376
        self.setup_plugin(source)
398
377
        loader = tests.TestUtil.TestLoader()
422
401
 
423
402
    def test_no_version_info___version__(self):
424
403
        self.setup_plugin()
425
 
        plugin = breezy.plugin.plugins()['plugin']
 
404
        plugin = brzlib.plugin.plugins()['plugin']
426
405
        self.assertEqual("unknown", plugin.__version__)
427
406
 
428
407
    def test_str__version__with_version_info(self):
429
408
        self.setup_plugin("version_info = '1.2.3'")
430
 
        plugin = breezy.plugin.plugins()['plugin']
 
409
        plugin = brzlib.plugin.plugins()['plugin']
431
410
        self.assertEqual("1.2.3", plugin.__version__)
432
411
 
433
412
    def test_noniterable__version__with_version_info(self):
434
413
        self.setup_plugin("version_info = (1)")
435
 
        plugin = breezy.plugin.plugins()['plugin']
 
414
        plugin = brzlib.plugin.plugins()['plugin']
436
415
        self.assertEqual("1", plugin.__version__)
437
416
 
438
417
    def test_1__version__with_version_info(self):
439
418
        self.setup_plugin("version_info = (1,)")
440
 
        plugin = breezy.plugin.plugins()['plugin']
 
419
        plugin = brzlib.plugin.plugins()['plugin']
441
420
        self.assertEqual("1", plugin.__version__)
442
421
 
443
422
    def test_1_2__version__with_version_info(self):
444
423
        self.setup_plugin("version_info = (1, 2)")
445
 
        plugin = breezy.plugin.plugins()['plugin']
 
424
        plugin = brzlib.plugin.plugins()['plugin']
446
425
        self.assertEqual("1.2", plugin.__version__)
447
426
 
448
427
    def test_1_2_3__version__with_version_info(self):
449
428
        self.setup_plugin("version_info = (1, 2, 3)")
450
 
        plugin = breezy.plugin.plugins()['plugin']
 
429
        plugin = brzlib.plugin.plugins()['plugin']
451
430
        self.assertEqual("1.2.3", plugin.__version__)
452
431
 
453
432
    def test_candidate__version__with_version_info(self):
454
433
        self.setup_plugin("version_info = (1, 2, 3, 'candidate', 1)")
455
 
        plugin = breezy.plugin.plugins()['plugin']
 
434
        plugin = brzlib.plugin.plugins()['plugin']
456
435
        self.assertEqual("1.2.3rc1", plugin.__version__)
457
436
 
458
437
    def test_dev__version__with_version_info(self):
459
438
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 0)")
460
 
        plugin = breezy.plugin.plugins()['plugin']
 
439
        plugin = brzlib.plugin.plugins()['plugin']
461
440
        self.assertEqual("1.2.3dev", plugin.__version__)
462
441
 
463
442
    def test_dev_fallback__version__with_version_info(self):
464
443
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
465
 
        plugin = breezy.plugin.plugins()['plugin']
 
444
        plugin = brzlib.plugin.plugins()['plugin']
466
445
        self.assertEqual("1.2.3dev4", plugin.__version__)
467
446
 
468
447
    def test_final__version__with_version_info(self):
469
448
        self.setup_plugin("version_info = (1, 2, 3, 'final', 0)")
470
 
        plugin = breezy.plugin.plugins()['plugin']
 
449
        plugin = brzlib.plugin.plugins()['plugin']
471
450
        self.assertEqual("1.2.3", plugin.__version__)
472
451
 
473
452
    def test_final_fallback__version__with_version_info(self):
474
453
        self.setup_plugin("version_info = (1, 2, 3, 'final', 2)")
475
 
        plugin = breezy.plugin.plugins()['plugin']
 
454
        plugin = brzlib.plugin.plugins()['plugin']
476
455
        self.assertEqual("1.2.3.2", plugin.__version__)
477
456
 
478
457
 
 
458
class TestPluginHelp(tests.TestCaseInTempDir):
 
459
 
 
460
    def split_help_commands(self):
 
461
        help = {}
 
462
        current = None
 
463
        out, err = self.run_bzr('--no-plugins help commands')
 
464
        for line in out.splitlines():
 
465
            if not line.startswith(' '):
 
466
                current = line.split()[0]
 
467
            help[current] = help.get(current, '') + line
 
468
 
 
469
        return help
 
470
 
 
471
    def test_plugin_help_builtins_unaffected(self):
 
472
        # Check we don't get false positives
 
473
        help_commands = self.split_help_commands()
 
474
        for cmd_name in brzlib.commands.builtin_command_names():
 
475
            if cmd_name in brzlib.commands.plugin_command_names():
 
476
                continue
 
477
            try:
 
478
                help = brzlib.commands.get_cmd_object(cmd_name).get_help_text()
 
479
            except NotImplementedError:
 
480
                # some commands have no help
 
481
                pass
 
482
            else:
 
483
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
 
484
 
 
485
            if cmd_name in help_commands.keys():
 
486
                # some commands are hidden
 
487
                help = help_commands[cmd_name]
 
488
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
 
489
 
 
490
    def test_plugin_help_shows_plugin(self):
 
491
        # Create a test plugin
 
492
        os.mkdir('plugin_test')
 
493
        f = open(osutils.pathjoin('plugin_test', 'myplug.py'), 'w')
 
494
        f.write("""\
 
495
from brzlib import commands
 
496
class cmd_myplug(commands.Command):
 
497
    __doc__ = '''Just a simple test plugin.'''
 
498
    aliases = ['mplg']
 
499
    def run(self):
 
500
        print 'Hello from my plugin'
 
501
 
 
502
"""
 
503
)
 
504
        f.close()
 
505
 
 
506
        try:
 
507
            # Check its help
 
508
            brzlib.plugin.load_from_path(['plugin_test'])
 
509
            brzlib.commands.register_command( brzlib.plugins.myplug.cmd_myplug)
 
510
            help = self.run_bzr('help myplug')[0]
 
511
            self.assertContainsRe(help, 'plugin "myplug"')
 
512
            help = self.split_help_commands()['myplug']
 
513
            self.assertContainsRe(help, '\[myplug\]')
 
514
        finally:
 
515
            # unregister command
 
516
            if 'myplug' in brzlib.commands.plugin_cmds:
 
517
                brzlib.commands.plugin_cmds.remove('myplug')
 
518
            # remove the plugin 'myplug'
 
519
            if getattr(brzlib.plugins, 'myplug', None):
 
520
                delattr(brzlib.plugins, 'myplug')
 
521
 
 
522
 
479
523
class TestHelpIndex(tests.TestCase):
480
524
    """Tests for the PluginsHelpIndex class."""
481
525
 
492
536
        index = plugin.PluginsHelpIndex()
493
537
        # make a new plugin here for this test, even if we're run with
494
538
        # --no-plugins
495
 
        self.assertFalse('breezy.plugins.demo_module' in sys.modules)
496
 
        demo_module = FakeModule('', 'breezy.plugins.demo_module')
497
 
        sys.modules['breezy.plugins.demo_module'] = demo_module
 
539
        self.assertFalse(sys.modules.has_key('brzlib.plugins.demo_module'))
 
540
        demo_module = FakeModule('', 'brzlib.plugins.demo_module')
 
541
        sys.modules['brzlib.plugins.demo_module'] = demo_module
498
542
        try:
499
543
            topics = index.get_topics('demo_module')
500
544
            self.assertEqual(1, len(topics))
501
545
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
502
546
            self.assertEqual(demo_module, topics[0].module)
503
547
        finally:
504
 
            del sys.modules['breezy.plugins.demo_module']
 
548
            del sys.modules['brzlib.plugins.demo_module']
505
549
 
506
550
    def test_get_topics_no_topic(self):
507
551
        """Searching for something that is not a plugin returns []."""
518
562
    def test_get_plugin_topic_with_prefix(self):
519
563
        """Searching for plugins/demo_module returns help."""
520
564
        index = plugin.PluginsHelpIndex()
521
 
        self.assertFalse('breezy.plugins.demo_module' in sys.modules)
522
 
        demo_module = FakeModule('', 'breezy.plugins.demo_module')
523
 
        sys.modules['breezy.plugins.demo_module'] = demo_module
 
565
        self.assertFalse(sys.modules.has_key('brzlib.plugins.demo_module'))
 
566
        demo_module = FakeModule('', 'brzlib.plugins.demo_module')
 
567
        sys.modules['brzlib.plugins.demo_module'] = demo_module
524
568
        try:
525
569
            topics = index.get_topics('plugins/demo_module')
526
570
            self.assertEqual(1, len(topics))
527
571
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
528
572
            self.assertEqual(demo_module, topics[0].module)
529
573
        finally:
530
 
            del sys.modules['breezy.plugins.demo_module']
 
574
            del sys.modules['brzlib.plugins.demo_module']
531
575
 
532
576
 
533
577
class FakeModule(object):
552
596
        mod = FakeModule(None, 'demo')
553
597
        topic = plugin.ModuleHelpTopic(mod)
554
598
        self.assertEqual("Plugin 'demo' has no docstring.\n",
555
 
                         topic.get_help_text())
 
599
            topic.get_help_text())
556
600
 
557
601
    def test_get_help_text_no_carriage_return(self):
558
602
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
559
603
        mod = FakeModule('one line of help', 'demo')
560
604
        topic = plugin.ModuleHelpTopic(mod)
561
605
        self.assertEqual("one line of help\n",
562
 
                         topic.get_help_text())
 
606
            topic.get_help_text())
563
607
 
564
608
    def test_get_help_text_carriage_return(self):
565
609
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
566
610
        mod = FakeModule('two lines of help\nand more\n', 'demo')
567
611
        topic = plugin.ModuleHelpTopic(mod)
568
612
        self.assertEqual("two lines of help\nand more\n",
569
 
                         topic.get_help_text())
 
613
            topic.get_help_text())
570
614
 
571
615
    def test_get_help_text_with_additional_see_also(self):
572
616
        mod = FakeModule('two lines of help\nand more', 'demo')
573
617
        topic = plugin.ModuleHelpTopic(mod)
574
 
        self.assertEqual(
575
 
            "two lines of help\nand more\n\n:See also: bar, foo\n",
576
 
            topic.get_help_text(['foo', 'bar']))
 
618
        self.assertEqual("two lines of help\nand more\n\n:See also: bar, foo\n",
 
619
                         topic.get_help_text(['foo', 'bar']))
577
620
 
578
621
    def test_get_help_topic(self):
579
622
        """The help topic for a plugin is its module name."""
580
 
        mod = FakeModule('two lines of help\nand more', 'breezy.plugins.demo')
 
623
        mod = FakeModule('two lines of help\nand more', 'brzlib.plugins.demo')
581
624
        topic = plugin.ModuleHelpTopic(mod)
582
625
        self.assertEqual('demo', topic.get_help_topic())
583
626
        mod = FakeModule('two lines of help\nand more',
584
 
                         'breezy.plugins.foo_bar')
 
627
                         'brzlib.plugins.foo_bar')
585
628
        topic = plugin.ModuleHelpTopic(mod)
586
629
        self.assertEqual('foo_bar', topic.get_help_topic())
587
630
 
588
631
 
 
632
class TestLoadFromPath(tests.TestCaseInTempDir):
 
633
 
 
634
    def setUp(self):
 
635
        super(TestLoadFromPath, self).setUp()
 
636
        # Change brzlib.plugin to think no plugins have been loaded yet.
 
637
        self.overrideAttr(brzlib.plugins, '__path__', [])
 
638
        self.overrideAttr(plugin, '_loaded', False)
 
639
 
 
640
        # Monkey-patch load_from_path to stop it from actually loading anything.
 
641
        self.overrideAttr(plugin, 'load_from_path', lambda dirs: None)
 
642
 
 
643
    def test_set_plugins_path_with_args(self):
 
644
        plugin.set_plugins_path(['a', 'b'])
 
645
        self.assertEqual(['a', 'b'], brzlib.plugins.__path__)
 
646
 
 
647
    def test_set_plugins_path_defaults(self):
 
648
        plugin.set_plugins_path()
 
649
        self.assertEqual(plugin.get_standard_plugins_path(),
 
650
                         brzlib.plugins.__path__)
 
651
 
 
652
    def test_get_standard_plugins_path(self):
 
653
        path = plugin.get_standard_plugins_path()
 
654
        for directory in path:
 
655
            self.assertNotContainsRe(directory, r'\\/$')
 
656
        try:
 
657
            from distutils.sysconfig import get_python_lib
 
658
        except ImportError:
 
659
            pass
 
660
        else:
 
661
            if sys.platform != 'win32':
 
662
                python_lib = get_python_lib()
 
663
                for directory in path:
 
664
                    if directory.startswith(python_lib):
 
665
                        break
 
666
                else:
 
667
                    self.fail('No path to global plugins')
 
668
 
 
669
    def test_get_standard_plugins_path_env(self):
 
670
        self.overrideEnv('BZR_PLUGIN_PATH', 'foo/')
 
671
        path = plugin.get_standard_plugins_path()
 
672
        for directory in path:
 
673
            self.assertNotContainsRe(directory, r'\\/$')
 
674
 
 
675
    def test_load_plugins(self):
 
676
        plugin.load_plugins(['.'])
 
677
        self.assertEqual(brzlib.plugins.__path__, ['.'])
 
678
        # subsequent loads are no-ops
 
679
        plugin.load_plugins(['foo'])
 
680
        self.assertEqual(brzlib.plugins.__path__, ['.'])
 
681
 
 
682
    def test_load_plugins_default(self):
 
683
        plugin.load_plugins()
 
684
        path = plugin.get_standard_plugins_path()
 
685
        self.assertEqual(path, brzlib.plugins.__path__)
 
686
 
 
687
 
589
688
class TestEnvPluginPath(tests.TestCase):
590
689
 
591
 
    user = "USER"
592
 
    core = "CORE"
593
 
    site = "SITE"
594
 
    entrypoints = "ENTRYPOINTS"
 
690
    def setUp(self):
 
691
        super(TestEnvPluginPath, self).setUp()
 
692
        self.overrideAttr(plugin, 'DEFAULT_PLUGIN_PATH', None)
 
693
 
 
694
        self.user = plugin.get_user_plugin_path()
 
695
        self.site = plugin.get_site_plugin_path()
 
696
        self.core = plugin.get_core_plugin_path()
 
697
 
 
698
    def _list2paths(self, *args):
 
699
        paths = []
 
700
        for p in args:
 
701
            plugin._append_new_path(paths, p)
 
702
        return paths
 
703
 
 
704
    def _set_path(self, *args):
 
705
        path = os.pathsep.join(self._list2paths(*args))
 
706
        self.overrideEnv('BZR_PLUGIN_PATH', path)
595
707
 
596
708
    def check_path(self, expected_dirs, setting_dirs):
597
 
        if setting_dirs is None:
598
 
            del os.environ['BRZ_PLUGIN_PATH']
599
 
        else:
600
 
            os.environ['BRZ_PLUGIN_PATH'] = os.pathsep.join(setting_dirs)
601
 
        actual = [(p if t == 'path' else t.upper())
602
 
                  for p, t in plugin._env_plugin_path()]
603
 
        self.assertEqual(expected_dirs, actual)
 
709
        if setting_dirs:
 
710
            self._set_path(*setting_dirs)
 
711
        actual = plugin.get_standard_plugins_path()
 
712
        self.assertEqual(self._list2paths(*expected_dirs), actual)
604
713
 
605
714
    def test_default(self):
606
 
        self.check_path([self.user, self.core, self.site], None)
 
715
        self.check_path([self.user, self.core, self.site],
 
716
                        None)
607
717
 
608
718
    def test_adhoc_policy(self):
609
719
        self.check_path([self.user, self.core, self.site],
617
727
        self.check_path([self.user, self.site, self.core],
618
728
                        ['+user', '+site', '+core'])
619
729
 
620
 
    def test_enable_entrypoints(self):
621
 
        self.check_path([self.user, self.core, self.site, self.entrypoints],
622
 
                        ['+user', '+core', '+site', '+entrypoints'])
623
 
 
624
730
    def test_disable_user(self):
625
731
        self.check_path([self.core, self.site], ['-user'])
626
732
 
662
768
                        ['mycore', '-core'])
663
769
 
664
770
    def test_my_plugin_only(self):
665
 
        self.check_path(
666
 
            ['myplugin'],
667
 
            ['myplugin', '-user', '-core', '-site', '-entrypoints'])
 
771
        self.check_path(['myplugin'], ['myplugin', '-user', '-core', '-site'])
668
772
 
669
773
    def test_my_plugin_first(self):
670
774
        self.check_path(['myplugin', self.core, self.site, self.user],
677
781
 
678
782
class TestDisablePlugin(BaseTestPlugins):
679
783
 
 
784
    def setUp(self):
 
785
        super(TestDisablePlugin, self).setUp()
 
786
        self.create_plugin_package('test_foo')
 
787
        # Make sure we don't pollute the plugins namespace
 
788
        self.overrideAttr(plugins, '__path__')
 
789
        # Be paranoid in case a test fail
 
790
        self.addCleanup(self._unregister_plugin, 'test_foo')
 
791
 
680
792
    def test_cannot_import(self):
681
 
        self.create_plugin_package('works')
682
 
        self.create_plugin_package('fails')
683
 
        self.overrideEnv('BRZ_DISABLE_PLUGINS', 'fails')
684
 
        self.update_module_paths(["."])
685
 
        import breezy.testingplugins.works as works
 
793
        self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
 
794
        plugin.set_plugins_path(['.'])
686
795
        try:
687
 
            import breezy.testingplugins.fails as fails
 
796
            import brzlib.plugins.test_foo
688
797
        except ImportError:
689
798
            pass
690
 
        else:
691
 
            self.fail("Loaded blocked plugin: " + repr(fails))
692
 
        self.assertPluginModules({'fails': None, 'works': works})
693
 
 
694
 
    def test_partial_imports(self):
695
 
        self.create_plugin('good')
696
 
        self.create_plugin('bad')
697
 
        self.create_plugin_package('ugly')
698
 
        self.overrideEnv('BRZ_DISABLE_PLUGINS', 'bad:ugly')
699
 
        self.load_with_paths(['.'])
700
 
        self.assertEqual({'good'}, self.plugins.keys())
701
 
        self.assertPluginModules({
702
 
            'good': self.plugins['good'].module,
703
 
            'bad': None,
704
 
            'ugly': None,
705
 
        })
706
 
        # Ensure there are no warnings about plugins not being imported as
707
 
        # the user has explictly requested they be disabled.
708
 
        self.assertNotContainsRe(self.get_log(), r"Unable to load plugin")
709
 
 
710
 
 
711
 
class TestEnvDisablePlugins(tests.TestCase):
712
 
 
713
 
    def _get_names(self, env_value):
714
 
        os.environ['BRZ_DISABLE_PLUGINS'] = env_value
715
 
        return plugin._env_disable_plugins()
716
 
 
717
 
    def test_unset(self):
718
 
        self.assertEqual([], plugin._env_disable_plugins())
719
 
 
720
 
    def test_empty(self):
721
 
        self.assertEqual([], self._get_names(''))
722
 
 
723
 
    def test_single(self):
724
 
        self.assertEqual(['single'], self._get_names('single'))
725
 
 
726
 
    def test_multi(self):
727
 
        expected = ['one', 'two']
728
 
        self.assertEqual(expected, self._get_names(os.pathsep.join(expected)))
729
 
 
730
 
    def test_mixed(self):
731
 
        value = os.pathsep.join(['valid', 'in-valid'])
732
 
        self.assertEqual(['valid'], self._get_names(value))
733
 
        self.assertContainsRe(
734
 
            self.get_log(),
735
 
            r"Invalid name 'in-valid' in BRZ_DISABLE_PLUGINS=" + repr(value))
736
 
 
737
 
 
738
 
class TestEnvPluginsAt(tests.TestCase):
739
 
 
740
 
    def _get_paths(self, env_value):
741
 
        os.environ['BRZ_PLUGINS_AT'] = env_value
742
 
        return plugin._env_plugins_at()
743
 
 
744
 
    def test_empty(self):
745
 
        self.assertEqual([], plugin._env_plugins_at())
 
799
        self.assertPluginUnknown('test_foo')
 
800
 
 
801
    def test_regular_load(self):
 
802
        self.overrideAttr(plugin, '_loaded', False)
 
803
        plugin.load_plugins(['.'])
 
804
        self.assertPluginKnown('test_foo')
 
805
        self.assertDocstring("This is the doc for test_foo",
 
806
                             brzlib.plugins.test_foo)
 
807
 
 
808
    def test_not_loaded(self):
 
809
        self.warnings = []
 
810
        def captured_warning(*args, **kwargs):
 
811
            self.warnings.append((args, kwargs))
 
812
        self.overrideAttr(trace, 'warning', captured_warning)
 
813
        # Reset the flag that protect against double loading
 
814
        self.overrideAttr(plugin, '_loaded', False)
 
815
        self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
 
816
        plugin.load_plugins(['.'])
 
817
        self.assertPluginUnknown('test_foo')
 
818
        # Make sure we don't warn about the plugin ImportError since this has
 
819
        # been *requested* by the user.
 
820
        self.assertLength(0, self.warnings)
 
821
 
 
822
 
 
823
 
 
824
class TestLoadPluginAtSyntax(tests.TestCase):
 
825
 
 
826
    def _get_paths(self, paths):
 
827
        return plugin._get_specific_plugin_paths(paths)
 
828
 
 
829
    def test_empty(self):
 
830
        self.assertEqual([], self._get_paths(None))
746
831
        self.assertEqual([], self._get_paths(''))
747
832
 
748
833
    def test_one_path(self):
749
834
        self.assertEqual([('b', 'man')], self._get_paths('b@man'))
750
835
 
751
 
    def test_multiple(self):
752
 
        self.assertEqual(
753
 
            [('tools', 'bzr-tools'), ('p', 'play.py')],
754
 
            self._get_paths(os.pathsep.join(('tools@bzr-tools', 'p@play.py'))))
755
 
 
756
 
    def test_many_at(self):
757
 
        self.assertEqual(
758
 
            [('church', 'StMichael@Plea@Norwich')],
759
 
            self._get_paths('church@StMichael@Plea@Norwich'))
760
 
 
761
 
    def test_only_py(self):
762
 
        self.assertEqual([('test', './test.py')], self._get_paths('./test.py'))
763
 
 
764
 
    def test_only_package(self):
765
 
        self.assertEqual([('py', '/opt/b/py')], self._get_paths('/opt/b/py'))
766
 
 
767
 
    def test_bad_name(self):
768
 
        self.assertEqual([], self._get_paths('/usr/local/bzr-git'))
769
 
        self.assertContainsRe(
770
 
            self.get_log(),
771
 
            r"Invalid name 'bzr-git' in BRZ_PLUGINS_AT='/usr/local/bzr-git'")
 
836
    def test_bogus_path(self):
 
837
        # We need a '@'
 
838
        self.assertRaises(errors.BzrCommandError, self._get_paths, 'batman')
 
839
        # Too much '@' isn't good either
 
840
        self.assertRaises(errors.BzrCommandError, self._get_paths,
 
841
                          'batman@mobile@cave')
 
842
        # An empty description probably indicates a problem
 
843
        self.assertRaises(errors.BzrCommandError, self._get_paths,
 
844
                          os.pathsep.join(['batman@cave', '', 'robin@mobile']))
772
845
 
773
846
 
774
847
class TestLoadPluginAt(BaseTestPlugins):
775
848
 
776
849
    def setUp(self):
777
850
        super(TestLoadPluginAt, self).setUp()
 
851
        # Make sure we don't pollute the plugins namespace
 
852
        self.overrideAttr(plugins, '__path__')
 
853
        # Reset the flag that protect against double loading
 
854
        self.overrideAttr(plugin, '_loaded', False)
778
855
        # Create the same plugin in two directories
779
856
        self.create_plugin_package('test_foo', dir='non-standard-dir')
780
857
        # The "normal" directory, we use 'standard' instead of 'plugins' to
781
858
        # avoid depending on the precise naming.
782
859
        self.create_plugin_package('test_foo', dir='standard/test_foo')
 
860
        # All the tests will load the 'test_foo' plugin from various locations
 
861
        self.addCleanup(self._unregister_plugin, 'test_foo')
 
862
        # Unfortunately there's global cached state for the specific
 
863
        # registered paths.
 
864
        self.addCleanup(plugin.PluginImporter.reset)
783
865
 
784
866
    def assertTestFooLoadedFrom(self, path):
785
867
        self.assertPluginKnown('test_foo')
786
868
        self.assertDocstring('This is the doc for test_foo',
787
 
                             self.module.test_foo)
788
 
        self.assertEqual(path, self.module.test_foo.dir_source)
 
869
                             brzlib.plugins.test_foo)
 
870
        self.assertEqual(path, brzlib.plugins.test_foo.dir_source)
789
871
 
790
872
    def test_regular_load(self):
791
 
        self.load_with_paths(['standard'])
 
873
        plugin.load_plugins(['standard'])
792
874
        self.assertTestFooLoadedFrom('standard/test_foo')
793
875
 
794
876
    def test_import(self):
795
 
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@non-standard-dir')
796
 
        self.update_module_paths(['standard'])
797
 
        import breezy.testingplugins.test_foo
 
877
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
878
        plugin.set_plugins_path(['standard'])
 
879
        try:
 
880
            import brzlib.plugins.test_foo
 
881
        except ImportError:
 
882
            pass
798
883
        self.assertTestFooLoadedFrom('non-standard-dir')
799
884
 
800
885
    def test_loading(self):
801
 
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@non-standard-dir')
802
 
        self.load_with_paths(['standard'])
803
 
        self.assertTestFooLoadedFrom('non-standard-dir')
804
 
 
805
 
    def test_loading_other_name(self):
806
 
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@non-standard-dir')
807
 
        os.rename('standard/test_foo', 'standard/test_bar')
808
 
        self.load_with_paths(['standard'])
 
886
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
887
        plugin.load_plugins(['standard'])
809
888
        self.assertTestFooLoadedFrom('non-standard-dir')
810
889
 
811
890
    def test_compiled_loaded(self):
812
 
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@non-standard-dir')
813
 
        self.load_with_paths(['standard'])
 
891
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
892
        plugin.load_plugins(['standard'])
814
893
        self.assertTestFooLoadedFrom('non-standard-dir')
815
894
        self.assertIsSameRealPath('non-standard-dir/__init__.py',
816
 
                                  self.module.test_foo.__file__)
 
895
                                  brzlib.plugins.test_foo.__file__)
817
896
 
818
897
        # Try importing again now that the source has been compiled
819
 
        os.remove('non-standard-dir/__init__.py')
820
 
        self.promote_cache('non-standard-dir')
821
 
        self.reset()
822
 
        self.load_with_paths(['standard'])
 
898
        self._unregister_plugin('test_foo')
 
899
        plugin._loaded = False
 
900
        plugin.load_plugins(['standard'])
823
901
        self.assertTestFooLoadedFrom('non-standard-dir')
824
 
        suffix = plugin.COMPILED_EXT
825
 
        self.assertIsSameRealPath('non-standard-dir/__init__' + suffix,
826
 
                                  self.module.test_foo.__file__)
 
902
        if __debug__:
 
903
            suffix = 'pyc'
 
904
        else:
 
905
            suffix = 'pyo'
 
906
        self.assertIsSameRealPath('non-standard-dir/__init__.%s' % suffix,
 
907
                                  brzlib.plugins.test_foo.__file__)
827
908
 
828
909
    def test_submodule_loading(self):
829
910
        # We create an additional directory under the one for test_foo
830
911
        self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
831
 
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@non-standard-dir')
832
 
        self.update_module_paths(['standard'])
833
 
        import breezy.testingplugins.test_foo  # noqa: F401
834
 
        self.assertEqual(self.module_prefix + 'test_foo',
835
 
                         self.module.test_foo.__package__)
836
 
        import breezy.testingplugins.test_foo.test_bar  # noqa: F401
 
912
        self.addCleanup(self._unregister_plugin_submodule,
 
913
                        'test_foo', 'test_bar')
 
914
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
915
        plugin.set_plugins_path(['standard'])
 
916
        import brzlib.plugins.test_foo
 
917
        self.assertEqual('brzlib.plugins.test_foo',
 
918
                         brzlib.plugins.test_foo.__package__)
 
919
        import brzlib.plugins.test_foo.test_bar
837
920
        self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
838
 
                                  self.module.test_foo.test_bar.__file__)
 
921
                                  brzlib.plugins.test_foo.test_bar.__file__)
839
922
 
840
923
    def test_relative_submodule_loading(self):
841
924
        self.create_plugin_package('test_foo', dir='another-dir', source='''
842
 
from . import test_bar
 
925
import test_bar
843
926
''')
844
927
        # We create an additional directory under the one for test_foo
845
928
        self.create_plugin_package('test_bar', dir='another-dir/test_bar')
846
 
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@another-dir')
847
 
        self.update_module_paths(['standard'])
848
 
        import breezy.testingplugins.test_foo  # noqa: F401
849
 
        self.assertEqual(self.module_prefix + 'test_foo',
850
 
                         self.module.test_foo.__package__)
 
929
        self.addCleanup(self._unregister_plugin_submodule,
 
930
                        'test_foo', 'test_bar')
 
931
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@another-dir')
 
932
        plugin.set_plugins_path(['standard'])
 
933
        import brzlib.plugins.test_foo
 
934
        self.assertEqual('brzlib.plugins.test_foo',
 
935
                         brzlib.plugins.test_foo.__package__)
851
936
        self.assertIsSameRealPath('another-dir/test_bar/__init__.py',
852
 
                                  self.module.test_foo.test_bar.__file__)
 
937
                                  brzlib.plugins.test_foo.test_bar.__file__)
853
938
 
854
939
    def test_loading_from___init__only(self):
855
940
        # We rename the existing __init__.py file to ensure that we don't load
857
942
        init = 'non-standard-dir/__init__.py'
858
943
        random = 'non-standard-dir/setup.py'
859
944
        os.rename(init, random)
860
 
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@non-standard-dir')
861
 
        self.load_with_paths(['standard'])
 
945
        self.addCleanup(os.rename, random, init)
 
946
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
947
        plugin.load_plugins(['standard'])
862
948
        self.assertPluginUnknown('test_foo')
863
949
 
864
950
    def test_loading_from_specific_file(self):
871
957
''' % ('test_foo', plugin_path)
872
958
        self.create_plugin('test_foo', source=source,
873
959
                           dir=plugin_dir, file_name=plugin_file_name)
874
 
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@%s' % plugin_path)
875
 
        self.load_with_paths(['standard'])
 
960
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
 
961
        plugin.load_plugins(['standard'])
876
962
        self.assertTestFooLoadedFrom(plugin_path)
877
963
 
878
964
 
881
967
    def test_describe_plugins(self):
882
968
        class DummyModule(object):
883
969
            __doc__ = 'Hi there'
884
 
 
885
970
        class DummyPlugin(object):
886
971
            __version__ = '0.1.0'
887
972
            module = DummyModule()
888
 
        self.plugin_warnings = {'bad': ['Failed to load (just testing)']}
889
 
        self.plugins = {'good': DummyPlugin()}
 
973
        def dummy_plugins():
 
974
            return { 'good': DummyPlugin() }
 
975
        self.overrideAttr(plugin, 'plugin_warnings',
 
976
            {'bad': ['Failed to load (just testing)']})
 
977
        self.overrideAttr(plugin, 'plugins', dummy_plugins)
890
978
        self.assertEqual("""\
891
979
bad (failed to load)
892
980
  ** Failed to load (just testing)
894
982
good 0.1.0
895
983
  Hi there
896
984
 
897
 
""", ''.join(plugin.describe_plugins(state=self)))
898
 
 
899
 
 
900
 
class DummyPlugin(object):
901
 
    """Plugin."""
902
 
 
903
 
 
904
 
class TestLoadEnvPlugin(BaseTestPlugins):
905
 
 
906
 
    _test_needs_features = [pkg_resources_feature]
907
 
 
908
 
    def setup_plugin(self, source=""):
909
 
        # This test tests a new plugin appears in breezy.plugin.plugins().
910
 
        # check the plugin is not loaded already
911
 
        self.assertPluginUnknown('plugin')
912
 
        # write a plugin that _cannot_ fail to load.
913
 
        import pkg_resources
914
 
        d = pkg_resources.Distribution(__file__)
915
 
        ep = pkg_resources.EntryPoint.parse(
916
 
            'plugin = ' + __name__ + ':DummyPlugin', dist=d)
917
 
        d._ep_map = {'breezy.plugin': {'plugin': ep}}
918
 
        pkg_resources.working_set.add(d, 'plugin', replace=True)
919
 
        eps = list(pkg_resources.iter_entry_points('breezy.plugin'))
920
 
        self.assertEqual(['plugin'], [ep.name for ep in eps])
921
 
        self.load_with_paths(['.'])
922
 
        self.addCleanup(d._ep_map.clear)
923
 
 
924
 
    def test_plugin_loaded(self):
925
 
        self.assertPluginUnknown('plugin')
926
 
        self.overrideEnv('BRZ_PLUGIN_PATH', '+entrypoints')
927
 
        self.setup_plugin()
928
 
        p = self.plugins['plugin']
929
 
        self.assertIsInstance(p, breezy.plugin.PlugIn)
930
 
        self.assertIs(p.module, sys.modules[self.module_prefix + 'plugin'])
931
 
 
932
 
    def test_plugin_loaded_disabled(self):
933
 
        self.assertPluginUnknown('plugin')
934
 
        self.overrideEnv('BRZ_PLUGIN_PATH', '+entrypoints')
935
 
        self.overrideEnv('BRZ_DISABLE_PLUGINS', 'plugin')
936
 
        self.setup_plugin()
937
 
        self.assertNotIn('plugin', self.plugins)
 
985
""", ''.join(plugin.describe_plugins()))