feat: 更新 ConfigManager 初始化,支持配置自动升级并添加相应测试
This commit is contained in:
@@ -16,7 +16,7 @@ async def test_handle_file_changes_throttles_reload():
|
||||
|
||||
called = 0
|
||||
|
||||
async def reload_stub() -> bool:
|
||||
async def reload_stub(changed_scopes=None) -> bool:
|
||||
nonlocal called
|
||||
called += 1
|
||||
return True
|
||||
@@ -36,7 +36,7 @@ async def test_handle_file_changes_timeout_logged(caplog):
|
||||
manager._hot_reload_min_interval_s = 0.0
|
||||
manager._hot_reload_timeout_s = 0.01
|
||||
|
||||
async def reload_stub() -> bool:
|
||||
async def reload_stub(changed_scopes=None) -> bool:
|
||||
await asyncio.sleep(0.05)
|
||||
return True
|
||||
|
||||
@@ -55,7 +55,7 @@ async def test_handle_file_changes_empty_skips_reload():
|
||||
|
||||
called = 0
|
||||
|
||||
async def reload_stub() -> bool:
|
||||
async def reload_stub(changed_scopes=None) -> bool:
|
||||
nonlocal called
|
||||
called += 1
|
||||
return True
|
||||
|
||||
33
pytests/config_test/test_config_manager_startup_upgrade.py
Normal file
33
pytests/config_test/test_config_manager_startup_upgrade.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from src.config import config as config_module
|
||||
from src.config.config import Config, ConfigManager, ModelConfig
|
||||
|
||||
|
||||
class _StartupUpgradeExit(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def test_initialize_upgrades_bot_and_model_config_before_exit(monkeypatch):
|
||||
manager = ConfigManager()
|
||||
loaded_config_classes: list[type[Any]] = []
|
||||
exit_codes: list[int | None] = []
|
||||
|
||||
def fake_load_config_from_file(config_class, config_path, new_ver, override_repr=False):
|
||||
loaded_config_classes.append(config_class)
|
||||
return object(), True
|
||||
|
||||
def fake_exit(code: int | None = None):
|
||||
exit_codes.append(code)
|
||||
raise _StartupUpgradeExit
|
||||
|
||||
monkeypatch.setattr(config_module, "load_config_from_file", fake_load_config_from_file)
|
||||
monkeypatch.setattr(config_module.sys, "exit", fake_exit)
|
||||
|
||||
with pytest.raises(_StartupUpgradeExit):
|
||||
manager.initialize()
|
||||
|
||||
assert loaded_config_classes == [Config, ModelConfig]
|
||||
assert exit_codes == [0]
|
||||
@@ -196,8 +196,15 @@ class ConfigManager:
|
||||
def initialize(self):
|
||||
logger.info(t("config.current_version", version=MMC_VERSION))
|
||||
logger.info(t("config.loading"))
|
||||
self.global_config = self.load_global_config()
|
||||
self.model_config = self.load_model_config()
|
||||
self.global_config, global_updated = load_config_from_file(Config, self.bot_config_path, CONFIG_VERSION)
|
||||
self.model_config, model_updated = load_config_from_file(
|
||||
ModelConfig,
|
||||
self.model_config_path,
|
||||
MODEL_CONFIG_VERSION,
|
||||
True,
|
||||
)
|
||||
if global_updated or model_updated:
|
||||
sys.exit(0) # 配置已自动升级,退出一次让用户确认新配置后再启动
|
||||
logger.info(t("config.loaded"))
|
||||
|
||||
def load_global_config(self) -> Config:
|
||||
|
||||
Reference in New Issue
Block a user