diff --git a/nonebot/plugin/load.py b/nonebot/plugin/load.py index 5a756672..e62b10b6 100644 --- a/nonebot/plugin/load.py +++ b/nonebot/plugin/load.py @@ -206,18 +206,17 @@ def inherit_supported_adapters(*names: str) -> Optional[set[str]]: meta = plugin.metadata if meta is None: raise ValueError(f'Plugin "{name}" has no metadata!') - support = meta.supported_adapters - if support is None: + + if (raw := meta.supported_adapters) is None: continue + + support = { + f"nonebot.adapters.{adapter[1:]}" if adapter.startswith("~") else adapter + for adapter in raw + } + final_supported = ( support if final_supported is None else (final_supported & support) ) - return final_supported and { - ( - f"nonebot.adapters.{adapter_name[1:]}" - if adapter_name.startswith("~") - else adapter_name - ) - for adapter_name in final_supported - } + return final_supported diff --git a/tests/plugins/metadata_3.py b/tests/plugins/metadata_3.py new file mode 100644 index 00000000..c18ed1b5 --- /dev/null +++ b/tests/plugins/metadata_3.py @@ -0,0 +1,15 @@ +from nonebot.plugin import PluginMetadata + +__plugin_meta__ = PluginMetadata( + name="测试插件3", + description="测试继承适配器, 使用内置适配器全名", + usage="无法使用", + type="application", + homepage="https://nonebot.dev", + supported_adapters={ + "nonebot.adapters.onebot.v11", + "nonebot.adapters.onebot.v12", + "~qq", + }, + extra={"author": "NoneBot"}, +) diff --git a/tests/test_plugin/test_load.py b/tests/test_plugin/test_load.py index cb933055..16160eb9 100644 --- a/tests/test_plugin/test_load.py +++ b/tests/test_plugin/test_load.py @@ -149,32 +149,82 @@ async def test_plugin_metadata(): @pytest.mark.asyncio -async def test_inherit_supported_adapters(): +async def test_inherit_supported_adapters_not_found(): with pytest.raises(RuntimeError): inherit_supported_adapters("some_plugin_not_exist") with pytest.raises(ValueError, match="has no metadata!"): inherit_supported_adapters("export") - echo = nonebot.get_plugin("echo") - assert echo - assert echo.metadata - assert inherit_supported_adapters("echo") is None - plugin_1 = nonebot.get_plugin("metadata") - assert plugin_1 - assert plugin_1.metadata - assert inherit_supported_adapters("metadata") == { - "nonebot.adapters.onebot.v11", - "plugins.metadata:FakeAdapter", - } - - plugin_2 = nonebot.get_plugin("metadata_2") - assert plugin_2 - assert plugin_2.metadata - assert inherit_supported_adapters("metadata", "metadata_2") == { - "nonebot.adapters.onebot.v11" - } - assert inherit_supported_adapters("metadata", "echo", "metadata_2") == { - "nonebot.adapters.onebot.v11" - } +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("inherit_plugins", "expected"), + [ + (("echo",), None), + ( + ("metadata",), + { + "nonebot.adapters.onebot.v11", + "plugins.metadata:FakeAdapter", + }, + ), + ( + ("metadata_2",), + { + "nonebot.adapters.onebot.v11", + "nonebot.adapters.onebot.v12", + }, + ), + ( + ("metadata_3",), + { + "nonebot.adapters.onebot.v11", + "nonebot.adapters.onebot.v12", + "nonebot.adapters.qq", + }, + ), + ( + ("metadata", "metadata_2"), + { + "nonebot.adapters.onebot.v11", + }, + ), + ( + ("metadata", "metadata_3"), + { + "nonebot.adapters.onebot.v11", + }, + ), + ( + ("metadata_2", "metadata_3"), + { + "nonebot.adapters.onebot.v11", + "nonebot.adapters.onebot.v12", + }, + ), + ( + ("metadata", "metadata_2", "metadata_3"), + { + "nonebot.adapters.onebot.v11", + }, + ), + ( + ("metadata", "echo"), + { + "nonebot.adapters.onebot.v11", + "plugins.metadata:FakeAdapter", + }, + ), + ( + ("metadata", "metadata_2", "echo"), + { + "nonebot.adapters.onebot.v11", + }, + ), + ], +) +async def test_inherit_supported_adapters_combine( + inherit_plugins: tuple[str], expected: set[str] +): + assert inherit_supported_adapters(*inherit_plugins) == expected