测试套件:锁屏识别 + 退出码取法 + BOM 兜底

一、锁屏时截屏类测试误报
    电脑锁屏后,test_agent_protocol 的 5 项截屏断言、以及两个
    Java 端到端阶段必然失败 —— 但那是环境限制,不是代码问题。
    不区分的话汇总表一片红,真正的失败反而被淹没。
    现在启动时用 OpenInputDesktop 探测,锁屏时这些阶段记
    SKIP(锁屏) 而不是 FAIL,并在开头黄字提示。

二、管道后读 $LASTEXITCODE 不可靠
    形如
        & python xxx.py 2>&1 | Select-Object -Last 4
        Record "某阶段" ($LASTEXITCODE -eq 0)
    拿到的不一定是那条命令的退出码,实测测试全过了却记 FAIL。
    改成先把码存进变量再走管道。共修 4 处。

三、edit 工具会剥掉 .ps1 的 UTF-8 BOM
    没有 BOM 的 .ps1 会被 PowerShell 按 GBK 读,中文全乱码,
    接着一堆 "Unexpected token" 语法错误,脚本直接跑不了。
    这个坑今天踩了两次。加了 _build/fix_ps1_bom.py 统一补 BOM,
    并顺带检查 .bat 是不是 GBK。

跑出来:33 个阶段 —— 30 PASS + 3 SKIP(锁屏),0 FAIL。
This commit is contained in:
xmz7891
2026-10-07 01:57:20 +08:00
parent df8056dd16
commit 1e73a789d1
5 changed files with 403 additions and 17 deletions
+97
View File
@@ -0,0 +1,97 @@
# -*- coding: utf-8 -*-
r'''修 verify_all.ps1 里"管道后读 $LASTEXITCODE"的写法。
问题模式:
& python xxx.py 2>&1 | Select-Object -Last 4
Record "某阶段" ($LASTEXITCODE -eq 0) <-- 拿到的不一定是那条命令的码
后果:
测试**实际全过了**(输出里能看到"通过 N 项,失败 0 项"),
但 Record 记成 FAIL,汇总表一片红,真实的失败反而被淹没。
正确写法(文件里前半段已经有几处是对的):
$stageOut = & python xxx.py 2>&1
$stageCode = $LASTEXITCODE
$stageOut | Select-Object -Last 4
Record "某阶段" ($stageCode -eq 0)
这个脚本按上面的模式做机械替换,改完复核 + 补 BOM。
'''
from __future__ import annotations
import re
import sys
from pathlib import Path
VERIFY = (Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote")
/ "_tests" / "verify_all.ps1")
BOM = b"\xef\xbb\xbf"
def main() -> int:
raw = VERIFY.read_bytes()
text = raw.decode("utf-8-sig")
lines = text.split("\n")
out = []
index = 0
fixed = 0
# 匹配: & <cmd> ... | Select-Object -Last <N>
# 下一行:Record "<名字>" ($LASTEXITCODE -eq 0)
pipe_re = re.compile(r"^(\s*)& (.+?) \| Select-Object -Last (\d+)\s*$")
record_re = re.compile(
r'^(\s*)Record "([^"]+)" \(\$LASTEXITCODE -eq 0\)\s*$')
while index < len(lines):
line = lines[index]
match = pipe_re.match(line)
if match and index + 1 < len(lines):
nxt = record_re.match(lines[index + 1])
if nxt and match.group(1) == nxt.group(1):
indent = match.group(1)
command = match.group(2)
count = match.group(3)
name = nxt.group(2)
out.append("%s$stageOut = & %s" % (indent, command))
out.append("%s$stageCode = $LASTEXITCODE" % indent)
out.append("%s$stageOut | Select-Object -Last %s"
% (indent, count))
out.append('%sRecord "%s" ($stageCode -eq 0)'
% (indent, name))
fixed += 1
index += 2
continue
out.append(line)
index += 1
new_text = "\n".join(out)
if fixed == 0:
print(" 没有要改的")
return 0
VERIFY.write_bytes(BOM + new_text.encode("utf-8"))
print(" 改了 %d 处" % fixed)
print()
# 复核:不该再有管道后直接读 $LASTEXITCODE 的
remaining = []
new_lines = new_text.split("\n")
for i, l in enumerate(new_lines):
if "$LASTEXITCODE" in l and "Record" in l:
remaining.append(i + 1)
print(" 复核:")
if remaining:
print(" 还有 %d 行 Record 直接读 $LASTEXITCODE: %s"
% (len(remaining), remaining))
else:
print(" 没有 Record 直接读 $LASTEXITCODE 了 ✓")
print()
print(" BOM: %s" % ("有 ✓" if VERIFY.read_bytes().startswith(BOM)
else "缺!"))
return 0
if __name__ == "__main__":
sys.exit(main())