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
29
from bzrlib import (
36
import bzrlib.commands
38
from bzrlib.tests import (
43
from bzrlib.osutils import pathjoin, abspath, normpath
47
import bzrlib.commands
48
class cmd_myplug(bzrlib.commands.Command):
49
'''Just a simple test plugin.'''
52
print 'Hello from my plugin'
39
55
# TODO: Write a test for plugin decoration of commands.
41
class TestPluginMixin(object):
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.failIf(getattr(bzrlib.plugins, name, None) is not None)
95
self.failIf('bzrlib.plugins.%s' % name in sys.modules)
97
def assertPluginKnown(self, name):
98
self.failUnless(getattr(bzrlib.plugins, name, None) is not None)
99
self.failUnless('bzrlib.plugins.%s' % name in sys.modules)
102
class TestLoadingPlugins(tests.TestCaseInTempDir, TestPluginMixin):
57
class TestLoadingPlugins(TestCaseInTempDir):
104
59
activeattributes = {}
292
251
"it to 'bad_plugin_name_'\.")
295
class TestPlugins(tests.TestCaseInTempDir, TestPluginMixin):
254
class TestPlugins(TestCaseInTempDir):
297
256
def setup_plugin(self, source=""):
298
257
# This test tests a new plugin appears in bzrlib.plugin.plugins().
299
258
# check the plugin is not loaded already
300
self.assertPluginUnknown('plugin')
259
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
301
260
# write a plugin that _cannot_ fail to load.
302
261
file('plugin.py', 'w').write(source + '\n')
303
262
self.addCleanup(self.teardown_plugin)
304
plugin.load_from_path(['.'])
263
bzrlib.plugin.load_from_path(['.'])
306
265
def teardown_plugin(self):
307
self._unregister_plugin('plugin')
308
self.assertPluginUnknown('plugin')
266
# remove the plugin 'plugin'
267
if 'bzrlib.plugins.plugin' in sys.modules:
268
del sys.modules['bzrlib.plugins.plugin']
269
if getattr(bzrlib.plugins, 'plugin', None):
270
del bzrlib.plugins.plugin
271
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
310
273
def test_plugin_appears_in_plugins(self):
311
274
self.setup_plugin()
312
self.assertPluginKnown('plugin')
313
p = plugin.plugins()['plugin']
314
self.assertIsInstance(p, bzrlib.plugin.PlugIn)
315
self.assertEqual(p.module, plugins.plugin)
275
self.failUnless('plugin' in bzrlib.plugin.plugins())
276
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
277
plugins = bzrlib.plugin.plugins()
278
plugin = plugins['plugin']
279
self.assertIsInstance(plugin, bzrlib.plugin.PlugIn)
280
self.assertEqual(bzrlib.plugins.plugin, plugin.module)
317
282
def test_trivial_plugin_get_path(self):
318
283
self.setup_plugin()
319
p = plugin.plugins()['plugin']
284
plugins = bzrlib.plugin.plugins()
285
plugin = plugins['plugin']
320
286
plugin_path = self.test_dir + '/plugin.py'
321
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
287
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
323
289
def test_plugin_get_path_py_not_pyc(self):
324
# first import creates plugin.pyc
290
self.setup_plugin() # after first import there will be plugin.pyc
326
291
self.teardown_plugin()
327
plugin.load_from_path(['.']) # import plugin.pyc
328
p = plugin.plugins()['plugin']
292
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
293
plugins = bzrlib.plugin.plugins()
294
plugin = plugins['plugin']
329
295
plugin_path = self.test_dir + '/plugin.py'
330
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
296
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
332
298
def test_plugin_get_path_pyc_only(self):
333
# first import creates plugin.pyc (or plugin.pyo depending on __debug__)
299
self.setup_plugin() # after first import there will be plugin.pyc
335
300
self.teardown_plugin()
336
301
os.unlink(self.test_dir + '/plugin.py')
337
plugin.load_from_path(['.']) # import plugin.pyc (or .pyo)
338
p = plugin.plugins()['plugin']
302
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
303
plugins = bzrlib.plugin.plugins()
304
plugin = plugins['plugin']
340
306
plugin_path = self.test_dir + '/plugin.pyc'
342
308
plugin_path = self.test_dir + '/plugin.pyo'
343
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
309
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
345
311
def test_no_test_suite_gives_None_for_test_suite(self):
346
312
self.setup_plugin()
347
p = plugin.plugins()['plugin']
348
self.assertEqual(None, p.test_suite())
313
plugin = bzrlib.plugin.plugins()['plugin']
314
self.assertEqual(None, plugin.test_suite())
350
316
def test_test_suite_gives_test_suite_result(self):
351
317
source = """def test_suite(): return 'foo'"""
352
318
self.setup_plugin(source)
353
p = plugin.plugins()['plugin']
354
self.assertEqual('foo', p.test_suite())
319
plugin = bzrlib.plugin.plugins()['plugin']
320
self.assertEqual('foo', plugin.test_suite())
356
322
def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
357
323
self.setup_plugin()
358
loader = tests.TestUtil.TestLoader()
359
p = plugin.plugins()['plugin']
360
self.assertEqual(None, p.load_plugin_tests(loader))
324
loader = TestUtil.TestLoader()
325
plugin = bzrlib.plugin.plugins()['plugin']
326
self.assertEqual(None, plugin.load_plugin_tests(loader))
362
328
def test_load_plugin_tests_gives_load_plugin_tests_result(self):
364
330
def load_tests(standard_tests, module, loader):
366
332
self.setup_plugin(source)
367
loader = tests.TestUtil.TestLoader()
368
p = plugin.plugins()['plugin']
369
self.assertEqual('foo', p.load_plugin_tests(loader))
371
def check_version_info(self, expected, source='', name='plugin'):
372
self.setup_plugin(source)
373
self.assertEqual(expected, plugin.plugins()[name].version_info())
333
loader = TestUtil.TestLoader()
334
plugin = bzrlib.plugin.plugins()['plugin']
335
self.assertEqual('foo', plugin.load_plugin_tests(loader))
375
337
def test_no_version_info(self):
376
self.check_version_info(None)
339
plugin = bzrlib.plugin.plugins()['plugin']
340
self.assertEqual(None, plugin.version_info())
378
342
def test_with_version_info(self):
379
self.check_version_info((1, 2, 3, 'dev', 4),
380
"version_info = (1, 2, 3, 'dev', 4)")
343
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
344
plugin = bzrlib.plugin.plugins()['plugin']
345
self.assertEqual((1, 2, 3, 'dev', 4), plugin.version_info())
382
347
def test_short_version_info_gets_padded(self):
383
348
# the gtk plugin has version_info = (1,2,3) rather than the 5-tuple.
385
self.check_version_info((1, 2, 3, 'final', 0),
386
"version_info = (1, 2, 3)")
388
def check_version(self, expected, source=None, name='plugin'):
389
self.setup_plugin(source)
390
self.assertEqual(expected, plugins[name].__version__)
350
self.setup_plugin("version_info = (1, 2, 3)")
351
plugin = bzrlib.plugin.plugins()['plugin']
352
self.assertEqual((1, 2, 3, 'final', 0), plugin.version_info())
392
354
def test_no_version_info___version__(self):
393
355
self.setup_plugin()
766
735
def test_bogus_references(self):
767
736
self.check_path(['+foo', '-bar', self.core, self.site],
768
737
['+foo', '-bar'])
771
class TestDisablePlugin(tests.TestCaseInTempDir, TestPluginMixin):
774
super(TestDisablePlugin, self).setUp()
775
self.create_plugin_package('test_foo')
776
# Make sure we don't pollute the plugins namespace
777
self.overrideAttr(plugins, '__path__')
778
# Be paranoid in case a test fail
779
self.addCleanup(self._unregister_plugin, 'test_foo')
781
def test_cannot_import(self):
782
osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
783
plugin.set_plugins_path(['.'])
785
import bzrlib.plugins.test_foo
788
self.assertPluginUnknown('test_foo')
790
def test_regular_load(self):
791
self.overrideAttr(plugin, '_loaded', False)
792
plugin.load_plugins(['.'])
793
self.assertPluginKnown('test_foo')
794
self.assertDocstring("This is the doc for test_foo",
795
bzrlib.plugins.test_foo)
797
def test_not_loaded(self):
799
def captured_warning(*args, **kwargs):
800
self.warnings.append((args, kwargs))
801
self.overrideAttr(trace, 'warning', captured_warning)
802
# Reset the flag that protect against double loading
803
self.overrideAttr(plugin, '_loaded', False)
804
osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
805
plugin.load_plugins(['.'])
806
self.assertPluginUnknown('test_foo')
807
# Make sure we don't warn about the plugin ImportError since this has
808
# been *requested* by the user.
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']))
835
class TestLoadPluginAt(tests.TestCaseInTempDir, TestPluginMixin):
838
super(TestLoadPluginAt, self).setUp()
839
# Make sure we don't pollute the plugins namespace
840
self.overrideAttr(plugins, '__path__')
841
# Reset the flag that protect against double loading
842
self.overrideAttr(plugin, '_loaded', False)
843
# Create the same plugin in two directories
844
self.create_plugin_package('test_foo', dir='non-standard-dir')
845
# The "normal" directory, we use 'standard' instead of 'plugins' to
846
# avoid depending on the precise naming.
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')
851
def assertTestFooLoadedFrom(self, path):
852
self.assertPluginKnown('test_foo')
853
self.assertDocstring('This is the doc for test_foo',
854
bzrlib.plugins.test_foo)
855
self.assertEqual(path, bzrlib.plugins.test_foo.dir_source)
857
def test_regular_load(self):
858
plugin.load_plugins(['standard'])
859
self.assertTestFooLoadedFrom('standard/test_foo')
861
def test_import(self):
862
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
863
plugin.set_plugins_path(['standard'])
865
import bzrlib.plugins.test_foo
868
self.assertTestFooLoadedFrom('non-standard-dir')
870
def test_loading(self):
871
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
872
plugin.load_plugins(['standard'])
873
self.assertTestFooLoadedFrom('non-standard-dir')
875
def test_compiled_loaded(self):
876
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
877
plugin.load_plugins(['standard'])
878
self.assertTestFooLoadedFrom('non-standard-dir')
879
self.assertIsSameRealPath('non-standard-dir/__init__.py',
880
bzrlib.plugins.test_foo.__file__)
882
# Try importing again now that the source has been compiled
883
self._unregister_plugin('test_foo')
884
plugin._loaded = False
885
plugin.load_plugins(['standard'])
886
self.assertTestFooLoadedFrom('non-standard-dir')
891
self.assertIsSameRealPath('non-standard-dir/__init__.%s' % suffix,
892
bzrlib.plugins.test_foo.__file__)
894
def test_submodule_loading(self):
895
# We create an additional directory under the one for test_foo
896
self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
897
self.addCleanup(self._unregister_plugin_submodule,
898
'test_foo', 'test_bar')
899
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
900
plugin.set_plugins_path(['standard'])
901
import bzrlib.plugins.test_foo
902
self.assertEqual('bzrlib.plugins.test_foo',
903
bzrlib.plugins.test_foo.__package__)
904
import bzrlib.plugins.test_foo.test_bar
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
osutils.set_or_unset_env('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__)
924
def test_loading_from___init__only(self):
925
# We rename the existing __init__.py file to ensure that we don't load
927
init = 'non-standard-dir/__init__.py'
928
random = 'non-standard-dir/setup.py'
929
os.rename(init, random)
930
self.addCleanup(os.rename, random, init)
931
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
932
plugin.load_plugins(['standard'])
933
self.assertPluginUnknown('test_foo')
935
def test_loading_from_specific_file(self):
936
plugin_dir = 'non-standard-dir'
937
plugin_file_name = 'iamtestfoo.py'
938
plugin_path = osutils.pathjoin(plugin_dir, plugin_file_name)
940
"""This is the doc for %s"""
942
''' % ('test_foo', plugin_path)
943
self.create_plugin('test_foo', source=source,
944
dir=plugin_dir, file_name=plugin_file_name)
945
osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
946
plugin.load_plugins(['standard'])
947
self.assertTestFooLoadedFrom(plugin_path)