ec6b9c7ae3
新增 scripts/api-client.bat 与 api-client.sh:上传图片 -> 轮询 -> 打印识别文本与 抽取字段。自动挑解释器(优先 .venv,退回系统 Python);api_client.py 只用标准库, 所以一台只装 Python、不装本项目依赖的机器也能当客户端用。 修掉两个 bat 上的真 bug(都是实测出来的,不是看代码看出来的): 1. 退出码被吞。`if exist ... ( ... exit /b %errorlevel% )` 里的 %errorlevel% 是**解析时**展开的,取到的是启动程序之前的值,于是失败也返回 0。 start.bat 同样中招(启动失败会被吞成成功)。两处都改成 goto 结构。 2. .bat 必须 CRLF。同一份内容,CRLF 正常跑完;LF 会让 cmd 解析挂住—— 无任何输出、也不退出,看起来像服务卡死。download-models.bat 原本也是 LF, 一并修正;新增 .gitattributes 钉死 *.bat=eol:crlf、*.sh=eol:lf。 另:参数校验挪到探活之前,参数写错时不会因为连不上服务报出误导性错误; stdout 加 flush,重定向到文件时输出顺序不再错乱。 测试:新增两条回归用例(bat 必须 CRLF;Windows 上 bat 能被 cmd 跑通,带超时 以免坏掉时把测试卡死)。99 passed。 Co-Authored-By: Claude Code <noreply@anthropic.com>
180 lines
6.8 KiB
Python
180 lines
6.8 KiB
Python
"""打包辅助脚本的回归测试(config 改写与 paddle 变体探测)。"""
|
||
|
||
import os
|
||
import subprocess
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
import pytest
|
||
|
||
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
||
PREPARE_CONFIG = PROJECT_ROOT / "scripts" / "prepare_release_config.py"
|
||
PADDLE_ENV = PROJECT_ROOT / "scripts" / "paddle_env.py"
|
||
INSTALL_PADDLE = PROJECT_ROOT / "scripts" / "install_paddle.py"
|
||
SWITCH_ENV = PROJECT_ROOT / "scripts" / "switch_env.py"
|
||
API_CLIENT = PROJECT_ROOT / "scripts" / "api_client.py"
|
||
API_CLIENT_BAT = PROJECT_ROOT / "scripts" / "api-client.bat"
|
||
|
||
SAMPLE = """\
|
||
# 中文注释必须原样保留
|
||
host: 127.0.0.1
|
||
device: cpu
|
||
text_recognition_batch_size: 32
|
||
"""
|
||
|
||
|
||
def run_script(script: Path, *args: str) -> subprocess.CompletedProcess[str]:
|
||
return subprocess.run(
|
||
[sys.executable, str(script), *args],
|
||
capture_output=True,
|
||
text=True,
|
||
encoding="utf-8",
|
||
cwd=PROJECT_ROOT,
|
||
check=False,
|
||
)
|
||
|
||
|
||
def test_prepare_release_config_overrides_device(tmp_path: Path) -> None:
|
||
source = tmp_path / "config.yaml"
|
||
source.write_text(SAMPLE, encoding="utf-8")
|
||
target = tmp_path / "release" / "config.yaml"
|
||
|
||
result = run_script(PREPARE_CONFIG, str(source), str(target), "device=gpu")
|
||
assert result.returncode == 0, result.stderr
|
||
|
||
written = target.read_text(encoding="utf-8")
|
||
assert "device: gpu" in written
|
||
assert "device: cpu" not in written
|
||
# 未被覆盖的项和中文注释都要原样保留(PowerShell 的默认编码会把中文写坏)。
|
||
assert "text_recognition_batch_size: 32" in written
|
||
assert "# 中文注释必须原样保留" in written
|
||
|
||
|
||
def test_prepare_release_config_appends_missing_key(tmp_path: Path) -> None:
|
||
source = tmp_path / "config.yaml"
|
||
source.write_text(SAMPLE.replace("device: cpu\n", ""), encoding="utf-8")
|
||
target = tmp_path / "config.yaml"
|
||
|
||
result = run_script(PREPARE_CONFIG, str(source), str(target), "device=gpu")
|
||
assert result.returncode == 0, result.stderr
|
||
assert "device: gpu" in target.read_text(encoding="utf-8")
|
||
|
||
|
||
def test_prepare_release_config_rejects_bad_override(tmp_path: Path) -> None:
|
||
source = tmp_path / "config.yaml"
|
||
source.write_text(SAMPLE, encoding="utf-8")
|
||
|
||
result = run_script(PREPARE_CONFIG, str(source), str(tmp_path / "out.yaml"), "device")
|
||
assert result.returncode == 2
|
||
|
||
|
||
def test_paddle_env_reports_variant_and_distribution() -> None:
|
||
variant = run_script(PADDLE_ENV, "variant")
|
||
assert variant.returncode == 0, variant.stderr
|
||
assert variant.stdout.strip() in {"cpu", "gpu"}
|
||
|
||
distribution = run_script(PADDLE_ENV, "distribution")
|
||
assert distribution.returncode == 0, distribution.stderr
|
||
reported = distribution.stdout.strip()
|
||
assert reported in {"paddlepaddle", "paddlepaddle-gpu"}
|
||
# 变体和发行包名必须一致,否则打包时 --copy-metadata 会取错。
|
||
expected = "paddlepaddle-gpu" if variant.stdout.strip() == "gpu" else "paddlepaddle"
|
||
assert reported == expected
|
||
|
||
|
||
def test_paddle_env_rejects_unknown_mode() -> None:
|
||
assert run_script(PADDLE_ENV, "whatever").returncode == 2
|
||
|
||
|
||
def test_install_paddle_dry_run_never_touches_environment() -> None:
|
||
"""--dry-run 只打印方案,不能真的去装东西。"""
|
||
result = run_script(INSTALL_PADDLE, "--dry-run")
|
||
assert result.returncode == 0, result.stderr
|
||
assert "[dry-run]" in result.stdout
|
||
assert "paddlepaddle" in result.stdout
|
||
|
||
|
||
def test_install_paddle_auto_picks_gpu_channel_for_rtx50() -> None:
|
||
"""RTX 50 系(Blackwell sm_120)必须走 cu129,否则推理时报 Mismatched GPU Architecture。"""
|
||
result = run_script(INSTALL_PADDLE, "--gpu", "--dry-run")
|
||
assert result.returncode == 0, result.stderr
|
||
assert "paddlepaddle-gpu==" in result.stdout
|
||
|
||
|
||
def test_install_paddle_rejects_unknown_cuda_channel() -> None:
|
||
result = run_script(INSTALL_PADDLE, "--gpu", "--cuda", "cu999", "--dry-run")
|
||
assert result.returncode == 2
|
||
|
||
|
||
def test_windows_batch_files_use_crlf() -> None:
|
||
""".bat 必须是 CRLF 换行。
|
||
|
||
实测:同一份内容,CRLF 的 bat 正常跑完;LF 的会让 cmd 解析挂住——无任何输出、
|
||
也不退出。这个坑很难查(看起来像服务或脚本卡死),所以用测试钉住。
|
||
"""
|
||
for bat in sorted((PROJECT_ROOT / "scripts").glob("*.bat")):
|
||
raw = bat.read_bytes()
|
||
assert b"\r\n" in raw, f"{bat.name} 没有 CRLF 换行"
|
||
assert raw.replace(b"\r\n", b"").count(b"\n") == 0, f"{bat.name} 里混有 LF 换行"
|
||
|
||
|
||
@pytest.mark.skipif(os.name != "nt", reason=".bat 只能在 Windows 上执行")
|
||
def test_api_client_bat_parses_and_runs() -> None:
|
||
"""bat 能被 cmd 正常解析并透传参数。带超时,坏掉时报失败而不是把测试卡死。"""
|
||
result = subprocess.run(
|
||
["cmd", "/c", str(API_CLIENT_BAT), "--help"],
|
||
capture_output=True,
|
||
text=True,
|
||
encoding="utf-8",
|
||
cwd=PROJECT_ROOT,
|
||
check=False,
|
||
timeout=60,
|
||
)
|
||
assert result.returncode == 0, result.stderr
|
||
assert "api_client.py" in result.stdout
|
||
|
||
|
||
def test_api_client_help_and_missing_args() -> None:
|
||
assert run_script(API_CLIENT, "--help").returncode == 0
|
||
# 不给图片又不带 --health:argparse 应该报错退出(2)
|
||
assert run_script(API_CLIENT).returncode == 2
|
||
|
||
|
||
def test_api_client_fails_cleanly_when_server_unreachable() -> None:
|
||
"""服务不可达时要干净退出并给出可读提示,而不是抛未捕获的异常。
|
||
|
||
不断言具体是「连接被拒」还是「HTTP 错误」:CI/沙箱里可能有透明代理,
|
||
两者都会出现。真正要保证的是有明确报错、没有 traceback。
|
||
"""
|
||
result = run_script(API_CLIENT, "--health", "--host", "http://127.0.0.1:1")
|
||
assert result.returncode == 1
|
||
assert "http://127.0.0.1:1" in result.stderr
|
||
assert "Traceback" not in result.stderr
|
||
|
||
|
||
def test_switch_env_list_reports_both_variants() -> None:
|
||
"""--list 只读,不能改动任何环境。"""
|
||
result = run_script(SWITCH_ENV, "--list")
|
||
assert result.returncode == 0, result.stderr
|
||
assert ".venv" in result.stdout
|
||
assert ".venv-gpu" in result.stdout
|
||
|
||
|
||
def test_switch_env_rejects_cuda_together_with_cpu() -> None:
|
||
result = run_script(SWITCH_ENV, "--cpu", "--cuda", "cu126")
|
||
assert result.returncode == 2
|
||
|
||
|
||
def test_recommend_cuda_matches_gpu_generation() -> None:
|
||
sys.path.insert(0, str(PROJECT_ROOT / "scripts"))
|
||
try:
|
||
import install_paddle
|
||
finally:
|
||
sys.path.pop(0)
|
||
|
||
assert install_paddle.recommend_cuda("NVIDIA GeForce RTX 5060 Laptop GPU") == "cu129"
|
||
assert install_paddle.recommend_cuda("NVIDIA GeForce RTX 4090") == "cu126"
|
||
assert install_paddle.recommend_cuda("NVIDIA GeForce RTX 3060") == "cu126"
|
||
assert install_paddle.recommend_cuda("Tesla T4") == "cu118"
|
||
assert install_paddle.recommend_cuda(None) == "cu126"
|