# -*- coding: utf-8 -*- r'''在每个 7x 小节开头插一次 stub_ok 调用,用来定位桩是什么时候丢的。 为什么需要: 测试跑着跑着,$STUB 目录会整个消失,于是 deploy.sh 找不到 cloudflared 就去真的下载 GitHub —— 整个测试卡死十分钟, 而且所有断言都变成"没启用 / 退出码不对"这种指不到根因的假失败。 插桩是为了找出它到底在哪一步消失。 ''' from __future__ import annotations import sys from pathlib import Path SRC = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote" r"\_build\test_deploy_sh.sh") MARKS = [ ("7c. 默认", "7c"), ("7d. --protocol quic", "7d"), ("7e. --protocol http2", "7e"), ("7f. --protocol 的非法值", "7f"), ("7g. --port", "7g"), ("7b. 再跑一次", "7b"), ("12. 有凭据但没给域名", "12"), ] def main() -> int: text = SRC.read_text(encoding="utf-8") if 'stub_ok "' in text: print(" 已经插过了,不重复") return 0 n = 0 for marker, tag in MARKS: key = "echo \"── %s" % marker i = text.find(key) if i < 0: print(" 找不到小节: %s" % marker) continue j = text.find("\n", i) + 1 text = text[:j] + 'stub_ok "%s"\n' % tag + text[j:] n += 1 SRC.write_text(text, encoding="utf-8", newline="\n") print(" 插了 %d 处" % n) # 语法自检 import subprocess r = subprocess.run([r"C:\Program Files\Git\bin\bash.exe", "-n", str(SRC).replace("\\", "/")], capture_output=True, text=True, encoding="utf-8", errors="replace") if r.returncode == 0: print(" 语法 OK") else: print(" 语法错误: %s" % (r.stderr or "").strip()[:300]) return 1 return 0 if __name__ == "__main__": sys.exit(main())