"""打包辅助脚本的回归测试(config 改写与 paddle 变体探测)。"""
import os
import re
import shutil
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"
GUI_HTML = PROJECT_ROOT / "scripts" / "GUI.html"
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_gui_html_javascript_parses(tmp_path: Path) -> None:
"""GUI.html 里的 script 必须能通过 JS 语法检查。
实测踩过:一句 `progressFill?.style.width = '100%'` 是语法错误(可选链不能做
赋值目标),整个 script 块解析失败——页面加载正常,但所有按钮都没反应,
浏览器只在控制台报一条错,很容易被误判成「服务连不上」。
"""
node = shutil.which("node")
if node is None:
pytest.skip("本机没有 node,跳过 JS 语法检查")
html = GUI_HTML.read_text(encoding="utf-8")
match = re.search(r"", html, re.S)
assert match, "GUI.html 里没找到