Skip to content

Commit b45ce34

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

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import os
2020
import pkgutil
2121
import sys
22+
from types import ModuleType
2223
from unittest.mock import ANY, Mock, patch
24+
import warnings
2325

2426
import pytest
2527
from mediafile import MediaFile
@@ -523,3 +525,40 @@ def test_import_plugin(self, caplog, plugin_name):
523525
assert "PluginImportError" not in caplog.text, (
524526
f"Plugin '{plugin_name}' has issues during import."
525527
)
528+
529+
530+
class MultiPluginModule(ModuleType):
531+
class DummyPlugin1(plugins.BeetsPlugin):
532+
pass
533+
534+
class DummyPlugin2(plugins.BeetsPlugin):
535+
pass
536+
537+
def __init__(self, *_, **__):
538+
module_name = "beetsplug.multi_export"
539+
super().__init__(module_name)
540+
self.DummyPlugin1.__module__ = module_name
541+
self.DummyPlugin1 = self.DummyPlugin1
542+
self.DummyPlugin2 = self.DummyPlugin2
543+
self.DummyPlugin2.__module__ = module_name
544+
545+
546+
class TestMultiPluginExport(PluginMixin):
547+
"""Test that exporting multiple plugins from a single namespace
548+
raises a warning.
549+
550+
TODO: Change to raises an error on migration to 3.0.0
551+
"""
552+
553+
plugin = "multi_export"
554+
555+
@pytest.fixture(autouse=True, scope="class")
556+
def setup(self):
557+
with patch.dict(
558+
sys.modules, {"beetsplug.multi_export": MultiPluginModule()}
559+
):
560+
yield
561+
562+
def test_multi_plugin_export(self):
563+
with pytest.deprecated_call():
564+
self.load_plugins("multi_export")

0 commit comments

Comments
 (0)