193
193
del bzrlib.plugins.ts_plugin
194
194
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
196
def load_and_capture(self, name):
197
"""Load plugins from '.' capturing the output.
199
:param name: The name of the plugin.
200
:return: A string with the log from the plugin loading call.
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()
203
201
stream = StringIO()
205
handler = logging.StreamHandler(stream)
206
log = logging.getLogger('bzr')
207
log.addHandler(handler)
210
bzrlib.plugin.load_from_path(['.'])
212
if 'bzrlib.plugins.%s' % name in sys.modules:
213
del sys.modules['bzrlib.plugins.%s' % name]
214
if getattr(bzrlib.plugins, name, None):
215
delattr(bzrlib.plugins, name)
217
# Stop capturing output
220
log.removeHandler(handler)
221
return stream.getvalue()
225
def test_plugin_with_bad_api_version_reports(self):
226
# This plugin asks for bzrlib api version 1.0.0, which is not supported
231
f.write("import bzrlib.api\n"
232
"bzrlib.api.require_any_api(bzrlib, [(1, 0, 0)])\n")
236
log = self.load_and_capture(name)
237
self.assertContainsRe(log,
238
r"It requested API version")
240
def test_plugin_with_bad_name_does_not_load(self):
241
# The file name here invalid for a python module.
242
name = 'bzr-bad plugin-name..py'
243
file(name, 'w').close()
244
log = self.load_and_capture(name)
245
self.assertContainsRe(log,
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(),
246
214
r"Unable to load 'bzr-bad plugin-name\.' in '\.' as a plugin "
247
215
"because the file path isn't a valid module name; try renaming "
248
216
"it to 'bad_plugin_name_'\.")
251
221
class TestPlugins(TestCaseInTempDir):
412
382
self.assertContainsRe(help, '\[myplug\]')
414
384
# unregister command
415
if 'myplug' in bzrlib.commands.plugin_cmds:
416
bzrlib.commands.plugin_cmds.remove('myplug')
385
if bzrlib.commands.plugin_cmds.get('myplug', None):
386
del bzrlib.commands.plugin_cmds['myplug']
417
387
# remove the plugin 'myplug'
418
388
if getattr(bzrlib.plugins, 'myplug', None):
419
389
delattr(bzrlib.plugins, 'myplug')
475
445
os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
477
447
bzrlib.plugin.set_plugins_path()
478
# We expect our nominated paths to have all path-seps removed,
479
# and this is testing only that.
480
expected_path = ['first', 'second']
448
expected_path = ['first', 'second',
449
os.path.dirname(bzrlib.plugins.__file__)]
481
450
self.assertEqual(expected_path,
482
451
bzrlib.plugins.__path__[:len(expected_path)])