Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins.handlers: fix some PyFilePlugin logic errors #2633

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sopel/plugins/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def __init__(self, filename):
spec = importlib.util.spec_from_file_location(
name,
os.path.join(filename, '__init__.py'),
submodule_search_locations=filename,
submodule_search_locations=[filename],
)
else:
raise exceptions.PluginError('Invalid Sopel plugin: %s' % filename)
Expand All @@ -487,9 +487,9 @@ def __init__(self, filename):

def _load(self):
module = importlib.util.module_from_spec(self.module_spec)
sys.modules[self.name] = module
if not self.module_spec.loader:
raise exceptions.PluginError('Could not determine loader for plugin: %s' % self.filename)
sys.modules[self.name] = module
self.module_spec.loader.exec_module(module)
return module

Expand Down
42 changes: 42 additions & 0 deletions test/plugins/test_plugins_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,45 @@ def test_get_label_entrypoint(plugin_tmpfile):
assert meta['label'] == 'plugin label'
assert meta['type'] == handlers.EntryPointPlugin.PLUGIN_TYPE
assert meta['source'] == 'test_plugin = file_mod'


MOCK_PARENT_MODULE = """
from sopel import plugin

from .sub import foo


@plugin.command('mock')
def mock(bot, trigger):
bot.say(foo)
"""

MOCK_SUB_MODULE = """
foo = 'bar baz'
"""


@pytest.fixture
def plugin_folder(tmp_path):
root = tmp_path / 'test_folder_plugin'
root.mkdir()

parent = root / '__init__.py'
with open(parent, 'w') as f:
f.write(MOCK_PARENT_MODULE)

submodule = root / 'sub.py'
with open(submodule, 'w') as f:
f.write(MOCK_SUB_MODULE)

return str(root)


def test_folder_plugin_imports(plugin_folder):
"""Ensure submodule imports work as expected in folder plugins.

Regression test for https://github.com/sopel-irc/sopel/issues/2619
"""
handler = handlers.PyFilePlugin(plugin_folder)
handler.load()
dgw marked this conversation as resolved.
Show resolved Hide resolved
assert handler.module.foo == 'bar baz'
Loading