Skip to content

Commit

Permalink
DO NOT PUSH - plugins: plugin specific exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Exirel committed Apr 14, 2019
1 parent b491602 commit 93ffdb5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
6 changes: 2 additions & 4 deletions sopel/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions sopel/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import os

from . import handlers # noqa
from .exceptions import PluginError, PluginNotFound, PluginNotRegistered # noqa


def _list_plugin_filenames(directory):
Expand Down
18 changes: 18 additions & 0 deletions sopel/plugins/exceptions.py
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 46 additions & 0 deletions test/test_bot.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 93ffdb5

Please sign in to comment.