454
458
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
461
class TestHelpIndex(tests.TestCase):
493
462
"""Tests for the PluginsHelpIndex class."""
597
566
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):
569
class TestLoadFromPath(tests.TestCaseInTempDir):
572
super(TestLoadFromPath, self).setUp()
573
# Save the attributes that we're about to monkey-patch.
574
old_plugins_path = bzrlib.plugins.__path__
575
old_loaded = plugin._loaded
576
old_load_from_path = plugin.load_from_path
579
bzrlib.plugins.__path__ = old_plugins_path
580
plugin._loaded = old_loaded
581
plugin.load_from_path = old_load_from_path
583
self.addCleanup(restore)
585
# Change bzrlib.plugin to think no plugins have been loaded yet.
586
bzrlib.plugins.__path__ = []
587
plugin._loaded = False
589
# Monkey-patch load_from_path to stop it from actually loading anything.
590
def load_from_path(dirs):
592
plugin.load_from_path = load_from_path
621
594
def test_set_plugins_path_with_args(self):
623
595
plugin.set_plugins_path(['a', 'b'])
624
596
self.assertEqual(['a', 'b'], bzrlib.plugins.__path__)
626
598
def test_set_plugins_path_defaults(self):
628
599
plugin.set_plugins_path()
629
600
self.assertEqual(plugin.get_standard_plugins_path(),
630
601
bzrlib.plugins.__path__)
632
603
def test_get_standard_plugins_path(self):
633
604
path = plugin.get_standard_plugins_path()
634
self.assertEqual(plugin.get_default_plugin_path(), path[0])
635
605
for directory in path:
636
606
self.assertNotContainsRe(directory, r'\\/$')
663
631
self.assertEqual(bzrlib.plugins.__path__, ['.'])
665
633
def test_load_plugins_default(self):
667
634
plugin.load_plugins()
668
635
path = plugin.get_standard_plugins_path()
669
636
self.assertEqual(path, bzrlib.plugins.__path__)
639
class TestEnvPluginPath(tests.TestCaseInTempDir):
642
super(TestEnvPluginPath, self).setUp()
643
old_default = plugin.DEFAULT_PLUGIN_PATH
646
plugin.DEFAULT_PLUGIN_PATH = old_default
648
self.addCleanup(restore)
650
plugin.DEFAULT_PLUGIN_PATH = None
652
self.user = plugin.get_user_plugin_path()
653
self.site = plugin.get_site_plugin_path()
654
self.core = plugin.get_core_plugin_path()
656
def _list2paths(self, *args):
659
plugin._append_new_path(paths, p)
662
def _set_path(self, *args):
663
path = os.pathsep.join(self._list2paths(*args))
664
osutils.set_or_unset_env('BZR_PLUGIN_PATH', path)
666
def check_path(self, expected_dirs, setting_dirs):
668
self._set_path(*setting_dirs)
669
actual = plugin.get_standard_plugins_path()
670
self.assertEquals(self._list2paths(*expected_dirs), actual)
672
def test_default(self):
673
self.check_path([self.user, self.core, self.site],
676
def test_adhoc_policy(self):
677
self.check_path([self.user, self.core, self.site],
678
['+user', '+core', '+site'])
680
def test_fallback_policy(self):
681
self.check_path([self.core, self.site, self.user],
682
['+core', '+site', '+user'])
684
def test_override_policy(self):
685
self.check_path([self.user, self.site, self.core],
686
['+user', '+site', '+core'])
688
def test_disable_user(self):
689
self.check_path([self.core, self.site], ['-user'])
691
def test_disable_user_twice(self):
692
# Ensures multiple removals don't left cruft
693
self.check_path([self.core, self.site], ['-user', '-user'])
695
def test_duplicates_are_removed(self):
696
self.check_path([self.user, self.core, self.site],
698
# And only the first reference is kept (since the later references will
699
# onnly produce <plugin> already loaded mutters)
700
self.check_path([self.user, self.core, self.site],
701
['+user', '+user', '+core',
702
'+user', '+site', '+site',
705
def test_disable_overrides_disable(self):
706
self.check_path([self.core, self.site], ['-user', '+user'])
708
def test_disable_core(self):
709
self.check_path([self.site], ['-core'])
710
self.check_path([self.user, self.site], ['+user', '-core'])
712
def test_disable_site(self):
713
self.check_path([self.core], ['-site'])
714
self.check_path([self.user, self.core], ['-site', '+user'])
716
def test_override_site(self):
717
self.check_path(['mysite', self.user, self.core],
718
['mysite', '-site', '+user'])
719
self.check_path(['mysite', self.core],
722
def test_override_core(self):
723
self.check_path(['mycore', self.user, self.site],
724
['mycore', '-core', '+user', '+site'])
725
self.check_path(['mycore', self.site],
728
def test_my_plugin_only(self):
729
self.check_path(['myplugin'], ['myplugin', '-user', '-core', '-site'])
731
def test_my_plugin_first(self):
732
self.check_path(['myplugin', self.core, self.site, self.user],
733
['myplugin', '+core', '+site', '+user'])
735
def test_bogus_references(self):
736
self.check_path(['+foo', '-bar', self.core, self.site],