test_check_configs.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """``scripts/check_configs.py`` 的配置契约单测(不需要模型权重 / 板卡 / 相机)。
  2. 守两件事:
  3. 1. **仓库里现存的每个 ``configs/*.json`` 都能过门禁**——防止新增模板时只改了 JSON
  4. 忘了 4 类 / 12+4 字 / 32 对齐这些契约(本次就是 ``demo-yolov8n.json`` 入库后被这条抓到的);
  5. 2. **豁免只对 ``demo-*.json`` 生效**:同样内容换个名字(``site.json``)必须继续失败,
  6. 否则「跳 4 类校验」会变成随便一个现场配置都能绕过的后门。
  7. """
  8. from __future__ import annotations
  9. import glob
  10. import importlib.util
  11. import json
  12. import os
  13. import sys
  14. import tempfile
  15. import unittest
  16. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  17. if ROOT not in sys.path:
  18. sys.path.insert(0, ROOT)
  19. def _load(name: str = "check_configs"):
  20. """按路径加载 ``scripts/<name>.py``(scripts 不是包,沿用 test_verify_rknn 的写法)。"""
  21. path = os.path.join(ROOT, "scripts", name + ".py")
  22. spec = importlib.util.spec_from_file_location(name, path)
  23. mod = importlib.util.module_from_spec(spec)
  24. spec.loader.exec_module(mod)
  25. return mod
  26. check_configs = _load()
  27. DEMO = os.path.join(ROOT, "configs", "demo-yolov8n.json")
  28. class RepoConfigsTests(unittest.TestCase):
  29. def test_every_repo_config_passes(self):
  30. targets = sorted(glob.glob(os.path.join(ROOT, "configs", "*.json")))
  31. self.assertTrue(targets, "configs/ 下应有模板")
  32. for path in targets:
  33. with self.subTest(config=os.path.basename(path)):
  34. check_configs.check_one(path) # 不抛即通过
  35. def test_demo_config_is_flagged_as_demo(self):
  36. summary = check_configs.check_one(DEMO)
  37. self.assertIn("demo", summary)
  38. self.assertIn("backend=rknn", summary)
  39. def test_cli_exit_code_on_repo_configs(self):
  40. cwd = os.getcwd()
  41. os.chdir(ROOT)
  42. try:
  43. self.assertEqual(check_configs.main(["configs"]), 0)
  44. finally:
  45. os.chdir(cwd)
  46. class DemoExemptionTests(unittest.TestCase):
  47. """豁免只看文件名前缀,不看内容——换个名字就必须继续失败。"""
  48. def setUp(self):
  49. with open(DEMO, encoding="utf-8") as fh:
  50. self.data = json.load(fh)
  51. def _write(self, directory: str, name: str) -> str:
  52. path = os.path.join(directory, name)
  53. with open(path, "w", encoding="utf-8") as fh:
  54. json.dump(self.data, fh, ensure_ascii=False)
  55. return path
  56. def test_same_content_as_site_fails_4_class_contract(self):
  57. with tempfile.TemporaryDirectory() as td:
  58. path = self._write(td, "site.json")
  59. with self.assertRaises(AssertionError) as ctx:
  60. check_configs.check_one(path)
  61. self.assertIn("4 类", str(ctx.exception))
  62. def test_same_content_as_demo_passes(self):
  63. with tempfile.TemporaryDirectory() as td:
  64. path = self._write(td, "demo-copy.json")
  65. check_configs.check_one(path) # 不应抛
  66. def test_demo_still_enforces_size_contract(self):
  67. """豁免只覆盖类别:输入尺寸 32 对齐仍然要报错。"""
  68. broken = json.loads(json.dumps(self.data))
  69. broken["model"]["input_size"] = [641, 640]
  70. with tempfile.TemporaryDirectory() as td:
  71. path = os.path.join(td, "demo-broken.json")
  72. with open(path, "w", encoding="utf-8") as fh:
  73. json.dump(broken, fh, ensure_ascii=False)
  74. with self.assertRaises(AssertionError) as ctx:
  75. check_configs.check_one(path)
  76. self.assertIn("32 对齐", str(ctx.exception))
  77. def test_demo_still_enforces_rknn_input_format(self):
  78. """豁免只覆盖类别:`backend=rknn` 仍必须配 `*_uint8` 输入格式。"""
  79. broken = json.loads(json.dumps(self.data))
  80. broken["model"]["input_format"] = "bgr_float"
  81. with tempfile.TemporaryDirectory() as td:
  82. path = os.path.join(td, "demo-broken.json")
  83. with open(path, "w", encoding="utf-8") as fh:
  84. json.dump(broken, fh, ensure_ascii=False)
  85. with self.assertRaises(AssertionError) as ctx:
  86. check_configs.check_one(path)
  87. self.assertIn("uint8", str(ctx.exception))
  88. if __name__ == "__main__":
  89. unittest.main()