20
20
# affects the global state of the process. See bzrlib/plugins.py for more
23
from cStringIO import StringIO
25
from StringIO import StringIO
29
from bzrlib import plugin, tests
32
import bzrlib.commands
34
from bzrlib.tests import (
39
from bzrlib.osutils import pathjoin, abspath, normpath
43
import bzrlib.commands
44
class cmd_myplug(bzrlib.commands.Command):
45
'''Just a simple test plugin.'''
48
print 'Hello from my plugin'
39
51
# TODO: Write a test for plugin decoration of commands.
41
class BaseTestPlugins(tests.TestCaseInTempDir):
43
def create_plugin(self, name, source=None, dir='.', file_name=None):
46
"""This is the doc for %s"""
49
file_name = name + '.py'
50
# 'source' must not fail to load
51
path = osutils.pathjoin(dir, file_name)
53
self.addCleanup(os.unlink, path)
55
f.write(source + '\n')
59
def create_plugin_package(self, name, dir=None, source=None):
64
"""This is the doc for %s"""
69
# Workaround lazy import random? madness
71
self.addCleanup(cleanup)
72
self.create_plugin(name, source, dir,
73
file_name='__init__.py')
75
def _unregister_plugin(self, name):
76
"""Remove the plugin from sys.modules and the bzrlib namespace."""
77
py_name = 'bzrlib.plugins.%s' % name
78
if py_name in sys.modules:
79
del sys.modules[py_name]
80
if getattr(bzrlib.plugins, name, None) is not None:
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)
93
def assertPluginUnknown(self, name):
94
self.assertFalse(getattr(bzrlib.plugins, name, None) is not None)
95
self.assertFalse('bzrlib.plugins.%s' % name in sys.modules)
97
def assertPluginKnown(self, name):
98
self.assertTrue(getattr(bzrlib.plugins, name, None) is not None)
99
self.assertTrue('bzrlib.plugins.%s' % name in sys.modules)
102
class TestLoadingPlugins(BaseTestPlugins):
53
class TestLoadingPlugins(TestCaseInTempDir):
104
55
activeattributes = {}
302
247
"it to 'bad_plugin_name_'\.")
305
class TestPlugins(BaseTestPlugins):
250
class TestPlugins(TestCaseInTempDir):
307
252
def setup_plugin(self, source=""):
308
253
# This test tests a new plugin appears in bzrlib.plugin.plugins().
309
254
# check the plugin is not loaded already
310
self.assertPluginUnknown('plugin')
255
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
311
256
# write a plugin that _cannot_ fail to load.
312
257
file('plugin.py', 'w').write(source + '\n')
313
258
self.addCleanup(self.teardown_plugin)
314
plugin.load_from_path(['.'])
259
bzrlib.plugin.load_from_path(['.'])
316
261
def teardown_plugin(self):
317
self._unregister_plugin('plugin')
318
self.assertPluginUnknown('plugin')
262
# remove the plugin 'plugin'
263
if 'bzrlib.plugins.plugin' in sys.modules:
264
del sys.modules['bzrlib.plugins.plugin']
265
if getattr(bzrlib.plugins, 'plugin', None):
266
del bzrlib.plugins.plugin
267
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
320
269
def test_plugin_appears_in_plugins(self):
321
270
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)
271
self.failUnless('plugin' in bzrlib.plugin.plugins())
272
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
273
plugins = bzrlib.plugin.plugins()
274
plugin = plugins['plugin']
275
self.assertIsInstance(plugin, bzrlib.plugin.PlugIn)
276
self.assertEqual(bzrlib.plugins.plugin, plugin.module)
327
278
def test_trivial_plugin_get_path(self):
328
279
self.setup_plugin()
329
p = plugin.plugins()['plugin']
280
plugins = bzrlib.plugin.plugins()
281
plugin = plugins['plugin']
330
282
plugin_path = self.test_dir + '/plugin.py'
331
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
283
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
333
285
def test_plugin_get_path_py_not_pyc(self):
334
# first import creates plugin.pyc
286
self.setup_plugin() # after first import there will be plugin.pyc
336
287
self.teardown_plugin()
337
plugin.load_from_path(['.']) # import plugin.pyc
338
p = plugin.plugins()['plugin']
288
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
289
plugins = bzrlib.plugin.plugins()
290
plugin = plugins['plugin']
339
291
plugin_path = self.test_dir + '/plugin.py'
340
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
292
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
342
294
def test_plugin_get_path_pyc_only(self):
343
# first import creates plugin.pyc (or plugin.pyo depending on __debug__)
295
self.setup_plugin() # after first import there will be plugin.pyc
345
296
self.teardown_plugin()
346
297
os.unlink(self.test_dir + '/plugin.py')
347
plugin.load_from_path(['.']) # import plugin.pyc (or .pyo)
348
p = plugin.plugins()['plugin']
298
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
299
plugins = bzrlib.plugin.plugins()
300
plugin = plugins['plugin']
350
302
plugin_path = self.test_dir + '/plugin.pyc'
352
304
plugin_path = self.test_dir + '/plugin.pyo'
353
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
305
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
355
307
def test_no_test_suite_gives_None_for_test_suite(self):
356
308
self.setup_plugin()
357
p = plugin.plugins()['plugin']
358
self.assertEqual(None, p.test_suite())
309
plugin = bzrlib.plugin.plugins()['plugin']
310
self.assertEqual(None, plugin.test_suite())
360
312
def test_test_suite_gives_test_suite_result(self):
361
313
source = """def test_suite(): return 'foo'"""
362
314
self.setup_plugin(source)
363
p = plugin.plugins()['plugin']
364
self.assertEqual('foo', p.test_suite())
315
plugin = bzrlib.plugin.plugins()['plugin']
316
self.assertEqual('foo', plugin.test_suite())
366
318
def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
367
319
self.setup_plugin()
368
loader = tests.TestUtil.TestLoader()
369
p = plugin.plugins()['plugin']
370
self.assertEqual(None, p.load_plugin_tests(loader))
320
loader = TestUtil.TestLoader()
321
plugin = bzrlib.plugin.plugins()['plugin']
322
self.assertEqual(None, plugin.load_plugin_tests(loader))
372
324
def test_load_plugin_tests_gives_load_plugin_tests_result(self):
374
326
def load_tests(standard_tests, module, loader):
376
328
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())
329
loader = TestUtil.TestLoader()
330
plugin = bzrlib.plugin.plugins()['plugin']
331
self.assertEqual('foo', plugin.load_plugin_tests(loader))
385
333
def test_no_version_info(self):
386
self.check_version_info(None)
335
plugin = bzrlib.plugin.plugins()['plugin']
336
self.assertEqual(None, plugin.version_info())
388
338
def test_with_version_info(self):
389
self.check_version_info((1, 2, 3, 'dev', 4),
390
"version_info = (1, 2, 3, 'dev', 4)")
339
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
340
plugin = bzrlib.plugin.plugins()['plugin']
341
self.assertEqual((1, 2, 3, 'dev', 4), plugin.version_info())
392
343
def test_short_version_info_gets_padded(self):
393
344
# 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__)
346
self.setup_plugin("version_info = (1, 2, 3)")
347
plugin = bzrlib.plugin.plugins()['plugin']
348
self.assertEqual((1, 2, 3, 'final', 0), plugin.version_info())
402
350
def test_no_version_info___version__(self):
403
351
self.setup_plugin()
679
663
self.assertEqual(bzrlib.plugins.__path__, ['.'])
681
665
def test_load_plugins_default(self):
682
667
plugin.load_plugins()
683
668
path = plugin.get_standard_plugins_path()
684
669
self.assertEqual(path, bzrlib.plugins.__path__)
687
class TestEnvPluginPath(tests.TestCase):
690
super(TestEnvPluginPath, self).setUp()
691
self.overrideAttr(plugin, 'DEFAULT_PLUGIN_PATH', None)
693
self.user = plugin.get_user_plugin_path()
694
self.site = plugin.get_site_plugin_path()
695
self.core = plugin.get_core_plugin_path()
697
def _list2paths(self, *args):
700
plugin._append_new_path(paths, p)
703
def _set_path(self, *args):
704
path = os.pathsep.join(self._list2paths(*args))
705
self.overrideEnv('BZR_PLUGIN_PATH', path)
707
def check_path(self, expected_dirs, setting_dirs):
709
self._set_path(*setting_dirs)
710
actual = plugin.get_standard_plugins_path()
711
self.assertEquals(self._list2paths(*expected_dirs), actual)
713
def test_default(self):
714
self.check_path([self.user, self.core, self.site],
717
def test_adhoc_policy(self):
718
self.check_path([self.user, self.core, self.site],
719
['+user', '+core', '+site'])
721
def test_fallback_policy(self):
722
self.check_path([self.core, self.site, self.user],
723
['+core', '+site', '+user'])
725
def test_override_policy(self):
726
self.check_path([self.user, self.site, self.core],
727
['+user', '+site', '+core'])
729
def test_disable_user(self):
730
self.check_path([self.core, self.site], ['-user'])
732
def test_disable_user_twice(self):
733
# Ensures multiple removals don't left cruft
734
self.check_path([self.core, self.site], ['-user', '-user'])
736
def test_duplicates_are_removed(self):
737
self.check_path([self.user, self.core, self.site],
739
# And only the first reference is kept (since the later references will
740
# only produce '<plugin> already loaded' mutters)
741
self.check_path([self.user, self.core, self.site],
742
['+user', '+user', '+core',
743
'+user', '+site', '+site',
746
def test_disable_overrides_enable(self):
747
self.check_path([self.core, self.site], ['-user', '+user'])
749
def test_disable_core(self):
750
self.check_path([self.site], ['-core'])
751
self.check_path([self.user, self.site], ['+user', '-core'])
753
def test_disable_site(self):
754
self.check_path([self.core], ['-site'])
755
self.check_path([self.user, self.core], ['-site', '+user'])
757
def test_override_site(self):
758
self.check_path(['mysite', self.user, self.core],
759
['mysite', '-site', '+user'])
760
self.check_path(['mysite', self.core],
763
def test_override_core(self):
764
self.check_path(['mycore', self.user, self.site],
765
['mycore', '-core', '+user', '+site'])
766
self.check_path(['mycore', self.site],
769
def test_my_plugin_only(self):
770
self.check_path(['myplugin'], ['myplugin', '-user', '-core', '-site'])
772
def test_my_plugin_first(self):
773
self.check_path(['myplugin', self.core, self.site, self.user],
774
['myplugin', '+core', '+site', '+user'])
776
def test_bogus_references(self):
777
self.check_path(['+foo', '-bar', self.core, self.site],
781
class TestDisablePlugin(BaseTestPlugins):
784
super(TestDisablePlugin, self).setUp()
785
self.create_plugin_package('test_foo')
786
# Make sure we don't pollute the plugins namespace
787
self.overrideAttr(plugins, '__path__')
788
# Be paranoid in case a test fail
789
self.addCleanup(self._unregister_plugin, 'test_foo')
791
def test_cannot_import(self):
792
self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
793
plugin.set_plugins_path(['.'])
795
import bzrlib.plugins.test_foo
798
self.assertPluginUnknown('test_foo')
800
def test_regular_load(self):
801
self.overrideAttr(plugin, '_loaded', False)
802
plugin.load_plugins(['.'])
803
self.assertPluginKnown('test_foo')
804
self.assertDocstring("This is the doc for test_foo",
805
bzrlib.plugins.test_foo)
807
def test_not_loaded(self):
809
def captured_warning(*args, **kwargs):
810
self.warnings.append((args, kwargs))
811
self.overrideAttr(trace, 'warning', captured_warning)
812
# Reset the flag that protect against double loading
813
self.overrideAttr(plugin, '_loaded', False)
814
self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
815
plugin.load_plugins(['.'])
816
self.assertPluginUnknown('test_foo')
817
# Make sure we don't warn about the plugin ImportError since this has
818
# been *requested* by the user.
819
self.assertLength(0, self.warnings)
823
class TestLoadPluginAtSyntax(tests.TestCase):
825
def _get_paths(self, paths):
826
return plugin._get_specific_plugin_paths(paths)
828
def test_empty(self):
829
self.assertEquals([], self._get_paths(None))
830
self.assertEquals([], self._get_paths(''))
832
def test_one_path(self):
833
self.assertEquals([('b', 'man')], self._get_paths('b@man'))
835
def test_bogus_path(self):
837
self.assertRaises(errors.BzrCommandError, self._get_paths, 'batman')
838
# Too much '@' isn't good either
839
self.assertRaises(errors.BzrCommandError, self._get_paths,
840
'batman@mobile@cave')
841
# An empty description probably indicates a problem
842
self.assertRaises(errors.BzrCommandError, self._get_paths,
843
os.pathsep.join(['batman@cave', '', 'robin@mobile']))
846
class TestLoadPluginAt(BaseTestPlugins):
849
super(TestLoadPluginAt, self).setUp()
850
# Make sure we don't pollute the plugins namespace
851
self.overrideAttr(plugins, '__path__')
852
# Reset the flag that protect against double loading
853
self.overrideAttr(plugin, '_loaded', False)
854
# Create the same plugin in two directories
855
self.create_plugin_package('test_foo', dir='non-standard-dir')
856
# The "normal" directory, we use 'standard' instead of 'plugins' to
857
# avoid depending on the precise naming.
858
self.create_plugin_package('test_foo', dir='standard/test_foo')
859
# All the tests will load the 'test_foo' plugin from various locations
860
self.addCleanup(self._unregister_plugin, 'test_foo')
861
# Unfortunately there's global cached state for the specific
863
self.addCleanup(plugin.PluginImporter.reset)
865
def assertTestFooLoadedFrom(self, path):
866
self.assertPluginKnown('test_foo')
867
self.assertDocstring('This is the doc for test_foo',
868
bzrlib.plugins.test_foo)
869
self.assertEqual(path, bzrlib.plugins.test_foo.dir_source)
871
def test_regular_load(self):
872
plugin.load_plugins(['standard'])
873
self.assertTestFooLoadedFrom('standard/test_foo')
875
def test_import(self):
876
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
877
plugin.set_plugins_path(['standard'])
879
import bzrlib.plugins.test_foo
882
self.assertTestFooLoadedFrom('non-standard-dir')
884
def test_loading(self):
885
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
886
plugin.load_plugins(['standard'])
887
self.assertTestFooLoadedFrom('non-standard-dir')
889
def test_compiled_loaded(self):
890
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
891
plugin.load_plugins(['standard'])
892
self.assertTestFooLoadedFrom('non-standard-dir')
893
self.assertIsSameRealPath('non-standard-dir/__init__.py',
894
bzrlib.plugins.test_foo.__file__)
896
# Try importing again now that the source has been compiled
897
self._unregister_plugin('test_foo')
898
plugin._loaded = False
899
plugin.load_plugins(['standard'])
900
self.assertTestFooLoadedFrom('non-standard-dir')
905
self.assertIsSameRealPath('non-standard-dir/__init__.%s' % suffix,
906
bzrlib.plugins.test_foo.__file__)
908
def test_submodule_loading(self):
909
# We create an additional directory under the one for test_foo
910
self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
911
self.addCleanup(self._unregister_plugin_submodule,
912
'test_foo', 'test_bar')
913
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
914
plugin.set_plugins_path(['standard'])
915
import bzrlib.plugins.test_foo
916
self.assertEqual('bzrlib.plugins.test_foo',
917
bzrlib.plugins.test_foo.__package__)
918
import bzrlib.plugins.test_foo.test_bar
919
self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
920
bzrlib.plugins.test_foo.test_bar.__file__)
922
def test_relative_submodule_loading(self):
923
self.create_plugin_package('test_foo', dir='another-dir', source='''
926
# We create an additional directory under the one for test_foo
927
self.create_plugin_package('test_bar', dir='another-dir/test_bar')
928
self.addCleanup(self._unregister_plugin_submodule,
929
'test_foo', 'test_bar')
930
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@another-dir')
931
plugin.set_plugins_path(['standard'])
932
import bzrlib.plugins.test_foo
933
self.assertEqual('bzrlib.plugins.test_foo',
934
bzrlib.plugins.test_foo.__package__)
935
self.assertIsSameRealPath('another-dir/test_bar/__init__.py',
936
bzrlib.plugins.test_foo.test_bar.__file__)
938
def test_loading_from___init__only(self):
939
# We rename the existing __init__.py file to ensure that we don't load
941
init = 'non-standard-dir/__init__.py'
942
random = 'non-standard-dir/setup.py'
943
os.rename(init, random)
944
self.addCleanup(os.rename, random, init)
945
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
946
plugin.load_plugins(['standard'])
947
self.assertPluginUnknown('test_foo')
949
def test_loading_from_specific_file(self):
950
plugin_dir = 'non-standard-dir'
951
plugin_file_name = 'iamtestfoo.py'
952
plugin_path = osutils.pathjoin(plugin_dir, plugin_file_name)
954
"""This is the doc for %s"""
956
''' % ('test_foo', plugin_path)
957
self.create_plugin('test_foo', source=source,
958
dir=plugin_dir, file_name=plugin_file_name)
959
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
960
plugin.load_plugins(['standard'])
961
self.assertTestFooLoadedFrom(plugin_path)
964
class TestDescribePlugins(BaseTestPlugins):
966
def test_describe_plugins(self):
967
class DummyModule(object):
969
class DummyPlugin(object):
970
__version__ = '0.1.0'
971
module = DummyModule()
973
return { 'good': DummyPlugin() }
974
self.overrideAttr(plugin, 'plugin_warnings',
975
{'bad': ['Failed to load (just testing)']})
976
self.overrideAttr(plugin, 'plugins', dummy_plugins)
977
self.assertEquals("""\
979
** Failed to load (just testing)
984
""", ''.join(plugin.describe_plugins()))