Fetch owned DLC with depotdownloader too; split recognize.py; enforce the 250-line cap

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-09-25 18:41:55 +02:00
parent 15f647f639
commit 8f641e235d
27 changed files with 1705 additions and 1207 deletions

View File

@ -0,0 +1,35 @@
name: file length
# The shared 250-line cap, enforced on push as well as at commit time: the
# pre-commit hook only sees staged files and a fresh clone may never have run
# `pre-commit install`. This checks the whole tree.
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
jobs:
file-length:
name: files at most 250 lines
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# The gate and its exemption list live in the shared utils repo.
- name: Check out the shared gate
uses: actions/checkout@v4
with:
repository: kuhyx/utils
path: .utils
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
# Tracked files only: --all would also walk the .utils checkout above,
# which is another repo's code and not this one's to split.
- name: Every file is at most 250 lines
run: git ls-files -z | xargs -0 bash .utils/scripts/check_file_length.sh

View File

@ -16,6 +16,15 @@ repos:
language: system
files: \.(md|markdown)$
# Shared 250-line cap on every file in the commit, code and prose alike.
# Checks only what is staged, so an unrelated commit never goes red, and
# it fails rather than warns: no baseline, no allowlist.
- id: file-length
name: files at most 250 lines
entry: scripts/check_file_length.sh
language: system
types: [text]
# Every dependency on its ecosystem's newest STABLE release, exact-pinned
# so the question is mechanically answerable at all.
#

View File

@ -51,6 +51,29 @@ place also rewrites Steam's copy. That is deliberate — both names converge on
the same corrected content — but it does mean the staged bytes are working data,
not a frozen backup. On ext4 there is no reflink that would give you both.
## Owned DLC comes down fast too
Some DLC keeps its content under an app id of its own rather than in the
game's depot list -- Cyberpunk 2077's Phantom Liberty is app 2138330, about
25 GB. A depotdownloader run for the game never sees it, and Steam, restarted
onto a manifest without it, downloads all of it at its own pace.
So every DLC in the game's `listofdlc` that has depots of its own gets its own
depotdownloader run into the same install dir. Depotdownloader's licence check
decides ownership: its "not available from this account" is skipped as
expected, any other failure aborts before the manifest is touched. Each DLC
depot is then written into `InstalledDepots` with its `dlcappid`, exactly as
Steam records it.
## Which depotdownloader
Always `~/.local/bin/depotdownloader` (the copy `install.sh` manages) when it
exists, never "whatever is first on PATH". Each depotdownloader binary keeps
its remembered login in its own storage slot, keyed by its path; with an AUR
copy in `/usr/bin` too, a terminal run and a `sudo`'d run (whose PATH lacks
`~/.local/bin`) used different binaries -- one logged in, one prompting for a
password nobody was there to type.
## Ghost installs
`steamcmd +login anonymous +app_update <paid appid>` has no licence for the app.
@ -95,6 +118,7 @@ terminal — with a plain pipe the prompt never appears and the run looks hung.
## How it works
1. `depotdownloader` pulls the content (fast path).
Then each owned DLC with depots of its own gets a run of its own.
2. `steamcmd app_update <id> validate` reconciles Steam's own bookkeeping.
3. If that fails or leaves the manifest incomplete, the manifest is finished by
hand — but only when every precondition below holds.

View File

@ -1,86 +0,0 @@
# Refactor TODO — enforce the 250-line file cap
> **This file is a ready-to-use prompt.** Paste it to Claude, or open this repo
> and say "do refactor_claude_todo". It is self-contained: everything needed to
> execute is below. Generated 2026-08-14 from a measured survey of every repo.
## Goal
Every file in this repo must be **at most 250 lines** — source, tests, and
prose (`.md`/`.txt`/`.rst`/`.tex`) alike — and must **stay** that way forever,
enforced by a gate that fails the commit, not by a note anyone can ignore.
Why: a file that cannot be read in one piece forces re-reads and partial edits,
which is the single largest avoidable cost in an LLM-assisted workflow. Aim by
churn, not size alone — refactoring pays where code is read and changed often
(Fowler, _refactoring economic benefit_).
## Scope in this repo
- **4 files** currently exceed 250 lines (of 21 eligible files).
- **1,877 lines** sit in violation; longest file is **765 lines**.
Exempt (do NOT split these):
- generated files — `*.g.dart`, `*.freezed.dart`, `*.gr.dart`, `**/l10n/generated/**`,
anything with a `GENERATED` header
- markup — `.html`, `.css`, `.scss`
- data files — `.json`, `.yaml`, `.csv`, wordlists and other data-ish `.txt`
(mean line length under 25 chars)
## Violations, highest ROI first
ROI = lines x commits in the last year. Work top-down; a long file nobody edits
has near-zero payoff and should not be first.
| lines | commits/yr | kind | file |
| ----: | ---------: | :--- | :----------------------------- |
| 765 | 3 | code | `tests/test_recognize.py` |
| 473 | 3 | code | `steam_installer/recognize.py` |
| 311 | 3 | code | `steam_installer/cli.py` |
| 328 | 1 | code | `tests/test_cli.py` |
## How to split
- **Python** — extract cohesive helpers into sibling modules; keep the public
API and imports stable.
- **Shell** — split into `lib/*.sh` sourced by a thin entry script. Keep
`set -euo pipefail` in each.
- **Dart / TypeScript** — extract widgets/components into their own files.
- **Tests** — split by test-group into sibling files
(`foo.test.ts` -> `foo.parsing.test.ts`, `foo.render.test.ts`). Coverage must
not drop.
- **Docs** — split into topic files under `docs/` with an index. For an
oversized `CLAUDE.md`, move detail into referenced docs so the
always-loaded part shrinks.
**Do not** game the cap: no one-lining, no deleting tests, no moving code into
an exempt extension, no `# noqa`-style suppressions.
## Make it permanent (required — this is the point)
A refactor without a gate silently regrows. Before this task is done:
1. Wire the shared gate `~/src/utils/scripts/check_file_length.sh` into this repo's
`.pre-commit-config.yaml` as a local hook. If the repo has no pre-commit
config, add a minimal one.
2. The hook checks **files in the commit** (not the whole tree), so unrelated
commits never break, and it **fails** — exit 1, not a warning.
3. No baseline file and no allowlist. Those are suppressions.
4. If this repo has CI (`.github/workflows`), add the same check there so it
also fails on push.
## Done condition
- `bash ~/src/utils/scripts/check_file_length.sh --all` from this repo root exits 0.
- The repo's own test suite and coverage bar are still green.
- `pre-commit run --files <changed files>` passes.
- A deliberately over-250-line test file, staged, makes `git commit` **fail**.
- For a deployed daemon/app: the entry point still actually runs.
## Verify
Run the repo's own suite, then run the program itself and confirm the
behaviour is unchanged. Testing is the last step, never the first.
REMOVE ME AFTER FINISH

View File

@ -0,0 +1,31 @@
#!/bin/bash
# ============================================================================
# Fail if any file in the commit exceeds the shared 250-line cap.
#
# Thin delegate to the shared gate in ~/src/utils, which owns the cap and the
# exemption list (generated files, markup, data). Copying that logic here is
# how one repo's idea of "too long" drifts from every other repo's -- so this
# script only locates the shared checker and forwards its arguments.
#
# Usage:
# scripts/check_file_length.sh <file> [<file> ...] # pre-commit passes these
# scripts/check_file_length.sh --all # whole tree, from cwd
# ============================================================================
set -euo pipefail
readonly SHARED_GATE="${UTILS_ROOT:-$HOME/src/utils}/scripts/check_file_length.sh"
main() {
if [[ ! -x "$SHARED_GATE" ]]; then
echo "Error: shared file-length gate not found at $SHARED_GATE" >&2
echo " Clone github.com/kuhyx/utils to ~/src/utils, or set" >&2
echo " UTILS_ROOT to where it lives." >&2
exit 1
fi
exec bash "$SHARED_GATE" "$@"
}
main "$@"

View File

@ -4,7 +4,7 @@ from __future__ import annotations
import re
import subprocess
from dataclasses import dataclass
from dataclasses import dataclass, field
from steam_installer import vdf
@ -31,13 +31,22 @@ class DepotPlan:
required: set[str]
shared: set[str]
# depot id -> the DLC app it belongs to. Steam writes that dlcappid into
# InstalledDepots, and a DLC depot recorded without it is not credited.
dlc: dict[str, str] = field(default_factory=dict)
@property
def all_expected(self) -> set[str]:
return self.required | self.shared
def _fetch_app_block(appid: int) -> dict:
def _fetch_app_blocks(appids: list[int]) -> dict[int, dict]:
"""App info for every id in `appids`, from a single anonymous steamcmd call.
One call for the lot: each steamcmd start costs seconds of login, and a game
can list dozens of DLC.
"""
prints = [arg for appid in appids for arg in ("+app_info_print", str(appid))]
result = subprocess.run(
[
"steamcmd",
@ -47,25 +56,39 @@ def _fetch_app_block(appid: int) -> dict:
"windows",
"+login",
"anonymous",
"+app_info_print",
str(appid),
*prints,
"+quit",
],
capture_output=True,
text=True,
timeout=60,
# The exit code says nothing useful; a missing block is caught below.
check=False,
timeout=60 + 5 * len(appids),
)
output = result.stdout
# app_info_print emits `"<appid>"\n{ ... }` inside a stream of steamcmd's own log lines;
# slice from the first line that starts the VDF block onward.
match = re.search(rf'^"{appid}"\s*$', output, re.MULTILINE)
if not match:
raise AppInfoError(f"steamcmd did not return app info for {appid}:\n{output}")
data = vdf.parse(output[match.start() :])
app_block = data.get(str(appid), {})
if not isinstance(app_block, dict):
raise AppInfoError(f"unexpected app_info_print structure for {appid}")
return app_block
# app_info_print emits `"<appid>"\n{ ... }` inside a stream of steamcmd's own
# log lines; each block runs from its own header to the next one.
starts = {}
for appid in appids:
match = re.search(rf'^"{appid}"\s*$', output, re.MULTILINE)
if not match:
raise AppInfoError(
f"steamcmd did not return app info for {appid}:\n{output}"
)
starts[appid] = match.start()
ordered = sorted(starts.values()) + [len(output)]
blocks: dict[int, dict] = {}
for appid, start in starts.items():
end = ordered[ordered.index(start) + 1]
app_block = vdf.parse(output[start:end]).get(str(appid), {})
if not isinstance(app_block, dict):
raise AppInfoError(f"unexpected app_info_print structure for {appid}")
blocks[appid] = app_block
return blocks
def _fetch_app_block(appid: int) -> dict:
return _fetch_app_blocks([appid])[appid]
def get_metadata(appid: int) -> AppMetadata:
@ -118,7 +141,49 @@ def get_depot_plan(
depots = app_block.get("depots", {})
if not isinstance(depots, dict):
raise AppInfoError(f"no 'depots' section in app info for {appid}")
plan = _plan_from_depots(appid, depots, language, os_name)
# Tag the game's own list's DLC depots too: depotdownloader fetches the
# owned ones in the base run, and Steam records each with its dlcappid.
for depot_id, entry in depots.items():
if isinstance(entry, dict) and isinstance(entry.get("dlcappid"), str):
plan.dlc[depot_id] = entry["dlcappid"]
return plan
def get_dlc_plans(
appid: int, language: str = "english", os_name: str = "windows"
) -> dict[int, set[str]]:
"""DLC app id -> the depots it installs itself, for DLC with depots of its own.
Some DLC (Cyberpunk 2077's Phantom Liberty, app 2138330, ~25 GB) keeps its
content under its own app id rather than in the game's depot list, so a
depotdownloader run for the game never sees it. DLC with no depots of its
own, or whose depots the game already lists, needs no run of its own.
"""
app_block = _fetch_app_block(appid)
extended = app_block.get("extended", {})
listed = extended.get("listofdlc", "") if isinstance(extended, dict) else ""
dlc_ids = [int(x) for x in str(listed).split(",") if x.strip().isdigit()]
if not dlc_ids:
return {}
game_depots = app_block.get("depots", {})
game_depot_ids = set(game_depots) if isinstance(game_depots, dict) else set()
plans: dict[int, set[str]] = {}
for dlc_id, dlc_block in _fetch_app_blocks(dlc_ids).items():
depots = dlc_block.get("depots", {})
if not isinstance(depots, dict):
continue
own = _plan_from_depots(dlc_id, depots, language, os_name).required
own -= game_depot_ids
if own:
plans[dlc_id] = own
return plans
def _plan_from_depots(
appid: int, depots: dict, language: str, os_name: str
) -> DepotPlan:
"""The depots of one app's `depots` section that an (os, language) install gets."""
required: set[str] = set()
shared: set[str] = set()
for depot_id, entry in depots.items():
@ -128,7 +193,10 @@ def get_depot_plan(
if "depotfromapp" in entry or "sharedinstall" in entry:
shared.add(depot_id)
continue
if entry.get("dlcappid") or entry.get("optional") == "1":
# Another app's DLC is not part of this install -- but a DLC app's own
# depots carry its own id as dlcappid, and are exactly what it installs.
dlc_owner = entry.get("dlcappid")
if (dlc_owner and dlc_owner != str(appid)) or entry.get("optional") == "1":
continue
config = entry.get("config", {})
config = config if isinstance(config, dict) else {}

View File

@ -14,11 +14,42 @@ from __future__ import annotations
import logging
from pathlib import Path
from steam_installer import runlog
from steam_installer import deps, runlog
DEFAULT_MAX_DOWNLOADS = 32
# depotdownloader's licence check refusing a DLC app. The one failure that is
# expected: most games list DLC the account never bought.
NOT_OWNED_MARKER = "is not available from this account"
def _command(
appid: int,
install_dir: Path,
username: str,
language: str,
os_name: str,
max_downloads: int,
) -> list[str]:
return [
deps.depotdownloader_bin(),
"-app",
str(appid),
"-os",
os_name,
"-language",
language,
"-max-downloads",
str(max_downloads),
"-username",
username,
"-remember-password",
"-dir",
str(install_dir),
]
def download(
appid: int,
install_dir: Path,
@ -35,21 +66,27 @@ def download(
whether touching the manifest is safe at all.
"""
install_dir.mkdir(parents=True, exist_ok=True)
cmd = [
"depotdownloader",
"-app",
str(appid),
"-os",
os_name,
"-language",
language,
"-max-downloads",
str(max_downloads),
"-username",
username,
"-remember-password",
"-dir",
str(install_dir),
]
cmd = _command(appid, install_dir, username, language, os_name, max_downloads)
returncode, _ = runlog.run_logged(cmd, logger)
return returncode
def download_dlc(
dlc_appid: int,
install_dir: Path,
username: str,
logger: logging.Logger,
language: str = "english",
os_name: str = "windows",
max_downloads: int = DEFAULT_MAX_DOWNLOADS,
) -> bool | None:
"""Fetch one DLC app into the game's own `install_dir`.
Returns True when downloaded, None when the account does not own it, and
False for any other failure -- which the caller must treat as fatal.
"""
cmd = _command(dlc_appid, install_dir, username, language, os_name, max_downloads)
returncode, output = runlog.run_logged(cmd, logger)
if returncode == 0:
return True
return None if NOT_OWNED_MARKER in output else False

View File

@ -2,7 +2,9 @@
from __future__ import annotations
import os
import shutil
from pathlib import Path
TOOL_HINTS = {
"depotdownloader": "Run ./install.sh, which fetches it from the SteamRE release.",
@ -10,16 +12,33 @@ TOOL_HINTS = {
}
# The copy install.sh manages. Preferred over whatever PATH finds first: each
# depotdownloader binary keeps its remembered login in its own IsolatedStorage
# slot, keyed by its path. With an AUR copy in /usr/bin as well, a terminal run
# (PATH has ~/.local/bin) and a sudo'd run (secure_path does not) used two
# different binaries -- one logged in, one asking for a password nobody was
# there to type.
MANAGED_DEPOTDOWNLOADER = Path.home() / ".local" / "bin" / "depotdownloader"
class DependencyError(Exception):
pass
def depotdownloader_bin() -> str:
"""The depotdownloader to run: install.sh's own copy, else PATH's."""
if os.access(MANAGED_DEPOTDOWNLOADER, os.X_OK):
return str(MANAGED_DEPOTDOWNLOADER)
return "depotdownloader"
def missing_tools(*, skip_steamcmd: bool) -> list[str]:
"""Which required binaries are not on PATH, in the order they'd be used."""
needed = ["depotdownloader"]
if not skip_steamcmd:
needed.append("steamcmd")
return [tool for tool in needed if shutil.which(tool) is None]
resolved = {"depotdownloader": depotdownloader_bin()}
return [tool for tool in needed if shutil.which(resolved.get(tool, tool)) is None]
def require(*, skip_steamcmd: bool) -> None:

View File

@ -0,0 +1,60 @@
"""Owned DLC that ships as an app of its own, fetched next to the base game.
depotdownloader's `-app <game>` run only walks the game's own depot list. DLC
such as Cyberpunk 2077's Phantom Liberty (app 2138330, ~25 GB) keeps its depot
under its own app id, so that run never touches it -- and the Steam client,
restarted onto a manifest without it, then downloaded all 24 GB at its own
pace. Each such DLC now gets a depotdownloader run of its own into the same
install dir, where the manifest step picks its depot up from the cache.
Ownership is left to depotdownloader's licence check. Its refusal is the one
failure that is expected (games list DLC nobody bought); anything else aborts
the install before the manifest is touched, like a failed base download.
"""
from __future__ import annotations
from pathlib import Path
from steam_installer import appinfo, depotdl, runlog
def download_owned(
appid: int,
install_dir: Path,
username: str,
language: str,
max_downloads: int,
log: runlog.RunLog,
) -> dict[str, str] | None:
"""Download every owned DLC of `appid` that has depots of its own.
Returns:
depot id -> DLC app id for each DLC depot now downloaded, or None if a
DLC download failed for a reason other than not owning it.
"""
logger = log.logger
plans = appinfo.get_dlc_plans(appid, language)
downloaded: dict[str, str] = {}
for dlc_appid, depots in sorted(plans.items()):
log.section(f"depotdownloader DLC {dlc_appid}")
outcome = depotdl.download_dlc(
dlc_appid=dlc_appid,
install_dir=install_dir,
username=username,
language=language,
max_downloads=max_downloads,
logger=logger,
)
if outcome is None:
logger.info("DLC %d is not owned by this account -- skipped.", dlc_appid)
continue
if not outcome:
logger.error(
"depotdownloader failed for DLC %d -- not touching the manifest.",
dlc_appid,
)
return None
logger.info("Downloaded DLC %d (depots %s).", dlc_appid, sorted(depots))
downloaded.update(dict.fromkeys(depots, str(dlc_appid)))
return downloaded

View File

@ -10,7 +10,7 @@ from __future__ import annotations
import time
from pathlib import Path
from steam_installer import appinfo, recognize, runlog
from steam_installer import appinfo, finish_check, manifest_write, recognize, runlog
from steam_installer.library import SteamLibrary
@ -47,7 +47,7 @@ def finish(
return 1
app_state = recognize.read_app_state(manifest_path)
problems = recognize.check_ready_to_finish(
problems = finish_check.check_ready_to_finish(
install_dir=target,
app_state=app_state,
required_depots=depot_plan.required,
@ -70,11 +70,12 @@ def finish(
if freed:
logger.info("Cleared %.1f GB of now-redundant Steam staging data.", freed / 1e9)
written = recognize.finish_manifest_manually(
written = manifest_write.finish_manifest_manually(
library=library,
appid=metadata.appid,
install_dir=target,
now_unix=int(time.time()),
dlc_depots=depot_plan.dlc,
)
log.file_contents("Manifest after manual finish", written)

View File

@ -0,0 +1,97 @@
"""The fail-closed preconditions for hand-finishing a manifest.
Split out of recognize.py (250-line cap). Everything here only *reads*; the
write it gates lives in :mod:`manifest_write`.
"""
from __future__ import annotations
from pathlib import Path
from steam_installer import manifest_write, recognize
def check_ready_to_finish(
install_dir: Path,
app_state: dict,
required_depots: set[str],
expected_build_id: str | None,
) -> list[str]:
"""Reasons the manifest must NOT be hand-finished. Empty list means go ahead.
Every one of these was a real failure mode: writing StateFlags=4 after a
steamcmd run that did nothing turned a dead run into a reported success.
"""
problems: list[str] = []
# An empty required set would make the depot check below vacuously true and
# wave through a download that fetched nothing. It means the depot filter
# matched no depot at all (wrong language or OS for this app), not that
# there is nothing to install.
if not required_depots:
problems.append(
"no depots match this app for the chosen language/OS -- refusing to "
"guess that the install is complete"
)
cached = recognize.discover_cached_depot_manifests(install_dir)
# `finish_manifest_manually` drops depots the on-disk manifest lists under
# SharedDepots, which is a different source of truth from app_info's
# depotfromapp/sharedinstall. If they disagree it can end up with nothing to
# install and raise -- and by then clear_steam_staging has already deleted
# Steam's resumable staging tree. Catch the disagreement here, while
# refusing is still free.
shared = app_state.get("SharedDepots")
shared_ids = set(shared) if isinstance(shared, dict) else set()
if cached and not (set(cached) - shared_ids):
problems.append(
"every cached depot is listed under SharedDepots in the existing "
"manifest, so there would be nothing to record as installed"
)
missing = sorted(required_depots - set(cached))
if missing:
problems.append(
f"depot(s) {', '.join(missing)} have no cached manifest under "
f"{install_dir}/{recognize.DEPOTDOWNLOADER_DIR} -- the download did not finish"
)
buildid = app_state.get("buildid")
target = app_state.get("TargetBuildID")
if not (buildid and buildid != "0") and not (target and target != "0"):
problems.append(
"manifest has no usable buildid (both buildid and TargetBuildID are 0) "
"-- steamcmd never recorded a real build"
)
resolved = buildid if buildid and buildid != "0" else target
if expected_build_id is None:
# Don't silently skip the comparison: a None here means app_info had no
# branches.public, so we cannot tell a current build from a stale one.
problems.append(
"could not determine the current public build from app info, so the "
"manifest's buildid cannot be checked"
)
elif resolved and resolved != expected_build_id:
problems.append(
f"manifest buildid {resolved} != current public build "
f"{expected_build_id} -- content is for a different build"
)
# Cache *filenames* are not content. Without this, an install dir holding
# only `.DepotDownloader/<id>_<id>.manifest` and no game files passes every
# other check and gets written as StateFlags=4 with SizeOnDisk=0.
if not manifest_write.measure_content_size(install_dir):
problems.append(
f"no game content found under {install_dir} (only depotdownloader "
"cache files) -- nothing was actually installed"
)
pids = recognize.steam_client_pids()
if pids:
problems.append(
f"the Steam client is running (pid {', '.join(str(p) for p in pids)}) "
"and will overwrite the manifest -- close Steam and re-run"
)
return problems

View File

@ -11,8 +11,11 @@ from pathlib import Path
from steam_installer import (
appinfo,
depotdl,
dlc,
manifest_write,
recognize,
runlog,
steamcmd_validate,
steamctl,
takeover,
)
@ -121,18 +124,32 @@ def _download_and_recognize(
logger.info("Run log (send this): %s", log.path)
return 1
logger.info("Checking for owned DLC with depots of its own...")
dlc_depots = dlc.download_owned(
metadata.appid, install_target, username, language, args.max_downloads, log
)
if dlc_depots is None:
logger.info("Run log (send this): %s", log.path)
return 1
# Downloaded DLC is part of this install now: the finish gate must find its
# depot cached, and the manifest must credit it to its DLC app.
depot_plan.required |= set(dlc_depots)
depot_plan.dlc.update(dlc_depots)
log.section("depotdownloader cache")
for depot_id, mid in sorted(
recognize.discover_cached_depot_manifests(install_target).items()
):
logger.debug("depot %s -> manifest %s", depot_id, mid)
recognize.ensure_installdir_correct(library, metadata.appid, install_target)
manifest_write.ensure_installdir_correct(library, metadata.appid, install_target)
if not args.skip_steamcmd:
logger.info("Reconciling with steamcmd (validate)...")
log.section("steamcmd app_update validate")
result = recognize.run_steamcmd_validate(metadata.appid, username, logger)
result = steamcmd_validate.run_steamcmd_validate(
metadata.appid, username, logger
)
log.file_contents("Manifest after steamcmd", manifest_path)
# Never trust steamcmd's exit code alone -- verify the manifest actually

View File

@ -30,7 +30,7 @@ import time
from dataclasses import dataclass
from pathlib import Path
from steam_installer import recognize, vdf
from steam_installer import manifest_write, recognize, vdf
from steam_installer.library import SteamLibrary
INSTALLED_STATE_FLAG = 4
@ -91,7 +91,7 @@ def inspect(library: SteamLibrary, appid: int) -> GhostReport:
install_dir = _install_dir(library, app_state)
content_bytes = (
recognize.measure_content_size(install_dir)
manifest_write.measure_content_size(install_dir)
if install_dir is not None and install_dir.is_dir()
else 0
)

View File

@ -0,0 +1,191 @@
"""Every write this tool makes to an appmanifest, and only from verified values.
Split out of recognize.py (250-line cap). When steamcmd's validate cannot
finish the job (see :mod:`steamcmd_validate`), the manifest is finished by hand
using ONLY values already verified: the exact manifest IDs from
depotdownloader's own local cache (never guessed), and the real on-disk size
measured directly.
Do NOT leave the download bookkeeping for Steam to tidy up on its next launch.
An earlier version did, on the belief that Steam self-corrects; it does not. A
manifest that says StateFlags=4 while `BytesDownloaded` still differs from
`BytesToDownload` is self-contradictory, and Steam resolves the contradiction
by discarding the "installed" claim and resuming the download -- 90 GB of it,
in the run that prompted this comment.
"""
from __future__ import annotations
import os
from pathlib import Path
from steam_installer import recognize, vdf
from steam_installer.library import SteamLibrary
def ensure_installdir_correct(
library: SteamLibrary, appid: int, install_dir: Path
) -> None:
"""Patch a stale `installdir` field BEFORE running steamcmd, not just after.
Confirmed necessary: a prior, unrelated Steam GUI glitch left this app's
manifest pointing at a different folder name. steamcmd's own validate run
didn't correct it despite a clean exit, and relaunching the Steam client
read that same stale field and wiped the correctly-downloaded folder as
"orphaned" while starting a fresh download into the wrong one. Fixing it
proactively, before steamcmd ever runs, closes that window entirely.
Does nothing if no manifest exists yet -- steamcmd's own app_update writes
one in that case, and it derives installdir from the app's metadata.
"""
manifest_path = library.steamapps / f"appmanifest_{appid}.acf"
if not manifest_path.is_file():
return
data = vdf.parse(manifest_path.read_text())
app_state = data.get("AppState", {})
if not isinstance(app_state, dict):
return
if app_state.get("installdir") == install_dir.name:
return
app_state["installdir"] = install_dir.name
backup_once(manifest_path)
write_atomic(manifest_path, vdf.dumps("AppState", app_state))
def measure_content_size(install_dir: Path) -> int:
"""Bytes of real game content, excluding depotdownloader's own cache/staging.
`.DepotDownloader/` holds manifests and a staging tree that Steam knows
nothing about; counting them overstates SizeOnDisk by gigabytes.
"""
total = 0
for entry in install_dir.rglob("*"):
if recognize.DEPOTDOWNLOADER_DIR in entry.parts:
continue
if entry.is_file() and not entry.is_symlink():
total += entry.stat().st_size
return total
def finish_manifest_manually(
library: SteamLibrary,
appid: int,
install_dir: Path,
now_unix: int,
dlc_depots: dict[str, str] | None = None,
) -> Path:
"""Patch appmanifest_<appid>.acf to StateFlags=4 using only verified-known values.
Requires steamcmd to have already written a partial manifest (even a crashed
run does) -- that's where buildid and SharedDepots come from. This never
fabricates a manifest from nothing.
`dlc_depots` maps a DLC depot to its DLC app id. Steam's own manifests
record each DLC depot with that `dlcappid`, so it is written the same way.
"""
manifest_path = library.steamapps / f"appmanifest_{appid}.acf"
if not manifest_path.is_file():
raise recognize.RecognizeError(
f"No existing {manifest_path.name} to finish -- run steamcmd's "
"app_update at least once first (even a crashed run writes one)."
)
data = vdf.parse(manifest_path.read_text())
app_state = data.get("AppState", {})
if not isinstance(app_state, dict):
raise recognize.RecognizeError(
f"{manifest_path} is not a valid AppState manifest"
)
depot_manifests = recognize.discover_cached_depot_manifests(install_dir)
if not depot_manifests:
raise recognize.RecognizeError(
f"No cached depot manifests found under {install_dir}/{recognize.DEPOTDOWNLOADER_DIR}"
)
size_on_disk = measure_content_size(install_dir)
# "0" means "no real build installed yet" (steamcmd/Steam use it as a
# sentinel) -- a bare `or` treats that non-empty string as truthy and
# never falls through to TargetBuildID, leaving a stale/wrong buildid.
existing_buildid = app_state.get("buildid")
buildid = existing_buildid if existing_buildid and existing_buildid != "0" else None
buildid = buildid or app_state.get("TargetBuildID") or "0"
# A shared depot (e.g. Steamworks Common Redistributables) is installed once
# under its owning app and only referenced here. Listing it in
# InstalledDepots as though this app owned a copy contradicts SharedDepots.
shared = app_state.get("SharedDepots")
shared_ids = set(shared) if isinstance(shared, dict) else set()
dlc_depots = dlc_depots or {}
installed_depots: dict = {}
for depot_id, manifest_id in depot_manifests.items():
if depot_id in shared_ids:
continue
entry = {"manifest": manifest_id, "size": "0"}
if depot_id in dlc_depots:
entry["dlcappid"] = dlc_depots[depot_id]
installed_depots[depot_id] = entry
if not installed_depots:
raise recognize.RecognizeError(
f"Every cached depot for {appid} is a shared depot -- nothing to install"
)
app_state["StateFlags"] = "4"
app_state["installdir"] = install_dir.name
app_state["LastUpdated"] = str(now_unix)
app_state["SizeOnDisk"] = str(size_on_disk)
app_state["StagingSize"] = "0"
app_state["buildid"] = buildid
app_state["UpdateResult"] = "0"
app_state["InstalledDepots"] = installed_depots
app_state.pop("StagedDepots", None)
# Steam's own per-DLC progress for a download it started: left in place it
# contradicts the settled counters and Steam resumes fetching the DLC.
app_state.pop("DlcDownloads", None)
settle_download_counters(app_state)
backup_once(manifest_path)
write_atomic(manifest_path, vdf.dumps("AppState", app_state))
return manifest_path
def backup_once(manifest_path: Path) -> None:
"""Keep the *pristine* manifest, not the previous run's output.
An unconditional backup means a second run overwrites the original with
run 1's already-patched file, so the recovery artifact for a 90+ GB install
is one re-run away from being useless. Only write it if absent.
"""
backup_path = manifest_path.with_suffix(".acf.bak")
if not backup_path.exists():
backup_path.write_text(manifest_path.read_text())
def write_atomic(path: Path, text: str) -> None:
"""Write via a temp file + rename so a crash can't truncate the manifest.
A partial write leaves an unparseable manifest, which Steam and this tool
both then choke on. `os.replace` is atomic within a filesystem, and the
temp file is created alongside the target to stay on the same one.
"""
tmp = path.with_name(f"{path.name}.tmp{os.getpid()}")
try:
tmp.write_text(text)
os.replace(tmp, path)
except BaseException:
tmp.unlink(missing_ok=True)
raise
def settle_download_counters(app_state: dict) -> None:
"""Mark the download bookkeeping complete: `<X>Downloaded` == `<X>ToDownload`.
Leaving these at their mid-download values is what made Steam discard a
StateFlags=4 manifest and resume fetching. Real installed manifests keep
these fields (they are not dropped) with the pair values equal.
"""
for total_key, done_key in (
("BytesToDownload", "BytesDownloaded"),
("BytesToStage", "BytesStaged"),
):
total = app_state.get(total_key)
if total is not None:
app_state[done_key] = total

View File

@ -1,43 +1,19 @@
"""Make the Steam client recognize downloaded content as installed.
"""Read-only questions about whether Steam will see an install as real.
Primary path: `steamcmd ... app_update <id> validate`. It hash-checks
existing files against the depot manifest and only fetches real deltas --
confirmed live against a real 90+ GB install, going from ~24% to ~41%
credited in one pass without redownloading from scratch.
Fallback: some titles ship a post-install script (installscript.vdf) that
steamcmd tries to run through a private, headless Proton setup. On at least
one real title this hit a confirmed steamcmd bug -- a stack-overflow segfault
in the closed-source steamclient.so error-handling path after the script
evaluator reports failure (crash backtrace: the same stack frame repeating
dozens of times). There's no known fix. When that specific crash is
detected, we finish the appmanifest by hand using ONLY values we already
verified ourselves: the exact manifest IDs from depotdownloader's own local
cache (never guessed), and the real on-disk size measured directly.
Do NOT leave the download bookkeeping for Steam to tidy up on its next
launch. An earlier version of this module did, on the belief that Steam
self-corrects; it does not. A manifest that says StateFlags=4 while
`BytesDownloaded` still differs from `BytesToDownload` is self-contradictory,
and Steam resolves the contradiction by discarding the "installed" claim and
resuming the download -- 90 GB of it, in the run that prompted this comment.
The byte counters (`_settle_download_counters`) and the staging tree
(`clear_steam_staging`) must both be settled here, and
`manifest_reflects_real_install` refuses to call an install real until they
are.
Split on 2026-09-25 (250-line cap): steamcmd's validate run now lives in
:mod:`steamcmd_validate`, every manifest write in :mod:`manifest_write`, and
the preconditions for writing in :mod:`finish_check`. What stays here is the
shared vocabulary they all read: depotdownloader's cache, Steam's staging tree
and client process, and whether a manifest's bookkeeping says "done".
"""
from __future__ import annotations
import logging
import os
import re
import shutil
import signal
from dataclasses import dataclass
from pathlib import Path
from steam_installer import runlog, vdf
from steam_installer import vdf
from steam_installer.library import SteamLibrary
MANIFEST_CACHE_RE = re.compile(r"^(\d+)_(\d+)\.manifest$")
@ -54,90 +30,6 @@ class RecognizeError(Exception):
pass
@dataclass
class ValidateResult:
completed: bool
crashed_on_install_script: bool
returncode: int
output: str = ""
def run_steamcmd_validate(
appid: int,
username: str,
logger: logging.Logger,
) -> ValidateResult:
"""Run steamcmd's app_update ... validate.
Takes no install dir on purpose. `force_install_dir` sets a Steam *library
root*, not an install directory: pointing it at the game folder made
steamcmd look for manifests in `<game>/steamapps/`, find none ("Loaded 0
apps from install folder"), create that stray directory, and then fail the
app with "Invalid platform" because it had no depot context for it. The app
already lives in a registered library, so letting steamcmd use the real one
is both correct and sufficient.
Output is teed through a pty (see runlog) rather than captured: steamcmd
prompts for a password and possibly a Steam Guard code on stdin, and prompt
text written to a captured/buffered pipe never reaches the terminal -- the
process looks hung with no visible request for input.
The confirmed steamcmd install-script bug (see module docstring) kills
the process with SIGSEGV -- Python reports that as returncode -11 on
POSIX. Checking the signal directly is more reliable than grepping
captured text for a message string.
"""
cmd = [
"steamcmd",
# A Linux steamcmd refuses a Windows-only app with "Invalid platform"
# unless told which platform to resolve depots for. Must precede +login.
"+@sSteamCmdForcePlatformType",
"windows",
"+login",
username,
"+app_update",
str(appid),
"validate",
"+quit",
]
returncode, output = runlog.run_logged(cmd, logger, timeout=3600)
crashed = returncode == -signal.SIGSEGV
return ValidateResult(
completed=returncode == 0,
crashed_on_install_script=crashed,
returncode=returncode,
output=output,
)
def ensure_installdir_correct(
library: SteamLibrary, appid: int, install_dir: Path
) -> None:
"""Patch a stale `installdir` field BEFORE running steamcmd, not just after.
Confirmed necessary: a prior, unrelated Steam GUI glitch left this app's
manifest pointing at a different folder name. steamcmd's own validate run
didn't correct it despite a clean exit, and relaunching the Steam client
read that same stale field and wiped the correctly-downloaded folder as
"orphaned" while starting a fresh download into the wrong one. Fixing it
proactively, before steamcmd ever runs, closes that window entirely.
Does nothing if no manifest exists yet -- steamcmd's own app_update writes
one in that case, and it derives installdir from the app's metadata.
"""
manifest_path = library.steamapps / f"appmanifest_{appid}.acf"
if not manifest_path.is_file():
return
data = vdf.parse(manifest_path.read_text())
app_state = data.get("AppState", {})
if not isinstance(app_state, dict):
return
if app_state.get("installdir") == install_dir.name:
return
app_state["installdir"] = install_dir.name
_backup_once(manifest_path)
_write_atomic(manifest_path, vdf.dumps("AppState", app_state))
def manifest_reflects_real_install(
library: SteamLibrary, appid: int, install_dir: Path
) -> bool:
@ -258,216 +150,3 @@ def steam_client_pids() -> list[int]:
if exe.name in ("steam", "steamwebhelper"):
pids.append(int(entry.name))
return pids
def check_ready_to_finish(
install_dir: Path,
app_state: dict,
required_depots: set[str],
expected_build_id: str | None,
) -> list[str]:
"""Reasons the manifest must NOT be hand-finished. Empty list means go ahead.
Every one of these was a real failure mode: writing StateFlags=4 after a
steamcmd run that did nothing turned a dead run into a reported success.
"""
problems: list[str] = []
# An empty required set would make the depot check below vacuously true and
# wave through a download that fetched nothing. It means the depot filter
# matched no depot at all (wrong language or OS for this app), not that
# there is nothing to install.
if not required_depots:
problems.append(
"no depots match this app for the chosen language/OS -- refusing to "
"guess that the install is complete"
)
cached = discover_cached_depot_manifests(install_dir)
# `finish_manifest_manually` drops depots the on-disk manifest lists under
# SharedDepots, which is a different source of truth from app_info's
# depotfromapp/sharedinstall. If they disagree it can end up with nothing to
# install and raise -- and by then clear_steam_staging has already deleted
# Steam's resumable staging tree. Catch the disagreement here, while
# refusing is still free.
shared = app_state.get("SharedDepots")
shared_ids = set(shared) if isinstance(shared, dict) else set()
if cached and not (set(cached) - shared_ids):
problems.append(
"every cached depot is listed under SharedDepots in the existing "
"manifest, so there would be nothing to record as installed"
)
missing = sorted(required_depots - set(cached))
if missing:
problems.append(
f"depot(s) {', '.join(missing)} have no cached manifest under "
f"{install_dir}/{DEPOTDOWNLOADER_DIR} -- the download did not finish"
)
buildid = app_state.get("buildid")
target = app_state.get("TargetBuildID")
if not (buildid and buildid != "0") and not (target and target != "0"):
problems.append(
"manifest has no usable buildid (both buildid and TargetBuildID are 0) "
"-- steamcmd never recorded a real build"
)
resolved = buildid if buildid and buildid != "0" else target
if expected_build_id is None:
# Don't silently skip the comparison: a None here means app_info had no
# branches.public, so we cannot tell a current build from a stale one.
problems.append(
"could not determine the current public build from app info, so the "
"manifest's buildid cannot be checked"
)
elif resolved and resolved != expected_build_id:
problems.append(
f"manifest buildid {resolved} != current public build "
f"{expected_build_id} -- content is for a different build"
)
# Cache *filenames* are not content. Without this, an install dir holding
# only `.DepotDownloader/<id>_<id>.manifest` and no game files passes every
# other check and gets written as StateFlags=4 with SizeOnDisk=0.
if not measure_content_size(install_dir):
problems.append(
f"no game content found under {install_dir} (only depotdownloader "
"cache files) -- nothing was actually installed"
)
pids = steam_client_pids()
if pids:
problems.append(
f"the Steam client is running (pid {', '.join(str(p) for p in pids)}) "
"and will overwrite the manifest -- close Steam and re-run"
)
return problems
def measure_content_size(install_dir: Path) -> int:
"""Bytes of real game content, excluding depotdownloader's own cache/staging.
`.DepotDownloader/` holds manifests and a staging tree that Steam knows
nothing about; counting them overstates SizeOnDisk by gigabytes.
"""
total = 0
for entry in install_dir.rglob("*"):
if DEPOTDOWNLOADER_DIR in entry.parts:
continue
if entry.is_file() and not entry.is_symlink():
total += entry.stat().st_size
return total
def finish_manifest_manually(
library: SteamLibrary,
appid: int,
install_dir: Path,
now_unix: int,
) -> Path:
"""Patch appmanifest_<appid>.acf to StateFlags=4 using only verified-known values.
Requires steamcmd to have already written a partial manifest (even a crashed
run does) -- that's where buildid and SharedDepots come from. This never
fabricates a manifest from nothing.
"""
manifest_path = library.steamapps / f"appmanifest_{appid}.acf"
if not manifest_path.is_file():
raise RecognizeError(
f"No existing {manifest_path.name} to finish -- run steamcmd's "
"app_update at least once first (even a crashed run writes one)."
)
data = vdf.parse(manifest_path.read_text())
app_state = data.get("AppState", {})
if not isinstance(app_state, dict):
raise RecognizeError(f"{manifest_path} is not a valid AppState manifest")
depot_manifests = discover_cached_depot_manifests(install_dir)
if not depot_manifests:
raise RecognizeError(
f"No cached depot manifests found under {install_dir}/{DEPOTDOWNLOADER_DIR}"
)
size_on_disk = measure_content_size(install_dir)
# "0" means "no real build installed yet" (steamcmd/Steam use it as a
# sentinel) -- a bare `or` treats that non-empty string as truthy and
# never falls through to TargetBuildID, leaving a stale/wrong buildid.
existing_buildid = app_state.get("buildid")
buildid = existing_buildid if existing_buildid and existing_buildid != "0" else None
buildid = buildid or app_state.get("TargetBuildID") or "0"
# A shared depot (e.g. Steamworks Common Redistributables) is installed once
# under its owning app and only referenced here. Listing it in
# InstalledDepots as though this app owned a copy contradicts SharedDepots.
shared = app_state.get("SharedDepots")
shared_ids = set(shared) if isinstance(shared, dict) else set()
installed_depots = {
depot_id: {"manifest": manifest_id, "size": "0"}
for depot_id, manifest_id in depot_manifests.items()
if depot_id not in shared_ids
}
if not installed_depots:
raise RecognizeError(
f"Every cached depot for {appid} is a shared depot -- nothing to install"
)
app_state["StateFlags"] = "4"
app_state["installdir"] = install_dir.name
app_state["LastUpdated"] = str(now_unix)
app_state["SizeOnDisk"] = str(size_on_disk)
app_state["StagingSize"] = "0"
app_state["buildid"] = buildid
app_state["UpdateResult"] = "0"
app_state["InstalledDepots"] = installed_depots
app_state.pop("StagedDepots", None)
_settle_download_counters(app_state)
_backup_once(manifest_path)
_write_atomic(manifest_path, vdf.dumps("AppState", app_state))
return manifest_path
def _backup_once(manifest_path: Path) -> None:
"""Keep the *pristine* manifest, not the previous run's output.
An unconditional backup means a second run overwrites the original with
run 1's already-patched file, so the recovery artifact for a 90+ GB install
is one re-run away from being useless. Only write it if absent.
"""
backup_path = manifest_path.with_suffix(".acf.bak")
if not backup_path.exists():
backup_path.write_text(manifest_path.read_text())
def _write_atomic(path: Path, text: str) -> None:
"""Write via a temp file + rename so a crash can't truncate the manifest.
A partial write leaves an unparseable manifest, which Steam and this tool
both then choke on. `os.replace` is atomic within a filesystem, and the
temp file is created alongside the target to stay on the same one.
"""
tmp = path.with_name(f"{path.name}.tmp{os.getpid()}")
try:
tmp.write_text(text)
os.replace(tmp, path)
except BaseException:
tmp.unlink(missing_ok=True)
raise
def _settle_download_counters(app_state: dict) -> None:
"""Mark the download bookkeeping complete: `<X>Downloaded` == `<X>ToDownload`.
Leaving these at their mid-download values is what made Steam discard a
StateFlags=4 manifest and resume fetching. Real installed manifests keep
these fields (they are not dropped) with the pair values equal.
"""
for total_key, done_key in (
("BytesToDownload", "BytesDownloaded"),
("BytesToStage", "BytesStaged"),
):
total = app_state.get(total_key)
if total is not None:
app_state[done_key] = total

View File

@ -0,0 +1,79 @@
"""Reconcile an install with Steam's own bookkeeping via `steamcmd ... validate`.
Split out of recognize.py (250-line cap). `app_update <id> validate` hash-checks
existing files against the depot manifest and only fetches real deltas --
confirmed live against a real 90+ GB install, going from ~24% to ~41% credited
in one pass without redownloading from scratch.
Some titles ship a post-install script (installscript.vdf) that steamcmd runs
through a private, headless Proton setup. On at least one real title this hit a
confirmed steamcmd bug -- a stack-overflow segfault in the closed-source
steamclient.so error-handling path after the script evaluator reports failure
(the same stack frame repeating dozens of times). There's no known fix; the
caller detects that crash and finishes the manifest by hand instead
(:mod:`manifest_write`).
"""
from __future__ import annotations
import logging
import signal
from dataclasses import dataclass
from steam_installer import runlog
@dataclass
class ValidateResult:
completed: bool
crashed_on_install_script: bool
returncode: int
output: str = ""
def run_steamcmd_validate(
appid: int,
username: str,
logger: logging.Logger,
) -> ValidateResult:
"""Run steamcmd's app_update ... validate.
Takes no install dir on purpose. `force_install_dir` sets a Steam *library
root*, not an install directory: pointing it at the game folder made
steamcmd look for manifests in `<game>/steamapps/`, find none ("Loaded 0
apps from install folder"), create that stray directory, and then fail the
app with "Invalid platform" because it had no depot context for it. The app
already lives in a registered library, so letting steamcmd use the real one
is both correct and sufficient.
Output is teed through a pty (see runlog) rather than captured: steamcmd
prompts for a password and possibly a Steam Guard code on stdin, and prompt
text written to a captured/buffered pipe never reaches the terminal -- the
process looks hung with no visible request for input.
The confirmed steamcmd install-script bug (see module docstring) kills
the process with SIGSEGV -- Python reports that as returncode -11 on
POSIX. Checking the signal directly is more reliable than grepping
captured text for a message string.
"""
cmd = [
"steamcmd",
# A Linux steamcmd refuses a Windows-only app with "Invalid platform"
# unless told which platform to resolve depots for. Must precede +login.
"+@sSteamCmdForcePlatformType",
"windows",
"+login",
username,
"+app_update",
str(appid),
"validate",
"+quit",
]
returncode, output = runlog.run_logged(cmd, logger, timeout=3600)
crashed = returncode == -signal.SIGSEGV
return ValidateResult(
completed=returncode == 0,
crashed_on_install_script=crashed,
returncode=returncode,
output=output,
)

View File

@ -0,0 +1,21 @@
"""Stubs shared by the CLI flow tests (plain functions, not fixtures)."""
from steam_installer import depotdl, steamcmd_validate
ARGV = ["--appid", "1771300", "--username", "u", "--language", "english"]
def _stub_download(monkeypatch, returncode=0):
monkeypatch.setattr(depotdl, "download", lambda **kw: returncode)
def _stub_steamcmd(monkeypatch, returncode, crashed=False):
monkeypatch.setattr(
steamcmd_validate,
"run_steamcmd_validate",
lambda appid, username, logger: steamcmd_validate.ValidateResult(
completed=returncode == 0,
crashed_on_install_script=crashed,
returncode=returncode,
),
)

View File

@ -0,0 +1,31 @@
"""Fixtures-as-functions shared by the recognize/manifest test files."""
from pathlib import Path
from steam_installer import vdf
def _write_partial_manifest(steamapps: Path, appid: int) -> None:
steamapps.mkdir(parents=True, exist_ok=True)
data = {
"appid": str(appid),
"StateFlags": "1026",
"buildid": "23914554",
"SharedDepots": {"228989": "228980"},
}
(steamapps / f"appmanifest_{appid}.acf").write_text(vdf.dumps("AppState", data))
def _fake_proc(tmp_path, entries):
"""Build a /proc-shaped tree: {pid: exe_target_path_or_None}."""
proc = tmp_path / "proc"
proc.mkdir()
for name, target in entries.items():
entry = proc / name
entry.mkdir()
if target is not None:
real = tmp_path / "bin" / target
real.parent.mkdir(parents=True, exist_ok=True)
real.write_text("")
(entry / "exe").symlink_to(real)
return proc

View File

@ -14,7 +14,6 @@ import os
import pathlib
import pytest
from steam_installer import (
appinfo,
cli,
@ -131,3 +130,67 @@ def cli_steam(tmp_path, monkeypatch, isolated_cli):
depotdl, "download", lambda **kw: pytest.fail("must not download")
)
return library
@pytest.fixture(autouse=True)
def _no_host_depotdownloader(tmp_path, monkeypatch):
"""Never let this machine's ~/.local/bin decide which binary a test sees."""
monkeypatch.setattr(deps, "MANAGED_DEPOTDOWNLOADER", tmp_path / "absent")
@pytest.fixture
def cli_logs(tmp_path, monkeypatch):
monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state")
yield
logger = logging.getLogger(runlog.LOGGER_NAME)
for handler in list(logger.handlers):
logger.removeHandler(handler)
handler.close()
@pytest.fixture
def steam(tmp_path, monkeypatch):
"""A Steam tree with KCD2 downloaded but its manifest not yet finished."""
root = tmp_path / "Steam"
library = SteamLibrary(root)
install_dir = library.common / "KingdomComeDeliverance2"
cache = install_dir / ".DepotDownloader"
cache.mkdir(parents=True)
(cache / "1771303_1999516201078544527.manifest").write_bytes(b"x")
(install_dir / "game.pak").write_bytes(b"x" * 1000)
library.steamapps.mkdir(parents=True, exist_ok=True)
(library.steamapps / "appmanifest_1771300.acf").write_text(
vdf.dumps(
"AppState",
{
"appid": "1771300",
"StateFlags": "1026",
"buildid": "23914554",
"BytesToDownload": "92009052960",
"BytesDownloaded": "22129058224",
},
)
)
monkeypatch.setattr(cli, "find_steam_root", lambda: root)
monkeypatch.setattr(cli, "list_libraries", lambda r: [library])
monkeypatch.setattr(cli, "pick_install_target", lambda libs, d: install_dir)
monkeypatch.setattr(deps.shutil, "which", lambda name: f"/usr/bin/{name}")
monkeypatch.setattr(
appinfo,
"get_metadata",
lambda appid: appinfo.AppMetadata(appid, "KCD2", "KingdomComeDeliverance2"),
)
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: appinfo.DepotPlan({"1771303"}, {"228989"}),
)
monkeypatch.setattr(appinfo, "get_build_id", lambda appid: "23914554")
# No DLC unless a test says so: the real lookup is a steamcmd call.
monkeypatch.setattr(appinfo, "get_dlc_plans", lambda appid, language: {})
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [])
monkeypatch.setattr("builtins.input", lambda *a: "y")
# Prompting is tty-gated now, and pytest's stdin is not a tty.
monkeypatch.setattr(cli, "_is_interactive", lambda args: True)
return library, install_dir

View File

@ -6,90 +6,20 @@ StateFlags=4, printed "launch Steam and confirm Play works" and returned 0.
These tests pin the fail-closed behaviour.
"""
import logging
import subprocess
import sys
import pytest
from steam_installer import (
appinfo,
cli,
depotdl,
manifest_write,
recognize,
steamcmd_validate,
vdf,
)
from steam_installer import appinfo, cli, depotdl, deps, recognize, runlog, vdf
from steam_installer.library import SteamLibrary
from _cli_helpers import ARGV, _stub_download, _stub_steamcmd
@pytest.fixture(autouse=True)
def isolated_logs(tmp_path, monkeypatch):
monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state")
yield
logger = logging.getLogger(runlog.LOGGER_NAME)
for handler in list(logger.handlers):
logger.removeHandler(handler)
handler.close()
@pytest.fixture
def steam(tmp_path, monkeypatch):
"""A Steam tree with KCD2 downloaded but its manifest not yet finished."""
root = tmp_path / "Steam"
library = SteamLibrary(root)
install_dir = library.common / "KingdomComeDeliverance2"
cache = install_dir / ".DepotDownloader"
cache.mkdir(parents=True)
(cache / "1771303_1999516201078544527.manifest").write_bytes(b"x")
(install_dir / "game.pak").write_bytes(b"x" * 1000)
library.steamapps.mkdir(parents=True, exist_ok=True)
(library.steamapps / "appmanifest_1771300.acf").write_text(
vdf.dumps(
"AppState",
{
"appid": "1771300",
"StateFlags": "1026",
"buildid": "23914554",
"BytesToDownload": "92009052960",
"BytesDownloaded": "22129058224",
},
)
)
monkeypatch.setattr(cli, "find_steam_root", lambda: root)
monkeypatch.setattr(cli, "list_libraries", lambda r: [library])
monkeypatch.setattr(cli, "pick_install_target", lambda libs, d: install_dir)
monkeypatch.setattr(deps.shutil, "which", lambda name: f"/usr/bin/{name}")
monkeypatch.setattr(
appinfo,
"get_metadata",
lambda appid: appinfo.AppMetadata(appid, "KCD2", "KingdomComeDeliverance2"),
)
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: appinfo.DepotPlan({"1771303"}, {"228989"}),
)
monkeypatch.setattr(appinfo, "get_build_id", lambda appid: "23914554")
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [])
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [])
monkeypatch.setattr("builtins.input", lambda *a: "y")
# Prompting is tty-gated now, and pytest's stdin is not a tty.
monkeypatch.setattr(cli, "_is_interactive", lambda args: True)
return library, install_dir
ARGV = ["--appid", "1771300", "--username", "u", "--language", "english"]
def _stub_download(monkeypatch, returncode=0):
monkeypatch.setattr(depotdl, "download", lambda **kw: returncode)
def _stub_steamcmd(monkeypatch, returncode, crashed=False):
monkeypatch.setattr(
recognize,
"run_steamcmd_validate",
lambda appid, username, logger: recognize.ValidateResult(
completed=returncode == 0,
crashed_on_install_script=crashed,
returncode=returncode,
),
)
pytestmark = pytest.mark.usefixtures("cli_logs")
def test_dead_steamcmd_run_still_finishes_when_content_is_present(steam, monkeypatch):
@ -167,12 +97,12 @@ def test_clean_steamcmd_run_short_circuits(steam, monkeypatch):
},
)
)
return recognize.ValidateResult(True, False, 0)
return steamcmd_validate.ValidateResult(True, False, 0)
monkeypatch.setattr(recognize, "run_steamcmd_validate", fake_validate)
monkeypatch.setattr(steamcmd_validate, "run_steamcmd_validate", fake_validate)
called = []
monkeypatch.setattr(
recognize,
manifest_write,
"finish_manifest_manually",
lambda **kw: called.append(1),
)
@ -207,12 +137,12 @@ def test_a_complete_install_after_a_failed_steamcmd_is_not_credited_to_it(
},
)
)
return recognize.ValidateResult(True, False, 5)
return steamcmd_validate.ValidateResult(True, False, 5)
monkeypatch.setattr(recognize, "run_steamcmd_validate", fake_validate)
monkeypatch.setattr(steamcmd_validate, "run_steamcmd_validate", fake_validate)
called = []
monkeypatch.setattr(
recognize, "finish_manifest_manually", lambda **kw: called.append(1)
manifest_write, "finish_manifest_manually", lambda **kw: called.append(1)
)
assert cli.main(ARGV) == 0
@ -243,106 +173,3 @@ def test_unhandled_error_returns_nonzero_and_names_the_log(steam, monkeypatch, c
def test_requires_a_name_or_appid():
with pytest.raises(SystemExit):
cli.main(["--username", "u"])
def test_missing_manifest_under_skip_steamcmd_does_not_blame_steamcmd(
steam, monkeypatch, capsys
):
"""--skip-steamcmd never runs steamcmd, so "steamcmd never ran far enough"
would send the user chasing the wrong thing."""
library, _ = steam
(library.steamapps / "appmanifest_1771300.acf").unlink()
_stub_download(monkeypatch)
assert cli.main([*ARGV, "--skip-steamcmd"]) == 1
out = capsys.readouterr().out
assert "never fabricates one" in out
assert "Re-run without --skip-steamcmd" in out
assert "steamcmd never ran far enough" not in out
def test_missing_manifest_with_steamcmd_blames_steamcmd(steam, monkeypatch, capsys):
library, _ = steam
(library.steamapps / "appmanifest_1771300.acf").unlink()
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=8)
assert cli.main(ARGV) == 1
assert "steamcmd never ran far enough" in capsys.readouterr().out
def test_language_defaults_to_english_when_prompt_is_empty(steam, monkeypatch):
seen = {}
monkeypatch.setattr("builtins.input", lambda *a: "y" if "Proceed" in a[0] else "")
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: (
seen.setdefault("language", language)
and None
or appinfo.DepotPlan({"1771303"}, set())
),
)
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=8)
cli.main(["--appid", "1771300", "--username", "u"])
assert seen["language"] == "english"
def test_reports_the_known_install_script_crash(steam, monkeypatch, capsys):
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=-11, crashed=True)
assert cli.main(ARGV) == 0
assert "known install-script crash" in capsys.readouterr().out
def test_adopts_staged_content_then_clears_it(steam, monkeypatch, capsys):
"""Steam's staging tree is adopted into the install dir before downloading,
and only cleared once the install is finished -- never discarded up front."""
library, install_dir = steam
staged = library.steamapps / "downloading" / "1771300"
staged.mkdir(parents=True)
(staged / "big.pak").write_bytes(b"x" * 2_000_000)
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=8)
assert cli.main(ARGV) == 0
out = capsys.readouterr().out
assert "Hardlinking it" in out
assert "now-redundant Steam staging data" in out
# Adopted, not copied: the file is one inode with two names.
assert (install_dir / "big.pak").stat().st_size == 2_000_000
def test_fails_when_written_manifest_does_not_read_back_as_installed(
steam, monkeypatch, capsys
):
"""Claiming success without re-reading the write is the same fail-open
mistake in a different place."""
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=8)
monkeypatch.setattr(
recognize, "manifest_reflects_real_install", lambda *a, **kw: False
)
assert cli.main(ARGV) == 1
assert "does not read back as a real install" in capsys.readouterr().out
def test_module_is_runnable_as_a_script(coverage_subprocess_env):
"""`python -m steam_installer.cli` is a real entry point (the __main__
guard), so it must not be dead code."""
result = subprocess.run(
[sys.executable, "-m", "steam_installer.cli", "--help"],
capture_output=True,
text=True,
timeout=60,
env=coverage_subprocess_env,
)
assert result.returncode == 0
assert "--skip-steamcmd" in result.stdout

View File

@ -0,0 +1,118 @@
"""CLI flow tests, part 2: the manifest-finishing end of a run."""
import subprocess
import sys
import pytest
from steam_installer import (
appinfo,
cli,
recognize,
)
from _cli_helpers import ARGV, _stub_download, _stub_steamcmd
pytestmark = pytest.mark.usefixtures("cli_logs")
def test_missing_manifest_under_skip_steamcmd_does_not_blame_steamcmd(
steam, monkeypatch, capsys
):
"""--skip-steamcmd never runs steamcmd, so "steamcmd never ran far enough"
would send the user chasing the wrong thing."""
library, _ = steam
(library.steamapps / "appmanifest_1771300.acf").unlink()
_stub_download(monkeypatch)
assert cli.main([*ARGV, "--skip-steamcmd"]) == 1
out = capsys.readouterr().out
assert "never fabricates one" in out
assert "Re-run without --skip-steamcmd" in out
assert "steamcmd never ran far enough" not in out
def test_missing_manifest_with_steamcmd_blames_steamcmd(steam, monkeypatch, capsys):
library, _ = steam
(library.steamapps / "appmanifest_1771300.acf").unlink()
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=8)
assert cli.main(ARGV) == 1
assert "steamcmd never ran far enough" in capsys.readouterr().out
def test_language_defaults_to_english_when_prompt_is_empty(steam, monkeypatch):
seen = {}
monkeypatch.setattr("builtins.input", lambda *a: "y" if "Proceed" in a[0] else "")
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: (
seen.setdefault("language", language)
and None
or appinfo.DepotPlan({"1771303"}, set())
),
)
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=8)
cli.main(["--appid", "1771300", "--username", "u"])
assert seen["language"] == "english"
def test_reports_the_known_install_script_crash(steam, monkeypatch, capsys):
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=-11, crashed=True)
assert cli.main(ARGV) == 0
assert "known install-script crash" in capsys.readouterr().out
def test_adopts_staged_content_then_clears_it(steam, monkeypatch, capsys):
"""Steam's staging tree is adopted into the install dir before downloading,
and only cleared once the install is finished -- never discarded up front."""
library, install_dir = steam
staged = library.steamapps / "downloading" / "1771300"
staged.mkdir(parents=True)
(staged / "big.pak").write_bytes(b"x" * 2_000_000)
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=8)
assert cli.main(ARGV) == 0
out = capsys.readouterr().out
assert "Hardlinking it" in out
assert "now-redundant Steam staging data" in out
# Adopted, not copied: the file is one inode with two names.
assert (install_dir / "big.pak").stat().st_size == 2_000_000
def test_fails_when_written_manifest_does_not_read_back_as_installed(
steam, monkeypatch, capsys
):
"""Claiming success without re-reading the write is the same fail-open
mistake in a different place."""
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=8)
monkeypatch.setattr(
recognize, "manifest_reflects_real_install", lambda *a, **kw: False
)
assert cli.main(ARGV) == 1
assert "does not read back as a real install" in capsys.readouterr().out
def test_module_is_runnable_as_a_script(coverage_subprocess_env):
"""`python -m steam_installer.cli` is a real entry point (the __main__
guard), so it must not be dead code."""
result = subprocess.run(
[sys.executable, "-m", "steam_installer.cli", "--help"],
capture_output=True,
text=True,
timeout=60,
env=coverage_subprocess_env,
)
assert result.returncode == 0
assert "--skip-steamcmd" in result.stdout

View File

@ -0,0 +1,184 @@
"""DLC that ships as its own app: discovery, download, and crediting it in the manifest.
Shapes are taken from the real `app_info_print` of Cyberpunk 2077 (1091500) and
its DLC, 2025-09-25: Phantom Liberty (2138330) keeps its ~25 GB depot under its
own app id; 2060310 is listed in the game's own depots; 2441600 has none.
"""
import logging
import subprocess
import pytest
from _cli_helpers import ARGV, _stub_download, _stub_steamcmd
from steam_installer import appinfo, cli, depotdl, deps, dlc, recognize, runlog, vdf
pytestmark = pytest.mark.usefixtures("cli_logs")
GAME = {
"extended": {"listofdlc": "2060310,2138330,2441600"},
"depots": {
"1091501": {"config": {"oslist": "windows"}},
"2060310": {"dlcappid": "2060310", "config": {"oslist": "windows"}},
},
}
DLC_BLOCKS = {
# Listed in the game's own depots: the base run already covers it.
2060310: {"depots": {"2060310": {"dlcappid": "2060310"}}},
# Its own depot, tagged with its own id -- exactly what it installs.
2138330: {
"depots": {
"2138330": {"dlcappid": "2138330", "config": {"oslist": "windows"}},
"2138331": {"config": {"oslist": "windows", "language": "german"}},
}
},
2441600: {"common": {"name": "Pre-Order Bonus"}},
}
def _app_info(monkeypatch, game=GAME, dlc_blocks=DLC_BLOCKS):
monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: game)
monkeypatch.setattr(
appinfo, "_fetch_app_blocks", lambda ids: {i: dlc_blocks[i] for i in ids}
)
def test_only_dlc_with_depots_of_its_own_needs_a_run(monkeypatch):
_app_info(monkeypatch)
assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330"}}
@pytest.mark.parametrize("game", [{}, {"extended": "scalar"}, {"extended": {}}])
def test_a_game_without_dlc_needs_no_lookup(monkeypatch, game):
monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: game)
monkeypatch.setattr(appinfo, "_fetch_app_blocks", lambda ids: pytest.fail())
assert appinfo.get_dlc_plans(1) == {}
def test_a_game_with_no_depot_list_still_finds_its_dlc(monkeypatch):
_app_info(monkeypatch, game={"extended": {"listofdlc": "2138330"}})
assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330"}}
def test_depot_plan_tags_the_games_own_dlc_depots(monkeypatch):
_app_info(monkeypatch)
plan = appinfo.get_depot_plan(1091500)
assert plan.required == {"1091501"}
assert plan.dlc == {"2060310": "2060310"}
def test_app_info_for_many_apps_comes_from_one_steamcmd_call(monkeypatch):
output = (
'Loading...\n"2060310"\n{\n\t"common"\n\t{\n\t\t"name"\t"A"\n\t}\n}\n'
'"2138330"\n{\n\t"common"\n\t{\n\t\t"name"\t"B"\n\t}\n}\nUnloading\n'
)
calls = []
def fake_run(cmd, **kwargs):
calls.append(cmd)
return subprocess.CompletedProcess(cmd, 0, stdout=output, stderr="")
monkeypatch.setattr(appinfo.subprocess, "run", fake_run)
blocks = appinfo._fetch_app_blocks([2138330, 2060310])
assert blocks[2060310]["common"]["name"] == "A"
assert blocks[2138330]["common"]["name"] == "B"
assert len(calls) == 1
assert calls[0].count("+app_info_print") == 2
def _logged(monkeypatch, returncode, output=""):
monkeypatch.setattr(
depotdl.runlog, "run_logged", lambda cmd, logger: (returncode, output)
)
@pytest.mark.parametrize(
("returncode", "output", "expected"),
[
(0, "", True),
(1, "App 2441600 (Bonus) is not available from this account.", None),
(1, "Connection lost", False),
],
)
def test_download_dlc_separates_not_owned_from_failed(
tmp_path, monkeypatch, returncode, output, expected
):
_logged(monkeypatch, returncode, output)
result = depotdl.download_dlc(2441600, tmp_path, "u", logging.getLogger("t"))
assert result is expected
def test_the_managed_depotdownloader_wins_over_path(tmp_path, monkeypatch):
managed = tmp_path / "depotdownloader"
managed.write_text("")
managed.chmod(0o755)
monkeypatch.setattr(deps, "MANAGED_DEPOTDOWNLOADER", managed)
assert deps.depotdownloader_bin() == str(managed)
@pytest.fixture
def run_log(tmp_path, monkeypatch):
monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state")
log = runlog.setup(1091500)
yield log
for handler in list(log.logger.handlers):
log.logger.removeHandler(handler)
handler.close()
def _download_owned(monkeypatch, run_log, tmp_path, outcomes):
monkeypatch.setattr(
appinfo, "get_dlc_plans", lambda appid, language: {7: {"70"}, 8: {"80"}}
)
monkeypatch.setattr(
depotdl, "download_dlc", lambda dlc_appid, **kw: outcomes[dlc_appid]
)
return dlc.download_owned(1, tmp_path, "u", "english", 8, run_log)
def test_owned_dlc_is_returned_and_unowned_skipped(monkeypatch, run_log, tmp_path):
assert _download_owned(monkeypatch, run_log, tmp_path, {7: True, 8: None}) == {
"70": "7"
}
def test_a_failed_dlc_download_aborts(monkeypatch, run_log, tmp_path):
assert _download_owned(monkeypatch, run_log, tmp_path, {7: False, 8: True}) is None
def test_downloaded_dlc_is_required_and_credited_to_its_app(steam, monkeypatch):
library, install_dir = steam
(install_dir / ".DepotDownloader" / "2138330_100389.manifest").write_bytes(b"x")
manifest = library.steamapps / "appmanifest_1771300.acf"
state = recognize.read_app_state(manifest)
# Steam's own progress for a DLC download it had started itself.
state["DlcDownloads"] = {
"2138330": {"BytesDownloaded": "1", "BytesToDownload": "9"}
}
manifest.write_text(vdf.dumps("AppState", state))
_stub_download(monkeypatch)
_stub_steamcmd(monkeypatch, returncode=8)
monkeypatch.setattr(dlc, "download_owned", lambda *a: {"2138330": "2138330"})
assert cli.main(ARGV) == 0
state = recognize.read_app_state(manifest)
assert state["InstalledDepots"]["2138330"]["dlcappid"] == "2138330"
assert "dlcappid" not in state["InstalledDepots"]["1771303"]
assert "DlcDownloads" not in state
def test_a_failed_dlc_download_leaves_the_manifest_alone(steam, monkeypatch):
library, _ = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
before = manifest.read_text()
_stub_download(monkeypatch)
monkeypatch.setattr(dlc, "download_owned", lambda *a: None)
assert cli.main(ARGV) == 1
assert manifest.read_text() == before
def test_a_dlc_with_a_malformed_depot_section_is_skipped(monkeypatch):
_app_info(monkeypatch, dlc_blocks={**DLC_BLOCKS, 2441600: {"depots": "scalar"}})
assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330"}}

View File

@ -0,0 +1,159 @@
from steam_installer.finish_check import check_ready_to_finish
def test_check_ready_to_finish_blocks_missing_depot(tmp_path, monkeypatch):
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771302_257127247679847518.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771302", "1771303"},
expected_build_id="23914554",
)
assert any("1771303" in p for p in problems)
def test_check_ready_to_finish_blocks_zero_buildid(tmp_path, monkeypatch):
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "0", "TargetBuildID": "0"},
required_depots={"1771303"},
expected_build_id=None,
)
assert any("buildid" in p for p in problems)
def test_check_ready_to_finish_blocks_build_mismatch(tmp_path, monkeypatch):
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "11111111"},
required_depots={"1771303"},
expected_build_id="23914554",
)
assert any("different build" in p for p in problems)
def test_check_ready_to_finish_blocks_running_steam(tmp_path, monkeypatch):
"""Steam owns the manifest while it runs and rewrites it from its own state."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [4242])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771303"},
expected_build_id="23914554",
)
assert any("4242" in p for p in problems)
def test_check_ready_to_finish_passes_when_everything_holds(tmp_path, monkeypatch):
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
(install_dir / "game.pak").write_bytes(b"x" * 1000)
assert (
check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771303"},
expected_build_id="23914554",
)
== []
)
def test_check_ready_to_finish_blocks_empty_required_depot_set(tmp_path, monkeypatch):
"""An empty required set would make the depot check vacuously true and wave
through a download that fetched nothing."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
(install_dir / ".DepotDownloader").mkdir(parents=True)
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots=set(),
expected_build_id="23914554",
)
assert any("no depots match" in p for p in problems)
def test_check_ready_to_finish_blocks_when_no_game_content(tmp_path, monkeypatch):
"""Cache filenames are not content: a dir holding only
.DepotDownloader/<id>_<id>.manifest must not be written as installed."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771303"},
expected_build_id="23914554",
)
assert any("no game content" in p for p in problems)
def test_check_ready_to_finish_blocks_unknown_public_build(tmp_path, monkeypatch):
"""A None expected build means we cannot tell current from stale -- refuse
rather than silently skipping the comparison."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
(install_dir / "game.pak").write_bytes(b"x" * 1000)
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771303"},
expected_build_id=None,
)
assert any("current public build" in p for p in problems)
def test_gate_blocks_before_staging_is_destroyed(tmp_path, monkeypatch):
"""finish_manifest_manually raises when every cached depot is shared. The
CLI deletes Steam's staging tree first, so the gate must catch this while
refusing is still free -- otherwise a failed run destroys tens of GB of
resumable download state."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1001_555.manifest").write_bytes(b"x")
(install_dir / "game.pak").write_bytes(b"x" * 1000)
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "999", "SharedDepots": {"1001": "228980"}},
required_depots={"1001"},
expected_build_id="999",
)
assert any("nothing to record as installed" in p for p in problems)

View File

@ -0,0 +1,187 @@
import pytest
from _recognize_helpers import _write_partial_manifest
from steam_installer import vdf
from steam_installer.library import SteamLibrary
from steam_installer.manifest_write import (
finish_manifest_manually,
)
from steam_installer.recognize import (
RecognizeError,
download_counters_settled,
)
def test_finish_manifest_manually_requires_existing_manifest(tmp_path):
library = SteamLibrary(tmp_path / "Steam")
install_dir = tmp_path / "Steam" / "steamapps" / "common" / "Game"
install_dir.mkdir(parents=True)
with pytest.raises(RecognizeError, match="run steamcmd"):
finish_manifest_manually(library, 1771300, install_dir, now_unix=1000)
def test_finish_manifest_manually_requires_cached_depot_manifests(tmp_path):
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
install_dir.mkdir(parents=True)
with pytest.raises(RecognizeError, match="No cached depot manifests"):
finish_manifest_manually(library, 1771300, install_dir, now_unix=1000)
def test_finish_manifest_manually_sets_state_flags_and_depots(tmp_path):
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771302_257127247679847518.manifest").write_bytes(b"x")
(install_dir / "game.bin").write_bytes(b"0123456789")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
result = vdf.parse(manifest_path.read_text())
app_state = result["AppState"]
assert app_state["StateFlags"] == "4"
assert app_state["buildid"] == "23914554"
assert app_state["InstalledDepots"]["1771302"]["manifest"] == "257127247679847518"
assert app_state["SharedDepots"] == {"228989": "228980"}
assert "StagedDepots" not in app_state
assert manifest_path.with_suffix(".acf.bak").is_file()
def test_finish_manifest_manually_falls_back_to_target_buildid_when_zero(tmp_path):
"""buildid "0" is a sentinel for "no real build installed yet", not a real
value -- a naive `or` chain treats it as truthy and never falls through."""
library = SteamLibrary(tmp_path / "Steam")
library.steamapps.mkdir(parents=True)
data = {
"appid": "1771300",
"StateFlags": "1026",
"buildid": "0",
"TargetBuildID": "23914554",
}
(library.steamapps / "appmanifest_1771300.acf").write_text(
vdf.dumps("AppState", data)
)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771302_257127247679847518.manifest").write_bytes(b"x")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
app_state = vdf.parse(manifest_path.read_text())["AppState"]
assert app_state["buildid"] == "23914554"
def test_finish_manifest_manually_corrects_installdir(tmp_path):
"""The exact bug that caused real data loss: a stale installdir field
pointing at a different folder than where content was actually downloaded."""
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
data = vdf.parse((library.steamapps / "appmanifest_1771300.acf").read_text())
data["AppState"]["installdir"] = "Some Stale Wrong Name"
(library.steamapps / "appmanifest_1771300.acf").write_text(
vdf.dumps("AppState", data["AppState"])
)
install_dir = library.common / "KingdomComeDeliverance2"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771302_257127247679847518.manifest").write_bytes(b"x")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
app_state = vdf.parse(manifest_path.read_text())["AppState"]
assert app_state["installdir"] == "KingdomComeDeliverance2"
def test_finish_manifest_settles_download_counters(tmp_path):
"""The regression that started all this: a manifest claiming StateFlags=4
while still saying 22 GB of 92 GB downloaded. Steam discarded the "installed"
claim and resumed a 90 GB download."""
library = SteamLibrary(tmp_path / "Steam")
library.steamapps.mkdir(parents=True)
data = {
"appid": "1771300",
"StateFlags": "1026",
"buildid": "23914554",
"DownloadType": "1",
"BytesToDownload": "92034161488",
"BytesDownloaded": "22129058224",
"BytesToStage": "96447764586",
"BytesStaged": "22462334559",
"StagingSize": "25876914424",
}
(library.steamapps / "appmanifest_1771300.acf").write_text(
vdf.dumps("AppState", data)
)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
app_state = vdf.parse(manifest_path.read_text())["AppState"]
assert app_state["BytesDownloaded"] == app_state["BytesToDownload"]
assert app_state["BytesStaged"] == app_state["BytesToStage"]
assert app_state["StagingSize"] == "0"
# DownloadType is present on every real installed manifest -- keep it.
assert app_state["DownloadType"] == "1"
assert download_counters_settled(app_state)
def test_finish_manifest_excludes_shared_depots_from_installed(tmp_path):
"""228989 is a SharedDepots entry owned by app 228980; claiming this app
installed its own copy contradicts SharedDepots."""
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "228989_5753583882400741046.manifest").write_bytes(b"x")
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
app_state = vdf.parse(manifest_path.read_text())["AppState"]
assert set(app_state["InstalledDepots"]) == {"1771303"}
assert app_state["SharedDepots"] == {"228989": "228980"}
def test_finish_manifest_rejects_only_shared_depots(tmp_path):
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "228989_5753583882400741046.manifest").write_bytes(b"x")
with pytest.raises(RecognizeError, match="shared depot"):
finish_manifest_manually(library, 1771300, install_dir, now_unix=1000)
def test_finish_manifest_rejects_malformed_appstate(tmp_path, monkeypatch):
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
cache = install_dir / ".DepotDownloader"
cache.mkdir(parents=True)
(cache / "1771303_1.manifest").write_bytes(b"x")
monkeypatch.setattr(
"steam_installer.vdf.parse", lambda text: {"AppState": "scalar"}
)
with pytest.raises(RecognizeError, match="not a valid AppState"):
finish_manifest_manually(library, 1771300, install_dir, now_unix=1)

View File

@ -0,0 +1,157 @@
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

View File

@ -1,26 +1,18 @@
import logging
import signal
from pathlib import Path
import pytest
from steam_installer import recognize, vdf
from steam_installer import vdf
from steam_installer.library import SteamLibrary
from steam_installer.recognize import (
RecognizeError,
check_ready_to_finish,
clear_steam_staging,
discover_cached_depot_manifests,
download_counters_settled,
ensure_installdir_correct,
finish_manifest_manually,
manifest_reflects_real_install,
measure_content_size,
read_app_state,
run_steamcmd_validate,
steam_client_pids,
)
from _recognize_helpers import _fake_proc
def test_discover_cached_depot_manifests_parses_filenames(tmp_path):
cache_dir = tmp_path / ".DepotDownloader"
@ -41,150 +33,6 @@ def test_discover_cached_depot_manifests_missing_dir_returns_empty(tmp_path):
assert discover_cached_depot_manifests(tmp_path) == {}
def _write_partial_manifest(steamapps: Path, appid: int) -> None:
steamapps.mkdir(parents=True, exist_ok=True)
data = {
"appid": str(appid),
"StateFlags": "1026",
"buildid": "23914554",
"SharedDepots": {"228989": "228980"},
}
(steamapps / f"appmanifest_{appid}.acf").write_text(vdf.dumps("AppState", data))
def test_finish_manifest_manually_requires_existing_manifest(tmp_path):
library = SteamLibrary(tmp_path / "Steam")
install_dir = tmp_path / "Steam" / "steamapps" / "common" / "Game"
install_dir.mkdir(parents=True)
with pytest.raises(RecognizeError, match="run steamcmd"):
finish_manifest_manually(library, 1771300, install_dir, now_unix=1000)
def test_finish_manifest_manually_requires_cached_depot_manifests(tmp_path):
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
install_dir.mkdir(parents=True)
with pytest.raises(RecognizeError, match="No cached depot manifests"):
finish_manifest_manually(library, 1771300, install_dir, now_unix=1000)
def test_finish_manifest_manually_sets_state_flags_and_depots(tmp_path):
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771302_257127247679847518.manifest").write_bytes(b"x")
(install_dir / "game.bin").write_bytes(b"0123456789")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
result = vdf.parse(manifest_path.read_text())
app_state = result["AppState"]
assert app_state["StateFlags"] == "4"
assert app_state["buildid"] == "23914554"
assert app_state["InstalledDepots"]["1771302"]["manifest"] == "257127247679847518"
assert app_state["SharedDepots"] == {"228989": "228980"}
assert "StagedDepots" not in app_state
assert manifest_path.with_suffix(".acf.bak").is_file()
def test_finish_manifest_manually_falls_back_to_target_buildid_when_zero(tmp_path):
"""buildid "0" is a sentinel for "no real build installed yet", not a real
value -- a naive `or` chain treats it as truthy and never falls through."""
library = SteamLibrary(tmp_path / "Steam")
library.steamapps.mkdir(parents=True)
data = {
"appid": "1771300",
"StateFlags": "1026",
"buildid": "0",
"TargetBuildID": "23914554",
}
(library.steamapps / "appmanifest_1771300.acf").write_text(
vdf.dumps("AppState", data)
)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771302_257127247679847518.manifest").write_bytes(b"x")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
app_state = vdf.parse(manifest_path.read_text())["AppState"]
assert app_state["buildid"] == "23914554"
def test_finish_manifest_manually_corrects_installdir(tmp_path):
"""The exact bug that caused real data loss: a stale installdir field
pointing at a different folder than where content was actually downloaded."""
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
data = vdf.parse((library.steamapps / "appmanifest_1771300.acf").read_text())
data["AppState"]["installdir"] = "Some Stale Wrong Name"
(library.steamapps / "appmanifest_1771300.acf").write_text(
vdf.dumps("AppState", data["AppState"])
)
install_dir = library.common / "KingdomComeDeliverance2"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771302_257127247679847518.manifest").write_bytes(b"x")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
app_state = vdf.parse(manifest_path.read_text())["AppState"]
assert app_state["installdir"] == "KingdomComeDeliverance2"
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_manifest_reflects_real_install_true_when_consistent(tmp_path):
library = SteamLibrary(tmp_path / "Steam")
library.steamapps.mkdir(parents=True)
@ -225,87 +73,6 @@ def test_manifest_reflects_real_install_false_when_no_manifest(tmp_path):
assert not manifest_reflects_real_install(library, 1771300, library.common / "Game")
def test_finish_manifest_settles_download_counters(tmp_path):
"""The regression that started all this: a manifest claiming StateFlags=4
while still saying 22 GB of 92 GB downloaded. Steam discarded the "installed"
claim and resumed a 90 GB download."""
library = SteamLibrary(tmp_path / "Steam")
library.steamapps.mkdir(parents=True)
data = {
"appid": "1771300",
"StateFlags": "1026",
"buildid": "23914554",
"DownloadType": "1",
"BytesToDownload": "92034161488",
"BytesDownloaded": "22129058224",
"BytesToStage": "96447764586",
"BytesStaged": "22462334559",
"StagingSize": "25876914424",
}
(library.steamapps / "appmanifest_1771300.acf").write_text(
vdf.dumps("AppState", data)
)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
app_state = vdf.parse(manifest_path.read_text())["AppState"]
assert app_state["BytesDownloaded"] == app_state["BytesToDownload"]
assert app_state["BytesStaged"] == app_state["BytesToStage"]
assert app_state["StagingSize"] == "0"
# DownloadType is present on every real installed manifest -- keep it.
assert app_state["DownloadType"] == "1"
assert download_counters_settled(app_state)
def test_finish_manifest_excludes_shared_depots_from_installed(tmp_path):
"""228989 is a SharedDepots entry owned by app 228980; claiming this app
installed its own copy contradicts SharedDepots."""
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "228989_5753583882400741046.manifest").write_bytes(b"x")
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
manifest_path = finish_manifest_manually(
library, 1771300, install_dir, now_unix=1000
)
app_state = vdf.parse(manifest_path.read_text())["AppState"]
assert set(app_state["InstalledDepots"]) == {"1771303"}
assert app_state["SharedDepots"] == {"228989": "228980"}
def test_finish_manifest_rejects_only_shared_depots(tmp_path):
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "228989_5753583882400741046.manifest").write_bytes(b"x")
with pytest.raises(RecognizeError, match="shared depot"):
finish_manifest_manually(library, 1771300, install_dir, now_unix=1000)
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_download_counters_settled_detects_partial_download():
assert not download_counters_settled(
{"BytesToDownload": "92", "BytesDownloaded": "22"}
@ -335,90 +102,6 @@ def test_manifest_reflects_real_install_false_when_counters_unsettled(tmp_path):
assert not manifest_reflects_real_install(library, 1771300, library.common / "Game")
def test_check_ready_to_finish_blocks_missing_depot(tmp_path, monkeypatch):
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771302_257127247679847518.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771302", "1771303"},
expected_build_id="23914554",
)
assert any("1771303" in p for p in problems)
def test_check_ready_to_finish_blocks_zero_buildid(tmp_path, monkeypatch):
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "0", "TargetBuildID": "0"},
required_depots={"1771303"},
expected_build_id=None,
)
assert any("buildid" in p for p in problems)
def test_check_ready_to_finish_blocks_build_mismatch(tmp_path, monkeypatch):
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "11111111"},
required_depots={"1771303"},
expected_build_id="23914554",
)
assert any("different build" in p for p in problems)
def test_check_ready_to_finish_blocks_running_steam(tmp_path, monkeypatch):
"""Steam owns the manifest while it runs and rewrites it from its own state."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [4242])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771303"},
expected_build_id="23914554",
)
assert any("4242" in p for p in problems)
def test_check_ready_to_finish_passes_when_everything_holds(tmp_path, monkeypatch):
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
(install_dir / "game.pak").write_bytes(b"x" * 1000)
assert (
check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771303"},
expected_build_id="23914554",
)
== []
)
def test_read_app_state_missing_file(tmp_path):
assert read_app_state(tmp_path / "nope.acf") == {}
@ -454,137 +137,6 @@ def test_clear_steam_staging_when_nothing_staged(tmp_path):
assert clear_steam_staging(library, 1771300) == 0
def test_check_ready_to_finish_blocks_empty_required_depot_set(tmp_path, monkeypatch):
"""An empty required set would make the depot check vacuously true and wave
through a download that fetched nothing."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
(install_dir / ".DepotDownloader").mkdir(parents=True)
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots=set(),
expected_build_id="23914554",
)
assert any("no depots match" in p for p in problems)
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_check_ready_to_finish_blocks_when_no_game_content(tmp_path, monkeypatch):
"""Cache filenames are not content: a dir holding only
.DepotDownloader/<id>_<id>.manifest must not be written as installed."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771303"},
expected_build_id="23914554",
)
assert any("no game content" in p for p in problems)
def test_check_ready_to_finish_blocks_unknown_public_build(tmp_path, monkeypatch):
"""A None expected build means we cannot tell current from stale -- refuse
rather than silently skipping the comparison."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1771303_1999516201078544527.manifest").write_bytes(b"x")
(install_dir / "game.pak").write_bytes(b"x" * 1000)
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "23914554"},
required_depots={"1771303"},
expected_build_id=None,
)
assert any("current public build" in p for p in problems)
def test_gate_blocks_before_staging_is_destroyed(tmp_path, monkeypatch):
"""finish_manifest_manually raises when every cached depot is shared. The
CLI deletes Steam's staging tree first, so the gate must catch this while
refusing is still free -- otherwise a failed run destroys tens of GB of
resumable download state."""
monkeypatch.setattr("steam_installer.recognize.steam_client_pids", lambda: [])
install_dir = tmp_path / "Game"
cache_dir = install_dir / ".DepotDownloader"
cache_dir.mkdir(parents=True)
(cache_dir / "1001_555.manifest").write_bytes(b"x")
(install_dir / "game.pak").write_bytes(b"x" * 1000)
problems = check_ready_to_finish(
install_dir=install_dir,
app_state={"buildid": "999", "SharedDepots": {"1001": "228980"}},
required_depots={"1001"},
expected_build_id="999",
)
assert any("nothing to record as installed" in p for p in problems)
def _fake_proc(tmp_path, entries):
"""Build a /proc-shaped tree: {pid: exe_target_path_or_None}."""
proc = tmp_path / "proc"
proc.mkdir()
for name, target in entries.items():
entry = proc / name
entry.mkdir()
if target is not None:
real = tmp_path / "bin" / target
real.parent.mkdir(parents=True, exist_ok=True)
real.write_text("")
(entry / "exe").symlink_to(real)
return proc
def test_steam_client_pids_finds_native_and_flatpak(tmp_path, monkeypatch):
"""Matching on the executable name only: an earlier version also demanded
'ubuntu12_32' in the path, so a Flatpak Steam (/app/bin/steam) silently
@ -618,75 +170,6 @@ def test_steam_client_pids_ignores_vanished_processes(tmp_path, monkeypatch):
assert steam_client_pids() == [111]
def test_run_steamcmd_validate_argv_and_result(monkeypatch):
seen = {}
def fake_run_logged(cmd, logger, timeout=None):
seen["cmd"] = cmd
seen["timeout"] = timeout
return 0, "all good"
monkeypatch.setattr("steam_installer.recognize.runlog.run_logged", fake_run_logged)
result = run_steamcmd_validate(1771300, "krzys2000", logging.getLogger("t"))
cmd = seen["cmd"]
# The platform flag must precede +login, and no force_install_dir may appear:
# it sets a LIBRARY ROOT and pointing it at the game dir caused the original
# "Invalid platform" failure.
assert cmd.index("+@sSteamCmdForcePlatformType") < cmd.index("+login")
assert "force_install_dir" not in " ".join(cmd)
assert cmd[cmd.index("+app_update") + 1 : cmd.index("+app_update") + 3] == [
"1771300",
"validate",
]
assert result.completed and result.returncode == 0
assert result.output == "all good"
assert not result.crashed_on_install_script
def test_run_steamcmd_validate_detects_the_install_script_segfault(monkeypatch):
"""The known steamcmd bug kills the process with SIGSEGV; checking the
signal beats grepping captured text for a message string."""
monkeypatch.setattr(
"steam_installer.recognize.runlog.run_logged",
lambda cmd, logger, timeout=None: (-signal.SIGSEGV, ""),
)
result = run_steamcmd_validate(1, "u", logging.getLogger("t"))
assert result.crashed_on_install_script
assert not result.completed
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.recognize.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_manifest_reflects_real_install_false_on_malformed_appstate(
tmp_path, monkeypatch
):
@ -696,26 +179,11 @@ def test_manifest_reflects_real_install_false_on_malformed_appstate(
vdf.dumps("AppState", {"StateFlags": "4"})
)
monkeypatch.setattr(
"steam_installer.recognize.vdf.parse", lambda text: {"AppState": "scalar"}
"steam_installer.vdf.parse", lambda text: {"AppState": "scalar"}
)
assert not manifest_reflects_real_install(library, 1771300, library.common / "Game")
def test_finish_manifest_rejects_malformed_appstate(tmp_path, monkeypatch):
library = SteamLibrary(tmp_path / "Steam")
_write_partial_manifest(library.steamapps, 1771300)
install_dir = library.common / "Game"
cache = install_dir / ".DepotDownloader"
cache.mkdir(parents=True)
(cache / "1771303_1.manifest").write_bytes(b"x")
monkeypatch.setattr(
"steam_installer.recognize.vdf.parse", lambda text: {"AppState": "scalar"}
)
with pytest.raises(RecognizeError, match="not a valid AppState"):
finish_manifest_manually(library, 1771300, install_dir, now_unix=1)
def test_steam_client_pids_skips_unreadable_exe(tmp_path, monkeypatch):
"""/proc/<pid>/exe raises for processes we don't own; that must not abort
the scan and silently disable the guard."""
@ -738,28 +206,3 @@ def test_steam_client_pids_skips_unreadable_exe(tmp_path, monkeypatch):
monkeypatch.setattr(Path, "resolve", flaky)
assert steam_client_pids() == [222]
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):
recognize._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

View File

@ -0,0 +1,47 @@
import logging
import signal
from steam_installer.steamcmd_validate import run_steamcmd_validate
def test_run_steamcmd_validate_argv_and_result(monkeypatch):
seen = {}
def fake_run_logged(cmd, logger, timeout=None):
seen["cmd"] = cmd
seen["timeout"] = timeout
return 0, "all good"
monkeypatch.setattr(
"steam_installer.steamcmd_validate.runlog.run_logged", fake_run_logged
)
result = run_steamcmd_validate(1771300, "krzys2000", logging.getLogger("t"))
cmd = seen["cmd"]
# The platform flag must precede +login, and no force_install_dir may appear:
# it sets a LIBRARY ROOT and pointing it at the game dir caused the original
# "Invalid platform" failure.
assert cmd.index("+@sSteamCmdForcePlatformType") < cmd.index("+login")
assert "force_install_dir" not in " ".join(cmd)
assert cmd[cmd.index("+app_update") + 1 : cmd.index("+app_update") + 3] == [
"1771300",
"validate",
]
assert result.completed and result.returncode == 0
assert result.output == "all good"
assert not result.crashed_on_install_script
def test_run_steamcmd_validate_detects_the_install_script_segfault(monkeypatch):
"""The known steamcmd bug kills the process with SIGSEGV; checking the
signal beats grepping captured text for a message string."""
monkeypatch.setattr(
"steam_installer.steamcmd_validate.runlog.run_logged",
lambda cmd, logger, timeout=None: (-signal.SIGSEGV, ""),
)
result = run_steamcmd_validate(1, "u", logging.getLogger("t"))
assert result.crashed_on_install_script
assert not result.completed