from __future__ import annotations import subprocess from pathlib import Path import pytest def _write_exe(path: Path) -> None: path.parent.mkdir(parents=True, exist_ok=True) path.write_bytes(b"MZfake") def test_pack_distribution_exes_invokes_upx_for_root_exes_only(tmp_path, monkeypatch): from tools.exe_packing import pack_distribution_exes dist = tmp_path / "dist" for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"): _write_exe(dist / name) _write_exe(dist / "_internal" / "python312.dll") _write_exe(dist / "_internal" / "helper.exe") calls: list[list[str]] = [] monkeypatch.setattr("tools.exe_packing.shutil.which", lambda name: "upx.exe") def fake_runner(cmd, *, check): calls.append([str(x) for x in cmd]) assert check is True return subprocess.CompletedProcess(cmd, 0) packed = pack_distribution_exes(dist, runner=fake_runner) assert packed == [ dist / "DouyinGuajiGUI.exe", dist / "web_gui.exe", dist / "run_cli.exe", ] assert len(calls) == 3 assert all(call[:3] == ["upx.exe", "--best", "--lzma"] for call in calls) assert all("_internal" not in " ".join(call) for call in calls) def test_pack_distribution_exes_can_force_guard_cf_files(tmp_path, monkeypatch): from tools.exe_packing import pack_distribution_exes dist = tmp_path / "dist" for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"): _write_exe(dist / name) calls: list[list[str]] = [] monkeypatch.setattr("tools.exe_packing.shutil.which", lambda name: "upx.exe") def fake_runner(cmd, *, check): calls.append([str(x) for x in cmd]) return subprocess.CompletedProcess(cmd, 0) pack_distribution_exes(dist, force=True, runner=fake_runner) assert all("--force" in call for call in calls) def test_smoke_check_packed_exes_runs_after_packing(tmp_path): from tools.exe_packing import smoke_check_distribution_exes dist = tmp_path / "dist" for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"): _write_exe(dist / name) calls: list[list[str]] = [] def fake_runner(cmd, *, capture_output, text, timeout, env): calls.append([str(x) for x in cmd]) assert capture_output is True assert text is True assert timeout == 20 assert env["DOUYIN_LICENSE_REQUIRED"] == "0" assert env["SKIP_OCR_INIT"] == "1" return subprocess.CompletedProcess(cmd, 0, stdout="ok") smoke_check_distribution_exes(dist, runner=fake_runner) assert calls == [ [str(dist / "run_cli.exe"), "--help"], ] def test_smoke_check_cleans_runtime_logs_it_creates(tmp_path): from tools.exe_packing import smoke_check_distribution_exes dist = tmp_path / "dist" for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"): _write_exe(dist / name) (dist / "logs").mkdir(parents=True) (dist / "logs" / ".keep").write_text("", encoding="utf-8") def fake_runner(cmd, *, capture_output, text, timeout, env): (dist / "logs" / "unified.log").write_text("smoke log", encoding="utf-8") (dist / "logs" / "error.log").write_text("smoke error", encoding="utf-8") return subprocess.CompletedProcess(cmd, 0, stdout="ok") smoke_check_distribution_exes(dist, runner=fake_runner) assert (dist / "logs" / ".keep").exists() assert not (dist / "logs" / "unified.log").exists() assert not (dist / "logs" / "error.log").exists() def test_smoke_check_packed_exes_fails_on_broken_exe(tmp_path): from tools.exe_packing import smoke_check_distribution_exes dist = tmp_path / "dist" for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"): _write_exe(dist / name) def fake_runner(cmd, *, capture_output, text, timeout, env): return subprocess.CompletedProcess(cmd, 1, stdout="", stderr="Failed to load Python DLL") with pytest.raises(RuntimeError, match="smoke check failed"): smoke_check_distribution_exes(dist, runner=fake_runner) def test_pack_distribution_exes_requires_upx(tmp_path, monkeypatch): from tools.exe_packing import pack_distribution_exes for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"): _write_exe(tmp_path / "dist" / name) monkeypatch.setattr("tools.exe_packing.shutil.which", lambda name: None) with pytest.raises(FileNotFoundError, match="upx"): pack_distribution_exes(tmp_path / "dist") def test_pack_distribution_exes_requires_expected_exes(tmp_path, monkeypatch): from tools.exe_packing import pack_distribution_exes monkeypatch.setattr("tools.exe_packing.shutil.which", lambda name: "upx.exe") _write_exe(tmp_path / "dist" / "run_cli.exe") with pytest.raises(RuntimeError, match="DouyinGuajiGUI.exe"): pack_distribution_exes(tmp_path / "dist") def test_windows_packaging_script_exposes_pack_exe_switch(): script = Path("packaging/pyinstaller/build_windows.ps1").read_text(encoding="utf-8") assert "[switch]$PackExe" in script assert "[switch]$PackExeForce" in script assert "[switch]$IncludeLicense" in script assert "[string]$UpxDir" in script assert "tools.exe_packing" in script assert "--smoke-only" in script assert "if ($LASTEXITCODE -ne 0)" in script def test_exe_packing_cli_smoke_only_skips_upx(monkeypatch, tmp_path): from tools import exe_packing dist = tmp_path / "dist" for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"): _write_exe(dist / name) called = {"pack": False, "smoke": False} def fake_pack(*args, **kwargs): called["pack"] = True return [] def fake_smoke(*args, **kwargs): called["smoke"] = True monkeypatch.setattr(exe_packing, "pack_distribution_exes", fake_pack) monkeypatch.setattr(exe_packing, "smoke_check_distribution_exes", fake_smoke) assert exe_packing.main([str(dist), "--smoke-only"]) == 0 assert called == {"pack": False, "smoke": True}