73
73
file_name='__init__.py')
75
75
def _unregister_plugin(self, name):
76
"""Remove the plugin from sys.modules and the brzlib namespace."""
77
py_name = 'brzlib.plugins.%s' % name
76
"""Remove the plugin from sys.modules and the breezy namespace."""
77
py_name = 'breezy.plugins.%s' % name
78
78
if py_name in sys.modules:
79
79
del sys.modules[py_name]
80
if getattr(brzlib.plugins, name, None) is not None:
81
delattr(brzlib.plugins, name)
80
if getattr(breezy.plugins, name, None) is not None:
81
delattr(breezy.plugins, name)
83
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)
84
"""Remove the submodule from sys.modules and the breezy namespace."""
85
py_name = 'breezy.plugins.%s.%s' % (plugin_name, submodule_name)
86
86
if py_name in sys.modules:
87
87
del sys.modules[py_name]
88
plugin = getattr(brzlib.plugins, plugin_name, None)
88
plugin = getattr(breezy.plugins, plugin_name, None)
89
89
if plugin is not None:
90
90
if getattr(plugin, submodule_name, None) is not None:
91
91
delattr(plugin, submodule_name)
93
93
def assertPluginUnknown(self, name):
94
self.assertFalse(getattr(brzlib.plugins, name, None) is not None)
95
self.assertFalse('brzlib.plugins.%s' % name in sys.modules)
94
self.assertFalse(getattr(breezy.plugins, name, None) is not None)
95
self.assertFalse('breezy.plugins.%s' % name in sys.modules)
97
97
def assertPluginKnown(self, name):
98
self.assertTrue(getattr(brzlib.plugins, name, None) is not None)
99
self.assertTrue('brzlib.plugins.%s' % name in sys.modules)
98
self.assertTrue(getattr(breezy.plugins, name, None) is not None)
99
self.assertTrue('breezy.plugins.%s' % name in sys.modules)
102
102
class TestLoadingPlugins(BaseTestPlugins):
187
oldpath = brzlib.plugins.__path__
187
oldpath = breezy.plugins.__path__
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"
189
self.assertFalse('breezy.plugins.pluginone' in sys.modules)
190
self.assertFalse('breezy.plugins.plugintwo' in sys.modules)
191
breezy.plugins.__path__ = ['first', 'second']
192
exec "import breezy.plugins.pluginone"
193
193
self.assertEqual(['first'], self.activeattributes[tempattribute])
194
exec "import brzlib.plugins.plugintwo"
194
exec "import breezy.plugins.plugintwo"
195
195
self.assertEqual(['first', 'second'],
196
196
self.activeattributes[tempattribute])
212
212
# set a place for the plugin to record its loading, and at the same
213
213
# time validate that the location the plugin should record to is
214
214
# valid and correct.
215
brzlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
215
breezy.tests.test_plugins.TestLoadingPlugins.activeattributes \
216
216
[tempattribute] = []
217
217
self.assertTrue(tempattribute in self.activeattributes)
218
218
# create a directory for the plugin
219
219
os.mkdir('plugin_test')
220
220
# write a plugin that will record when its loaded in the
221
221
# tempattribute list.
222
template = ("from brzlib.tests.test_plugins import TestLoadingPlugins\n"
222
template = ("from breezy.tests.test_plugins import TestLoadingPlugins\n"
223
223
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
225
225
outfile = open(os.path.join('plugin_test', 'ts_plugin.py'), 'w')
251
251
log.addHandler(handler)
254
brzlib.plugin.load_from_path(['.'])
254
breezy.plugin.load_from_path(['.'])
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)
256
if 'breezy.plugins.%s' % name in sys.modules:
257
del sys.modules['breezy.plugins.%s' % name]
258
if getattr(breezy.plugins, name, None):
259
delattr(breezy.plugins, name)
261
261
# Stop capturing output
402
402
def test_no_version_info___version__(self):
403
403
self.setup_plugin()
404
plugin = brzlib.plugin.plugins()['plugin']
404
plugin = breezy.plugin.plugins()['plugin']
405
405
self.assertEqual("unknown", plugin.__version__)
407
407
def test_str__version__with_version_info(self):
408
408
self.setup_plugin("version_info = '1.2.3'")
409
plugin = brzlib.plugin.plugins()['plugin']
409
plugin = breezy.plugin.plugins()['plugin']
410
410
self.assertEqual("1.2.3", plugin.__version__)
412
412
def test_noniterable__version__with_version_info(self):
413
413
self.setup_plugin("version_info = (1)")
414
plugin = brzlib.plugin.plugins()['plugin']
414
plugin = breezy.plugin.plugins()['plugin']
415
415
self.assertEqual("1", plugin.__version__)
417
417
def test_1__version__with_version_info(self):
418
418
self.setup_plugin("version_info = (1,)")
419
plugin = brzlib.plugin.plugins()['plugin']
419
plugin = breezy.plugin.plugins()['plugin']
420
420
self.assertEqual("1", plugin.__version__)
422
422
def test_1_2__version__with_version_info(self):
423
423
self.setup_plugin("version_info = (1, 2)")
424
plugin = brzlib.plugin.plugins()['plugin']
424
plugin = breezy.plugin.plugins()['plugin']
425
425
self.assertEqual("1.2", plugin.__version__)
427
427
def test_1_2_3__version__with_version_info(self):
428
428
self.setup_plugin("version_info = (1, 2, 3)")
429
plugin = brzlib.plugin.plugins()['plugin']
429
plugin = breezy.plugin.plugins()['plugin']
430
430
self.assertEqual("1.2.3", plugin.__version__)
432
432
def test_candidate__version__with_version_info(self):
433
433
self.setup_plugin("version_info = (1, 2, 3, 'candidate', 1)")
434
plugin = brzlib.plugin.plugins()['plugin']
434
plugin = breezy.plugin.plugins()['plugin']
435
435
self.assertEqual("1.2.3rc1", plugin.__version__)
437
437
def test_dev__version__with_version_info(self):
438
438
self.setup_plugin("version_info = (1, 2, 3, 'dev', 0)")
439
plugin = brzlib.plugin.plugins()['plugin']
439
plugin = breezy.plugin.plugins()['plugin']
440
440
self.assertEqual("1.2.3dev", plugin.__version__)
442
442
def test_dev_fallback__version__with_version_info(self):
443
443
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
444
plugin = brzlib.plugin.plugins()['plugin']
444
plugin = breezy.plugin.plugins()['plugin']
445
445
self.assertEqual("1.2.3dev4", plugin.__version__)
447
447
def test_final__version__with_version_info(self):
448
448
self.setup_plugin("version_info = (1, 2, 3, 'final', 0)")
449
plugin = brzlib.plugin.plugins()['plugin']
449
plugin = breezy.plugin.plugins()['plugin']
450
450
self.assertEqual("1.2.3", plugin.__version__)
452
452
def test_final_fallback__version__with_version_info(self):
453
453
self.setup_plugin("version_info = (1, 2, 3, 'final', 2)")
454
plugin = brzlib.plugin.plugins()['plugin']
454
plugin = breezy.plugin.plugins()['plugin']
455
455
self.assertEqual("1.2.3.2", plugin.__version__)
471
471
def test_plugin_help_builtins_unaffected(self):
472
472
# Check we don't get false positives
473
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():
474
for cmd_name in breezy.commands.builtin_command_names():
475
if cmd_name in breezy.commands.plugin_command_names():
478
help = brzlib.commands.get_cmd_object(cmd_name).get_help_text()
478
help = breezy.commands.get_cmd_object(cmd_name).get_help_text()
479
479
except NotImplementedError:
480
480
# some commands have no help
508
brzlib.plugin.load_from_path(['plugin_test'])
509
brzlib.commands.register_command( brzlib.plugins.myplug.cmd_myplug)
508
breezy.plugin.load_from_path(['plugin_test'])
509
breezy.commands.register_command( breezy.plugins.myplug.cmd_myplug)
510
510
help = self.run_bzr('help myplug')[0]
511
511
self.assertContainsRe(help, 'plugin "myplug"')
512
512
help = self.split_help_commands()['myplug']
513
513
self.assertContainsRe(help, '\[myplug\]')
515
515
# unregister command
516
if 'myplug' in brzlib.commands.plugin_cmds:
517
brzlib.commands.plugin_cmds.remove('myplug')
516
if 'myplug' in breezy.commands.plugin_cmds:
517
breezy.commands.plugin_cmds.remove('myplug')
518
518
# remove the plugin 'myplug'
519
if getattr(brzlib.plugins, 'myplug', None):
520
delattr(brzlib.plugins, 'myplug')
519
if getattr(breezy.plugins, 'myplug', None):
520
delattr(breezy.plugins, 'myplug')
523
523
class TestHelpIndex(tests.TestCase):
536
536
index = plugin.PluginsHelpIndex()
537
537
# make a new plugin here for this test, even if we're run with
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
539
self.assertFalse(sys.modules.has_key('breezy.plugins.demo_module'))
540
demo_module = FakeModule('', 'breezy.plugins.demo_module')
541
sys.modules['breezy.plugins.demo_module'] = demo_module
543
543
topics = index.get_topics('demo_module')
544
544
self.assertEqual(1, len(topics))
545
545
self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
546
546
self.assertEqual(demo_module, topics[0].module)
548
del sys.modules['brzlib.plugins.demo_module']
548
del sys.modules['breezy.plugins.demo_module']
550
550
def test_get_topics_no_topic(self):
551
551
"""Searching for something that is not a plugin returns []."""
562
562
def test_get_plugin_topic_with_prefix(self):
563
563
"""Searching for plugins/demo_module returns help."""
564
564
index = plugin.PluginsHelpIndex()
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
565
self.assertFalse(sys.modules.has_key('breezy.plugins.demo_module'))
566
demo_module = FakeModule('', 'breezy.plugins.demo_module')
567
sys.modules['breezy.plugins.demo_module'] = demo_module
569
569
topics = index.get_topics('plugins/demo_module')
570
570
self.assertEqual(1, len(topics))
571
571
self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
572
572
self.assertEqual(demo_module, topics[0].module)
574
del sys.modules['brzlib.plugins.demo_module']
574
del sys.modules['breezy.plugins.demo_module']
577
577
class FakeModule(object):
621
621
def test_get_help_topic(self):
622
622
"""The help topic for a plugin is its module name."""
623
mod = FakeModule('two lines of help\nand more', 'brzlib.plugins.demo')
623
mod = FakeModule('two lines of help\nand more', 'breezy.plugins.demo')
624
624
topic = plugin.ModuleHelpTopic(mod)
625
625
self.assertEqual('demo', topic.get_help_topic())
626
626
mod = FakeModule('two lines of help\nand more',
627
'brzlib.plugins.foo_bar')
627
'breezy.plugins.foo_bar')
628
628
topic = plugin.ModuleHelpTopic(mod)
629
629
self.assertEqual('foo_bar', topic.get_help_topic())
643
643
def test_set_plugins_path_with_args(self):
644
644
plugin.set_plugins_path(['a', 'b'])
645
self.assertEqual(['a', 'b'], brzlib.plugins.__path__)
645
self.assertEqual(['a', 'b'], breezy.plugins.__path__)
647
647
def test_set_plugins_path_defaults(self):
648
648
plugin.set_plugins_path()
649
649
self.assertEqual(plugin.get_standard_plugins_path(),
650
brzlib.plugins.__path__)
650
breezy.plugins.__path__)
652
652
def test_get_standard_plugins_path(self):
653
653
path = plugin.get_standard_plugins_path()
675
675
def test_load_plugins(self):
676
676
plugin.load_plugins(['.'])
677
self.assertEqual(brzlib.plugins.__path__, ['.'])
677
self.assertEqual(breezy.plugins.__path__, ['.'])
678
678
# subsequent loads are no-ops
679
679
plugin.load_plugins(['foo'])
680
self.assertEqual(brzlib.plugins.__path__, ['.'])
680
self.assertEqual(breezy.plugins.__path__, ['.'])
682
682
def test_load_plugins_default(self):
683
683
plugin.load_plugins()
684
684
path = plugin.get_standard_plugins_path()
685
self.assertEqual(path, brzlib.plugins.__path__)
685
self.assertEqual(path, breezy.plugins.__path__)
688
688
class TestEnvPluginPath(tests.TestCase):
913
913
'test_foo', 'test_bar')
914
914
self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@non-standard-dir')
915
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
916
import breezy.plugins.test_foo
917
self.assertEqual('breezy.plugins.test_foo',
918
breezy.plugins.test_foo.__package__)
919
import breezy.plugins.test_foo.test_bar
920
920
self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
921
brzlib.plugins.test_foo.test_bar.__file__)
921
breezy.plugins.test_foo.test_bar.__file__)
923
923
def test_relative_submodule_loading(self):
924
924
self.create_plugin_package('test_foo', dir='another-dir', source='''
930
930
'test_foo', 'test_bar')
931
931
self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@another-dir')
932
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__)
933
import breezy.plugins.test_foo
934
self.assertEqual('breezy.plugins.test_foo',
935
breezy.plugins.test_foo.__package__)
936
936
self.assertIsSameRealPath('another-dir/test_bar/__init__.py',
937
brzlib.plugins.test_foo.test_bar.__file__)
937
breezy.plugins.test_foo.test_bar.__file__)
939
939
def test_loading_from___init__only(self):
940
940
# We rename the existing __init__.py file to ensure that we don't load