Skip to content

Commit 8ba41b6

Browse files
committed
add validator dummy context config and data dir
1 parent 176953c commit 8ba41b6

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

scripts/validate_plugins/run.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,55 @@ def __bool__(self) -> bool:
652652
return False
653653

654654

655+
class DummyConfig(dict):
656+
def __init__(self, initial=None) -> None:
657+
super().__init__()
658+
if initial:
659+
for key, value in initial.items():
660+
self[key] = value
661+
662+
@staticmethod
663+
def _wrap(value):
664+
if isinstance(value, dict) and not isinstance(value, DummyConfig):
665+
return DummyConfig(value)
666+
return value
667+
668+
def __setitem__(self, key, value) -> None:
669+
super().__setitem__(key, self._wrap(value))
670+
671+
def __getitem__(self, key):
672+
if key in self:
673+
return super().__getitem__(key)
674+
return NullStub()
675+
676+
def __getattr__(self, name: str):
677+
return self.get(name)
678+
679+
655680
class DummyContext:
656681
def __init__(self) -> None:
657682
self._star_manager = None
683+
self._astrbot_root = Path(os.environ.get("ASTRBOT_ROOT", Path.cwd())).resolve()
684+
self._data_root = self._astrbot_root / "data"
685+
self._plugin_data_dir = self._data_root / "plugin_data"
686+
self._plugin_data_dir.mkdir(parents=True, exist_ok=True)
687+
self._config = DummyConfig(
688+
{
689+
"wake_prefix": [],
690+
"dashboard": {},
691+
"admins_id": [],
692+
"admin_ids": [],
693+
"platform_settings": {
694+
"aiocqhttp": {},
695+
"qqofficial": {},
696+
"telegram": {},
697+
"gewechat": {},
698+
"wechatpadpro": {},
699+
},
700+
"data_dir": str(self._data_root),
701+
}
702+
)
703+
self.config = self._config
658704

659705
def get_all_stars(self):
660706
try:
@@ -684,6 +730,17 @@ def register_llm_tool(self, name: str, func_args, desc: str, func_obj) -> None:
684730
def unregister_llm_tool(self, name: str) -> None:
685731
del name
686732

733+
def get_config(self, umo: str | None = None):
734+
del umo
735+
return self._config
736+
737+
def get_context_config(self):
738+
return self._config
739+
740+
def get_data_dir(self) -> str:
741+
self._plugin_data_dir.mkdir(parents=True, exist_ok=True)
742+
return str(self._plugin_data_dir)
743+
687744
def __getattr__(self, name: str) -> NullStub:
688745
del name
689746
return NullStub()

tests/test_validate_plugins.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,18 @@ def test_load_plugins_index_rejects_non_dict_values(self):
386386

387387

388388
class DummyContextStubTests(unittest.IsolatedAsyncioTestCase):
389+
def test_dummy_context_returns_worker_data_dir_for_plugin_storage(self):
390+
module = load_validator_module()
391+
392+
with tempfile.TemporaryDirectory() as tmp_dir:
393+
astrbot_root = Path(tmp_dir) / "astrbot-root"
394+
with mock.patch.dict(os.environ, {"ASTRBOT_ROOT": str(astrbot_root)}, clear=True):
395+
data_dir = Path(module.DummyContext().get_data_dir())
396+
data_dir_exists = data_dir.is_dir()
397+
398+
self.assertEqual(data_dir.resolve(), (astrbot_root / "data" / "plugin_data").resolve())
399+
self.assertTrue(data_dir_exists)
400+
389401
async def test_null_stub_supports_async_database_context_pattern(self):
390402
module = load_validator_module()
391403

@@ -411,6 +423,16 @@ async def test_null_stub_returns_defaults_for_restart_style_config_access(self):
411423
6185,
412424
)
413425

426+
def test_dummy_context_exposes_dict_like_config_defaults(self):
427+
module = load_validator_module()
428+
429+
with mock.patch.dict(os.environ, {}, clear=True):
430+
context = module.DummyContext()
431+
432+
self.assertEqual(context.get_config()["wake_prefix"], [])
433+
self.assertEqual(context.get_config()["dashboard"].get("port", 6185), 6185)
434+
self.assertEqual(context._config.get("expire_seconds", 300), 300)
435+
414436

415437
class ValidationProgressTests(unittest.TestCase):
416438
def test_build_parser_defaults_max_workers_to_sixteen(self):

0 commit comments

Comments
 (0)