diff --git a/sopel/bot.py b/sopel/bot.py index 5d644b7959..d4b31cef02 100644 --- a/sopel/bot.py +++ b/sopel/bot.py @@ -227,8 +227,7 @@ def setup(self): def reload_plugin(self, name): if name not in self._plugins: - # TODO: raise more specific exception - raise Exception('Plugin %s is not loaded' % name) + raise plugins.PluginNotRegistered(name) plugin = self._plugins[name] # tear down @@ -270,8 +269,7 @@ def remove_plugin(self, plugin, callables, jobs, shutdowns, urls): """Remove a loaded plugin from the bot's registry""" name = plugin.name if name not in self._plugins: - # TODO: raise more specific exception - raise Exception('Plugin %s is not loaded' % name) + raise plugins.PluginNotRegistered(name) try: # remove commands, jobs, and shutdown functions diff --git a/sopel/plugins/__init__.py b/sopel/plugins/__init__.py index 143ec307b4..0facc0522e 100644 --- a/sopel/plugins/__init__.py +++ b/sopel/plugins/__init__.py @@ -31,6 +31,7 @@ import os from . import handlers # noqa +from .exceptions import PluginError, PluginNotFound, PluginNotRegistered # noqa def _list_plugin_filenames(directory): diff --git a/sopel/plugins/exceptions.py b/sopel/plugins/exceptions.py new file mode 100644 index 0000000000..d18110f482 --- /dev/null +++ b/sopel/plugins/exceptions.py @@ -0,0 +1,18 @@ +# coding=utf-8 +from __future__ import unicode_literals, absolute_import, print_function, division + + +class PluginError(Exception): + pass + + +class PluginNotFound(PluginError): + def __init__(self, name): + message = 'Plugin "%s" not found' + super(PluginError, self).__init__(message) + + +class PluginNotRegistered(PluginError): + def __init__(self, name): + message = 'Plugin "%s" not registered' + super(PluginError, self).__init__(message) diff --git a/test/test_bot.py b/test/test_bot.py new file mode 100644 index 0000000000..d19e39f04b --- /dev/null +++ b/test/test_bot.py @@ -0,0 +1,46 @@ +# coding=utf-8 +"""Tests for core ``sopel.bot`` module""" +from __future__ import unicode_literals, absolute_import, print_function, division + +import pytest + +from sopel import bot, config, plugins + + +@pytest.fixture +def tmpconfig(tmpdir): + conf_file = tmpdir.join('conf.ini') + conf_file.write("\n".join([ + "[core]", + "owner=testnick", + "nick = TestBot", + "enable = coretasks" + "" + ])) + return config.Config(conf_file.strpath) + + +def test_remove_plugin_unregistered_plugin(tmpconfig): + sopel = bot.Sopel(tmpconfig, daemon=False) + plugin = sopel._plugins.get('coretasks') + + assert plugin is not None, 'coretasks should be always loaded' + + # Unregister the plugin + plugin.unregister(sopel) + # And now it must raises + with pytest.raises(plugins.PluginNotRegistered): + sopel.remove_plugin(plugin, [], [], [], []) + + +def test_reload_plugin_unregistered_plugin(tmpconfig): + sopel = bot.Sopel(tmpconfig, daemon=False) + plugin = sopel._plugins.get('coretasks') + + assert plugin is not None, 'coretasks should be always loaded' + + # Unregister the plugin + plugin.unregister(sopel) + # And now it must raises + with pytest.raises(plugins.PluginNotRegistered): + sopel.reload_plugin(plugin.name)