feat: add complete yiyezhqiu productivity suite
This commit is contained in:
commit
e422ca8b2a
155 files changed
+18959
No files matched your search
@@ -0,0 +1,67 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
|
||||
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"
|
||||
@@ -0,0 +1,71 @@
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
|
||||
def make_local_release_pair(tmp_path: Path):
|
||||
from dx_productivity.update.models import UpdateManifest
|
||||
|
||||
old_root = tmp_path / "portable"
|
||||
old_root.mkdir()
|
||||
(old_root / "DxDesignerTools.exe").write_bytes(b"old")
|
||||
(old_root / "version.json").write_text('{"version":"1.0.0"}', encoding="utf-8")
|
||||
new_root = tmp_path / "new-release"
|
||||
new_root.mkdir()
|
||||
(new_root / "DxDesignerTools.exe").write_bytes(b"new")
|
||||
(new_root / "version.json").write_text('{"version":"1.1.0"}', encoding="utf-8")
|
||||
new_zip = tmp_path / "release-1.1.0.zip"
|
||||
with zipfile.ZipFile(new_zip, "w") as archive:
|
||||
for path in new_root.iterdir():
|
||||
archive.write(path, path.name)
|
||||
digest = hashlib.sha256(new_zip.read_bytes()).hexdigest()
|
||||
manifest_path = tmp_path / "manifest.json"
|
||||
manifest_path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": "1.1.0",
|
||||
"channel": "development",
|
||||
"min_supported_version": "1.0.0",
|
||||
"package_url": new_zip.as_uri(),
|
||||
"sha256": digest,
|
||||
"release_notes": "local test",
|
||||
"published_at": "2026-09-11",
|
||||
}
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
return old_root, new_zip, manifest_path
|
||||
|
||||
|
||||
def run_update_check_and_install(root: Path, manifest: Path):
|
||||
from dx_productivity.update.installer import InstallPlan, UpdateInstaller
|
||||
from dx_productivity.update.service import UpdateService
|
||||
from dx_productivity.update.stager import UpdateStager
|
||||
|
||||
result = UpdateService().check(manifest.as_uri(), read_version(root), channel="development")
|
||||
assert result.status == "available"
|
||||
assert result.manifest is not None
|
||||
staged = UpdateStager().download_and_verify(result.manifest, root.parent / "updates")
|
||||
plan = InstallPlan(
|
||||
current_root=root,
|
||||
staged_zip=staged,
|
||||
backup_root=root.parent / "backups",
|
||||
restart_command=(sys.executable, "-c", "raise SystemExit(0)"),
|
||||
)
|
||||
return UpdateInstaller().install(plan)
|
||||
|
||||
|
||||
def read_version(root: Path) -> str:
|
||||
return json.loads((root / "version.json").read_text(encoding="utf-8"))["version"]
|
||||
|
||||
|
||||
def test_local_manifest_updates_older_portable_copy(tmp_path: Path):
|
||||
old_root, _, manifest = make_local_release_pair(tmp_path)
|
||||
|
||||
result = run_update_check_and_install(old_root, manifest)
|
||||
|
||||
assert result.state == "installed"
|
||||
assert read_version(old_root) == "1.1.0"
|
||||
@@ -0,0 +1,51 @@
|
||||
import pytest
|
||||
|
||||
|
||||
def _mapping(**overrides):
|
||||
data = {
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": "1.1.0",
|
||||
"channel": "stable",
|
||||
"min_supported_version": "1.0.0",
|
||||
"package_url": "file:///release.zip",
|
||||
"sha256": "a" * 64,
|
||||
"release_notes": "模块迁移",
|
||||
"published_at": "2026-09-11",
|
||||
}
|
||||
data.update(overrides)
|
||||
return data
|
||||
|
||||
|
||||
def test_manifest_parses_semver_and_rejects_wrong_product():
|
||||
from dx_productivity.update.models import UpdateManifest
|
||||
|
||||
manifest = UpdateManifest.from_mapping(_mapping())
|
||||
|
||||
assert manifest.version.is_newer_than(manifest.min_supported_version)
|
||||
assert manifest.is_compatible_with("1.0.0")
|
||||
with pytest.raises(ValueError, match="product"):
|
||||
UpdateManifest.from_mapping(_mapping(product="other-product"))
|
||||
|
||||
|
||||
def test_semver_orders_prerelease_before_release():
|
||||
from dx_productivity.update.models import SemVer
|
||||
|
||||
assert SemVer.parse("1.0.0-rc.1") < SemVer.parse("1.0.0")
|
||||
assert SemVer.parse("1.0.0-rc.2") > SemVer.parse("1.0.0-rc.1")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["1", "1.0", "01.0.0", "v1.0.0", "1.0.0+"])
|
||||
def test_semver_requires_strict_three_part_version(value):
|
||||
from dx_productivity.update.models import SemVer
|
||||
|
||||
with pytest.raises(ValueError, match="version"):
|
||||
SemVer.parse(value)
|
||||
|
||||
|
||||
def test_manifest_rejects_invalid_digest_and_incompatible_current_version():
|
||||
from dx_productivity.update.models import UpdateManifest
|
||||
|
||||
with pytest.raises(ValueError, match="sha256"):
|
||||
UpdateManifest.from_mapping(_mapping(sha256="bad"))
|
||||
manifest = UpdateManifest.from_mapping(_mapping(min_supported_version="2.0.0"))
|
||||
assert manifest.is_compatible_with("1.0.0") is False
|
||||
@@ -0,0 +1,209 @@
|
||||
import base64
|
||||
import json
|
||||
from pathlib import Path
|
||||
import zipfile
|
||||
|
||||
import pytest
|
||||
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
||||
|
||||
from dx_productivity.update import keys
|
||||
from dx_productivity.update.models import SemVer, UpdateManifest
|
||||
from dx_productivity.update.stager import UpdateStager, UpdateVerificationError
|
||||
|
||||
|
||||
def test_stager_uses_the_embedded_public_key_by_default():
|
||||
stager = UpdateStager()
|
||||
|
||||
assert stager.public_key == keys.release_public_key()
|
||||
|
||||
|
||||
def test_stable_channel_signed_package_passes_end_to_end(tmp_path):
|
||||
private_key = Ed25519PrivateKey.generate()
|
||||
public_key = private_key.public_key().public_bytes(
|
||||
serialization.Encoding.Raw, serialization.PublicFormat.Raw
|
||||
)
|
||||
package = tmp_path / "pkg.zip"
|
||||
with zipfile.ZipFile(package, "w") as archive:
|
||||
archive.writestr("DxDesignerTools.exe", "binary")
|
||||
from dx_productivity.update.verification import sha256_file
|
||||
|
||||
manifest = UpdateManifest.from_mapping(
|
||||
{
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": "0.2.0",
|
||||
"channel": "stable",
|
||||
"min_supported_version": "0.1.0",
|
||||
"package_url": package.as_uri(),
|
||||
"sha256": sha256_file(package),
|
||||
"release_notes": "",
|
||||
"published_at": "2026-09-29",
|
||||
"signature": base64.b64encode(
|
||||
private_key.sign(package.read_bytes())
|
||||
).decode("ascii"),
|
||||
}
|
||||
)
|
||||
|
||||
staged = UpdateStager(public_key=public_key).download_and_verify(
|
||||
manifest, tmp_path / "updates"
|
||||
)
|
||||
|
||||
assert staged.is_file()
|
||||
|
||||
|
||||
def test_staged_archive_is_reused_when_digest_matches(tmp_path):
|
||||
package = tmp_path / "pkg.zip"
|
||||
with zipfile.ZipFile(package, "w") as archive:
|
||||
archive.writestr("DxDesignerTools.exe", "binary")
|
||||
from dx_productivity.update.verification import sha256_file
|
||||
|
||||
manifest = UpdateManifest.from_mapping(
|
||||
{
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": "0.2.0",
|
||||
"channel": "development",
|
||||
"min_supported_version": "0.1.0",
|
||||
"package_url": package.as_uri(),
|
||||
"sha256": sha256_file(package),
|
||||
"release_notes": "",
|
||||
"published_at": "2026-09-29",
|
||||
}
|
||||
)
|
||||
destination = tmp_path / "updates"
|
||||
first = UpdateStager().download_and_verify(manifest, destination)
|
||||
first_mtime = first.stat().st_mtime_ns
|
||||
|
||||
second = UpdateStager().download_and_verify(manifest, destination)
|
||||
|
||||
assert second == first
|
||||
assert second.stat().st_mtime_ns == first_mtime
|
||||
|
||||
|
||||
def test_corrupt_staged_archive_is_replaced(tmp_path):
|
||||
package = tmp_path / "pkg.zip"
|
||||
with zipfile.ZipFile(package, "w") as archive:
|
||||
archive.writestr("DxDesignerTools.exe", "binary")
|
||||
from dx_productivity.update.verification import sha256_file
|
||||
|
||||
manifest = UpdateManifest.from_mapping(
|
||||
{
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": "0.2.0",
|
||||
"channel": "development",
|
||||
"min_supported_version": "0.1.0",
|
||||
"package_url": package.as_uri(),
|
||||
"sha256": sha256_file(package),
|
||||
"release_notes": "",
|
||||
"published_at": "2026-09-29",
|
||||
}
|
||||
)
|
||||
destination = tmp_path / "updates"
|
||||
UpdateStager().download_and_verify(manifest, destination)
|
||||
(destination / "0.2.0.zip").write_text("corrupted", encoding="utf-8")
|
||||
|
||||
staged = UpdateStager().download_and_verify(manifest, destination)
|
||||
|
||||
assert staged.read_bytes() == package.read_bytes()
|
||||
|
||||
|
||||
def test_package_url_rejects_plain_http():
|
||||
with pytest.raises(ValueError, match="not allowed"):
|
||||
UpdateManifest.from_mapping(
|
||||
{
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": "0.2.0",
|
||||
"channel": "development",
|
||||
"min_supported_version": "0.1.0",
|
||||
"package_url": "http://intranet/pkg.zip",
|
||||
"sha256": "a" * 64,
|
||||
"release_notes": "",
|
||||
"published_at": "2026-09-29",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_semver_prerelease_multidigit_compare():
|
||||
assert SemVer.parse("1.0.0-rc.10") > SemVer.parse("1.0.0-rc.9")
|
||||
assert SemVer.parse("1.0.0-rc.9") < SemVer.parse("1.0.0")
|
||||
assert SemVer.parse("1.0.0") > SemVer.parse("1.0.0-rc.99")
|
||||
|
||||
|
||||
def test_installer_reverifies_staged_digest(tmp_path, monkeypatch):
|
||||
from dx_productivity.update.installer import InstallPlan, UpdateInstaller
|
||||
|
||||
current = tmp_path / "app"
|
||||
current.mkdir()
|
||||
(current / "DxDesignerTools.exe").write_bytes(b"old")
|
||||
staged_zip = tmp_path / "staged.zip"
|
||||
with zipfile.ZipFile(staged_zip, "w") as archive:
|
||||
archive.writestr("DxDesignerTools.exe", "binary")
|
||||
plan = InstallPlan(
|
||||
current_root=current,
|
||||
staged_zip=staged_zip,
|
||||
backup_root=tmp_path / "backups",
|
||||
restart_command=("cmd", "/c", "exit", "0"),
|
||||
sha256="b" * 64,
|
||||
)
|
||||
|
||||
result = UpdateInstaller().install(plan)
|
||||
|
||||
assert result.state == "failed"
|
||||
assert "digest" in result.message
|
||||
|
||||
|
||||
def test_installer_waits_for_parent_exit(tmp_path):
|
||||
from dx_productivity.update import installer as installer_module
|
||||
from dx_productivity.update.installer import InstallPlan, UpdateInstaller
|
||||
|
||||
current = tmp_path / "app"
|
||||
current.mkdir()
|
||||
exe = current / "DxDesignerTools.exe"
|
||||
exe.write_bytes(b"old")
|
||||
staged_zip = tmp_path / "staged.zip"
|
||||
with zipfile.ZipFile(staged_zip, "w") as archive:
|
||||
archive.writestr("DxDesignerTools.exe", "new")
|
||||
plan = InstallPlan(
|
||||
current_root=current,
|
||||
staged_zip=staged_zip,
|
||||
backup_root=tmp_path / "backups",
|
||||
restart_command=("cmd", "/c", "exit", "0"),
|
||||
)
|
||||
|
||||
called = []
|
||||
def fake_wait(self, exe_path):
|
||||
called.append(exe_path)
|
||||
return True
|
||||
|
||||
monkeypatch = pytest.MonkeyPatch()
|
||||
monkeypatch.setattr(
|
||||
installer_module.UpdateInstaller, "_wait_for_parent_exit", fake_wait
|
||||
)
|
||||
result = UpdateInstaller().install(plan)
|
||||
monkeypatch.undo()
|
||||
|
||||
assert called == [exe]
|
||||
assert result.state == "installed"
|
||||
assert (current / "DxDesignerTools.exe").read_bytes() == b"new"
|
||||
|
||||
|
||||
def test_installer_rollback_failure_is_reported(tmp_path, monkeypatch):
|
||||
from dx_productivity.update.installer import InstallPlan, UpdateInstaller
|
||||
|
||||
current = tmp_path / "app"
|
||||
current.mkdir()
|
||||
(current / "DxDesignerTools.exe").write_bytes(b"old")
|
||||
staged_zip = tmp_path / "staged.zip"
|
||||
with zipfile.ZipFile(staged_zip, "w") as archive:
|
||||
archive.writestr("DxDesignerTools.exe", "new")
|
||||
plan = InstallPlan(
|
||||
current_root=current,
|
||||
staged_zip=staged_zip,
|
||||
backup_root=tmp_path / "backups",
|
||||
restart_command=("cmd", "/c", "exit", "3"), # failing probe
|
||||
)
|
||||
|
||||
result = UpdateInstaller().install(plan)
|
||||
|
||||
assert result.state == "rolled-back"
|
||||
assert (current / "DxDesignerTools.exe").read_bytes() == b"old"
|
||||
@@ -0,0 +1,72 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def write_manifest(path: Path, package_url: str, version: str = "1.1.0") -> None:
|
||||
path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": version,
|
||||
"channel": "stable",
|
||||
"min_supported_version": "1.0.0",
|
||||
"package_url": package_url,
|
||||
"sha256": "a" * 64,
|
||||
"release_notes": "模块迁移",
|
||||
"published_at": "2026-09-11",
|
||||
},
|
||||
ensure_ascii=False,
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
|
||||
def test_check_returns_available_for_file_manifest(tmp_path: Path) -> None:
|
||||
from dx_productivity.update.service import UpdateService
|
||||
|
||||
manifest_path = tmp_path / "manifest.json"
|
||||
write_manifest(manifest_path, "file:///release.zip")
|
||||
|
||||
result = UpdateService().check(manifest_path.as_uri(), "1.0.0")
|
||||
|
||||
assert result.status == "available"
|
||||
assert result.manifest is not None
|
||||
assert str(result.manifest.version) == "1.1.0"
|
||||
|
||||
|
||||
def test_check_returns_up_to_date_for_equal_version(tmp_path: Path) -> None:
|
||||
from dx_productivity.update.service import UpdateService
|
||||
|
||||
manifest_path = tmp_path / "manifest.json"
|
||||
write_manifest(manifest_path, "file:///release.zip", version="1.0.0")
|
||||
|
||||
result = UpdateService().check(manifest_path.as_uri(), "1.0.0")
|
||||
|
||||
assert result.status == "up-to-date"
|
||||
|
||||
|
||||
def test_check_returns_error_for_offline_or_malformed_source(tmp_path: Path) -> None:
|
||||
from dx_productivity.update.service import UpdateService
|
||||
|
||||
malformed = tmp_path / "bad.json"
|
||||
malformed.write_text("not-json", encoding="utf-8")
|
||||
result = UpdateService().check(malformed.as_uri(), "1.0.0")
|
||||
|
||||
assert result.status == "error"
|
||||
assert result.manifest is None
|
||||
assert result.message
|
||||
|
||||
|
||||
def test_check_filters_development_manifest_from_stable_channel(tmp_path: Path) -> None:
|
||||
from dx_productivity.update.service import UpdateService
|
||||
|
||||
manifest_path = tmp_path / "dev.json"
|
||||
write_manifest(manifest_path, "file:///release.zip")
|
||||
data = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
data["channel"] = "development"
|
||||
manifest_path.write_text(json.dumps(data), encoding="utf-8")
|
||||
|
||||
result = UpdateService().check(manifest_path.as_uri(), "1.0.0", channel="stable")
|
||||
|
||||
assert result.status == "incompatible"
|
||||
assert "channel" in result.message
|
||||
@@ -0,0 +1,68 @@
|
||||
import base64
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import zipfile
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _make_manifest(package_url: str, sha256: str, signature: str | None = None):
|
||||
from dx_productivity.update.models import UpdateManifest
|
||||
|
||||
return UpdateManifest.from_mapping(
|
||||
{
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": "1.1.0",
|
||||
"channel": "development" if signature is None else "stable",
|
||||
"min_supported_version": "1.0.0",
|
||||
"package_url": package_url,
|
||||
"sha256": sha256,
|
||||
"release_notes": "test",
|
||||
"published_at": "2026-09-11",
|
||||
**({"signature": signature} if signature is not None else {}),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _zip(path: Path, name: str = "DxDesignerTools.exe") -> bytes:
|
||||
with zipfile.ZipFile(path, "w") as archive:
|
||||
archive.writestr(name, b"new")
|
||||
return path.read_bytes()
|
||||
|
||||
|
||||
def test_download_and_verify_rejects_digest_mismatch(tmp_path: Path):
|
||||
from dx_productivity.update.stager import UpdateStager, UpdateVerificationError
|
||||
|
||||
package_path = tmp_path / "release.zip"
|
||||
payload = _zip(package_path)
|
||||
manifest = _make_manifest(package_path.as_uri(), "0" * 64)
|
||||
|
||||
with pytest.raises(UpdateVerificationError, match="sha256"):
|
||||
UpdateStager().download_and_verify(manifest, tmp_path / "download")
|
||||
assert not list((tmp_path / "download").glob("*.part"))
|
||||
|
||||
|
||||
def test_download_and_verify_accepts_development_package(tmp_path: Path):
|
||||
from dx_productivity.update.stager import UpdateStager
|
||||
|
||||
package_path = tmp_path / "release.zip"
|
||||
payload = _zip(package_path)
|
||||
manifest = _make_manifest(package_path.as_uri(), hashlib.sha256(payload).hexdigest())
|
||||
|
||||
verified = UpdateStager().download_and_verify(manifest, tmp_path / "download")
|
||||
|
||||
assert verified.is_file()
|
||||
assert verified.read_bytes() == payload
|
||||
|
||||
|
||||
def test_download_and_verify_rejects_zip_path_traversal(tmp_path: Path):
|
||||
from dx_productivity.update.stager import UpdateStager, UpdateVerificationError
|
||||
|
||||
package_path = tmp_path / "unsafe.zip"
|
||||
with zipfile.ZipFile(package_path, "w") as archive:
|
||||
archive.writestr("../escape.txt", b"bad")
|
||||
payload = package_path.read_bytes()
|
||||
manifest = _make_manifest(package_path.as_uri(), hashlib.sha256(payload).hexdigest())
|
||||
|
||||
with pytest.raises(UpdateVerificationError, match="path"):
|
||||
UpdateStager().download_and_verify(manifest, tmp_path / "download")
|
||||
@@ -0,0 +1,19 @@
|
||||
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
||||
|
||||
|
||||
def test_sha256_file_and_ed25519_verification(tmp_path):
|
||||
from dx_productivity.update.verification import sha256_file, verify_ed25519
|
||||
|
||||
payload = b"release payload"
|
||||
path = tmp_path / "payload.bin"
|
||||
path.write_bytes(payload)
|
||||
private_key = Ed25519PrivateKey.generate()
|
||||
signature = private_key.sign(payload)
|
||||
|
||||
assert sha256_file(path) == "d11a02dfb3d8dfdc9229678b67f948c32aabeb9e5d409434b171cd7e5275774f"
|
||||
assert verify_ed25519(
|
||||
private_key.public_key().public_bytes_raw(), payload, signature
|
||||
)
|
||||
assert not verify_ed25519(
|
||||
private_key.public_key().public_bytes_raw(), b"changed", signature
|
||||
)
|
||||
Reference in new issue
Block a user