30
31
import bzrlib.plugins
31
32
import bzrlib.commands
33
from bzrlib.symbol_versioning import zero_ninetyone
34
from bzrlib.tests import TestCase, TestCaseInTempDir
34
from bzrlib.symbol_versioning import one_three
35
from bzrlib.tests import (
35
40
from bzrlib.osutils import pathjoin, abspath, normpath
188
193
del bzrlib.plugins.ts_plugin
189
194
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
192
class TestAllPlugins(TestCaseInTempDir):
194
def test_plugin_appears_in_all_plugins(self):
195
# This test tests a new plugin appears in bzrlib.plugin.all_plugins().
196
# check the plugin is not loaded already
197
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
198
# write a plugin that _cannot_ fail to load.
199
file('plugin.py', 'w').write("\n")
201
bzrlib.plugin.load_from_path(['.'])
202
all_plugins = self.applyDeprecated(zero_ninetyone,
203
bzrlib.plugin.all_plugins)
204
self.failUnless('plugin' in all_plugins)
205
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
206
self.assertEqual(all_plugins['plugin'], bzrlib.plugins.plugin)
208
# remove the plugin 'plugin'
209
if 'bzrlib.plugins.plugin' in sys.modules:
210
del sys.modules['bzrlib.plugins.plugin']
211
if getattr(bzrlib.plugins, 'plugin', None):
212
del bzrlib.plugins.plugin
213
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
196
def test_plugin_with_bad_name_does_not_load(self):
197
# Create badly-named plugin
198
file('bzr-bad plugin-name..py', 'w').close()
202
handler = logging.StreamHandler(stream)
203
log = logging.getLogger('bzr')
204
log.addHandler(handler)
206
bzrlib.plugin.load_from_dir('.')
208
# Stop capturing output
211
log.removeHandler(handler)
213
self.assertContainsRe(stream.getvalue(),
214
r"Unable to load 'bzr-bad plugin-name\.' in '\.' as a plugin "
215
"because the file path isn't a valid module name; try renaming "
216
"it to 'bad_plugin_name_'\.")
216
221
class TestPlugins(TestCaseInTempDir):
248
253
plugin_path = self.test_dir + '/plugin.py'
249
254
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
256
def test_plugin_get_path_py_not_pyc(self):
257
self.setup_plugin() # after first import there will be plugin.pyc
258
self.teardown_plugin()
259
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
260
plugins = bzrlib.plugin.plugins()
261
plugin = plugins['plugin']
262
plugin_path = self.test_dir + '/plugin.py'
263
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
265
def test_plugin_get_path_pyc_only(self):
266
self.setup_plugin() # after first import there will be plugin.pyc
267
self.teardown_plugin()
268
os.unlink(self.test_dir + '/plugin.py')
269
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
270
plugins = bzrlib.plugin.plugins()
271
plugin = plugins['plugin']
273
plugin_path = self.test_dir + '/plugin.pyc'
275
plugin_path = self.test_dir + '/plugin.pyo'
276
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
251
278
def test_no_test_suite_gives_None_for_test_suite(self):
252
279
self.setup_plugin()
253
280
plugin = bzrlib.plugin.plugins()['plugin']
259
286
plugin = bzrlib.plugin.plugins()['plugin']
260
287
self.assertEqual('foo', plugin.test_suite())
289
def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
291
loader = TestUtil.TestLoader()
292
plugin = bzrlib.plugin.plugins()['plugin']
293
self.assertEqual(None, plugin.load_plugin_tests(loader))
295
def test_load_plugin_tests_gives_load_plugin_tests_result(self):
297
def load_tests(standard_tests, module, loader):
299
self.setup_plugin(source)
300
loader = TestUtil.TestLoader()
301
plugin = bzrlib.plugin.plugins()['plugin']
302
self.assertEqual('foo', plugin.load_plugin_tests(loader))
262
304
def test_no_version_info(self):
263
305
self.setup_plugin()
264
306
plugin = bzrlib.plugin.plugins()['plugin']
362
404
# this is normally done by load_plugins -> set_plugins_path
363
405
bzrlib.plugins.__path__ = [zip_name]
364
bzrlib.plugin.load_from_zip(zip_name)
406
self.applyDeprecated(one_three,
407
bzrlib.plugin.load_from_zip, zip_name)
365
408
self.assertTrue(plugin_name in dir(bzrlib.plugins),
366
409
'Plugin is not loaded')
395
438
def test_set_plugins_path_with_trailing_slashes(self):
396
439
"""set_plugins_path should set the module __path__ based on
440
BZR_PLUGIN_PATH after removing all trailing slashes."""
398
441
old_path = bzrlib.plugins.__path__
399
442
old_env = os.environ.get('BZR_PLUGIN_PATH')
402
445
os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
404
447
bzrlib.plugin.set_plugins_path()
405
expected_path = ['first', 'second',
406
os.path.dirname(bzrlib.plugins.__file__)]
407
self.assertEqual(expected_path, bzrlib.plugins.__path__)
448
# We expect our nominated paths to have all path-seps removed,
449
# and this is testing only that.
450
expected_path = ['first', 'second']
451
self.assertEqual(expected_path,
452
bzrlib.plugins.__path__[:len(expected_path)])
409
454
bzrlib.plugins.__path__ = old_path
455
if old_env is not None:
411
456
os.environ['BZR_PLUGIN_PATH'] = old_env
413
458
del os.environ['BZR_PLUGIN_PATH']
415
461
class TestHelpIndex(tests.TestCase):
416
462
"""Tests for the PluginsHelpIndex class."""