Skip to content

Commit

Permalink
Merge pull request #2633 from sopel-irc/plugins.handlers-tweaks
Browse files Browse the repository at this point in the history
plugins.handlers: fix some PyFilePlugin logic errors
  • Loading branch information
dgw authored Oct 20, 2024
2 parents 7150e02 + 54372e6 commit 3f7d0c3
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sopel/plugins/handlers.py
Original file line number Diff line number Diff line change
@@ -478,7 +478,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)
@@ -494,9 +494,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

42 changes: 42 additions & 0 deletions test/plugins/test_plugins_handlers.py
Original file line number Diff line number Diff line change
@@ -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()
assert handler.module.foo == 'bar baz'

0 comments on commit 3f7d0c3

Please sign in to comment.