import json import os from pathlib import Path import subprocess import sys import zipfile import pytest def make_plan(tmp_path: Path, restart_probe_exit_code: int): from dx_productivity.update.installer import InstallPlan current = tmp_path / "portable" current.mkdir() (current / "DxDesignerTools.exe").write_bytes(b"old") staged = tmp_path / "staged.zip" with zipfile.ZipFile(staged, "w") as archive: archive.writestr("DxDesignerTools.exe", b"new") backup = tmp_path / "backups" return InstallPlan( current_root=current, staged_zip=staged, backup_root=backup, restart_command=( sys.executable, "-c", f"raise SystemExit({restart_probe_exit_code})", ), ) def test_install_plan_restores_previous_root_when_restart_probe_fails(tmp_path: Path): from dx_productivity.update.installer import UpdateInstaller plan = make_plan(tmp_path, restart_probe_exit_code=1) result = UpdateInstaller().install(plan) assert result.state == "rolled-back" assert (plan.current_root / "DxDesignerTools.exe").read_bytes() == b"old" assert any(plan.backup_root.iterdir()) def test_install_plan_switches_to_verified_root_on_success(tmp_path: Path): from dx_productivity.update.installer import UpdateInstaller plan = make_plan(tmp_path, restart_probe_exit_code=0) result = UpdateInstaller().install(plan) assert result.state == "installed" assert (plan.current_root / "DxDesignerTools.exe").read_bytes() == b"new" def test_install_plan_rejects_missing_staged_zip(tmp_path: Path): from dx_productivity.update.installer import InstallPlan, UpdateInstaller plan = InstallPlan( current_root=tmp_path / "portable", staged_zip=tmp_path / "missing.zip", backup_root=tmp_path / "backups", restart_command=(sys.executable, "-c", "raise SystemExit(0)"), ) plan.current_root.mkdir() (plan.current_root / "DxDesignerTools.exe").write_bytes(b"old") result = UpdateInstaller().install(plan) assert result.state == "failed" def test_installer_accepts_release_zip_with_portable_folder(tmp_path: Path): """Compress-Archive ships the complete portable folder inside the ZIP.""" from dx_productivity.update.installer import UpdateInstaller plan = make_plan(tmp_path, restart_probe_exit_code=0) with zipfile.ZipFile(plan.staged_zip, "w") as archive: archive.writestr("一叶知秋_重构版/DxDesignerTools.exe", b"new") archive.writestr("一叶知秋_重构版/_runtime/example.dll", b"runtime") result = UpdateInstaller().install(plan) assert result.state == "installed", result.message assert (plan.current_root / "DxDesignerTools.exe").read_bytes() == b"new" assert (plan.current_root / "_runtime/example.dll").read_bytes() == b"runtime" def test_prepare_updater_copies_runtime_outside_running_portable_root(tmp_path: Path): from dx_productivity.update.installer import prepare_updater current = tmp_path / "portable" (current / "_runtime").mkdir(parents=True) (current / "DxDesignerTools.exe").write_bytes(b"exe") (current / "_runtime/python312.dll").write_bytes(b"runtime") executable = prepare_updater(current, tmp_path / "updates") assert not executable.is_relative_to(current) assert executable.read_bytes() == b"exe" assert (executable.parent / "_runtime/python312.dll").read_bytes() == b"runtime" @pytest.mark.skipif(os.name != "nt", reason="Windows update mutex") def test_update_mutex_blocks_another_python_process(tmp_path: Path): from dx_productivity.update.installer import _single_instance_lock script = "\n".join(( "import sys", "from pathlib import Path", "from dx_productivity.update.installer import _single_instance_lock", "try:", " with _single_instance_lock(Path(sys.argv[1])):", " raise SystemExit(2)", "except RuntimeError:", " raise SystemExit(0)", )) environment = os.environ.copy() environment["PYTHONPATH"] = str(Path(__file__).resolve().parents[2] / "src") with _single_instance_lock(tmp_path): child = subprocess.run( [sys.executable, "-c", script, str(tmp_path)], env=environment, timeout=10, capture_output=True, ) assert child.returncode == 0, child.stderr