~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Vincent Ladeuil
  • Date: 2007-06-06 13:52:02 UTC
  • mto: (2485.8.44 bzr.connection.sharing)
  • mto: This revision was merged to the branch mainline in revision 2646.
  • Revision ID: v.ladeuil+lp@free.fr-20070606135202-mqhxcv6z57uce434
Fix merge multiple connections. Test suite *not* passing (sftp
refactoring pending but unrelated to merge).

* bzrlib/builtins.py:
(cmd_merge.run): Fix the multiple connections bug by reusing the
tramsport used to check for a bundle and keep all other used
transports in possible_transports.
(_merge_helper): Add a possible_transports parameter for
reuse.

* bzrlib/transport/__init__.py:
(Transport._reuse_for): By default, Transports are not reusable.
(ConnectedTransport._reuse_for): ConnectedTransports are reusable
under certain conditions.
(_urlRE): Fix misleading group name.
(_try_transport_factories): Moved after get_transport (another use
case for moved lines). The do_catching_redirections was
incorrectly inserted between get_transport and
_try_transport_factories.

* bzrlib/tests/test_transport.py:
(TestReusedTransports.test_reuse_same_transport)
(TestReusedTransports.test_don_t_reuse_different_transport): Add
more tests.

* bzrlib/merge.py:
(_get_tree, Merger.set_other): Add a possible_transports parameter
for reuse.

* bzrlib/bzrdir.py:
(BzrDir.open_containing): Add a possible_transports parameter for
reuse.

* bzrlib/branch.py:
(Branch.open_containing): Add a possible_transports parameter for
reuse.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
# affects the global state of the process.  See bzrlib/plugins.py for more
21
21
# comments.
22
22
 
23
 
import logging
24
23
import os
25
24
from StringIO import StringIO
26
25
import sys
31
30
import bzrlib.plugins
32
31
import bzrlib.commands
33
32
import bzrlib.help
34
 
from bzrlib.symbol_versioning import one_three
35
 
from bzrlib.tests import (
36
 
    TestCase,
37
 
    TestCaseInTempDir,
38
 
    TestUtil,
39
 
    )
40
 
from bzrlib.osutils import pathjoin, abspath, normpath
 
33
from bzrlib.tests import TestCase, TestCaseInTempDir
 
34
from bzrlib.osutils import pathjoin, abspath
41
35
 
42
36
 
43
37
PLUGIN_TEXT = """\
75
69
        # tempattribute list.
76
70
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
77
71
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
78
 
 
79
 
        outfile = open(os.path.join('first', 'plugin.py'), 'w')
80
 
        try:
81
 
            outfile.write(template % (tempattribute, 'first'))
82
 
            outfile.write('\n')
83
 
        finally:
84
 
            outfile.close()
85
 
 
86
 
        outfile = open(os.path.join('second', 'plugin.py'), 'w')
87
 
        try:
88
 
            outfile.write(template % (tempattribute, 'second'))
89
 
            outfile.write('\n')
90
 
        finally:
91
 
            outfile.close()
92
 
 
 
72
        print >> file(os.path.join('first', 'plugin.py'), 'w'), template % (tempattribute, 'first')
 
73
        print >> file(os.path.join('second', 'plugin.py'), 'w'), template % (tempattribute, 'second')
93
74
        try:
94
75
            bzrlib.plugin.load_from_path(['first', 'second'])
95
76
            self.assertEqual(['first'], self.activeattributes[tempattribute])
96
77
        finally:
97
78
            # remove the plugin 'plugin'
98
79
            del self.activeattributes[tempattribute]
99
 
            if 'bzrlib.plugins.plugin' in sys.modules:
100
 
                del sys.modules['bzrlib.plugins.plugin']
101
80
            if getattr(bzrlib.plugins, 'plugin', None):
102
81
                del bzrlib.plugins.plugin
103
82
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
123
102
        # tempattribute list.
124
103
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
125
104
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
126
 
 
127
 
        outfile = open(os.path.join('first', 'pluginone.py'), 'w')
128
 
        try:
129
 
            outfile.write(template % (tempattribute, 'first'))
130
 
            outfile.write('\n')
131
 
        finally:
132
 
            outfile.close()
133
 
 
134
 
        outfile = open(os.path.join('second', 'plugintwo.py'), 'w')
135
 
        try:
136
 
            outfile.write(template % (tempattribute, 'second'))
137
 
            outfile.write('\n')
138
 
        finally:
139
 
            outfile.close()
140
 
 
 
105
        print >> file(os.path.join('first', 'pluginone.py'), 'w'), template % (tempattribute, 'first')
 
106
        print >> file(os.path.join('second', 'plugintwo.py'), 'w'), template % (tempattribute, 'second')
141
107
        oldpath = bzrlib.plugins.__path__
142
108
        try:
143
109
            bzrlib.plugins.__path__ = ['first', 'second']
149
115
        finally:
150
116
            # remove the plugin 'plugin'
151
117
            del self.activeattributes[tempattribute]
152
 
            if getattr(bzrlib.plugins, 'pluginone', None):
153
 
                del bzrlib.plugins.pluginone
154
 
            if getattr(bzrlib.plugins, 'plugintwo', None):
155
 
                del bzrlib.plugins.plugintwo
156
 
        self.failIf(getattr(bzrlib.plugins, 'pluginone', None))
157
 
        self.failIf(getattr(bzrlib.plugins, 'plugintwo', None))
158
 
 
159
 
    def test_plugins_can_load_from_directory_with_trailing_slash(self):
160
 
        # This test tests that a plugin can load from a directory when the
161
 
        # directory in the path has a trailing slash.
162
 
        # check the plugin is not loaded already
163
 
        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
164
 
        tempattribute = "trailing-slash"
165
 
        self.failIf(tempattribute in self.activeattributes)
166
 
        # set a place for the plugin to record its loading, and at the same
167
 
        # time validate that the location the plugin should record to is
168
 
        # valid and correct.
169
 
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
170
 
            [tempattribute] = []
171
 
        self.failUnless(tempattribute in self.activeattributes)
172
 
        # create a directory for the plugin
173
 
        os.mkdir('plugin_test')
174
 
        # write a plugin that will record when its loaded in the 
175
 
        # tempattribute list.
176
 
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
177
 
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
178
 
 
179
 
        outfile = open(os.path.join('plugin_test', 'ts_plugin.py'), 'w')
180
 
        try:
181
 
            outfile.write(template % (tempattribute, 'plugin'))
182
 
            outfile.write('\n')
183
 
        finally:
184
 
            outfile.close()
185
 
 
186
 
        try:
187
 
            bzrlib.plugin.load_from_path(['plugin_test'+os.sep])
188
 
            self.assertEqual(['plugin'], self.activeattributes[tempattribute])
189
 
        finally:
190
 
            # remove the plugin 'plugin'
191
 
            del self.activeattributes[tempattribute]
192
 
            if getattr(bzrlib.plugins, 'ts_plugin', None):
193
 
                del bzrlib.plugins.ts_plugin
194
 
        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
195
 
 
196
 
    def load_and_capture(self, name):
197
 
        """Load plugins from '.' capturing the output.
198
 
        
199
 
        :param name: The name of the plugin.
200
 
        :return: A string with the log from the plugin loading call.
201
 
        """
202
 
        # Capture output
203
 
        stream = StringIO()
204
 
        try:
205
 
            handler = logging.StreamHandler(stream)
206
 
            log = logging.getLogger('bzr')
207
 
            log.addHandler(handler)
208
 
            try:
209
 
                try:
210
 
                    bzrlib.plugin.load_from_path(['.'])
211
 
                finally:
212
 
                    if 'bzrlib.plugins.%s' % name in sys.modules:
213
 
                        del sys.modules['bzrlib.plugins.%s' % name]
214
 
                    if getattr(bzrlib.plugins, name, None):
215
 
                        delattr(bzrlib.plugins, name)
216
 
            finally:
217
 
                # Stop capturing output
218
 
                handler.flush()
219
 
                handler.close()
220
 
                log.removeHandler(handler)
221
 
            return stream.getvalue()
222
 
        finally:
223
 
            stream.close()
224
 
    
225
 
    def test_plugin_with_bad_api_version_reports(self):
226
 
        # This plugin asks for bzrlib api version 1.0.0, which is not supported
227
 
        # anymore.
228
 
        name = 'wants100.py'
229
 
        f = file(name, 'w')
230
 
        try:
231
 
            f.write("import bzrlib.api\n"
232
 
                "bzrlib.api.require_any_api(bzrlib, [(1, 0, 0)])\n")
233
 
        finally:
234
 
            f.close()
235
 
 
236
 
        log = self.load_and_capture(name)
237
 
        self.assertContainsRe(log,
238
 
            r"It requested API version")
239
 
 
240
 
    def test_plugin_with_bad_name_does_not_load(self):
241
 
        # The file name here invalid for a python module.
242
 
        name = 'bzr-bad plugin-name..py'
243
 
        file(name, 'w').close()
244
 
        log = self.load_and_capture(name)
245
 
        self.assertContainsRe(log,
246
 
            r"Unable to load 'bzr-bad plugin-name\.' in '\.' as a plugin "
247
 
            "because the file path isn't a valid module name; try renaming "
248
 
            "it to 'bad_plugin_name_'\.")
249
 
 
250
 
 
251
 
class TestPlugins(TestCaseInTempDir):
252
 
 
253
 
    def setup_plugin(self, source=""):
254
 
        # This test tests a new plugin appears in bzrlib.plugin.plugins().
 
118
            if getattr(bzrlib.plugins, 'plugin', None):
 
119
                del bzrlib.plugins.plugin
 
120
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
 
121
 
 
122
 
 
123
class TestAllPlugins(TestCaseInTempDir):
 
124
 
 
125
    def test_plugin_appears_in_all_plugins(self):
 
126
        # This test tests a new plugin appears in bzrlib.plugin.all_plugins().
255
127
        # check the plugin is not loaded already
256
128
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
257
129
        # write a plugin that _cannot_ fail to load.
258
 
        file('plugin.py', 'w').write(source + '\n')
259
 
        self.addCleanup(self.teardown_plugin)
260
 
        bzrlib.plugin.load_from_path(['.'])
261
 
    
262
 
    def teardown_plugin(self):
263
 
        # remove the plugin 'plugin'
264
 
        if 'bzrlib.plugins.plugin' in sys.modules:
265
 
            del sys.modules['bzrlib.plugins.plugin']
266
 
        if getattr(bzrlib.plugins, 'plugin', None):
267
 
            del bzrlib.plugins.plugin
 
130
        print >> file('plugin.py', 'w'), ""
 
131
        try:
 
132
            bzrlib.plugin.load_from_path(['.'])
 
133
            self.failUnless('plugin' in bzrlib.plugin.all_plugins())
 
134
            self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
 
135
            self.assertEqual(bzrlib.plugin.all_plugins()['plugin'],
 
136
                             bzrlib.plugins.plugin)
 
137
        finally:
 
138
            # remove the plugin 'plugin'
 
139
            if 'bzrlib.plugins.plugin' in sys.modules:
 
140
                del sys.modules['bzrlib.plugins.plugin']
 
141
            if getattr(bzrlib.plugins, 'plugin', None):
 
142
                del bzrlib.plugins.plugin
268
143
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
269
144
 
270
 
    def test_plugin_appears_in_plugins(self):
271
 
        self.setup_plugin()
272
 
        self.failUnless('plugin' in bzrlib.plugin.plugins())
273
 
        self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
274
 
        plugins = bzrlib.plugin.plugins()
275
 
        plugin = plugins['plugin']
276
 
        self.assertIsInstance(plugin, bzrlib.plugin.PlugIn)
277
 
        self.assertEqual(bzrlib.plugins.plugin, plugin.module)
278
 
 
279
 
    def test_trivial_plugin_get_path(self):
280
 
        self.setup_plugin()
281
 
        plugins = bzrlib.plugin.plugins()
282
 
        plugin = plugins['plugin']
283
 
        plugin_path = self.test_dir + '/plugin.py'
284
 
        self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
285
 
 
286
 
    def test_plugin_get_path_py_not_pyc(self):
287
 
        self.setup_plugin()         # after first import there will be plugin.pyc
288
 
        self.teardown_plugin()
289
 
        bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
290
 
        plugins = bzrlib.plugin.plugins()
291
 
        plugin = plugins['plugin']
292
 
        plugin_path = self.test_dir + '/plugin.py'
293
 
        self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
294
 
 
295
 
    def test_plugin_get_path_pyc_only(self):
296
 
        self.setup_plugin()         # after first import there will be plugin.pyc
297
 
        self.teardown_plugin()
298
 
        os.unlink(self.test_dir + '/plugin.py')
299
 
        bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
300
 
        plugins = bzrlib.plugin.plugins()
301
 
        plugin = plugins['plugin']
302
 
        if __debug__:
303
 
            plugin_path = self.test_dir + '/plugin.pyc'
304
 
        else:
305
 
            plugin_path = self.test_dir + '/plugin.pyo'
306
 
        self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
307
 
 
308
 
    def test_no_test_suite_gives_None_for_test_suite(self):
309
 
        self.setup_plugin()
310
 
        plugin = bzrlib.plugin.plugins()['plugin']
311
 
        self.assertEqual(None, plugin.test_suite())
312
 
 
313
 
    def test_test_suite_gives_test_suite_result(self):
314
 
        source = """def test_suite(): return 'foo'"""
315
 
        self.setup_plugin(source)
316
 
        plugin = bzrlib.plugin.plugins()['plugin']
317
 
        self.assertEqual('foo', plugin.test_suite())
318
 
 
319
 
    def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
320
 
        self.setup_plugin()
321
 
        loader = TestUtil.TestLoader()
322
 
        plugin = bzrlib.plugin.plugins()['plugin']
323
 
        self.assertEqual(None, plugin.load_plugin_tests(loader))
324
 
 
325
 
    def test_load_plugin_tests_gives_load_plugin_tests_result(self):
326
 
        source = """
327
 
def load_tests(standard_tests, module, loader):
328
 
    return 'foo'"""
329
 
        self.setup_plugin(source)
330
 
        loader = TestUtil.TestLoader()
331
 
        plugin = bzrlib.plugin.plugins()['plugin']
332
 
        self.assertEqual('foo', plugin.load_plugin_tests(loader))
333
 
 
334
 
    def test_no_version_info(self):
335
 
        self.setup_plugin()
336
 
        plugin = bzrlib.plugin.plugins()['plugin']
337
 
        self.assertEqual(None, plugin.version_info())
338
 
 
339
 
    def test_with_version_info(self):
340
 
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
341
 
        plugin = bzrlib.plugin.plugins()['plugin']
342
 
        self.assertEqual((1, 2, 3, 'dev', 4), plugin.version_info())
343
 
 
344
 
    def test_short_version_info_gets_padded(self):
345
 
        # the gtk plugin has version_info = (1,2,3) rather than the 5-tuple.
346
 
        # so we adapt it
347
 
        self.setup_plugin("version_info = (1, 2, 3)")
348
 
        plugin = bzrlib.plugin.plugins()['plugin']
349
 
        self.assertEqual((1, 2, 3, 'final', 0), plugin.version_info())
350
 
 
351
 
    def test_no_version_info___version__(self):
352
 
        self.setup_plugin()
353
 
        plugin = bzrlib.plugin.plugins()['plugin']
354
 
        self.assertEqual("unknown", plugin.__version__)
355
 
 
356
 
    def test_str__version__with_version_info(self):
357
 
        self.setup_plugin("version_info = '1.2.3'")
358
 
        plugin = bzrlib.plugin.plugins()['plugin']
359
 
        self.assertEqual("1.2.3", plugin.__version__)
360
 
 
361
 
    def test_noniterable__version__with_version_info(self):
362
 
        self.setup_plugin("version_info = (1)")
363
 
        plugin = bzrlib.plugin.plugins()['plugin']
364
 
        self.assertEqual("1", plugin.__version__)
365
 
 
366
 
    def test_1__version__with_version_info(self):
367
 
        self.setup_plugin("version_info = (1,)")
368
 
        plugin = bzrlib.plugin.plugins()['plugin']
369
 
        self.assertEqual("1", plugin.__version__)
370
 
 
371
 
    def test_1_2__version__with_version_info(self):
372
 
        self.setup_plugin("version_info = (1, 2)")
373
 
        plugin = bzrlib.plugin.plugins()['plugin']
374
 
        self.assertEqual("1.2", plugin.__version__)
375
 
 
376
 
    def test_1_2_3__version__with_version_info(self):
377
 
        self.setup_plugin("version_info = (1, 2, 3)")
378
 
        plugin = bzrlib.plugin.plugins()['plugin']
379
 
        self.assertEqual("1.2.3", plugin.__version__)
380
 
 
381
 
    def test_candidate__version__with_version_info(self):
382
 
        self.setup_plugin("version_info = (1, 2, 3, 'candidate', 1)")
383
 
        plugin = bzrlib.plugin.plugins()['plugin']
384
 
        self.assertEqual("1.2.3rc1", plugin.__version__)
385
 
 
386
 
    def test_dev__version__with_version_info(self):
387
 
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 0)")
388
 
        plugin = bzrlib.plugin.plugins()['plugin']
389
 
        self.assertEqual("1.2.3dev", plugin.__version__)
390
 
 
391
 
    def test_dev_fallback__version__with_version_info(self):
392
 
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
393
 
        plugin = bzrlib.plugin.plugins()['plugin']
394
 
        self.assertEqual("1.2.3.dev.4", plugin.__version__)
395
 
 
396
 
    def test_final__version__with_version_info(self):
397
 
        self.setup_plugin("version_info = (1, 2, 3, 'final', 0)")
398
 
        plugin = bzrlib.plugin.plugins()['plugin']
399
 
        self.assertEqual("1.2.3", plugin.__version__)
400
 
 
401
145
 
402
146
class TestPluginHelp(TestCaseInTempDir):
403
147
 
404
148
    def split_help_commands(self):
405
149
        help = {}
406
150
        current = None
407
 
        out, err = self.run_bzr('--no-plugins help commands')
408
 
        for line in out.splitlines():
 
151
        for line in self.capture('help commands').splitlines():
409
152
            if not line.startswith(' '):
410
153
                current = line.split()[0]
411
154
            help[current] = help.get(current, '') + line
424
167
                # some commands have no help
425
168
                pass
426
169
            else:
427
 
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
 
170
                self.assertNotContainsRe(help, 'From plugin "[^"]*"')
428
171
 
429
172
            if cmd_name in help_commands.keys():
430
173
                # some commands are hidden
431
174
                help = help_commands[cmd_name]
432
 
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
 
175
                self.assertNotContainsRe(help, 'From plugin "[^"]*"')
433
176
 
434
177
    def test_plugin_help_shows_plugin(self):
435
178
        # Create a test plugin
442
185
            # Check its help
443
186
            bzrlib.plugin.load_from_path(['plugin_test'])
444
187
            bzrlib.commands.register_command( bzrlib.plugins.myplug.cmd_myplug)
445
 
            help = self.run_bzr('help myplug')[0]
446
 
            self.assertContainsRe(help, 'plugin "myplug"')
 
188
            help = self.capture('help myplug')
 
189
            self.assertContainsRe(help, 'From plugin "myplug"')
447
190
            help = self.split_help_commands()['myplug']
448
191
            self.assertContainsRe(help, '\[myplug\]')
449
192
        finally:
450
193
            # unregister command
451
 
            if 'myplug' in bzrlib.commands.plugin_cmds:
452
 
                bzrlib.commands.plugin_cmds.remove('myplug')
 
194
            if bzrlib.commands.plugin_cmds.get('myplug', None):
 
195
                del bzrlib.commands.plugin_cmds['myplug']
453
196
            # remove the plugin 'myplug'
454
197
            if getattr(bzrlib.plugins, 'myplug', None):
455
198
                delattr(bzrlib.plugins, 'myplug')
465
208
    def check_plugin_load(self, zip_name, plugin_name):
466
209
        self.assertFalse(plugin_name in dir(bzrlib.plugins),
467
210
                         'Plugin already loaded')
468
 
        old_path = bzrlib.plugins.__path__
469
211
        try:
470
 
            # this is normally done by load_plugins -> set_plugins_path
471
 
            bzrlib.plugins.__path__ = [zip_name]
472
 
            self.applyDeprecated(one_three,
473
 
                bzrlib.plugin.load_from_zip, zip_name)
 
212
            bzrlib.plugin.load_from_zip(zip_name)
474
213
            self.assertTrue(plugin_name in dir(bzrlib.plugins),
475
214
                            'Plugin is not loaded')
476
215
        finally:
477
216
            # unregister plugin
478
217
            if getattr(bzrlib.plugins, plugin_name, None):
479
218
                delattr(bzrlib.plugins, plugin_name)
480
 
                del sys.modules['bzrlib.plugins.' + plugin_name]
481
 
            bzrlib.plugins.__path__ = old_path
482
219
 
483
220
    def test_load_module(self):
484
221
        self.make_zipped_plugin('./test.zip', 'ziplug.py')
501
238
        finally:
502
239
            bzrlib.plugins.__path__ = old_path
503
240
 
504
 
    def test_set_plugins_path_with_trailing_slashes(self):
505
 
        """set_plugins_path should set the module __path__ based on
506
 
        BZR_PLUGIN_PATH after removing all trailing slashes."""
507
 
        old_path = bzrlib.plugins.__path__
508
 
        old_env = os.environ.get('BZR_PLUGIN_PATH')
509
 
        try:
510
 
            bzrlib.plugins.__path__ = []
511
 
            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
512
 
                "second/\\/\\/"
513
 
            bzrlib.plugin.set_plugins_path()
514
 
            # We expect our nominated paths to have all path-seps removed,
515
 
            # and this is testing only that.
516
 
            expected_path = ['first', 'second']
517
 
            self.assertEqual(expected_path,
518
 
                bzrlib.plugins.__path__[:len(expected_path)])
519
 
        finally:
520
 
            bzrlib.plugins.__path__ = old_path
521
 
            if old_env is not None:
522
 
                os.environ['BZR_PLUGIN_PATH'] = old_env
523
 
            else:
524
 
                del os.environ['BZR_PLUGIN_PATH']
525
 
 
526
241
 
527
242
class TestHelpIndex(tests.TestCase):
528
243
    """Tests for the PluginsHelpIndex class."""
630
345
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
631
346
        topic = plugin.ModuleHelpTopic(mod)
632
347
        self.assertEqual('foo_bar', topic.get_help_topic())
633
 
 
634
 
 
635
 
def clear_plugins(test_case):
636
 
    # Save the attributes that we're about to monkey-patch.
637
 
    old_plugins_path = bzrlib.plugins.__path__
638
 
    old_loaded = plugin._loaded
639
 
    old_load_from_path = plugin.load_from_path
640
 
    # Change bzrlib.plugin to think no plugins have been loaded yet.
641
 
    bzrlib.plugins.__path__ = []
642
 
    plugin._loaded = False
643
 
    # Monkey-patch load_from_path to stop it from actually loading anything.
644
 
    def load_from_path(dirs):
645
 
        pass
646
 
    plugin.load_from_path = load_from_path
647
 
    def restore_plugins():
648
 
        bzrlib.plugins.__path__ = old_plugins_path
649
 
        plugin._loaded = old_loaded
650
 
        plugin.load_from_path = old_load_from_path
651
 
    test_case.addCleanup(restore_plugins)
652
 
 
653
 
 
654
 
class TestPluginPaths(tests.TestCase):
655
 
 
656
 
    def test_set_plugins_path_with_args(self):
657
 
        clear_plugins(self)
658
 
        plugin.set_plugins_path(['a', 'b'])
659
 
        self.assertEqual(['a', 'b'], bzrlib.plugins.__path__)
660
 
 
661
 
    def test_set_plugins_path_defaults(self):
662
 
        clear_plugins(self)
663
 
        plugin.set_plugins_path()
664
 
        self.assertEqual(plugin.get_standard_plugins_path(),
665
 
                         bzrlib.plugins.__path__)
666
 
 
667
 
    def test_get_standard_plugins_path(self):
668
 
        path = plugin.get_standard_plugins_path()
669
 
        self.assertEqual(plugin.get_default_plugin_path(), path[0])
670
 
        for directory in path:
671
 
            self.assertNotContainsRe(r'\\/$', directory)
672
 
        try:
673
 
            from distutils.sysconfig import get_python_lib
674
 
        except ImportError:
675
 
            pass
676
 
        else:
677
 
            if sys.platform != 'win32':
678
 
                python_lib = get_python_lib()
679
 
                for directory in path:
680
 
                    if directory.startswith(python_lib):
681
 
                        break
682
 
                else:
683
 
                    self.fail('No path to global plugins')
684
 
 
685
 
    def test_get_standard_plugins_path_env(self):
686
 
        os.environ['BZR_PLUGIN_PATH'] = 'foo/'
687
 
        self.assertEqual('foo', plugin.get_standard_plugins_path()[0])
688
 
 
689
 
 
690
 
class TestLoadPlugins(tests.TestCaseInTempDir):
691
 
 
692
 
    def test_load_plugins(self):
693
 
        clear_plugins(self)
694
 
        plugin.load_plugins(['.'])
695
 
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
696
 
        # subsequent loads are no-ops
697
 
        plugin.load_plugins(['foo'])
698
 
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
699
 
 
700
 
    def test_load_plugins_default(self):
701
 
        clear_plugins(self)
702
 
        plugin.load_plugins()
703
 
        path = plugin.get_standard_plugins_path()
704
 
        self.assertEqual(path, bzrlib.plugins.__path__)