steam-backlog-enforcer/_installer_import/tests/test_library.py
Krzysztof kuhy Rudnicki aaf9c18a8a fix(recognize): stop Steam re-downloading after a hand-finished install
The KCD2 run reported success but Steam re-downloaded 90 GB anyway. Two
independent causes, plus a defect that hid both.

force_install_dir sets a Steam *library root*, not an install directory.
Pointing it at the game folder made steamcmd search <game>/steamapps/ for
manifests ("Loaded 0 apps from install folder"), create that stray dir, and
fail the app with "Invalid platform" for lack of depot context. Dropped it;
the app already lives in a registered library. Separately, a Linux steamcmd
needs +@sSteamCmdForcePlatformType windows for a Windows-only title.

Steam then discarded the manifest because it was self-contradictory:
StateFlags=4 while BytesDownloaded (22 GB) != BytesToDownload (92 GB), with
tens of GB still staged under steamapps/downloading/. Across every genuinely
installed manifest the byte counters are equal in pairs, StagingSize is 0 and
staging is empty; DownloadType is kept, not dropped. Settle all of it, and
refuse to call an install real until it holds.

The defect that hid this: steamcmd exited 8 having done nothing and the tool
still wrote StateFlags=4, printed a success line and returned 0. Now gated on
depotdownloader succeeding, every required depot being cached, real game
content on disk, a matching buildid and no running Steam client -- and it
re-reads the manifest after writing.

Also fixes data corruption found in review: vdf.dumps never re-escaped
backslashes, so each parse/dump cycle halved them and silently mangled the
Windows paths in appmanifest_228980.acf's InstallScripts. Manifest writes are
now atomic with a preserved pristine .acf.bak.

Adds per-run logging to ~/.local/state/steam-game-installer (argv, exit codes,
full subprocess output, manifest before/after), teed through a pty so
steamcmd's password prompt still works.

Verified end-to-end on KCD2: Steam logged "Fully Installed, App Running" and
the game launched with no download queued. Coverage 100% statements+branches,
enforced by fail_under.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EK4urCmbTqN4Vrc3nGBcXF
2026-08-05 10:11:34 +02:00

157 lines
5.2 KiB
Python

from pathlib import Path
import pytest
from steam_installer import library, vdf
from steam_installer.library import (
SteamLibrary,
find_existing_install,
list_libraries,
pick_install_target,
)
def _make_steam_root(tmp_path: Path, extra_library: Path | None = None) -> Path:
root = tmp_path / "Steam"
(root / "steamapps").mkdir(parents=True)
if extra_library is not None:
(root / "steamapps" / "libraryfolders.vdf").write_text(
f"""
"libraryfolders"
{{
"0"
{{
"path" "{extra_library}"
}}
}}
"""
)
return root
def test_list_libraries_includes_root_when_no_vdf(tmp_path):
root = _make_steam_root(tmp_path)
libraries = list_libraries(root)
assert [lib.path for lib in libraries] == [root]
def test_list_libraries_includes_extra_library_from_vdf(tmp_path):
extra = tmp_path / "SecondDrive"
root = _make_steam_root(tmp_path, extra_library=extra)
libraries = list_libraries(root)
assert {lib.path for lib in libraries} == {root, extra.resolve()}
def test_find_existing_install_locates_game_in_any_library(tmp_path):
root = _make_steam_root(tmp_path)
other = tmp_path / "Other"
(other / "steamapps" / "common" / "MyGame").mkdir(parents=True)
libraries = [SteamLibrary(root), SteamLibrary(other)]
found = find_existing_install(libraries, "MyGame")
assert found == other / "steamapps" / "common" / "MyGame"
def test_find_existing_install_returns_none_when_absent(tmp_path):
root = _make_steam_root(tmp_path)
libraries = [SteamLibrary(root)]
assert find_existing_install(libraries, "NoSuchGame") is None
def test_pick_install_target_prefers_existing_install(tmp_path):
root = _make_steam_root(tmp_path)
other = tmp_path / "Other"
(other / "steamapps" / "common" / "MyGame").mkdir(parents=True)
libraries = [SteamLibrary(root), SteamLibrary(other)]
target = pick_install_target(libraries, "MyGame")
assert target == other / "steamapps" / "common" / "MyGame"
def test_pick_install_target_defaults_to_primary_library(tmp_path):
root = _make_steam_root(tmp_path)
libraries = [SteamLibrary(root)]
target = pick_install_target(libraries, "NewGame")
assert target == root / "steamapps" / "common" / "NewGame"
def test_find_steam_root_prefers_default_location(tmp_path, monkeypatch):
root = tmp_path / ".local/share/Steam"
(root / "steamapps").mkdir(parents=True)
monkeypatch.setattr(library, "DEFAULT_STEAM_ROOT", root)
monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path))
assert library.find_steam_root() == root
def test_find_steam_root_falls_back_to_dot_steam(tmp_path, monkeypatch):
fallback = tmp_path / ".steam/steam"
(fallback / "steamapps").mkdir(parents=True)
monkeypatch.setattr(library, "DEFAULT_STEAM_ROOT", tmp_path / "absent")
monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path))
assert library.find_steam_root() == fallback
def test_find_steam_root_raises_when_no_steam_install(tmp_path, monkeypatch):
monkeypatch.setattr(library, "DEFAULT_STEAM_ROOT", tmp_path / "absent")
monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path))
with pytest.raises(FileNotFoundError, match="No Steam installation"):
library.find_steam_root()
def test_list_libraries_ignores_malformed_libraryfolders(tmp_path):
"""A malformed or partial libraryfolders.vdf must degrade to "just the
primary library", never crash the run."""
root = tmp_path / "Steam"
(root / "steamapps").mkdir(parents=True)
(root / "steamapps" / "libraryfolders.vdf").write_text(
vdf.dumps("libraryfolders", {"contentstatsid": "123"})
)
assert [lib.path for lib in library.list_libraries(root)] == [root]
def test_list_libraries_skips_entries_without_a_path(tmp_path):
root = tmp_path / "Steam"
(root / "steamapps").mkdir(parents=True)
other = tmp_path / "hdd"
other.mkdir()
(root / "steamapps" / "libraryfolders.vdf").write_text(
vdf.dumps(
"libraryfolders",
{
"0": {"path": str(root)},
"1": {"label": "no path here"},
"2": {"path": str(other)},
},
)
)
paths = [lib.path for lib in library.list_libraries(root)]
assert paths == [root, other.resolve()]
def test_list_libraries_ignores_non_dict_folder_entries(tmp_path):
root = tmp_path / "Steam"
(root / "steamapps").mkdir(parents=True)
(root / "steamapps" / "libraryfolders.vdf").write_text(
'"libraryfolders"\n{\n\t"0"\t\t"not-a-dict"\n}\n'
)
assert [lib.path for lib in library.list_libraries(root)] == [root]
def test_list_libraries_ignores_non_dict_libraryfolders_root(tmp_path, monkeypatch):
"""Defensive guard: vdf.parse always yields a dict for a block today, so
inject a parser that does not, keeping the guard honest if vdf changes."""
root = tmp_path / "Steam"
(root / "steamapps").mkdir(parents=True)
(root / "steamapps" / "libraryfolders.vdf").write_text(
vdf.dumps("libraryfolders", {"0": {"path": str(tmp_path)}})
)
monkeypatch.setattr(
"steam_installer.library.vdf.parse", lambda text: {"libraryfolders": "scalar"}
)
assert [lib.path for lib in library.list_libraries(root)] == [root]