Skip to content

Commit c5d9917

Browse files
committed
Added a test for multiple plugin classes in a single module.
1 parent f5f76f2 commit c5d9917

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

beets/plugins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,15 @@ def _get_plugin(name: str) -> BeetsPlugin | None:
399399

400400
if len(plugin_classes) > 1:
401401
warnings.warn(
402-
f"Plugin {name} defines multiple plugin classes; "
403-
f"using the first one found ({plugin_classes[0].__name__})."
402+
f"Plugin '{name}' defines multiple plugin classes; "
403+
f"using the first one found ({plugin_classes[0].__name__}). "
404404
f"This will become an error in beets 3.0.0. Consider exporting "
405405
f"the desired plugin class explicitly using `__all__`.",
406406
DeprecationWarning,
407407
stacklevel=2,
408408
)
409409

410-
if len(plugin_classes) == 1:
410+
if len(plugin_classes) != 0:
411411
return plugin_classes[0]()
412412

413413
except Exception:

test/test_plugins.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import pkgutil
2121
import sys
22+
from types import ModuleType
2223
from unittest.mock import ANY, Mock, patch
2324

2425
import pytest
@@ -523,3 +524,44 @@ def test_import_plugin(self, caplog, plugin_name):
523524
assert "PluginImportError" not in caplog.text, (
524525
f"Plugin '{plugin_name}' has issues during import."
525526
)
527+
528+
529+
class MultiPluginModule(ModuleType):
530+
class DummyPlugin1(plugins.BeetsPlugin):
531+
pass
532+
533+
class DummyPlugin2(plugins.BeetsPlugin):
534+
pass
535+
536+
def __init__(self, *_, **__):
537+
module_name = "beetsplug.multi_export"
538+
super().__init__(module_name)
539+
self.DummyPlugin1.__module__ = module_name
540+
self.DummyPlugin1 = self.DummyPlugin1
541+
self.DummyPlugin2 = self.DummyPlugin2
542+
self.DummyPlugin2.__module__ = module_name
543+
544+
545+
class TestMultiPluginExport(PluginTestCase):
546+
"""Test that exporting multiple plugins from a single namespace
547+
raises a warning.
548+
549+
TODO: Change to raises an error on migration to 3.0.0
550+
"""
551+
552+
plugin = "multi_export"
553+
554+
@classmethod
555+
def setUpClass(cls):
556+
patcher = patch.dict(
557+
sys.modules, {"beetsplug.multi_export": MultiPluginModule()}
558+
)
559+
patcher.start()
560+
cls.addClassCleanup(patcher.stop)
561+
562+
super().setUpClass()
563+
564+
@pytest.mark.filterwarnings("ignore")
565+
def test_multi_plugin_export(self):
566+
with self.assertWarns(DeprecationWarning):
567+
self.load_plugins("multi_export")

0 commit comments

Comments
 (0)