Add forced startup updater
This commit is contained in:
1 parent
9ffbe47bdb
commit
d48fa4eec3
9 files changed
+493
No files matched your search
@@ -0,0 +1,180 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_compare_versions_handles_v_prefix_and_patch_order():
|
||||
from douyin.updater.force_update import compare_versions
|
||||
|
||||
assert compare_versions("V3.3.10", "3.3.9") > 0
|
||||
assert compare_versions("3.3.3", "V3.3.3") == 0
|
||||
assert compare_versions("3.3.2", "3.3.3") < 0
|
||||
|
||||
|
||||
def test_load_settings_is_disabled_without_manifest_url(tmp_path, monkeypatch):
|
||||
from douyin.core import path_config
|
||||
from douyin.updater.force_update import load_update_settings
|
||||
|
||||
cfg = tmp_path / "config.json"
|
||||
cfg.write_text(json.dumps({"auto_update": {"enabled": True}}), encoding="utf-8")
|
||||
monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg))
|
||||
monkeypatch.delenv("DOUYIN_UPDATE_MANIFEST_URL", raising=False)
|
||||
|
||||
settings = load_update_settings()
|
||||
|
||||
assert settings.enabled is False
|
||||
assert settings.manifest_url == ""
|
||||
|
||||
|
||||
def test_forced_update_downloads_verifies_and_launches_installer(tmp_path):
|
||||
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
||||
|
||||
payload = b"fake installer bytes"
|
||||
expected_sha = hashlib.sha256(payload).hexdigest()
|
||||
settings = UpdateSettings(
|
||||
enabled=True,
|
||||
manifest_url="https://example.test/update.json",
|
||||
download_dir=tmp_path,
|
||||
installer_args=("/VERYSILENT", "/CURRENTUSER"),
|
||||
)
|
||||
downloaded: list[str] = []
|
||||
launched: list[list[str]] = []
|
||||
|
||||
def fetch_manifest(url: str, timeout: float) -> dict:
|
||||
assert url == settings.manifest_url
|
||||
return {
|
||||
"version": "3.3.4",
|
||||
"url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
||||
"sha256": expected_sha,
|
||||
"force": True,
|
||||
}
|
||||
|
||||
def download(url: str, dest: Path, timeout: float) -> None:
|
||||
downloaded.append(url)
|
||||
assert dest.parent.exists()
|
||||
dest.write_bytes(payload)
|
||||
|
||||
def launch(cmd: list[str]) -> None:
|
||||
launched.append(cmd)
|
||||
|
||||
result = perform_forced_update(
|
||||
settings,
|
||||
current_version="3.3.3",
|
||||
fetch_manifest=fetch_manifest,
|
||||
download_file=download,
|
||||
launch_installer=launch,
|
||||
)
|
||||
|
||||
assert result.status == "update_started"
|
||||
assert result.exit_required is True
|
||||
assert downloaded == ["https://example.test/DouyinGuaji-3.3.4-Setup.exe"]
|
||||
assert launched == [
|
||||
[str(tmp_path / "DouyinGuaji-3.3.4-Setup.exe"), "/VERYSILENT", "/CURRENTUSER"]
|
||||
]
|
||||
|
||||
|
||||
def test_forced_update_rejects_hash_mismatch(tmp_path):
|
||||
from douyin.updater.force_update import (
|
||||
UpdateError,
|
||||
UpdateSettings,
|
||||
perform_forced_update,
|
||||
)
|
||||
|
||||
settings = UpdateSettings(
|
||||
enabled=True,
|
||||
manifest_url="https://example.test/update.json",
|
||||
download_dir=tmp_path,
|
||||
)
|
||||
|
||||
def fetch_manifest(url: str, timeout: float) -> dict:
|
||||
return {
|
||||
"version": "3.3.4",
|
||||
"url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
||||
"sha256": "0" * 64,
|
||||
"force": True,
|
||||
}
|
||||
|
||||
def download(url: str, dest: Path, timeout: float) -> None:
|
||||
dest.write_bytes(b"tampered")
|
||||
|
||||
with pytest.raises(UpdateError, match="sha256"):
|
||||
perform_forced_update(
|
||||
settings,
|
||||
current_version="3.3.3",
|
||||
fetch_manifest=fetch_manifest,
|
||||
download_file=download,
|
||||
launch_installer=lambda cmd: None,
|
||||
)
|
||||
|
||||
|
||||
def test_forced_update_ignores_manifest_force_false_for_newer_version(tmp_path):
|
||||
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
||||
|
||||
payload = b"forced installer"
|
||||
expected_sha = hashlib.sha256(payload).hexdigest()
|
||||
launched: list[list[str]] = []
|
||||
|
||||
def fetch_manifest(url: str, timeout: float) -> dict:
|
||||
return {
|
||||
"version": "3.3.4",
|
||||
"url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
||||
"sha256": expected_sha,
|
||||
"force": False,
|
||||
}
|
||||
|
||||
def download(url: str, dest: Path, timeout: float) -> None:
|
||||
dest.write_bytes(payload)
|
||||
|
||||
result = perform_forced_update(
|
||||
UpdateSettings(enabled=True, manifest_url="https://example.test/update.json", download_dir=tmp_path),
|
||||
current_version="3.3.3",
|
||||
fetch_manifest=fetch_manifest,
|
||||
download_file=download,
|
||||
launch_installer=lambda cmd: launched.append(cmd),
|
||||
)
|
||||
|
||||
assert result.status == "update_started"
|
||||
assert launched
|
||||
|
||||
|
||||
def test_startup_update_raises_system_exit_after_launch(tmp_path):
|
||||
from douyin.updater.force_update import UpdateResult, run_startup_update_check
|
||||
|
||||
called: list[str] = []
|
||||
|
||||
def perform(*, settings, current_version):
|
||||
called.append(current_version)
|
||||
return UpdateResult("update_started", "started", exit_required=True)
|
||||
|
||||
with pytest.raises(SystemExit) as exc:
|
||||
run_startup_update_check(
|
||||
app_name="test_app",
|
||||
current_version="3.3.3",
|
||||
perform_update=perform,
|
||||
)
|
||||
|
||||
assert exc.value.code == 0
|
||||
assert called == ["3.3.3"]
|
||||
|
||||
|
||||
def test_startup_update_fail_closed_blocks_old_client(tmp_path, monkeypatch):
|
||||
from douyin.updater.force_update import UpdateError, run_startup_update_check
|
||||
|
||||
monkeypatch.setenv("DOUYIN_AUTO_UPDATE_ENABLED", "1")
|
||||
monkeypatch.setenv("DOUYIN_UPDATE_MANIFEST_URL", "https://example.test/update.json")
|
||||
|
||||
def perform(*, settings, current_version):
|
||||
raise UpdateError("network down")
|
||||
|
||||
with pytest.raises(SystemExit) as exc:
|
||||
run_startup_update_check(
|
||||
app_name="test_app",
|
||||
current_version="3.3.3",
|
||||
perform_update=perform,
|
||||
)
|
||||
|
||||
assert exc.value.code == 3
|
||||
@@ -14,6 +14,8 @@ def _write(path: Path, text: str = "x = 1\n") -> None:
|
||||
def _make_minimal_project(root: Path) -> None:
|
||||
_write(root / "douyin" / "__init__.py")
|
||||
_write(root / "douyin" / "core" / "version.py")
|
||||
_write(root / "douyin" / "updater" / "__init__.py")
|
||||
_write(root / "douyin" / "updater" / "force_update.py")
|
||||
_write(root / "gui" / "gui_app.py")
|
||||
_write(root / "gui" / "web_gui.py")
|
||||
_write(root / "gui" / "components" / "left_config_area.py")
|
||||
|
||||
Reference in new issue
Block a user