mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-22 23:22:18 +01:00
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
158 lines
6.3 KiB
Python
158 lines
6.3 KiB
Python
import pytest
|
|
from _recognize_helpers import _write_partial_manifest
|
|
from steam_installer import manifest_write, vdf
|
|
from steam_installer.library import SteamLibrary
|
|
from steam_installer.manifest_write import (
|
|
ensure_installdir_correct,
|
|
finish_manifest_manually,
|
|
measure_content_size,
|
|
)
|
|
|
|
|
|
def test_ensure_installdir_correct_patches_stale_field(tmp_path):
|
|
library = SteamLibrary(tmp_path / "Steam")
|
|
_write_partial_manifest(library.steamapps, 1771300)
|
|
data = vdf.parse((library.steamapps / "appmanifest_1771300.acf").read_text())
|
|
data["AppState"]["installdir"] = "Kingdom Come: Deliverance II"
|
|
(library.steamapps / "appmanifest_1771300.acf").write_text(
|
|
vdf.dumps("AppState", data["AppState"])
|
|
)
|
|
|
|
install_dir = library.common / "KingdomComeDeliverance2"
|
|
ensure_installdir_correct(library, 1771300, install_dir)
|
|
|
|
app_state = vdf.parse((library.steamapps / "appmanifest_1771300.acf").read_text())[
|
|
"AppState"
|
|
]
|
|
assert app_state["installdir"] == "KingdomComeDeliverance2"
|
|
|
|
|
|
def test_ensure_installdir_correct_noop_when_already_correct(tmp_path):
|
|
library = SteamLibrary(tmp_path / "Steam")
|
|
_write_partial_manifest(library.steamapps, 1771300)
|
|
manifest_path = library.steamapps / "appmanifest_1771300.acf"
|
|
data = vdf.parse(manifest_path.read_text())
|
|
data["AppState"]["installdir"] = "KingdomComeDeliverance2"
|
|
manifest_path.write_text(vdf.dumps("AppState", data["AppState"]))
|
|
before = manifest_path.read_text()
|
|
|
|
ensure_installdir_correct(
|
|
library, 1771300, library.common / "KingdomComeDeliverance2"
|
|
)
|
|
|
|
assert manifest_path.read_text() == before
|
|
|
|
|
|
def test_ensure_installdir_correct_noop_when_no_manifest(tmp_path):
|
|
library = SteamLibrary(tmp_path / "Steam")
|
|
library.steamapps.mkdir(parents=True)
|
|
# Should not raise, and should not create a manifest out of nothing.
|
|
ensure_installdir_correct(library, 1771300, library.common / "Game")
|
|
assert not (library.steamapps / "appmanifest_1771300.acf").exists()
|
|
|
|
|
|
def test_measure_content_size_excludes_depotdownloader_cache(tmp_path):
|
|
install_dir = tmp_path / "Game"
|
|
cache_dir = install_dir / ".DepotDownloader" / "staging" / "Data"
|
|
cache_dir.mkdir(parents=True)
|
|
(install_dir / "game.bin").write_bytes(b"x" * 100)
|
|
(install_dir / ".DepotDownloader" / "d.manifest").write_bytes(b"y" * 5000)
|
|
(cache_dir / "staged.pak").write_bytes(b"z" * 9000)
|
|
|
|
assert measure_content_size(install_dir) == 100
|
|
|
|
|
|
def test_backup_keeps_the_pristine_manifest_across_reruns(tmp_path):
|
|
"""A second run must not overwrite the backup with run 1's patched output --
|
|
that turns the recovery artifact for a 90+ GB install into a copy of the
|
|
thing it is supposed to protect against."""
|
|
library = SteamLibrary(tmp_path / "Steam")
|
|
_write_partial_manifest(library.steamapps, 1771300)
|
|
manifest = library.steamapps / "appmanifest_1771300.acf"
|
|
pristine = manifest.read_text()
|
|
install_dir = library.common / "Game"
|
|
cache = install_dir / ".DepotDownloader"
|
|
cache.mkdir(parents=True)
|
|
(cache / "1771303_1999516201078544527.manifest").write_bytes(b"x")
|
|
|
|
finish_manifest_manually(library, 1771300, install_dir, now_unix=1000)
|
|
finish_manifest_manually(library, 1771300, install_dir, now_unix=2000)
|
|
|
|
assert manifest.with_suffix(".acf.bak").read_text() == pristine
|
|
|
|
|
|
def test_manifest_write_is_atomic(tmp_path, monkeypatch):
|
|
"""A crash mid-write must not leave a truncated, unparseable manifest."""
|
|
library = SteamLibrary(tmp_path / "Steam")
|
|
_write_partial_manifest(library.steamapps, 1771300)
|
|
manifest = library.steamapps / "appmanifest_1771300.acf"
|
|
before = manifest.read_text()
|
|
install_dir = library.common / "Game"
|
|
cache = install_dir / ".DepotDownloader"
|
|
cache.mkdir(parents=True)
|
|
(cache / "1771303_1999516201078544527.manifest").write_bytes(b"x")
|
|
|
|
def explode(self, *args, **kwargs):
|
|
raise OSError(28, "No space left on device")
|
|
|
|
monkeypatch.setattr("pathlib.Path.write_text", explode)
|
|
with pytest.raises(OSError):
|
|
finish_manifest_manually(library, 1771300, install_dir, now_unix=1000)
|
|
|
|
# Original intact, and no temp file left behind.
|
|
assert manifest.read_text() == before
|
|
assert list(library.steamapps.glob("*.tmp*")) == []
|
|
|
|
|
|
def test_ensure_installdir_correct_ignores_malformed_manifest(tmp_path, monkeypatch):
|
|
"""Defensive guard: vdf.parse always yields a dict today, so inject a parser
|
|
that does not, to keep the guard honest if vdf ever changes."""
|
|
library = SteamLibrary(tmp_path / "Steam")
|
|
library.steamapps.mkdir(parents=True)
|
|
manifest = library.steamapps / "appmanifest_1771300.acf"
|
|
manifest.write_text(vdf.dumps("AppState", {"installdir": "Stale"}))
|
|
before = manifest.read_text()
|
|
monkeypatch.setattr(
|
|
"steam_installer.vdf.parse", lambda text: {"AppState": "scalar"}
|
|
)
|
|
|
|
ensure_installdir_correct(library, 1771300, library.common / "Game")
|
|
|
|
assert manifest.read_text() == before
|
|
|
|
|
|
def test_ensure_installdir_correct_is_a_noop_when_already_right(tmp_path):
|
|
library = SteamLibrary(tmp_path / "Steam")
|
|
library.steamapps.mkdir(parents=True)
|
|
manifest = library.steamapps / "appmanifest_1771300.acf"
|
|
manifest.write_text(vdf.dumps("AppState", {"installdir": "Game"}))
|
|
|
|
ensure_installdir_correct(library, 1771300, library.common / "Game")
|
|
|
|
assert not manifest.with_suffix(".acf.bak").exists()
|
|
|
|
|
|
def test_write_atomic_cleans_up_its_temp_file_on_failure(tmp_path, monkeypatch):
|
|
target = tmp_path / "appmanifest_1.acf"
|
|
target.write_text("original")
|
|
|
|
def explode(self, *args, **kwargs):
|
|
raise OSError(28, "No space left on device")
|
|
|
|
monkeypatch.setattr("pathlib.Path.write_text", explode)
|
|
with pytest.raises(OSError):
|
|
manifest_write.write_atomic(target, "new content")
|
|
|
|
assert list(tmp_path.glob("*.tmp*")) == []
|
|
|
|
|
|
def test_measure_content_size_skips_directories_and_symlinks(tmp_path):
|
|
"""Directories have a size of their own, and a symlink would double-count
|
|
its target -- neither is game content Steam should see in SizeOnDisk."""
|
|
install_dir = tmp_path / "Game"
|
|
(install_dir / "Data" / "Levels").mkdir(parents=True)
|
|
(install_dir / "Data" / "real.pak").write_bytes(b"x" * 250)
|
|
(install_dir / "Data" / "link.pak").symlink_to(install_dir / "Data" / "real.pak")
|
|
|
|
assert measure_content_size(install_dir) == 250
|