54
def create_plugin_package(self, name, source='', dir='.'):
55
plugin_dir = osutils.pathjoin(dir, name)
57
self.addCleanup(osutils.rmtree, plugin_dir)
58
self.create_plugin(name, source, dir=plugin_dir,
58
def create_plugin_package(self, name, dir=None, source=None):
63
"""This is the doc for %s"""
68
# Workaround lazy import random? madness
70
self.addCleanup(cleanup)
71
self.create_plugin(name, source, dir,
59
72
file_name='__init__.py')
61
74
def _unregister_plugin(self, name):
767
780
self.overrideAttr(plugin, '_loaded', False)
768
781
plugin.load_plugins(['.'])
769
782
self.assertPluginKnown('test_foo')
783
self.assertEqual("This is the doc for test_foo",
784
bzrlib.plugins.test_foo.__doc__)
771
786
def test_not_loaded(self):
772
787
self.warnings = []
773
788
def captured_warning(*args, **kwargs):
774
789
self.warnings.append((args, kwargs))
775
790
self.overrideAttr(trace, 'warning', captured_warning)
791
# Reset the flag that protect against double loading
776
792
self.overrideAttr(plugin, '_loaded', False)
777
793
osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
778
plugin.load_plugins(plugin.set_plugins_path(['.']))
794
plugin.load_plugins(['.'])
779
795
self.assertPluginUnknown('test_foo')
780
796
# Make sure we don't warn about the plugin ImportError since this has
781
797
# been *requested* by the user.
782
798
self.assertLength(0, self.warnings)
801
class TestLoadPluginAt(tests.TestCaseInTempDir, TestPluginMixin):
804
super(TestLoadPluginAt, self).setUp()
805
# Make sure we don't pollute the plugins namespace
806
self.overrideAttr(plugins, '__path__')
807
# Be paranoid in case a test fail
808
self.addCleanup(self._unregister_plugin, 'test_foo')
809
# Reset the flag that protect against double loading
810
self.overrideAttr(plugin, '_loaded', False)
811
# Create the same plugin in two directories
812
self.create_plugin_package('test_foo', dir='non-standard-dir')
813
self.create_plugin_package('test_foo', dir='b/test_foo')
815
def assertTestFooLoadedFrom(self, dir):
816
self.assertPluginKnown('test_foo')
817
self.assertEqual('This is the doc for test_foo',
818
bzrlib.plugins.test_foo.__doc__)
819
self.assertEqual(dir, bzrlib.plugins.test_foo.dir_source)
821
def test_regular_load(self):
822
plugin.load_plugins(['b'])
823
self.assertTestFooLoadedFrom('b/test_foo')
825
def test_import(self):
826
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
827
plugin.set_plugins_path(['b'])
829
import bzrlib.plugins.test_foo
832
self.assertTestFooLoadedFrom('non-standard-dir')
834
def test_loading(self):
835
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
836
plugin.load_plugins(['b'])
837
self.assertTestFooLoadedFrom('non-standard-dir')
839
def test_compiled_loaded(self):
840
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
841
plugin.load_plugins(['b'])
842
self.assertTestFooLoadedFrom('non-standard-dir')
843
self.assertEqual('non-standard-dir/__init__.py',
844
bzrlib.plugins.test_foo.__file__)
846
# Try importing again now that the source has been compiled
847
self._unregister_plugin('test_foo')
848
plugin._loaded = False
849
plugin.load_plugins(['b'])
850
self.assertTestFooLoadedFrom('non-standard-dir')
855
self.assertEqual('non-standard-dir/__init__.%s' % suffix,
856
bzrlib.plugins.test_foo.__file__)
858
def test_submodule_loading(self):
859
# We create an additional directory under the one for test_foo
860
self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
861
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
862
plugin.set_plugins_path(['b'])
863
import bzrlib.plugins.test_foo
864
self.assertEqual('bzrlib.plugins.test_foo',
865
bzrlib.plugins.test_foo.__package__)
866
import bzrlib.plugins.test_foo.test_bar
867
self.assertEqual('non-standard-dir/test_bar/__init__.py',
868
bzrlib.plugins.test_foo.test_bar.__file__)