233
182
bzrlib.plugin.load_from_path(['plugin_test'+os.sep])
234
183
self.assertEqual(['plugin'], self.activeattributes[tempattribute])
185
# remove the plugin 'plugin'
236
186
del self.activeattributes[tempattribute]
237
self._unregister_plugin('ts_plugin')
238
self.assertPluginUnknown('ts_plugin')
240
def load_and_capture(self, name):
241
"""Load plugins from '.' capturing the output.
243
:param name: The name of the plugin.
244
:return: A string with the log from the plugin loading call.
249
handler = logging.StreamHandler(stream)
250
log = logging.getLogger('bzr')
251
log.addHandler(handler)
254
bzrlib.plugin.load_from_path(['.'])
256
if 'bzrlib.plugins.%s' % name in sys.modules:
257
del sys.modules['bzrlib.plugins.%s' % name]
258
if getattr(bzrlib.plugins, name, None):
259
delattr(bzrlib.plugins, name)
261
# Stop capturing output
264
log.removeHandler(handler)
265
return stream.getvalue()
269
def test_plugin_with_bad_api_version_reports(self):
270
"""Try loading a plugin that requests an unsupported api.
272
Observe that it records the problem but doesn't complain on stderr.
274
See https://bugs.launchpad.net/bzr/+bug/704195
276
self.overrideAttr(plugin, 'plugin_warnings', {})
280
f.write("import bzrlib.api\n"
281
"bzrlib.api.require_any_api(bzrlib, [(1, 0, 0)])\n")
284
log = self.load_and_capture(name)
285
self.assertNotContainsRe(log,
286
r"It requested API version")
289
plugin.plugin_warnings.keys())
290
self.assertContainsRe(
291
plugin.plugin_warnings['wants100'][0],
292
r"It requested API version")
294
def test_plugin_with_bad_name_does_not_load(self):
295
# The file name here invalid for a python module.
296
name = 'bzr-bad plugin-name..py'
297
file(name, 'w').close()
298
log = self.load_and_capture(name)
299
self.assertContainsRe(log,
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_'\.")
305
class TestPlugins(BaseTestPlugins):
187
if getattr(bzrlib.plugins, 'ts_plugin', None):
188
del bzrlib.plugins.ts_plugin
189
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
192
class TestAllPlugins(TestCaseInTempDir):
194
def test_plugin_appears_in_all_plugins(self):
195
# This test tests a new plugin appears in bzrlib.plugin.all_plugins().
196
# check the plugin is not loaded already
197
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
198
# write a plugin that _cannot_ fail to load.
199
file('plugin.py', 'w').write("\n")
201
bzrlib.plugin.load_from_path(['.'])
202
all_plugins = self.applyDeprecated(zero_ninetyone,
203
bzrlib.plugin.all_plugins)
204
self.failUnless('plugin' in all_plugins)
205
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
206
self.assertEqual(all_plugins['plugin'], bzrlib.plugins.plugin)
208
# remove the plugin 'plugin'
209
if 'bzrlib.plugins.plugin' in sys.modules:
210
del sys.modules['bzrlib.plugins.plugin']
211
if getattr(bzrlib.plugins, 'plugin', None):
212
del bzrlib.plugins.plugin
213
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
216
class TestPlugins(TestCaseInTempDir):
307
218
def setup_plugin(self, source=""):
308
219
# This test tests a new plugin appears in bzrlib.plugin.plugins().
309
220
# check the plugin is not loaded already
310
self.assertPluginUnknown('plugin')
221
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
311
222
# write a plugin that _cannot_ fail to load.
312
with file('plugin.py', 'w') as f: f.write(source + '\n')
223
file('plugin.py', 'w').write(source + '\n')
313
224
self.addCleanup(self.teardown_plugin)
314
plugin.load_from_path(['.'])
225
bzrlib.plugin.load_from_path(['.'])
316
227
def teardown_plugin(self):
317
self._unregister_plugin('plugin')
318
self.assertPluginUnknown('plugin')
228
# remove the plugin 'plugin'
229
if 'bzrlib.plugins.plugin' in sys.modules:
230
del sys.modules['bzrlib.plugins.plugin']
231
if getattr(bzrlib.plugins, 'plugin', None):
232
del bzrlib.plugins.plugin
233
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
320
235
def test_plugin_appears_in_plugins(self):
321
236
self.setup_plugin()
322
self.assertPluginKnown('plugin')
323
p = plugin.plugins()['plugin']
324
self.assertIsInstance(p, bzrlib.plugin.PlugIn)
325
self.assertEqual(p.module, plugins.plugin)
237
self.failUnless('plugin' in bzrlib.plugin.plugins())
238
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
239
plugins = bzrlib.plugin.plugins()
240
plugin = plugins['plugin']
241
self.assertIsInstance(plugin, bzrlib.plugin.PlugIn)
242
self.assertEqual(bzrlib.plugins.plugin, plugin.module)
327
244
def test_trivial_plugin_get_path(self):
328
245
self.setup_plugin()
329
p = plugin.plugins()['plugin']
330
plugin_path = self.test_dir + '/plugin.py'
331
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
333
def test_plugin_get_path_py_not_pyc(self):
334
# first import creates plugin.pyc
336
self.teardown_plugin()
337
plugin.load_from_path(['.']) # import plugin.pyc
338
p = plugin.plugins()['plugin']
339
plugin_path = self.test_dir + '/plugin.py'
340
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
342
def test_plugin_get_path_pyc_only(self):
343
# first import creates plugin.pyc (or plugin.pyo depending on __debug__)
345
self.teardown_plugin()
346
os.unlink(self.test_dir + '/plugin.py')
347
plugin.load_from_path(['.']) # import plugin.pyc (or .pyo)
348
p = plugin.plugins()['plugin']
350
plugin_path = self.test_dir + '/plugin.pyc'
352
plugin_path = self.test_dir + '/plugin.pyo'
353
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
246
plugins = bzrlib.plugin.plugins()
247
plugin = plugins['plugin']
248
plugin_path = self.test_dir + '/plugin.py'
249
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
355
251
def test_no_test_suite_gives_None_for_test_suite(self):
356
252
self.setup_plugin()
357
p = plugin.plugins()['plugin']
358
self.assertEqual(None, p.test_suite())
253
plugin = bzrlib.plugin.plugins()['plugin']
254
self.assertEqual(None, plugin.test_suite())
360
256
def test_test_suite_gives_test_suite_result(self):
361
257
source = """def test_suite(): return 'foo'"""
362
258
self.setup_plugin(source)
363
p = plugin.plugins()['plugin']
364
self.assertEqual('foo', p.test_suite())
366
def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
368
loader = tests.TestUtil.TestLoader()
369
p = plugin.plugins()['plugin']
370
self.assertEqual(None, p.load_plugin_tests(loader))
372
def test_load_plugin_tests_gives_load_plugin_tests_result(self):
374
def load_tests(standard_tests, module, loader):
376
self.setup_plugin(source)
377
loader = tests.TestUtil.TestLoader()
378
p = plugin.plugins()['plugin']
379
self.assertEqual('foo', p.load_plugin_tests(loader))
381
def check_version_info(self, expected, source='', name='plugin'):
382
self.setup_plugin(source)
383
self.assertEqual(expected, plugin.plugins()[name].version_info())
259
plugin = bzrlib.plugin.plugins()['plugin']
260
self.assertEqual('foo', plugin.test_suite())
385
262
def test_no_version_info(self):
386
self.check_version_info(None)
264
plugin = bzrlib.plugin.plugins()['plugin']
265
self.assertEqual(None, plugin.version_info())
388
267
def test_with_version_info(self):
389
self.check_version_info((1, 2, 3, 'dev', 4),
390
"version_info = (1, 2, 3, 'dev', 4)")
268
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
269
plugin = bzrlib.plugin.plugins()['plugin']
270
self.assertEqual((1, 2, 3, 'dev', 4), plugin.version_info())
392
272
def test_short_version_info_gets_padded(self):
393
273
# the gtk plugin has version_info = (1,2,3) rather than the 5-tuple.
395
self.check_version_info((1, 2, 3, 'final', 0),
396
"version_info = (1, 2, 3)")
398
def check_version(self, expected, source=None, name='plugin'):
399
self.setup_plugin(source)
400
self.assertEqual(expected, plugins[name].__version__)
275
self.setup_plugin("version_info = (1, 2, 3)")
276
plugin = bzrlib.plugin.plugins()['plugin']
277
self.assertEqual((1, 2, 3, 'final', 0), plugin.version_info())
402
279
def test_no_version_info___version__(self):
403
280
self.setup_plugin()
404
281
plugin = bzrlib.plugin.plugins()['plugin']
405
282
self.assertEqual("unknown", plugin.__version__)
407
def test_str__version__with_version_info(self):
408
self.setup_plugin("version_info = '1.2.3'")
409
plugin = bzrlib.plugin.plugins()['plugin']
410
self.assertEqual("1.2.3", plugin.__version__)
412
def test_noniterable__version__with_version_info(self):
413
self.setup_plugin("version_info = (1)")
414
plugin = bzrlib.plugin.plugins()['plugin']
415
self.assertEqual("1", plugin.__version__)
417
def test_1__version__with_version_info(self):
418
self.setup_plugin("version_info = (1,)")
419
plugin = bzrlib.plugin.plugins()['plugin']
420
self.assertEqual("1", plugin.__version__)
422
def test_1_2__version__with_version_info(self):
423
self.setup_plugin("version_info = (1, 2)")
424
plugin = bzrlib.plugin.plugins()['plugin']
425
self.assertEqual("1.2", plugin.__version__)
427
def test_1_2_3__version__with_version_info(self):
428
self.setup_plugin("version_info = (1, 2, 3)")
429
plugin = bzrlib.plugin.plugins()['plugin']
430
self.assertEqual("1.2.3", plugin.__version__)
432
def test_candidate__version__with_version_info(self):
433
self.setup_plugin("version_info = (1, 2, 3, 'candidate', 1)")
434
plugin = bzrlib.plugin.plugins()['plugin']
435
self.assertEqual("1.2.3rc1", plugin.__version__)
437
def test_dev__version__with_version_info(self):
438
self.setup_plugin("version_info = (1, 2, 3, 'dev', 0)")
439
plugin = bzrlib.plugin.plugins()['plugin']
440
self.assertEqual("1.2.3dev", plugin.__version__)
442
def test_dev_fallback__version__with_version_info(self):
284
def test___version__with_version_info(self):
443
285
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
444
286
plugin = bzrlib.plugin.plugins()['plugin']
445
287
self.assertEqual("1.2.3dev4", plugin.__version__)
447
289
def test_final__version__with_version_info(self):
448
self.setup_plugin("version_info = (1, 2, 3, 'final', 0)")
290
self.setup_plugin("version_info = (1, 2, 3, 'final', 4)")
449
291
plugin = bzrlib.plugin.plugins()['plugin']
450
292
self.assertEqual("1.2.3", plugin.__version__)
452
def test_final_fallback__version__with_version_info(self):
453
self.setup_plugin("version_info = (1, 2, 3, 'final', 2)")
454
plugin = bzrlib.plugin.plugins()['plugin']
455
self.assertEqual("1.2.3.2", plugin.__version__)
458
class TestPluginHelp(tests.TestCaseInTempDir):
295
class TestPluginHelp(TestCaseInTempDir):
460
297
def split_help_commands(self):
463
out, err = self.run_bzr('--no-plugins help commands')
464
for line in out.splitlines():
300
for line in self.run_bzr('help commands')[0].splitlines():
465
301
if not line.startswith(' '):
466
302
current = line.split()[0]
467
303
help[current] = help.get(current, '') + line
513
340
self.assertContainsRe(help, '\[myplug\]')
515
342
# unregister command
516
if 'myplug' in bzrlib.commands.plugin_cmds:
517
bzrlib.commands.plugin_cmds.remove('myplug')
343
if bzrlib.commands.plugin_cmds.get('myplug', None):
344
del bzrlib.commands.plugin_cmds['myplug']
518
345
# remove the plugin 'myplug'
519
346
if getattr(bzrlib.plugins, 'myplug', None):
520
347
delattr(bzrlib.plugins, 'myplug')
350
class TestPluginFromZip(TestCaseInTempDir):
352
def make_zipped_plugin(self, zip_name, filename):
353
z = zipfile.ZipFile(zip_name, 'w')
354
z.writestr(filename, PLUGIN_TEXT)
357
def check_plugin_load(self, zip_name, plugin_name):
358
self.assertFalse(plugin_name in dir(bzrlib.plugins),
359
'Plugin already loaded')
360
old_path = bzrlib.plugins.__path__
362
# this is normally done by load_plugins -> set_plugins_path
363
bzrlib.plugins.__path__ = [zip_name]
364
bzrlib.plugin.load_from_zip(zip_name)
365
self.assertTrue(plugin_name in dir(bzrlib.plugins),
366
'Plugin is not loaded')
369
if getattr(bzrlib.plugins, plugin_name, None):
370
delattr(bzrlib.plugins, plugin_name)
371
del sys.modules['bzrlib.plugins.' + plugin_name]
372
bzrlib.plugins.__path__ = old_path
374
def test_load_module(self):
375
self.make_zipped_plugin('./test.zip', 'ziplug.py')
376
self.check_plugin_load('./test.zip', 'ziplug')
378
def test_load_package(self):
379
self.make_zipped_plugin('./test.zip', 'ziplug/__init__.py')
380
self.check_plugin_load('./test.zip', 'ziplug')
383
class TestSetPluginsPath(TestCase):
385
def test_set_plugins_path(self):
386
"""set_plugins_path should set the module __path__ correctly."""
387
old_path = bzrlib.plugins.__path__
389
bzrlib.plugins.__path__ = []
390
expected_path = bzrlib.plugin.set_plugins_path()
391
self.assertEqual(expected_path, bzrlib.plugins.__path__)
393
bzrlib.plugins.__path__ = old_path
395
def test_set_plugins_path_with_trailing_slashes(self):
396
"""set_plugins_path should set the module __path__ based on
398
old_path = bzrlib.plugins.__path__
399
old_env = os.environ.get('BZR_PLUGIN_PATH')
401
bzrlib.plugins.__path__ = []
402
os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
404
bzrlib.plugin.set_plugins_path()
405
expected_path = ['first', 'second',
406
os.path.dirname(bzrlib.plugins.__file__)]
407
self.assertEqual(expected_path, bzrlib.plugins.__path__)
409
bzrlib.plugins.__path__ = old_path
411
os.environ['BZR_PLUGIN_PATH'] = old_env
413
del os.environ['BZR_PLUGIN_PATH']
523
415
class TestHelpIndex(tests.TestCase):
524
416
"""Tests for the PluginsHelpIndex class."""
615
507
def test_get_help_text_with_additional_see_also(self):
616
508
mod = FakeModule('two lines of help\nand more', 'demo')
617
509
topic = plugin.ModuleHelpTopic(mod)
618
self.assertEqual("two lines of help\nand more\n\n:See also: bar, foo\n",
619
topic.get_help_text(['foo', 'bar']))
510
self.assertEqual("two lines of help\nand more\nSee also: bar, foo\n",
511
topic.get_help_text(['foo', 'bar']))
621
513
def test_get_help_topic(self):
622
514
"""The help topic for a plugin is its module name."""
623
515
mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.demo')
624
516
topic = plugin.ModuleHelpTopic(mod)
625
517
self.assertEqual('demo', topic.get_help_topic())
626
mod = FakeModule('two lines of help\nand more',
627
'bzrlib.plugins.foo_bar')
518
mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
628
519
topic = plugin.ModuleHelpTopic(mod)
629
520
self.assertEqual('foo_bar', topic.get_help_topic())
632
class TestLoadFromPath(tests.TestCaseInTempDir):
635
super(TestLoadFromPath, self).setUp()
636
# Change bzrlib.plugin to think no plugins have been loaded yet.
637
self.overrideAttr(bzrlib.plugins, '__path__', [])
638
self.overrideAttr(plugin, '_loaded', False)
640
# Monkey-patch load_from_path to stop it from actually loading anything.
641
self.overrideAttr(plugin, 'load_from_path', lambda dirs: None)
643
def test_set_plugins_path_with_args(self):
644
plugin.set_plugins_path(['a', 'b'])
645
self.assertEqual(['a', 'b'], bzrlib.plugins.__path__)
647
def test_set_plugins_path_defaults(self):
648
plugin.set_plugins_path()
649
self.assertEqual(plugin.get_standard_plugins_path(),
650
bzrlib.plugins.__path__)
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'\\/$')
657
from distutils.sysconfig import get_python_lib
661
if sys.platform != 'win32':
662
python_lib = get_python_lib()
663
for directory in path:
664
if directory.startswith(python_lib):
667
self.fail('No path to global plugins')
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'\\/$')
675
def test_load_plugins(self):
676
plugin.load_plugins(['.'])
677
self.assertEqual(bzrlib.plugins.__path__, ['.'])
678
# subsequent loads are no-ops
679
plugin.load_plugins(['foo'])
680
self.assertEqual(bzrlib.plugins.__path__, ['.'])
682
def test_load_plugins_default(self):
683
plugin.load_plugins()
684
path = plugin.get_standard_plugins_path()
685
self.assertEqual(path, bzrlib.plugins.__path__)
688
class TestEnvPluginPath(tests.TestCase):
691
super(TestEnvPluginPath, self).setUp()
692
self.overrideAttr(plugin, 'DEFAULT_PLUGIN_PATH', None)
694
self.user = plugin.get_user_plugin_path()
695
self.site = plugin.get_site_plugin_path()
696
self.core = plugin.get_core_plugin_path()
698
def _list2paths(self, *args):
701
plugin._append_new_path(paths, p)
704
def _set_path(self, *args):
705
path = os.pathsep.join(self._list2paths(*args))
706
self.overrideEnv('BZR_PLUGIN_PATH', path)
708
def check_path(self, expected_dirs, setting_dirs):
710
self._set_path(*setting_dirs)
711
actual = plugin.get_standard_plugins_path()
712
self.assertEqual(self._list2paths(*expected_dirs), actual)
714
def test_default(self):
715
self.check_path([self.user, self.core, self.site],
718
def test_adhoc_policy(self):
719
self.check_path([self.user, self.core, self.site],
720
['+user', '+core', '+site'])
722
def test_fallback_policy(self):
723
self.check_path([self.core, self.site, self.user],
724
['+core', '+site', '+user'])
726
def test_override_policy(self):
727
self.check_path([self.user, self.site, self.core],
728
['+user', '+site', '+core'])
730
def test_disable_user(self):
731
self.check_path([self.core, self.site], ['-user'])
733
def test_disable_user_twice(self):
734
# Ensures multiple removals don't left cruft
735
self.check_path([self.core, self.site], ['-user', '-user'])
737
def test_duplicates_are_removed(self):
738
self.check_path([self.user, self.core, self.site],
740
# And only the first reference is kept (since the later references will
741
# only produce '<plugin> already loaded' mutters)
742
self.check_path([self.user, self.core, self.site],
743
['+user', '+user', '+core',
744
'+user', '+site', '+site',
747
def test_disable_overrides_enable(self):
748
self.check_path([self.core, self.site], ['-user', '+user'])
750
def test_disable_core(self):
751
self.check_path([self.site], ['-core'])
752
self.check_path([self.user, self.site], ['+user', '-core'])
754
def test_disable_site(self):
755
self.check_path([self.core], ['-site'])
756
self.check_path([self.user, self.core], ['-site', '+user'])
758
def test_override_site(self):
759
self.check_path(['mysite', self.user, self.core],
760
['mysite', '-site', '+user'])
761
self.check_path(['mysite', self.core],
764
def test_override_core(self):
765
self.check_path(['mycore', self.user, self.site],
766
['mycore', '-core', '+user', '+site'])
767
self.check_path(['mycore', self.site],
770
def test_my_plugin_only(self):
771
self.check_path(['myplugin'], ['myplugin', '-user', '-core', '-site'])
773
def test_my_plugin_first(self):
774
self.check_path(['myplugin', self.core, self.site, self.user],
775
['myplugin', '+core', '+site', '+user'])
777
def test_bogus_references(self):
778
self.check_path(['+foo', '-bar', self.core, self.site],
782
class TestDisablePlugin(BaseTestPlugins):
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')
792
def test_cannot_import(self):
793
self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
794
plugin.set_plugins_path(['.'])
796
import bzrlib.plugins.test_foo
799
self.assertPluginUnknown('test_foo')
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
bzrlib.plugins.test_foo)
808
def test_not_loaded(self):
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)
824
class TestLoadPluginAtSyntax(tests.TestCase):
826
def _get_paths(self, paths):
827
return plugin._get_specific_plugin_paths(paths)
829
def test_empty(self):
830
self.assertEqual([], self._get_paths(None))
831
self.assertEqual([], self._get_paths(''))
833
def test_one_path(self):
834
self.assertEqual([('b', 'man')], self._get_paths('b@man'))
836
def test_bogus_path(self):
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']))
847
class TestLoadPluginAt(BaseTestPlugins):
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)
855
# Create the same plugin in two directories
856
self.create_plugin_package('test_foo', dir='non-standard-dir')
857
# The "normal" directory, we use 'standard' instead of 'plugins' to
858
# avoid depending on the precise naming.
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
864
self.addCleanup(plugin.PluginImporter.reset)
866
def assertTestFooLoadedFrom(self, path):
867
self.assertPluginKnown('test_foo')
868
self.assertDocstring('This is the doc for test_foo',
869
bzrlib.plugins.test_foo)
870
self.assertEqual(path, bzrlib.plugins.test_foo.dir_source)
872
def test_regular_load(self):
873
plugin.load_plugins(['standard'])
874
self.assertTestFooLoadedFrom('standard/test_foo')
876
def test_import(self):
877
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
878
plugin.set_plugins_path(['standard'])
880
import bzrlib.plugins.test_foo
883
self.assertTestFooLoadedFrom('non-standard-dir')
885
def test_loading(self):
886
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
887
plugin.load_plugins(['standard'])
888
self.assertTestFooLoadedFrom('non-standard-dir')
890
def test_compiled_loaded(self):
891
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
892
plugin.load_plugins(['standard'])
893
self.assertTestFooLoadedFrom('non-standard-dir')
894
self.assertIsSameRealPath('non-standard-dir/__init__.py',
895
bzrlib.plugins.test_foo.__file__)
897
# Try importing again now that the source has been compiled
898
self._unregister_plugin('test_foo')
899
plugin._loaded = False
900
plugin.load_plugins(['standard'])
901
self.assertTestFooLoadedFrom('non-standard-dir')
906
self.assertIsSameRealPath('non-standard-dir/__init__.%s' % suffix,
907
bzrlib.plugins.test_foo.__file__)
909
def test_submodule_loading(self):
910
# We create an additional directory under the one for test_foo
911
self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
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 bzrlib.plugins.test_foo
917
self.assertEqual('bzrlib.plugins.test_foo',
918
bzrlib.plugins.test_foo.__package__)
919
import bzrlib.plugins.test_foo.test_bar
920
self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
921
bzrlib.plugins.test_foo.test_bar.__file__)
923
def test_relative_submodule_loading(self):
924
self.create_plugin_package('test_foo', dir='another-dir', source='''
927
# We create an additional directory under the one for test_foo
928
self.create_plugin_package('test_bar', dir='another-dir/test_bar')
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 bzrlib.plugins.test_foo
934
self.assertEqual('bzrlib.plugins.test_foo',
935
bzrlib.plugins.test_foo.__package__)
936
self.assertIsSameRealPath('another-dir/test_bar/__init__.py',
937
bzrlib.plugins.test_foo.test_bar.__file__)
939
def test_loading_from___init__only(self):
940
# We rename the existing __init__.py file to ensure that we don't load
942
init = 'non-standard-dir/__init__.py'
943
random = 'non-standard-dir/setup.py'
944
os.rename(init, random)
945
self.addCleanup(os.rename, random, init)
946
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
947
plugin.load_plugins(['standard'])
948
self.assertPluginUnknown('test_foo')
950
def test_loading_from_specific_file(self):
951
plugin_dir = 'non-standard-dir'
952
plugin_file_name = 'iamtestfoo.py'
953
plugin_path = osutils.pathjoin(plugin_dir, plugin_file_name)
955
"""This is the doc for %s"""
957
''' % ('test_foo', plugin_path)
958
self.create_plugin('test_foo', source=source,
959
dir=plugin_dir, file_name=plugin_file_name)
960
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
961
plugin.load_plugins(['standard'])
962
self.assertTestFooLoadedFrom(plugin_path)
965
class TestDescribePlugins(BaseTestPlugins):
967
def test_describe_plugins(self):
968
class DummyModule(object):
970
class DummyPlugin(object):
971
__version__ = '0.1.0'
972
module = DummyModule()
974
return { 'good': DummyPlugin() }
975
self.overrideAttr(plugin, 'plugin_warnings',
976
{'bad': ['Failed to load (just testing)']})
977
self.overrideAttr(plugin, 'plugins', dummy_plugins)
978
self.assertEqual("""\
980
** Failed to load (just testing)
985
""", ''.join(plugin.describe_plugins()))