"""``scripts/check_configs.py`` 的配置契约单测(不需要模型权重 / 板卡 / 相机)。 守两件事: 1. **仓库里现存的每个 ``configs/*.json`` 都能过门禁**——防止新增模板时只改了 JSON 忘了 4 类 / 12+4 字 / 32 对齐这些契约(本次就是 ``demo-yolov8n.json`` 入库后被这条抓到的); 2. **豁免只对 ``demo-*.json`` 生效**:同样内容换个名字(``site.json``)必须继续失败, 否则「跳 4 类校验」会变成随便一个现场配置都能绕过的后门。 """ from __future__ import annotations import glob import importlib.util import json import os import sys import tempfile import unittest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if ROOT not in sys.path: sys.path.insert(0, ROOT) def _load(name: str = "check_configs"): """按路径加载 ``scripts/.py``(scripts 不是包,沿用 test_verify_rknn 的写法)。""" path = os.path.join(ROOT, "scripts", name + ".py") spec = importlib.util.spec_from_file_location(name, path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod check_configs = _load() DEMO = os.path.join(ROOT, "configs", "demo-yolov8n.json") class RepoConfigsTests(unittest.TestCase): def test_every_repo_config_passes(self): targets = sorted(glob.glob(os.path.join(ROOT, "configs", "*.json"))) self.assertTrue(targets, "configs/ 下应有模板") for path in targets: with self.subTest(config=os.path.basename(path)): check_configs.check_one(path) # 不抛即通过 def test_demo_config_is_flagged_as_demo(self): summary = check_configs.check_one(DEMO) self.assertIn("demo", summary) self.assertIn("backend=rknn", summary) def test_cli_exit_code_on_repo_configs(self): cwd = os.getcwd() os.chdir(ROOT) try: self.assertEqual(check_configs.main(["configs"]), 0) finally: os.chdir(cwd) class DemoExemptionTests(unittest.TestCase): """豁免只看文件名前缀,不看内容——换个名字就必须继续失败。""" def setUp(self): with open(DEMO, encoding="utf-8") as fh: self.data = json.load(fh) def _write(self, directory: str, name: str) -> str: path = os.path.join(directory, name) with open(path, "w", encoding="utf-8") as fh: json.dump(self.data, fh, ensure_ascii=False) return path def test_same_content_as_site_fails_4_class_contract(self): with tempfile.TemporaryDirectory() as td: path = self._write(td, "site.json") with self.assertRaises(AssertionError) as ctx: check_configs.check_one(path) self.assertIn("4 类", str(ctx.exception)) def test_same_content_as_demo_passes(self): with tempfile.TemporaryDirectory() as td: path = self._write(td, "demo-copy.json") check_configs.check_one(path) # 不应抛 def test_demo_still_enforces_size_contract(self): """豁免只覆盖类别:输入尺寸 32 对齐仍然要报错。""" broken = json.loads(json.dumps(self.data)) broken["model"]["input_size"] = [641, 640] with tempfile.TemporaryDirectory() as td: path = os.path.join(td, "demo-broken.json") with open(path, "w", encoding="utf-8") as fh: json.dump(broken, fh, ensure_ascii=False) with self.assertRaises(AssertionError) as ctx: check_configs.check_one(path) self.assertIn("32 对齐", str(ctx.exception)) def test_demo_still_enforces_rknn_input_format(self): """豁免只覆盖类别:`backend=rknn` 仍必须配 `*_uint8` 输入格式。""" broken = json.loads(json.dumps(self.data)) broken["model"]["input_format"] = "bgr_float" with tempfile.TemporaryDirectory() as td: path = os.path.join(td, "demo-broken.json") with open(path, "w", encoding="utf-8") as fh: json.dump(broken, fh, ensure_ascii=False) with self.assertRaises(AssertionError) as ctx: check_configs.check_one(path) self.assertIn("uint8", str(ctx.exception)) if __name__ == "__main__": unittest.main()