import hashlib import json import os from pathlib import Path import shutil import subprocess REPO_ROOT = Path(__file__).resolve().parents[2] RELEASE_ROOT = REPO_ROOT / "release" / "DxDesigner效率工具" def _sanitized_environment() -> dict[str, str]: environment = os.environ.copy() windows = Path(os.environ.get("WINDIR", "C:/Windows")) environment["PATH"] = os.pathsep.join( (str(windows / "System32"), str(windows)) ) environment["QT_QPA_PLATFORM"] = "offscreen" environment.pop("PYTHONHOME", None) environment.pop("PYTHONPATH", None) return environment def test_release_contains_portable_runtime_and_root_scripts() -> None: expected = ( "DxDesignerTools.exe", "_runtime/python312.dll", "_runtime/PySide6/plugins/platforms/qwindows.dll", "_runtime/vbs/BridgeCommon.vbs", "_runtime/vbs/Snapshot.vbs", "_runtime/vbs/Transaction.vbs", "_runtime/vbs/Mailbox.vbs", "_runtime/vbs/BomExport.vbs", "MenuAdd.vbs", "DesignBridge.vbs", "CompatibilityProbe.vbs", "templates/BOM模板.xls", "expedition/ExpeditionBridge.vbs", "docs/DxDesigner_7.9.5_自动位号工具使用说明.docx", ) for relative_path in expected: assert (RELEASE_ROOT / relative_path).is_file(), relative_path assert any( path.suffix in {".ttf", ".otf"} for path in (RELEASE_ROOT / "_runtime").rglob("*") ) def test_release_does_not_bundle_foreign_icu_from_build_path() -> None: runtime = RELEASE_ROOT / "_runtime" assert not (runtime / "icuuc.dll").exists() assert not list(runtime.glob("icudt*.dll")) def test_packaged_probe_runs_without_python_on_path(tmp_path: Path) -> None: output = tmp_path / "兼容性 结果.json" result = subprocess.run( [ str(RELEASE_ROOT / "DxDesignerTools.exe"), "probe", "--output", str(output), "--route", "named", "--payload-json", '{"portable":true}', ], env=_sanitized_environment(), timeout=30, check=False, ) assert result.returncode == 0 assert json.loads(output.read_text(encoding="utf-8"))["portable"] is True def test_packaged_ui_loads_request_without_python_on_path() -> None: result = subprocess.run( [ str(RELEASE_ROOT / "DxDesignerTools.exe"), "ui", "--request", str(REPO_ROOT / "tests" / "fixtures" / "request-basic.json"), "--smoke-test-ms", "250", ], env=_sanitized_environment(), timeout=30, check=False, ) assert result.returncode == 0 def test_packaged_application_runs_from_chinese_path_with_spaces( tmp_path: Path, ) -> None: copied_root = tmp_path / "中文 工具" shutil.copytree(RELEASE_ROOT, copied_root) output = tmp_path / "兼容性 结果.json" probe = subprocess.run( [ str(copied_root / "DxDesignerTools.exe"), "probe", "--output", str(output), "--route", "named", "--payload-json", '{"portable":true}', ], env=_sanitized_environment(), timeout=30, check=False, ) ui = subprocess.run( [ str(copied_root / "DxDesignerTools.exe"), "ui", "--request", str(REPO_ROOT / "tests" / "fixtures" / "request-basic.json"), "--smoke-test-ms", "250", ], env=_sanitized_environment(), timeout=30, check=False, ) assert probe.returncode == 0 assert json.loads(output.read_text(encoding="utf-8"))["portable"] is True assert ui.returncode == 0 def test_release_archive_matches_sha256_manifest() -> None: archive = REPO_ROOT / "release" / "DxDesigner效率工具_Win11_x64.zip" manifest = REPO_ROOT / "release" / "SHA256SUMS.txt" digest = hashlib.sha256(archive.read_bytes()).hexdigest() assert manifest.read_text(encoding="utf-8").split()[0] == digest