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'
55
39
# TODO: Write a test for plugin decoration of commands.
57
class TestLoadingPlugins(TestCaseInTempDir):
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):
59
104
activeattributes = {}
153
198
# remove the plugin 'plugin'
154
199
del self.activeattributes[tempattribute]
155
if getattr(bzrlib.plugins, 'pluginone', None):
156
del bzrlib.plugins.pluginone
157
if getattr(bzrlib.plugins, 'plugintwo', None):
158
del bzrlib.plugins.plugintwo
159
self.failIf(getattr(bzrlib.plugins, 'pluginone', None))
160
self.failIf(getattr(bzrlib.plugins, 'plugintwo', None))
200
self._unregister_plugin('pluginone')
201
self._unregister_plugin('plugintwo')
202
self.assertPluginUnknown('pluginone')
203
self.assertPluginUnknown('plugintwo')
162
205
def test_plugins_can_load_from_directory_with_trailing_slash(self):
163
206
# This test tests that a plugin can load from a directory when the
164
207
# directory in the path has a trailing slash.
165
208
# check the plugin is not loaded already
166
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
209
self.assertPluginUnknown('ts_plugin')
167
210
tempattribute = "trailing-slash"
168
self.failIf(tempattribute in self.activeattributes)
211
self.assertFalse(tempattribute in self.activeattributes)
169
212
# set a place for the plugin to record its loading, and at the same
170
213
# time validate that the location the plugin should record to is
171
214
# valid and correct.
172
215
bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
173
216
[tempattribute] = []
174
self.failUnless(tempattribute in self.activeattributes)
217
self.assertTrue(tempattribute in self.activeattributes)
175
218
# create a directory for the plugin
176
219
os.mkdir('plugin_test')
177
220
# write a plugin that will record when its loaded in the
251
302
"it to 'bad_plugin_name_'\.")
254
class TestPlugins(TestCaseInTempDir):
305
class TestPlugins(BaseTestPlugins):
256
307
def setup_plugin(self, source=""):
257
308
# This test tests a new plugin appears in bzrlib.plugin.plugins().
258
309
# check the plugin is not loaded already
259
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
310
self.assertPluginUnknown('plugin')
260
311
# write a plugin that _cannot_ fail to load.
261
file('plugin.py', 'w').write(source + '\n')
312
with file('plugin.py', 'w') as f: f.write(source + '\n')
262
313
self.addCleanup(self.teardown_plugin)
263
bzrlib.plugin.load_from_path(['.'])
314
plugin.load_from_path(['.'])
265
316
def teardown_plugin(self):
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))
317
self._unregister_plugin('plugin')
318
self.assertPluginUnknown('plugin')
273
320
def test_plugin_appears_in_plugins(self):
274
321
self.setup_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)
322
self.assertPluginKnown('plugin')
323
p = plugin.plugins()['plugin']
324
self.assertIsInstance(p, bzrlib.plugin.PlugIn)
325
self.assertEqual(p.module, plugins.plugin)
282
327
def test_trivial_plugin_get_path(self):
283
328
self.setup_plugin()
284
plugins = bzrlib.plugin.plugins()
285
plugin = plugins['plugin']
329
p = plugin.plugins()['plugin']
286
330
plugin_path = self.test_dir + '/plugin.py'
287
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
331
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
289
333
def test_plugin_get_path_py_not_pyc(self):
290
self.setup_plugin() # after first import there will be plugin.pyc
334
# first import creates plugin.pyc
291
336
self.teardown_plugin()
292
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
293
plugins = bzrlib.plugin.plugins()
294
plugin = plugins['plugin']
337
plugin.load_from_path(['.']) # import plugin.pyc
338
p = plugin.plugins()['plugin']
295
339
plugin_path = self.test_dir + '/plugin.py'
296
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
340
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
298
342
def test_plugin_get_path_pyc_only(self):
299
self.setup_plugin() # after first import there will be plugin.pyc
343
# first import creates plugin.pyc (or plugin.pyo depending on __debug__)
300
345
self.teardown_plugin()
301
346
os.unlink(self.test_dir + '/plugin.py')
302
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
303
plugins = bzrlib.plugin.plugins()
304
plugin = plugins['plugin']
347
plugin.load_from_path(['.']) # import plugin.pyc (or .pyo)
348
p = plugin.plugins()['plugin']
306
350
plugin_path = self.test_dir + '/plugin.pyc'
308
352
plugin_path = self.test_dir + '/plugin.pyo'
309
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
353
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
311
355
def test_no_test_suite_gives_None_for_test_suite(self):
312
356
self.setup_plugin()
313
plugin = bzrlib.plugin.plugins()['plugin']
314
self.assertEqual(None, plugin.test_suite())
357
p = plugin.plugins()['plugin']
358
self.assertEqual(None, p.test_suite())
316
360
def test_test_suite_gives_test_suite_result(self):
317
361
source = """def test_suite(): return 'foo'"""
318
362
self.setup_plugin(source)
319
plugin = bzrlib.plugin.plugins()['plugin']
320
self.assertEqual('foo', plugin.test_suite())
363
p = plugin.plugins()['plugin']
364
self.assertEqual('foo', p.test_suite())
322
366
def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
323
367
self.setup_plugin()
324
loader = TestUtil.TestLoader()
325
plugin = bzrlib.plugin.plugins()['plugin']
326
self.assertEqual(None, plugin.load_plugin_tests(loader))
368
loader = tests.TestUtil.TestLoader()
369
p = plugin.plugins()['plugin']
370
self.assertEqual(None, p.load_plugin_tests(loader))
328
372
def test_load_plugin_tests_gives_load_plugin_tests_result(self):
330
374
def load_tests(standard_tests, module, loader):
332
376
self.setup_plugin(source)
333
loader = TestUtil.TestLoader()
334
plugin = bzrlib.plugin.plugins()['plugin']
335
self.assertEqual('foo', plugin.load_plugin_tests(loader))
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())
337
385
def test_no_version_info(self):
339
plugin = bzrlib.plugin.plugins()['plugin']
340
self.assertEqual(None, plugin.version_info())
386
self.check_version_info(None)
342
388
def test_with_version_info(self):
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())
389
self.check_version_info((1, 2, 3, 'dev', 4),
390
"version_info = (1, 2, 3, 'dev', 4)")
347
392
def test_short_version_info_gets_padded(self):
348
393
# the gtk plugin has version_info = (1,2,3) rather than the 5-tuple.
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())
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__)
354
402
def test_no_version_info___version__(self):
355
403
self.setup_plugin()
719
777
def test_bogus_references(self):
720
778
self.check_path(['+foo', '-bar', self.core, self.site],
721
779
['+foo', '-bar'])
782
class TestDisablePlugin(BaseTestPlugins):
785
super(TestDisablePlugin, self).setUp()
786
self.create_plugin_package('test_foo')
787
# Make sure we don't pollute the plugins namespace
788
self.overrideAttr(plugins, '__path__')
789
# Be paranoid in case a test fail
790
self.addCleanup(self._unregister_plugin, 'test_foo')
792
def test_cannot_import(self):
793
self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
794
plugin.set_plugins_path(['.'])
796
import bzrlib.plugins.test_foo
799
self.assertPluginUnknown('test_foo')
801
def test_regular_load(self):
802
self.overrideAttr(plugin, '_loaded', False)
803
plugin.load_plugins(['.'])
804
self.assertPluginKnown('test_foo')
805
self.assertDocstring("This is the doc for test_foo",
806
bzrlib.plugins.test_foo)
808
def test_not_loaded(self):
810
def captured_warning(*args, **kwargs):
811
self.warnings.append((args, kwargs))
812
self.overrideAttr(trace, 'warning', captured_warning)
813
# Reset the flag that protect against double loading
814
self.overrideAttr(plugin, '_loaded', False)
815
self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
816
plugin.load_plugins(['.'])
817
self.assertPluginUnknown('test_foo')
818
# Make sure we don't warn about the plugin ImportError since this has
819
# been *requested* by the user.
820
self.assertLength(0, self.warnings)
824
class TestLoadPluginAtSyntax(tests.TestCase):
826
def _get_paths(self, paths):
827
return plugin._get_specific_plugin_paths(paths)
829
def test_empty(self):
830
self.assertEquals([], self._get_paths(None))
831
self.assertEquals([], self._get_paths(''))
833
def test_one_path(self):
834
self.assertEquals([('b', 'man')], self._get_paths('b@man'))
836
def test_bogus_path(self):
838
self.assertRaises(errors.BzrCommandError, self._get_paths, 'batman')
839
# Too much '@' isn't good either
840
self.assertRaises(errors.BzrCommandError, self._get_paths,
841
'batman@mobile@cave')
842
# An empty description probably indicates a problem
843
self.assertRaises(errors.BzrCommandError, self._get_paths,
844
os.pathsep.join(['batman@cave', '', 'robin@mobile']))
847
class TestLoadPluginAt(BaseTestPlugins):
850
super(TestLoadPluginAt, self).setUp()
851
# Make sure we don't pollute the plugins namespace
852
self.overrideAttr(plugins, '__path__')
853
# Reset the flag that protect against double loading
854
self.overrideAttr(plugin, '_loaded', False)
855
# Create the same plugin in two directories
856
self.create_plugin_package('test_foo', dir='non-standard-dir')
857
# The "normal" directory, we use 'standard' instead of 'plugins' to
858
# avoid depending on the precise naming.
859
self.create_plugin_package('test_foo', dir='standard/test_foo')
860
# All the tests will load the 'test_foo' plugin from various locations
861
self.addCleanup(self._unregister_plugin, 'test_foo')
862
# Unfortunately there's global cached state for the specific
864
self.addCleanup(plugin.PluginImporter.reset)
866
def assertTestFooLoadedFrom(self, path):
867
self.assertPluginKnown('test_foo')
868
self.assertDocstring('This is the doc for test_foo',
869
bzrlib.plugins.test_foo)
870
self.assertEqual(path, bzrlib.plugins.test_foo.dir_source)
872
def test_regular_load(self):
873
plugin.load_plugins(['standard'])
874
self.assertTestFooLoadedFrom('standard/test_foo')
876
def test_import(self):
877
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
878
plugin.set_plugins_path(['standard'])
880
import bzrlib.plugins.test_foo
883
self.assertTestFooLoadedFrom('non-standard-dir')
885
def test_loading(self):
886
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
887
plugin.load_plugins(['standard'])
888
self.assertTestFooLoadedFrom('non-standard-dir')
890
def test_compiled_loaded(self):
891
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
892
plugin.load_plugins(['standard'])
893
self.assertTestFooLoadedFrom('non-standard-dir')
894
self.assertIsSameRealPath('non-standard-dir/__init__.py',
895
bzrlib.plugins.test_foo.__file__)
897
# Try importing again now that the source has been compiled
898
self._unregister_plugin('test_foo')
899
plugin._loaded = False
900
plugin.load_plugins(['standard'])
901
self.assertTestFooLoadedFrom('non-standard-dir')
906
self.assertIsSameRealPath('non-standard-dir/__init__.%s' % suffix,
907
bzrlib.plugins.test_foo.__file__)
909
def test_submodule_loading(self):
910
# We create an additional directory under the one for test_foo
911
self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
912
self.addCleanup(self._unregister_plugin_submodule,
913
'test_foo', 'test_bar')
914
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
915
plugin.set_plugins_path(['standard'])
916
import bzrlib.plugins.test_foo
917
self.assertEqual('bzrlib.plugins.test_foo',
918
bzrlib.plugins.test_foo.__package__)
919
import bzrlib.plugins.test_foo.test_bar
920
self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
921
bzrlib.plugins.test_foo.test_bar.__file__)
923
def test_relative_submodule_loading(self):
924
self.create_plugin_package('test_foo', dir='another-dir', source='''
927
# We create an additional directory under the one for test_foo
928
self.create_plugin_package('test_bar', dir='another-dir/test_bar')
929
self.addCleanup(self._unregister_plugin_submodule,
930
'test_foo', 'test_bar')
931
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@another-dir')
932
plugin.set_plugins_path(['standard'])
933
import bzrlib.plugins.test_foo
934
self.assertEqual('bzrlib.plugins.test_foo',
935
bzrlib.plugins.test_foo.__package__)
936
self.assertIsSameRealPath('another-dir/test_bar/__init__.py',
937
bzrlib.plugins.test_foo.test_bar.__file__)
939
def test_loading_from___init__only(self):
940
# We rename the existing __init__.py file to ensure that we don't load
942
init = 'non-standard-dir/__init__.py'
943
random = 'non-standard-dir/setup.py'
944
os.rename(init, random)
945
self.addCleanup(os.rename, random, init)
946
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
947
plugin.load_plugins(['standard'])
948
self.assertPluginUnknown('test_foo')
950
def test_loading_from_specific_file(self):
951
plugin_dir = 'non-standard-dir'
952
plugin_file_name = 'iamtestfoo.py'
953
plugin_path = osutils.pathjoin(plugin_dir, plugin_file_name)
955
"""This is the doc for %s"""
957
''' % ('test_foo', plugin_path)
958
self.create_plugin('test_foo', source=source,
959
dir=plugin_dir, file_name=plugin_file_name)
960
self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
961
plugin.load_plugins(['standard'])
962
self.assertTestFooLoadedFrom(plugin_path)
965
class TestDescribePlugins(BaseTestPlugins):
967
def test_describe_plugins(self):
968
class DummyModule(object):
970
class DummyPlugin(object):
971
__version__ = '0.1.0'
972
module = DummyModule()
974
return { 'good': DummyPlugin() }
975
self.overrideAttr(plugin, 'plugin_warnings',
976
{'bad': ['Failed to load (just testing)']})
977
self.overrideAttr(plugin, 'plugins', dummy_plugins)
978
self.assertEquals("""\
980
** Failed to load (just testing)
985
""", ''.join(plugin.describe_plugins()))