1
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
17
"""Tests for plugins"""
22
# **************************************************
24
# **************************************************
33
from bzrlib.selftest import TestCaseInTempDir
36
class PluginTest(TestCaseInTempDir):
37
"""Create an external plugin and test loading."""
38
def test_plugin_loading(self):
41
orig_help = self.backtick('bzr help commands') # No plugins yet
42
os.mkdir('plugin_test')
43
f = open(os.path.join('plugin_test', 'myplug.py'), 'wt')
19
# XXX: There are no plugin tests at the moment because the plugin module
20
# affects the global state of the process. See bzrlib/plugins.py for more
25
from StringIO import StringIO
29
from bzrlib import plugin, tests
32
import bzrlib.commands
34
from bzrlib.tests import (
39
from bzrlib.osutils import pathjoin, abspath, normpath
43
import bzrlib.commands
44
class cmd_myplug(bzrlib.commands.Command):
45
'''Just a simple test plugin.'''
48
print 'Hello from my plugin'
51
# TODO: Write a test for plugin decoration of commands.
53
class TestLoadingPlugins(TestCaseInTempDir):
57
def test_plugins_with_the_same_name_are_not_loaded(self):
58
# This test tests that having two plugins in different directories does
59
# not result in both being loaded when they have the same name. get a
60
# file name we can use which is also a valid attribute for accessing in
61
# activeattributes. - we cannot give import parameters.
63
self.failIf(tempattribute in self.activeattributes)
64
# set a place for the plugins to record their loading, and at the same
65
# time validate that the location the plugins should record to is
67
bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
69
self.failUnless(tempattribute in self.activeattributes)
70
# create two plugin directories
73
# write a plugin that will record when its loaded in the
75
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
76
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
78
outfile = open(os.path.join('first', 'plugin.py'), 'w')
80
outfile.write(template % (tempattribute, 'first'))
85
outfile = open(os.path.join('second', 'plugin.py'), 'w')
87
outfile.write(template % (tempattribute, 'second'))
93
bzrlib.plugin.load_from_path(['first', 'second'])
94
self.assertEqual(['first'], self.activeattributes[tempattribute])
96
# remove the plugin 'plugin'
97
del self.activeattributes[tempattribute]
98
if 'bzrlib.plugins.plugin' in sys.modules:
99
del sys.modules['bzrlib.plugins.plugin']
100
if getattr(bzrlib.plugins, 'plugin', None):
101
del bzrlib.plugins.plugin
102
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
104
def test_plugins_from_different_dirs_can_demand_load(self):
105
# This test tests that having two plugins in different
106
# directories with different names allows them both to be loaded, when
107
# we do a direct import statement.
108
# Determine a file name we can use which is also a valid attribute
109
# for accessing in activeattributes. - we cannot give import parameters.
110
tempattribute = "different-dirs"
111
self.failIf(tempattribute in self.activeattributes)
112
# set a place for the plugins to record their loading, and at the same
113
# time validate that the location the plugins should record to is
115
bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
117
self.failUnless(tempattribute in self.activeattributes)
118
# create two plugin directories
121
# write plugins that will record when they are loaded in the
122
# tempattribute list.
123
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
124
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
126
outfile = open(os.path.join('first', 'pluginone.py'), 'w')
128
outfile.write(template % (tempattribute, 'first'))
133
outfile = open(os.path.join('second', 'plugintwo.py'), 'w')
135
outfile.write(template % (tempattribute, 'second'))
140
oldpath = bzrlib.plugins.__path__
142
bzrlib.plugins.__path__ = ['first', 'second']
143
exec "import bzrlib.plugins.pluginone"
144
self.assertEqual(['first'], self.activeattributes[tempattribute])
145
exec "import bzrlib.plugins.plugintwo"
146
self.assertEqual(['first', 'second'],
147
self.activeattributes[tempattribute])
149
# remove the plugin 'plugin'
150
del self.activeattributes[tempattribute]
151
if getattr(bzrlib.plugins, 'pluginone', None):
152
del bzrlib.plugins.pluginone
153
if getattr(bzrlib.plugins, 'plugintwo', None):
154
del bzrlib.plugins.plugintwo
155
self.failIf(getattr(bzrlib.plugins, 'pluginone', None))
156
self.failIf(getattr(bzrlib.plugins, 'plugintwo', None))
158
def test_plugins_can_load_from_directory_with_trailing_slash(self):
159
# This test tests that a plugin can load from a directory when the
160
# directory in the path has a trailing slash.
161
# check the plugin is not loaded already
162
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
163
tempattribute = "trailing-slash"
164
self.failIf(tempattribute in self.activeattributes)
165
# set a place for the plugin to record its loading, and at the same
166
# time validate that the location the plugin should record to is
168
bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
170
self.failUnless(tempattribute in self.activeattributes)
171
# create a directory for the plugin
172
os.mkdir('plugin_test')
173
# write a plugin that will record when its loaded in the
174
# tempattribute list.
175
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
176
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
178
outfile = open(os.path.join('plugin_test', 'ts_plugin.py'), 'w')
180
outfile.write(template % (tempattribute, 'plugin'))
186
bzrlib.plugin.load_from_path(['plugin_test'+os.sep])
187
self.assertEqual(['plugin'], self.activeattributes[tempattribute])
189
# remove the plugin 'plugin'
190
del self.activeattributes[tempattribute]
191
if getattr(bzrlib.plugins, 'ts_plugin', None):
192
del bzrlib.plugins.ts_plugin
193
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
195
def load_and_capture(self, name):
196
"""Load plugins from '.' capturing the output.
198
:param name: The name of the plugin.
199
:return: A string with the log from the plugin loading call.
204
handler = logging.StreamHandler(stream)
205
log = logging.getLogger('bzr')
206
log.addHandler(handler)
209
bzrlib.plugin.load_from_path(['.'])
211
if 'bzrlib.plugins.%s' % name in sys.modules:
212
del sys.modules['bzrlib.plugins.%s' % name]
213
if getattr(bzrlib.plugins, name, None):
214
delattr(bzrlib.plugins, name)
216
# Stop capturing output
219
log.removeHandler(handler)
220
return stream.getvalue()
224
def test_plugin_with_bad_api_version_reports(self):
225
# This plugin asks for bzrlib api version 1.0.0, which is not supported
230
f.write("import bzrlib.api\n"
231
"bzrlib.api.require_any_api(bzrlib, [(1, 0, 0)])\n")
235
log = self.load_and_capture(name)
236
self.assertContainsRe(log,
237
r"It requested API version")
239
def test_plugin_with_bad_name_does_not_load(self):
240
# The file name here invalid for a python module.
241
name = 'bzr-bad plugin-name..py'
242
file(name, 'w').close()
243
log = self.load_and_capture(name)
244
self.assertContainsRe(log,
245
r"Unable to load 'bzr-bad plugin-name\.' in '\.' as a plugin "
246
"because the file path isn't a valid module name; try renaming "
247
"it to 'bad_plugin_name_'\.")
250
class TestPlugins(TestCaseInTempDir):
252
def setup_plugin(self, source=""):
253
# This test tests a new plugin appears in bzrlib.plugin.plugins().
254
# check the plugin is not loaded already
255
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
256
# write a plugin that _cannot_ fail to load.
257
file('plugin.py', 'w').write(source + '\n')
258
self.addCleanup(self.teardown_plugin)
259
bzrlib.plugin.load_from_path(['.'])
261
def teardown_plugin(self):
262
# remove the plugin 'plugin'
263
if 'bzrlib.plugins.plugin' in sys.modules:
264
del sys.modules['bzrlib.plugins.plugin']
265
if getattr(bzrlib.plugins, 'plugin', None):
266
del bzrlib.plugins.plugin
267
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
269
def test_plugin_appears_in_plugins(self):
271
self.failUnless('plugin' in bzrlib.plugin.plugins())
272
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
273
plugins = bzrlib.plugin.plugins()
274
plugin = plugins['plugin']
275
self.assertIsInstance(plugin, bzrlib.plugin.PlugIn)
276
self.assertEqual(bzrlib.plugins.plugin, plugin.module)
278
def test_trivial_plugin_get_path(self):
280
plugins = bzrlib.plugin.plugins()
281
plugin = plugins['plugin']
282
plugin_path = self.test_dir + '/plugin.py'
283
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
285
def test_plugin_get_path_py_not_pyc(self):
286
self.setup_plugin() # after first import there will be plugin.pyc
287
self.teardown_plugin()
288
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
289
plugins = bzrlib.plugin.plugins()
290
plugin = plugins['plugin']
291
plugin_path = self.test_dir + '/plugin.py'
292
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
294
def test_plugin_get_path_pyc_only(self):
295
self.setup_plugin() # after first import there will be plugin.pyc
296
self.teardown_plugin()
297
os.unlink(self.test_dir + '/plugin.py')
298
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
299
plugins = bzrlib.plugin.plugins()
300
plugin = plugins['plugin']
302
plugin_path = self.test_dir + '/plugin.pyc'
304
plugin_path = self.test_dir + '/plugin.pyo'
305
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
307
def test_no_test_suite_gives_None_for_test_suite(self):
309
plugin = bzrlib.plugin.plugins()['plugin']
310
self.assertEqual(None, plugin.test_suite())
312
def test_test_suite_gives_test_suite_result(self):
313
source = """def test_suite(): return 'foo'"""
314
self.setup_plugin(source)
315
plugin = bzrlib.plugin.plugins()['plugin']
316
self.assertEqual('foo', plugin.test_suite())
318
def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
320
loader = TestUtil.TestLoader()
321
plugin = bzrlib.plugin.plugins()['plugin']
322
self.assertEqual(None, plugin.load_plugin_tests(loader))
324
def test_load_plugin_tests_gives_load_plugin_tests_result(self):
326
def load_tests(standard_tests, module, loader):
328
self.setup_plugin(source)
329
loader = TestUtil.TestLoader()
330
plugin = bzrlib.plugin.plugins()['plugin']
331
self.assertEqual('foo', plugin.load_plugin_tests(loader))
333
def test_no_version_info(self):
335
plugin = bzrlib.plugin.plugins()['plugin']
336
self.assertEqual(None, plugin.version_info())
338
def test_with_version_info(self):
339
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
340
plugin = bzrlib.plugin.plugins()['plugin']
341
self.assertEqual((1, 2, 3, 'dev', 4), plugin.version_info())
343
def test_short_version_info_gets_padded(self):
344
# the gtk plugin has version_info = (1,2,3) rather than the 5-tuple.
346
self.setup_plugin("version_info = (1, 2, 3)")
347
plugin = bzrlib.plugin.plugins()['plugin']
348
self.assertEqual((1, 2, 3, 'final', 0), plugin.version_info())
350
def test_no_version_info___version__(self):
352
plugin = bzrlib.plugin.plugins()['plugin']
353
self.assertEqual("unknown", plugin.__version__)
355
def test_str__version__with_version_info(self):
356
self.setup_plugin("version_info = '1.2.3'")
357
plugin = bzrlib.plugin.plugins()['plugin']
358
self.assertEqual("1.2.3", plugin.__version__)
360
def test_noniterable__version__with_version_info(self):
361
self.setup_plugin("version_info = (1)")
362
plugin = bzrlib.plugin.plugins()['plugin']
363
self.assertEqual("1", plugin.__version__)
365
def test_1__version__with_version_info(self):
366
self.setup_plugin("version_info = (1,)")
367
plugin = bzrlib.plugin.plugins()['plugin']
368
self.assertEqual("1", plugin.__version__)
370
def test_1_2__version__with_version_info(self):
371
self.setup_plugin("version_info = (1, 2)")
372
plugin = bzrlib.plugin.plugins()['plugin']
373
self.assertEqual("1.2", plugin.__version__)
375
def test_1_2_3__version__with_version_info(self):
376
self.setup_plugin("version_info = (1, 2, 3)")
377
plugin = bzrlib.plugin.plugins()['plugin']
378
self.assertEqual("1.2.3", plugin.__version__)
380
def test_candidate__version__with_version_info(self):
381
self.setup_plugin("version_info = (1, 2, 3, 'candidate', 1)")
382
plugin = bzrlib.plugin.plugins()['plugin']
383
self.assertEqual("1.2.3rc1", plugin.__version__)
385
def test_dev__version__with_version_info(self):
386
self.setup_plugin("version_info = (1, 2, 3, 'dev', 0)")
387
plugin = bzrlib.plugin.plugins()['plugin']
388
self.assertEqual("1.2.3dev", plugin.__version__)
390
def test_dev_fallback__version__with_version_info(self):
391
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
392
plugin = bzrlib.plugin.plugins()['plugin']
393
self.assertEqual("1.2.3.dev.4", plugin.__version__)
395
def test_final__version__with_version_info(self):
396
self.setup_plugin("version_info = (1, 2, 3, 'final', 0)")
397
plugin = bzrlib.plugin.plugins()['plugin']
398
self.assertEqual("1.2.3", plugin.__version__)
401
class TestPluginHelp(TestCaseInTempDir):
403
def split_help_commands(self):
406
out, err = self.run_bzr('--no-plugins help commands')
407
for line in out.splitlines():
408
if not line.startswith(' '):
409
current = line.split()[0]
410
help[current] = help.get(current, '') + line
414
def test_plugin_help_builtins_unaffected(self):
415
# Check we don't get false positives
416
help_commands = self.split_help_commands()
417
for cmd_name in bzrlib.commands.builtin_command_names():
418
if cmd_name in bzrlib.commands.plugin_command_names():
421
help = bzrlib.commands.get_cmd_object(cmd_name).get_help_text()
422
except NotImplementedError:
423
# some commands have no help
426
self.assertNotContainsRe(help, 'plugin "[^"]*"')
428
if cmd_name in help_commands.keys():
429
# some commands are hidden
430
help = help_commands[cmd_name]
431
self.assertNotContainsRe(help, 'plugin "[^"]*"')
433
def test_plugin_help_shows_plugin(self):
434
# Create a test plugin
435
os.mkdir('plugin_test')
436
f = open(pathjoin('plugin_test', 'myplug.py'), 'w')
44
437
f.write(PLUGIN_TEXT)
47
newhelp = backtick('bzr help commands')
48
assert newhelp.startswith('You have been overridden\n')
49
# We added a line, but the rest should work
50
assert newhelp[25:] == help
52
assert backtick('bzr commit -m test') == "I'm sorry dave, you can't do that\n"
54
shutil.rmtree('plugin_test')
60
# """import bzrlib, bzrlib.commands
61
# class cmd_myplug(bzrlib.commands.Command):
62
# '''Just a simple test plugin.'''
65
# print 'Hello from my plugin'
69
# os.environ['BZRPLUGINPATH'] = os.path.abspath('plugin_test')
70
# help = backtick('bzr help commands')
71
# assert help.find('myplug') != -1
72
# assert help.find('Just a simple test plugin.') != -1
75
# assert backtick('bzr myplug') == 'Hello from my plugin\n'
76
# assert backtick('bzr mplg') == 'Hello from my plugin\n'
78
# f = open(os.path.join('plugin_test', 'override.py'), 'wb')
79
# f.write("""import bzrlib, bzrlib.commands
80
# class cmd_commit(bzrlib.commands.cmd_commit):
81
# '''Commit changes into a new revision.'''
82
# def run(self, *args, **kwargs):
83
# print "I'm sorry dave, you can't do that"
85
# class cmd_help(bzrlib.commands.cmd_help):
86
# '''Show help on a command or other topic.'''
87
# def run(self, *args, **kwargs):
88
# print "You have been overridden"
89
# bzrlib.commands.cmd_help.run(self, *args, **kwargs)
442
bzrlib.plugin.load_from_path(['plugin_test'])
443
bzrlib.commands.register_command( bzrlib.plugins.myplug.cmd_myplug)
444
help = self.run_bzr('help myplug')[0]
445
self.assertContainsRe(help, 'plugin "myplug"')
446
help = self.split_help_commands()['myplug']
447
self.assertContainsRe(help, '\[myplug\]')
450
if 'myplug' in bzrlib.commands.plugin_cmds:
451
bzrlib.commands.plugin_cmds.remove('myplug')
452
# remove the plugin 'myplug'
453
if getattr(bzrlib.plugins, 'myplug', None):
454
delattr(bzrlib.plugins, 'myplug')
457
class TestSetPluginsPath(TestCase):
459
def test_set_plugins_path(self):
460
"""set_plugins_path should set the module __path__ correctly."""
461
old_path = bzrlib.plugins.__path__
463
bzrlib.plugins.__path__ = []
464
expected_path = bzrlib.plugin.set_plugins_path()
465
self.assertEqual(expected_path, bzrlib.plugins.__path__)
467
bzrlib.plugins.__path__ = old_path
469
def test_set_plugins_path_with_trailing_slashes(self):
470
"""set_plugins_path should set the module __path__ based on
471
BZR_PLUGIN_PATH after removing all trailing slashes."""
472
old_path = bzrlib.plugins.__path__
473
old_env = os.environ.get('BZR_PLUGIN_PATH')
475
bzrlib.plugins.__path__ = []
476
os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
478
bzrlib.plugin.set_plugins_path()
479
# We expect our nominated paths to have all path-seps removed,
480
# and this is testing only that.
481
expected_path = ['first', 'second']
482
self.assertEqual(expected_path,
483
bzrlib.plugins.__path__[:len(expected_path)])
485
bzrlib.plugins.__path__ = old_path
486
if old_env is not None:
487
os.environ['BZR_PLUGIN_PATH'] = old_env
489
del os.environ['BZR_PLUGIN_PATH']
492
class TestHelpIndex(tests.TestCase):
493
"""Tests for the PluginsHelpIndex class."""
495
def test_default_constructable(self):
496
index = plugin.PluginsHelpIndex()
498
def test_get_topics_None(self):
499
"""Searching for None returns an empty list."""
500
index = plugin.PluginsHelpIndex()
501
self.assertEqual([], index.get_topics(None))
503
def test_get_topics_for_plugin(self):
504
"""Searching for plugin name gets its docstring."""
505
index = plugin.PluginsHelpIndex()
506
# make a new plugin here for this test, even if we're run with
508
self.assertFalse(sys.modules.has_key('bzrlib.plugins.demo_module'))
509
demo_module = FakeModule('', 'bzrlib.plugins.demo_module')
510
sys.modules['bzrlib.plugins.demo_module'] = demo_module
512
topics = index.get_topics('demo_module')
513
self.assertEqual(1, len(topics))
514
self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
515
self.assertEqual(demo_module, topics[0].module)
517
del sys.modules['bzrlib.plugins.demo_module']
519
def test_get_topics_no_topic(self):
520
"""Searching for something that is not a plugin returns []."""
521
# test this by using a name that cannot be a plugin - its not
522
# a valid python identifier.
523
index = plugin.PluginsHelpIndex()
524
self.assertEqual([], index.get_topics('nothing by this name'))
526
def test_prefix(self):
527
"""PluginsHelpIndex has a prefix of 'plugins/'."""
528
index = plugin.PluginsHelpIndex()
529
self.assertEqual('plugins/', index.prefix)
531
def test_get_plugin_topic_with_prefix(self):
532
"""Searching for plugins/demo_module returns help."""
533
index = plugin.PluginsHelpIndex()
534
self.assertFalse(sys.modules.has_key('bzrlib.plugins.demo_module'))
535
demo_module = FakeModule('', 'bzrlib.plugins.demo_module')
536
sys.modules['bzrlib.plugins.demo_module'] = demo_module
538
topics = index.get_topics('plugins/demo_module')
539
self.assertEqual(1, len(topics))
540
self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
541
self.assertEqual(demo_module, topics[0].module)
543
del sys.modules['bzrlib.plugins.demo_module']
546
class FakeModule(object):
547
"""A fake module to test with."""
549
def __init__(self, doc, name):
554
class TestModuleHelpTopic(tests.TestCase):
555
"""Tests for the ModuleHelpTopic class."""
557
def test_contruct(self):
558
"""Construction takes the module to document."""
559
mod = FakeModule('foo', 'foo')
560
topic = plugin.ModuleHelpTopic(mod)
561
self.assertEqual(mod, topic.module)
563
def test_get_help_text_None(self):
564
"""A ModuleHelpTopic returns the docstring for get_help_text."""
565
mod = FakeModule(None, 'demo')
566
topic = plugin.ModuleHelpTopic(mod)
567
self.assertEqual("Plugin 'demo' has no docstring.\n",
568
topic.get_help_text())
570
def test_get_help_text_no_carriage_return(self):
571
"""ModuleHelpTopic.get_help_text adds a \n if needed."""
572
mod = FakeModule('one line of help', 'demo')
573
topic = plugin.ModuleHelpTopic(mod)
574
self.assertEqual("one line of help\n",
575
topic.get_help_text())
577
def test_get_help_text_carriage_return(self):
578
"""ModuleHelpTopic.get_help_text adds a \n if needed."""
579
mod = FakeModule('two lines of help\nand more\n', 'demo')
580
topic = plugin.ModuleHelpTopic(mod)
581
self.assertEqual("two lines of help\nand more\n",
582
topic.get_help_text())
584
def test_get_help_text_with_additional_see_also(self):
585
mod = FakeModule('two lines of help\nand more', 'demo')
586
topic = plugin.ModuleHelpTopic(mod)
587
self.assertEqual("two lines of help\nand more\nSee also: bar, foo\n",
588
topic.get_help_text(['foo', 'bar']))
590
def test_get_help_topic(self):
591
"""The help topic for a plugin is its module name."""
592
mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.demo')
593
topic = plugin.ModuleHelpTopic(mod)
594
self.assertEqual('demo', topic.get_help_topic())
595
mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
596
topic = plugin.ModuleHelpTopic(mod)
597
self.assertEqual('foo_bar', topic.get_help_topic())
600
def clear_plugins(test_case):
601
# Save the attributes that we're about to monkey-patch.
602
old_plugins_path = bzrlib.plugins.__path__
603
old_loaded = plugin._loaded
604
old_load_from_path = plugin.load_from_path
605
# Change bzrlib.plugin to think no plugins have been loaded yet.
606
bzrlib.plugins.__path__ = []
607
plugin._loaded = False
608
# Monkey-patch load_from_path to stop it from actually loading anything.
609
def load_from_path(dirs):
611
plugin.load_from_path = load_from_path
612
def restore_plugins():
613
bzrlib.plugins.__path__ = old_plugins_path
614
plugin._loaded = old_loaded
615
plugin.load_from_path = old_load_from_path
616
test_case.addCleanup(restore_plugins)
619
class TestPluginPaths(tests.TestCase):
621
def test_set_plugins_path_with_args(self):
623
plugin.set_plugins_path(['a', 'b'])
624
self.assertEqual(['a', 'b'], bzrlib.plugins.__path__)
626
def test_set_plugins_path_defaults(self):
628
plugin.set_plugins_path()
629
self.assertEqual(plugin.get_standard_plugins_path(),
630
bzrlib.plugins.__path__)
632
def test_get_standard_plugins_path(self):
633
path = plugin.get_standard_plugins_path()
634
self.assertEqual(plugin.get_default_plugin_path(), path[0])
635
for directory in path:
636
self.assertNotContainsRe(directory, r'\\/$')
638
from distutils.sysconfig import get_python_lib
642
if sys.platform != 'win32':
643
python_lib = get_python_lib()
644
for directory in path:
645
if directory.startswith(python_lib):
648
self.fail('No path to global plugins')
650
def test_get_standard_plugins_path_env(self):
651
os.environ['BZR_PLUGIN_PATH'] = 'foo/'
652
self.assertEqual('foo', plugin.get_standard_plugins_path()[0])
655
class TestLoadPlugins(tests.TestCaseInTempDir):
657
def test_load_plugins(self):
659
plugin.load_plugins(['.'])
660
self.assertEqual(bzrlib.plugins.__path__, ['.'])
661
# subsequent loads are no-ops
662
plugin.load_plugins(['foo'])
663
self.assertEqual(bzrlib.plugins.__path__, ['.'])
665
def test_load_plugins_default(self):
667
plugin.load_plugins()
668
path = plugin.get_standard_plugins_path()
669
self.assertEqual(path, bzrlib.plugins.__path__)