Skip to content

Commit

Permalink
Merge pull request #102 from TotallyNotRobots/gonzobot+fix-hook-reg
Browse files Browse the repository at this point in the history
Fix trying to load dynamic objects as hooks
  • Loading branch information
linuxdaemon authored Mar 8, 2020
2 parents 04e1225 + 95619ab commit 200f0c3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix random truncations of search result URLs
- Fix listfacts not listing some facts
- Fix wikipedia summary fetching
- Fix loading modules with dynamic objects at the module scope (#102)
### Removed
- Removed rottentomatoes plugin as the API has been removed
- Removed dig plugin as jsondns is gone
Expand Down
2 changes: 1 addition & 1 deletion cloudbot/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def find_hooks(parent, module):
"""
hooks = defaultdict(list)
for func in module.__dict__.values():
if hasattr(func, HOOK_ATTR):
if hasattr(func, HOOK_ATTR) and not hasattr(func, "_not_" + HOOK_ATTR):
# if it has cloudbot hook
func_hooks = getattr(func, HOOK_ATTR)

Expand Down
51 changes: 49 additions & 2 deletions tests/core_tests/test_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path

import pytest
from mock import patch
from mock import MagicMock, patch

from cloudbot import hook
from cloudbot.plugin import PluginManager
Expand Down Expand Up @@ -31,7 +31,8 @@ def mock_manager(mock_bot):


class MockModule:
pass
def __init__(self, **kwargs):
self.__dict__.update(kwargs)


def test_get_plugin(mock_manager):
Expand Down Expand Up @@ -147,6 +148,52 @@ def test_plugin_load(mock_manager, patch_import_module, patch_import_reload):
assert mock_manager.get_plugin('plugins/test.py').code is newmod


class WeirdObject:
"""
This represents an object that returns a value for any attribute you ask for
"""

def __init__(self, func):
self.func = func

def __getattr__(self, item):
return self.func(self, item)


def _test_weird_obj(patch_import_module, mock_manager, weird_obj):
patch_import_module.return_value = MockModule(some_import=weird_obj)

mock_manager.bot.loop.run_until_complete(
mock_manager.load_plugin('plugins/test.py')
)


def test_plugin_with_objs_none_attr(mock_manager, patch_import_module):
_test_weird_obj(
patch_import_module, mock_manager, WeirdObject(lambda *args: None)
)


def test_plugin_with_objs_mock_attr(mock_manager, patch_import_module):
_test_weird_obj(
patch_import_module, mock_manager, WeirdObject(lambda *args: MagicMock())
)


def test_plugin_with_objs_dict_attr(mock_manager, patch_import_module):
_test_weird_obj(
patch_import_module, mock_manager, WeirdObject(lambda *args: {})
)


def test_plugin_with_objs_full_dict_attr(mock_manager, patch_import_module):
_test_weird_obj(
patch_import_module, mock_manager, WeirdObject(lambda *args: {
'some_thing': object(),
})
)


def test_plugin_load_disabled(mock_manager, patch_import_module, patch_import_reload):
patch_import_module.return_value = MockModule()
mock_manager.bot.config.update({
Expand Down

0 comments on commit 200f0c3

Please sign in to comment.