29
29
from bzrlib import (
36
37
# TODO: Write a test for plugin decoration of commands.
38
class TestLoadingPlugins(tests.TestCaseInTempDir):
39
class TestPluginMixin(object):
41
def _unregister_plugin(self, name):
42
"""Remove the plugin from sys.modules and the bzrlib namespace."""
43
py_name = 'bzrlib.plugins.%s' % name
44
if py_name in sys.modules:
45
del sys.modules[py_name]
46
if getattr(bzrlib.plugins, name, None) is not None:
47
delattr(bzrlib.plugins, name)
49
def assertPluginUnknown(self, name):
50
self.failIf(getattr(bzrlib.plugins, 'plugin', None) is not None)
51
self.failIf('bzrlib.plugins.%s' % name in sys.modules)
53
def assertPluginKnown(self, name):
54
self.failUnless(getattr(bzrlib.plugins, 'plugin', None) is not None)
55
self.failUnless('bzrlib.plugins.%s' % name in sys.modules)
58
class TestLoadingPlugins(tests.TestCaseInTempDir, TestPluginMixin):
40
60
activeattributes = {}
81
100
# remove the plugin 'plugin'
82
101
del self.activeattributes[tempattribute]
83
if 'bzrlib.plugins.plugin' in sys.modules:
84
del sys.modules['bzrlib.plugins.plugin']
85
if getattr(bzrlib.plugins, 'plugin', None):
86
del bzrlib.plugins.plugin
87
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
102
self._unregister_plugin('plugin')
103
self.assertPluginUnknown('plugin')
89
105
def test_plugins_from_different_dirs_can_demand_load(self):
106
self.failIf('bzrlib.plugins.pluginone' in sys.modules)
107
self.failIf('bzrlib.plugins.plugintwo' in sys.modules)
90
108
# This test tests that having two plugins in different
91
109
# directories with different names allows them both to be loaded, when
92
110
# we do a direct import statement.
134
154
# remove the plugin 'plugin'
135
155
del self.activeattributes[tempattribute]
136
if getattr(bzrlib.plugins, 'pluginone', None):
137
del bzrlib.plugins.pluginone
138
if getattr(bzrlib.plugins, 'plugintwo', None):
139
del bzrlib.plugins.plugintwo
140
self.failIf(getattr(bzrlib.plugins, 'pluginone', None))
141
self.failIf(getattr(bzrlib.plugins, 'plugintwo', None))
156
self._unregister_plugin('pluginone')
157
self._unregister_plugin('plugintwo')
158
self.assertPluginUnknown('pluginone')
159
self.assertPluginUnknown('plugintwo')
143
161
def test_plugins_can_load_from_directory_with_trailing_slash(self):
144
162
# This test tests that a plugin can load from a directory when the
145
163
# directory in the path has a trailing slash.
146
164
# check the plugin is not loaded already
147
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
165
self.assertPluginUnknown('ts_plugin')
148
166
tempattribute = "trailing-slash"
149
167
self.failIf(tempattribute in self.activeattributes)
150
168
# set a place for the plugin to record its loading, and at the same
232
248
"it to 'bad_plugin_name_'\.")
235
class TestPlugins(tests.TestCaseInTempDir):
251
class TestPlugins(tests.TestCaseInTempDir, TestPluginMixin):
237
253
def setup_plugin(self, source=""):
238
254
# This test tests a new plugin appears in bzrlib.plugin.plugins().
239
255
# check the plugin is not loaded already
240
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
256
self.assertPluginUnknown('plugin')
241
257
# write a plugin that _cannot_ fail to load.
242
258
file('plugin.py', 'w').write(source + '\n')
243
259
self.addCleanup(self.teardown_plugin)
244
bzrlib.plugin.load_from_path(['.'])
260
plugin.load_from_path(['.'])
246
262
def teardown_plugin(self):
247
# remove the plugin 'plugin'
248
if 'bzrlib.plugins.plugin' in sys.modules:
249
del sys.modules['bzrlib.plugins.plugin']
250
if getattr(bzrlib.plugins, 'plugin', None):
251
del bzrlib.plugins.plugin
252
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
263
self._unregister_plugin('plugin')
264
self.assertPluginUnknown('plugin')
254
266
def test_plugin_appears_in_plugins(self):
255
267
self.setup_plugin()
256
self.failUnless('plugin' in bzrlib.plugin.plugins())
257
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
258
plugins = bzrlib.plugin.plugins()
259
plugin = plugins['plugin']
260
self.assertIsInstance(plugin, bzrlib.plugin.PlugIn)
261
self.assertEqual(bzrlib.plugins.plugin, plugin.module)
268
self.assertPluginKnown('plugin')
269
p = plugin.plugins()['plugin']
270
self.assertIsInstance(p, bzrlib.plugin.PlugIn)
271
self.assertEqual(p.module, plugins.plugin)
263
273
def test_trivial_plugin_get_path(self):
264
274
self.setup_plugin()
265
plugins = bzrlib.plugin.plugins()
266
plugin = plugins['plugin']
275
p = plugin.plugins()['plugin']
267
276
plugin_path = self.test_dir + '/plugin.py'
268
self.assertIsSameRealPath(plugin_path, osutils.normpath(plugin.path()))
277
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
270
279
def test_plugin_get_path_py_not_pyc(self):
271
280
# first import creates plugin.pyc
272
281
self.setup_plugin()
273
282
self.teardown_plugin()
274
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
275
plugins = bzrlib.plugin.plugins()
276
plugin = plugins['plugin']
283
plugin.load_from_path(['.']) # import plugin.pyc
284
p = plugin.plugins()['plugin']
277
285
plugin_path = self.test_dir + '/plugin.py'
278
self.assertIsSameRealPath(plugin_path, osutils.normpath(plugin.path()))
286
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
280
288
def test_plugin_get_path_pyc_only(self):
281
289
# first import creates plugin.pyc (or plugin.pyo depending on __debug__)
282
290
self.setup_plugin()
283
291
self.teardown_plugin()
284
292
os.unlink(self.test_dir + '/plugin.py')
285
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc (or .pyo)
286
plugins = bzrlib.plugin.plugins()
287
plugin = plugins['plugin']
293
plugin.load_from_path(['.']) # import plugin.pyc (or .pyo)
294
p = plugin.plugins()['plugin']
289
296
plugin_path = self.test_dir + '/plugin.pyc'
291
298
plugin_path = self.test_dir + '/plugin.pyo'
292
self.assertIsSameRealPath(plugin_path, osutils.normpath(plugin.path()))
299
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
294
301
def test_no_test_suite_gives_None_for_test_suite(self):
295
302
self.setup_plugin()
296
plugin = bzrlib.plugin.plugins()['plugin']
297
self.assertEqual(None, plugin.test_suite())
303
p = plugin.plugins()['plugin']
304
self.assertEqual(None, p.test_suite())
299
306
def test_test_suite_gives_test_suite_result(self):
300
307
source = """def test_suite(): return 'foo'"""
301
308
self.setup_plugin(source)
302
plugin = bzrlib.plugin.plugins()['plugin']
303
self.assertEqual('foo', plugin.test_suite())
309
p = plugin.plugins()['plugin']
310
self.assertEqual('foo', p.test_suite())
305
312
def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
306
313
self.setup_plugin()
307
314
loader = tests.TestUtil.TestLoader()
308
plugin = bzrlib.plugin.plugins()['plugin']
309
self.assertEqual(None, plugin.load_plugin_tests(loader))
315
p = plugin.plugins()['plugin']
316
self.assertEqual(None, p.load_plugin_tests(loader))
311
318
def test_load_plugin_tests_gives_load_plugin_tests_result(self):
315
322
self.setup_plugin(source)
316
323
loader = tests.TestUtil.TestLoader()
317
plugin = bzrlib.plugin.plugins()['plugin']
318
self.assertEqual('foo', plugin.load_plugin_tests(loader))
324
p = plugin.plugins()['plugin']
325
self.assertEqual('foo', p.load_plugin_tests(loader))
327
def check_version_info(self, expected, source='', name='plugin'):
328
self.setup_plugin(source)
329
self.assertEqual(expected, plugin.plugins()[name].version_info())
320
331
def test_no_version_info(self):
322
plugin = bzrlib.plugin.plugins()['plugin']
323
self.assertEqual(None, plugin.version_info())
332
self.check_version_info(None)
325
334
def test_with_version_info(self):
326
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
327
plugin = bzrlib.plugin.plugins()['plugin']
328
self.assertEqual((1, 2, 3, 'dev', 4), plugin.version_info())
335
self.check_version_info((1, 2, 3, 'dev', 4),
336
"version_info = (1, 2, 3, 'dev', 4)")
330
338
def test_short_version_info_gets_padded(self):
331
339
# the gtk plugin has version_info = (1,2,3) rather than the 5-tuple.
333
self.setup_plugin("version_info = (1, 2, 3)")
334
plugin = bzrlib.plugin.plugins()['plugin']
335
self.assertEqual((1, 2, 3, 'final', 0), plugin.version_info())
341
self.check_version_info((1, 2, 3, 'final', 0),
342
"version_info = (1, 2, 3)")
344
def check_version(self, expected, source=None, name='plugin'):
345
self.setup_plugin(source)
346
self.assertEqual(expected, plugins[name].__version__)
337
348
def test_no_version_info___version__(self):
338
349
self.setup_plugin()