💥 remove deprecated nonebot.plugins toml table (#1151)

Feature: 移除过时的 `nonebot.plugins` toml 配置
This commit is contained in:
Ju4tCode 2022-08-16 10:03:37 +08:00 committed by GitHub
parent cdc507bab9
commit 898c29d7ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 10 deletions

View File

@ -74,6 +74,8 @@ def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
""" """
with open(file_path, "r", encoding=encoding) as f: with open(file_path, "r", encoding=encoding) as f:
data = json.load(f) data = json.load(f)
if not isinstance(data, dict):
raise TypeError("json file must contains a dict!")
plugins = data.get("plugins") plugins = data.get("plugins")
plugin_dirs = data.get("plugin_dirs") plugin_dirs = data.get("plugin_dirs")
assert isinstance(plugins, list), "plugins must be a list of plugin name" assert isinstance(plugins, list), "plugins must be a list of plugin name"
@ -103,15 +105,10 @@ def load_from_toml(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
data = tomlkit.parse(f.read()) # type: ignore data = tomlkit.parse(f.read()) # type: ignore
nonebot_data = data.get("tool", {}).get("nonebot") nonebot_data = data.get("tool", {}).get("nonebot")
if not nonebot_data: if nonebot_data is None:
nonebot_data = data.get("nonebot", {}).get("plugins")
if nonebot_data:
warnings.warn(
"[nonebot.plugins] table is deprecated. Use [tool.nonebot] instead.",
DeprecationWarning,
)
else:
raise ValueError("Cannot find '[tool.nonebot]' in given toml file!") raise ValueError("Cannot find '[tool.nonebot]' in given toml file!")
if not isinstance(nonebot_data, dict):
raise TypeError("'[tool.nonebot]' must be a Table!")
plugins = nonebot_data.get("plugins", []) plugins = nonebot_data.get("plugins", [])
plugin_dirs = nonebot_data.get("plugin_dirs", []) plugin_dirs = nonebot_data.get("plugin_dirs", [])
assert isinstance(plugins, list), "plugins must be a list of plugin name" assert isinstance(plugins, list), "plugins must be a list of plugin name"

0
tests/plugins.empty.toml Normal file
View File

View File

@ -0,0 +1 @@
[]

View File

@ -0,0 +1,2 @@
[tool]
nonebot = []

4
tests/plugins.json Normal file
View File

@ -0,0 +1,4 @@
{
"plugins": [],
"plugin_dirs": ["plugins"]
}

3
tests/plugins.toml Normal file
View File

@ -0,0 +1,3 @@
[tool.nonebot]
plugins = []
plugin_dirs = ["plugins"]

View File

@ -3,5 +3,7 @@ from datetime import datetime, timedelta
from nonebot.matcher import Matcher from nonebot.matcher import Matcher
test_temp_matcher = Matcher.new("test", temp=True) test_temp_matcher = Matcher.new("test", temp=True)
test_datetime_matcher = Matcher.new("test", expire_time=datetime.now()) test_datetime_matcher = Matcher.new(
"test", expire_time=datetime.now() - timedelta(seconds=1)
)
test_timedelta_matcher = Matcher.new("test", expire_time=timedelta(seconds=-1)) test_timedelta_matcher = Matcher.new("test", expire_time=timedelta(seconds=-1))

View File

@ -51,6 +51,29 @@ async def test_load_nested_plugin(app: App, load_plugin: Set["Plugin"]):
assert parent_plugin.sub_plugins == {sub_plugin, sub_plugin2} assert parent_plugin.sub_plugins == {sub_plugin, sub_plugin2}
@pytest.mark.asyncio
async def test_load_json(app: App):
import nonebot
nonebot.load_from_json("./plugins.json")
with pytest.raises(TypeError):
nonebot.load_from_json("./plugins.invalid.json")
@pytest.mark.asyncio
async def test_load_toml(app: App):
import nonebot
nonebot.load_from_toml("./plugins.toml")
with pytest.raises(ValueError):
nonebot.load_from_toml("./plugins.empty.toml")
with pytest.raises(TypeError):
nonebot.load_from_toml("./plugins.invalid.toml")
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_bad_plugin(app: App): async def test_bad_plugin(app: App):
import nonebot import nonebot