Files
37duAutomation/tests/sdk_compat_check/_run_cases.py
T
puddy 9873eb1963 restore: 恢复 11-06~11-09 本地未提交改动
- libs: 新增 hr_sdk / hotel_report / dingtalk,更新 desktopauto
- controlplane-next: SDK 管理模块(controller/entity/migrations)与前端视图
- electron-shell: 插件同步、注册表、runner 调整
- plugins: XQ0035/XQ0041/XQ0042/XQ0045_XQ0046/XQ0048/XQ0076/XQ0078 等版本更新
- tests/tools: 新增 SDK 兼容性检查与打包工具(sdk_pack.py、sdk_compat_check.py)
2026-11-09 17:27:10 +08:00

110 lines
4.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""sdk_compat_check 自检:用固定夹具跑完所有判定分支。
运行(仓库根)::
.venv\\Scripts\\python.exe tests\\sdk_compat_check\\_run_cases.py
仓库没有 pytest 组织(tests/ 是探针脚本集合),所以这里用可直接运行的自检脚本,
任一用例不符即非 0 退出。
"""
from __future__ import annotations
import json
import subprocess
import sys
import tempfile
from pathlib import Path
HERE = Path(__file__).resolve().parent
REPO = HERE.parents[1]
TOOL = REPO / "tools" / "sdk_compat_check.py"
PLUGINS = HERE / "plugins"
SDK_OLD = HERE / "sdk_v1.0.0"
SDK_NEW = HERE / "sdk_v1.1.0"
SDK_MAJOR = HERE / "sdk_v2.0.0"
CASES = [
# name, extra args, 期望分类, 期望退出码, 期望 error 码集合, 期望 summary 片段
("p_ok", ["--only", "p_ok"], "COMPATIBLE", 0, set(), {"pass": 1}),
("p_undeclared", ["--only", "p_undeclared"], "PLUGIN_BUMP", 1, {"UNDECLARED_SDK"}, {}),
("p_missing", ["--only", "p_missing"], "ACCIDENTAL_BREAK", 1, {"MISSING_API"}, {}),
("p_import_fail", ["--only", "p_import_fail"], "ACCIDENTAL_BREAK", 1, {"IMPORT_FAIL"}, {}),
("p_old_major", ["--sdk", str(SDK_MAJOR), "--baseline-sdk", str(SDK_OLD),
"--only", "p_old_major"], "PLUGIN_BUMP", 1, {"NEEDS_BUMP"}, {}),
("p_legacy(API_REMOVED)", ["--baseline-sdk", str(SDK_OLD), "--only", "p_legacy"],
"ACCIDENTAL_BREAK", 1, {"API_REMOVED", "MISSING_API"}, {}, {"API_SIGNATURE_CHANGED"}),
("p_skip", ["--only", "p_skip"], "COMPATIBLE", 0, set(), {"skipped": 1, "checked": 0}),
("p_selftest_fail", ["--only", "p_selftest_fail"], "ACCIDENTAL_BREAK", 1,
{"SELFTEST_FAIL"}, {}),
("p_dynamic", ["--only", "p_dynamic"], "COMPATIBLE", 0, set(), {}),
("p_vendored", ["--only", "p_vendored"], "COMPATIBLE", 0, set(), {}, {"VENDORED_SDK"}),
]
def run_tool(extra: list) -> tuple:
with tempfile.TemporaryDirectory() as td:
out = Path(td) / "report.json"
cmd = [sys.executable, str(TOOL), "--plugins", str(PLUGINS),
"--json", str(out), *extra]
proc = subprocess.run(cmd, cwd=str(REPO), capture_output=True, text=True,
encoding="utf-8", errors="replace", timeout=600)
rep = json.loads(out.read_text(encoding="utf-8")) if out.is_file() else None
return proc.returncode, rep, (proc.stdout or "") + (proc.stderr or "")
def main() -> int:
failures = 0
for case in CASES:
name, extra, want_class, want_exit, want_codes, want_summary = case[:6]
want_warns = case[6] if len(case) > 6 else set()
args = list(extra)
if "--sdk" not in args:
args = ["--sdk", str(SDK_NEW)] + args
code, rep, log = run_tool(args)
problems = []
if rep is None:
problems.append("无 JSON 报告")
else:
if rep["classification"] != want_class:
problems.append(f"分类 {rep['classification']} != {want_class}")
if code != want_exit:
problems.append(f"退出码 {code} != {want_exit}")
got = {f["code"] for r in rep["plugins"] for f in r["findings"]
if f["severity"] == "error"}
got |= {f["code"] for f in rep["sdk_findings"] if f["severity"] == "error"}
missing = want_codes - got
if missing:
problems.append(f"缺少 error 码 {sorted(missing)}(实际 {sorted(got)})")
warns = {f["code"] for r in rep["plugins"] for f in r["findings"]
if f["severity"] == "warning"}
warns |= {f["code"] for f in rep["sdk_findings"] if f["severity"] == "warning"}
miss_warn = want_warns - warns
if miss_warn:
problems.append(f"缺少 warning 码 {sorted(miss_warn)}(实际 {sorted(warns)})")
for key, val in want_summary.items():
if rep["summary"].get(key) != val:
problems.append(f"summary.{key}={rep['summary'].get(key)} != {val}")
if problems:
failures += 1
print(f"[FAIL] {name}: " + ";".join(problems))
print(" " + log.strip().splitlines()[0][:200] if log.strip() else "")
else:
print(f"[ok] {name:<24}{want_class:<18}exit={code}")
# 非法 SDK:路径不存在 → 退出码 2
code, rep, log = run_tool(["--sdk", str(HERE / "no_such_sdk"), "--only", "p_ok"])
if code != 2:
failures += 1
print(f"[FAIL] sdk_invalid: 退出码 {code} != 2")
else:
print(f"[ok] {'sdk_invalid':<24}{'SDK_INVALID':<18}exit={code}")
print("—— 全部用例通过 ——" if not failures else f"—— {failures} 个用例失败 ——")
return 1 if failures else 0
if __name__ == "__main__":
raise SystemExit(main())