39
40
class TestPluginMixin(object):
42
def create_plugin(self, name, source=None, dir='.', file_name=None):
45
"""This is the doc for %s"""
48
file_name = name + '.py'
49
# 'source' must not fail to load
50
path = osutils.pathjoin(dir, file_name)
52
self.addCleanup(os.unlink, path)
54
f.write(source + '\n')
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,
72
file_name='__init__.py')
41
74
def _unregister_plugin(self, name):
42
75
"""Remove the plugin from sys.modules and the bzrlib namespace."""
43
76
py_name = 'bzrlib.plugins.%s' % name
723
756
self.check_path(['+foo', '-bar', self.core, self.site],
724
757
['+foo', '-bar'])
760
class TestDisablePlugin(tests.TestCaseInTempDir, TestPluginMixin):
763
super(TestDisablePlugin, self).setUp()
764
self.create_plugin_package('test_foo')
765
# Make sure we don't pollute the plugins namespace
766
self.overrideAttr(plugins, '__path__')
767
# Be paranoid in case a test fail
768
self.addCleanup(self._unregister_plugin, 'test_foo')
770
def test_cannot_import(self):
771
osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
772
plugin.set_plugins_path(['.'])
774
import bzrlib.plugins.test_foo
777
self.assertPluginUnknown('test_foo')
779
def test_regular_load(self):
780
self.overrideAttr(plugin, '_loaded', False)
781
plugin.load_plugins(['.'])
782
self.assertPluginKnown('test_foo')
783
self.assertEqual("This is the doc for test_foo",
784
bzrlib.plugins.test_foo.__doc__)
786
def test_not_loaded(self):
788
def captured_warning(*args, **kwargs):
789
self.warnings.append((args, kwargs))
790
self.overrideAttr(trace, 'warning', captured_warning)
791
# Reset the flag that protect against double loading
792
self.overrideAttr(plugin, '_loaded', False)
793
osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
794
plugin.load_plugins(['.'])
795
self.assertPluginUnknown('test_foo')
796
# Make sure we don't warn about the plugin ImportError since this has
797
# been *requested* by the user.
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__)