79
80
if getattr(bzrlib.plugins, name, None) is not None:
80
81
delattr(bzrlib.plugins, name)
83
def _unregister_plugin_submodule(self, plugin_name, submodule_name):
84
"""Remove the submodule from sys.modules and the bzrlib namespace."""
85
py_name = 'bzrlib.plugins.%s.%s' % (plugin_name, submodule_name)
86
if py_name in sys.modules:
87
del sys.modules[py_name]
88
plugin = getattr(bzrlib.plugins, plugin_name, None)
89
if plugin is not None:
90
if getattr(plugin, submodule_name, None) is not None:
91
delattr(plugin, submodule_name)
82
93
def assertPluginUnknown(self, name):
83
94
self.failIf(getattr(bzrlib.plugins, name, None) is not None)
84
95
self.failIf('bzrlib.plugins.%s' % name in sys.modules)
645
656
self.fail('No path to global plugins')
647
658
def test_get_standard_plugins_path_env(self):
648
os.environ['BZR_PLUGIN_PATH'] = 'foo/'
659
self.overrideEnv('BZR_PLUGIN_PATH', 'foo/')
649
660
path = plugin.get_standard_plugins_path()
650
661
for directory in path:
651
662
self.assertNotContainsRe(directory, r'\\/$')
682
693
def _set_path(self, *args):
683
694
path = os.pathsep.join(self._list2paths(*args))
684
osutils.set_or_unset_env('BZR_PLUGIN_PATH', path)
695
self.overrideEnv('BZR_PLUGIN_PATH', path)
686
697
def check_path(self, expected_dirs, setting_dirs):
768
779
self.addCleanup(self._unregister_plugin, 'test_foo')
770
781
def test_cannot_import(self):
771
osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
782
self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
772
783
plugin.set_plugins_path(['.'])
774
785
import bzrlib.plugins.test_foo
790
801
self.overrideAttr(trace, 'warning', captured_warning)
791
802
# Reset the flag that protect against double loading
792
803
self.overrideAttr(plugin, '_loaded', False)
793
osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
804
self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
794
805
plugin.load_plugins(['.'])
795
806
self.assertPluginUnknown('test_foo')
796
807
# Make sure we don't warn about the plugin ImportError since this has
798
809
self.assertLength(0, self.warnings)
812
class TestLoadPluginAtSyntax(tests.TestCase):
814
def _get_paths(self, paths):
815
return plugin._get_specific_plugin_paths(paths)
817
def test_empty(self):
818
self.assertEquals([], self._get_paths(None))
819
self.assertEquals([], self._get_paths(''))
821
def test_one_path(self):
822
self.assertEquals([('b', 'man')], self._get_paths('b@man'))
824
def test_bogus_path(self):
826
self.assertRaises(errors.BzrCommandError, self._get_paths, 'batman')
827
# Too much '@' isn't good either
828
self.assertRaises(errors.BzrCommandError, self._get_paths,
829
'batman@mobile@cave')
830
# An empty description probably indicates a problem
831
self.assertRaises(errors.BzrCommandError, self._get_paths,
832
os.pathsep.join(['batman@cave', '', 'robin@mobile']))
801
835
class TestLoadPluginAt(tests.TestCaseInTempDir, TestPluginMixin):
804
838
super(TestLoadPluginAt, self).setUp()
805
839
# Make sure we don't pollute the plugins namespace
806
840
self.overrideAttr(plugins, '__path__')
807
# Be paranoid in case a test fail
808
self.addCleanup(self._unregister_plugin, 'test_foo')
809
841
# Reset the flag that protect against double loading
810
842
self.overrideAttr(plugin, '_loaded', False)
811
843
# Create the same plugin in two directories
813
845
# The "normal" directory, we use 'standard' instead of 'plugins' to
814
846
# avoid depending on the precise naming.
815
847
self.create_plugin_package('test_foo', dir='standard/test_foo')
848
# All the tests will load the 'test_foo' plugin from various locations
849
self.addCleanup(self._unregister_plugin, 'test_foo')
817
851
def assertTestFooLoadedFrom(self, path):
818
852
self.assertPluginKnown('test_foo')
825
859
self.assertTestFooLoadedFrom('standard/test_foo')
827
861
def test_import(self):
828
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
862
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
829
863
plugin.set_plugins_path(['standard'])
831
865
import bzrlib.plugins.test_foo
834
868
self.assertTestFooLoadedFrom('non-standard-dir')
836
870
def test_loading(self):
837
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
871
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
838
872
plugin.load_plugins(['standard'])
839
873
self.assertTestFooLoadedFrom('non-standard-dir')
841
875
def test_compiled_loaded(self):
842
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
876
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
843
877
plugin.load_plugins(['standard'])
844
878
self.assertTestFooLoadedFrom('non-standard-dir')
845
self.assertEqual('non-standard-dir/__init__.py',
846
bzrlib.plugins.test_foo.__file__)
879
self.assertIsSameRealPath('non-standard-dir/__init__.py',
880
bzrlib.plugins.test_foo.__file__)
848
882
# Try importing again now that the source has been compiled
849
883
self._unregister_plugin('test_foo')
857
self.assertEqual('non-standard-dir/__init__.%s' % suffix,
858
bzrlib.plugins.test_foo.__file__)
891
self.assertIsSameRealPath('non-standard-dir/__init__.%s' % suffix,
892
bzrlib.plugins.test_foo.__file__)
860
894
def test_submodule_loading(self):
861
895
# We create an additional directory under the one for test_foo
862
896
self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
863
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
897
self.addCleanup(self._unregister_plugin_submodule,
898
'test_foo', 'test_bar')
899
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
864
900
plugin.set_plugins_path(['standard'])
865
901
import bzrlib.plugins.test_foo
866
902
self.assertEqual('bzrlib.plugins.test_foo',
867
903
bzrlib.plugins.test_foo.__package__)
868
904
import bzrlib.plugins.test_foo.test_bar
869
self.assertEqual('non-standard-dir/test_bar/__init__.py',
870
bzrlib.plugins.test_foo.test_bar.__file__)
905
self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
906
bzrlib.plugins.test_foo.test_bar.__file__)
908
def test_relative_submodule_loading(self):
909
self.create_plugin_package('test_foo', dir='another-dir', source='''
912
# We create an additional directory under the one for test_foo
913
self.create_plugin_package('test_bar', dir='another-dir/test_bar')
914
self.addCleanup(self._unregister_plugin_submodule,
915
'test_foo', 'test_bar')
916
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@another-dir')
917
plugin.set_plugins_path(['standard'])
918
import bzrlib.plugins.test_foo
919
self.assertEqual('bzrlib.plugins.test_foo',
920
bzrlib.plugins.test_foo.__package__)
921
self.assertIsSameRealPath('another-dir/test_bar/__init__.py',
922
bzrlib.plugins.test_foo.test_bar.__file__)
872
924
def test_loading_from___init__only(self):
873
925
# We rename the existing __init__.py file to ensure that we don't load
876
928
random = 'non-standard-dir/setup.py'
877
929
os.rename(init, random)
878
930
self.addCleanup(os.rename, random, init)
879
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
931
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
880
932
plugin.load_plugins(['standard'])
881
933
self.assertPluginUnknown('test_foo')
890
942
''' % ('test_foo', plugin_path)
891
943
self.create_plugin('test_foo', source=source,
892
944
dir=plugin_dir, file_name=plugin_file_name)
893
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
945
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
894
946
plugin.load_plugins(['standard'])
895
947
self.assertTestFooLoadedFrom(plugin_path)