mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-22 23:22:18 +01:00
The KCD2 run reported success but Steam re-downloaded 90 GB anyway. Two
independent causes, plus a defect that hid both.
force_install_dir sets a Steam *library root*, not an install directory.
Pointing it at the game folder made steamcmd search <game>/steamapps/ for
manifests ("Loaded 0 apps from install folder"), create that stray dir, and
fail the app with "Invalid platform" for lack of depot context. Dropped it;
the app already lives in a registered library. Separately, a Linux steamcmd
needs +@sSteamCmdForcePlatformType windows for a Windows-only title.
Steam then discarded the manifest because it was self-contradictory:
StateFlags=4 while BytesDownloaded (22 GB) != BytesToDownload (92 GB), with
tens of GB still staged under steamapps/downloading/. Across every genuinely
installed manifest the byte counters are equal in pairs, StagingSize is 0 and
staging is empty; DownloadType is kept, not dropped. Settle all of it, and
refuse to call an install real until it holds.
The defect that hid this: steamcmd exited 8 having done nothing and the tool
still wrote StateFlags=4, printed a success line and returned 0. Now gated on
depotdownloader succeeding, every required depot being cached, real game
content on disk, a matching buildid and no running Steam client -- and it
re-reads the manifest after writing.
Also fixes data corruption found in review: vdf.dumps never re-escaped
backslashes, so each parse/dump cycle halved them and silently mangled the
Windows paths in appmanifest_228980.acf's InstallScripts. Manifest writes are
now atomic with a preserved pristine .acf.bak.
Adds per-run logging to ~/.local/state/steam-game-installer (argv, exit codes,
full subprocess output, manifest before/after), teed through a pty so
steamcmd's password prompt still works.
Verified end-to-end on KCD2: Steam logged "Fully Installed, App Running" and
the game launched with no download queued. Coverage 100% statements+branches,
enforced by fail_under.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EK4urCmbTqN4Vrc3nGBcXF
211 lines
6.7 KiB
Python
211 lines
6.7 KiB
Python
import errno
|
|
import logging
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
from steam_installer import runlog
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def isolated_log_dir(tmp_path, monkeypatch):
|
|
"""Never write to the real ~/.local/state during tests."""
|
|
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()
|
|
|
|
|
|
def test_setup_creates_log_file_and_latest_symlink():
|
|
log = runlog.setup(1771300)
|
|
|
|
assert log.path.is_file()
|
|
assert "1771300" in log.path.name
|
|
latest = runlog.LOG_DIR / "latest.log"
|
|
assert latest.is_symlink()
|
|
assert latest.resolve() == log.path.resolve()
|
|
|
|
|
|
def test_setup_without_appid_still_names_the_file():
|
|
log = runlog.setup(None)
|
|
assert "noappid" in log.path.name
|
|
|
|
|
|
def test_setup_twice_does_not_duplicate_handlers():
|
|
"""A second setup() in one process must not write every line twice."""
|
|
runlog.setup(1)
|
|
log = runlog.setup(2)
|
|
log.logger.info("once")
|
|
assert log.path.read_text().count("once") == 1
|
|
|
|
|
|
def test_run_logged_records_command_output_and_exit_code(capfd):
|
|
log = runlog.setup(1771300)
|
|
|
|
returncode, output = runlog.run_logged(
|
|
["bash", "-c", "echo to-stdout; echo to-stderr >&2; exit 7"], log.logger
|
|
)
|
|
|
|
assert returncode == 7
|
|
assert "to-stdout" in output
|
|
assert "to-stderr" in output
|
|
|
|
# Mirrored to the terminal...
|
|
assert "to-stdout" in capfd.readouterr().out
|
|
# ...and recorded in the log, along with the argv and exit code.
|
|
contents = log.path.read_text()
|
|
assert "to-stdout" in contents
|
|
assert "echo to-stdout" in contents
|
|
assert "exit=7" in contents
|
|
|
|
|
|
def test_run_logged_child_sees_a_tty():
|
|
"""The whole reason for the pty: steamcmd only prompts interactively for a
|
|
password when it believes it is attached to a terminal. A plain pipe makes
|
|
the prompt invisible and the run looks hung.
|
|
"""
|
|
log = runlog.setup(1771300)
|
|
_, output = runlog.run_logged(
|
|
[sys.executable, "-c", "import sys; print(sys.stdout.isatty())"], log.logger
|
|
)
|
|
assert "True" in output
|
|
|
|
|
|
def test_run_logged_times_out():
|
|
log = runlog.setup(1771300)
|
|
with pytest.raises(subprocess.TimeoutExpired):
|
|
runlog.run_logged(["sleep", "10"], log.logger, timeout=0.5)
|
|
|
|
|
|
def test_file_contents_records_manifest_verbatim(tmp_path):
|
|
log = runlog.setup(1771300)
|
|
manifest = tmp_path / "appmanifest_1771300.acf"
|
|
manifest.write_text('"AppState"\n{\n\t"StateFlags"\t\t"4"\n}\n')
|
|
|
|
log.file_contents("Manifest before run", manifest)
|
|
|
|
contents = log.path.read_text()
|
|
assert "Manifest before run" in contents
|
|
assert '"StateFlags"' in contents
|
|
|
|
|
|
def test_file_contents_survives_missing_file(tmp_path):
|
|
"""A first-ever install has no manifest yet; that must not kill the run."""
|
|
log = runlog.setup(1771300)
|
|
log.file_contents("Manifest before run", tmp_path / "absent.acf")
|
|
assert "unreadable" in log.path.read_text()
|
|
|
|
|
|
def test_run_logged_records_output_even_on_timeout():
|
|
"""A steamcmd hang at the 3600s timeout is exactly when the log matters
|
|
most; discarding what the child said before hanging defeats the point."""
|
|
log = runlog.setup(1771300)
|
|
marker = "TIMEOUT" + "MARKER"
|
|
with pytest.raises(subprocess.TimeoutExpired) as excinfo:
|
|
runlog.run_logged(
|
|
["bash", "-c", f'printf "%s\\n" "{marker}"; sleep 30'],
|
|
log.logger,
|
|
timeout=1.0,
|
|
)
|
|
|
|
contents = log.path.read_text()
|
|
assert "--- output" in contents
|
|
assert marker in contents
|
|
assert "TIMED OUT" in contents
|
|
assert marker.encode() in excinfo.value.output
|
|
|
|
|
|
def test_point_latest_at_survives_a_broken_log_dir(monkeypatch, tmp_path):
|
|
"""A stale or unwritable latest.log must never fail an otherwise fine run."""
|
|
log = runlog.setup(1)
|
|
latest = runlog.LOG_DIR / "latest.log"
|
|
latest.unlink()
|
|
latest.mkdir() # a directory where the symlink should go
|
|
|
|
runlog._point_latest_at(log.path) # must not raise
|
|
|
|
assert latest.is_dir()
|
|
|
|
|
|
def test_run_logged_closes_pty_fds_when_spawn_fails(monkeypatch):
|
|
"""A leaked master/slave pair per failed spawn would exhaust the fd table."""
|
|
log = runlog.setup(1)
|
|
before = len(os.listdir("/proc/self/fd"))
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
runlog.run_logged(["definitely-not-a-real-binary-xyz"], log.logger)
|
|
|
|
assert len(os.listdir("/proc/self/fd")) == before
|
|
|
|
|
|
def test_run_logged_stops_when_select_fails(monkeypatch):
|
|
"""A closed master fd surfaces as OSError from select; end the relay."""
|
|
log = runlog.setup(1)
|
|
monkeypatch.setattr(
|
|
runlog.select, "select", lambda *a, **kw: (_ for _ in ()).throw(OSError)
|
|
)
|
|
|
|
returncode, output = runlog.run_logged(["true"], log.logger)
|
|
|
|
assert returncode == 0
|
|
assert output == ""
|
|
|
|
|
|
def test_run_logged_reraises_unexpected_read_errors(monkeypatch):
|
|
"""EIO means "child closed the pty" and ends the loop; anything else is a
|
|
real failure and must not be swallowed."""
|
|
log = runlog.setup(1)
|
|
# Force select to always report readable so os.read is definitely reached.
|
|
monkeypatch.setattr(runlog.select, "select", lambda *a, **kw: ([a[0][0]], [], []))
|
|
|
|
def bad_read(fd, size):
|
|
raise OSError(errno.EBADF, "bad file descriptor")
|
|
|
|
monkeypatch.setattr(runlog, "_read_pty", bad_read)
|
|
|
|
with pytest.raises(OSError) as excinfo:
|
|
runlog.run_logged(["bash", "-c", "echo hi; sleep 0.2"], log.logger)
|
|
assert excinfo.value.errno == errno.EBADF
|
|
|
|
|
|
def test_run_logged_exits_when_child_dies_without_output(monkeypatch):
|
|
"""select never reports readable, so the loop must notice via poll() that
|
|
the child is gone rather than spinning forever."""
|
|
log = runlog.setup(1)
|
|
monkeypatch.setattr(runlog.select, "select", lambda *a, **kw: ([], [], []))
|
|
|
|
returncode, output = runlog.run_logged(["true"], log.logger)
|
|
|
|
assert returncode == 0
|
|
assert output == ""
|
|
|
|
|
|
def test_run_logged_handles_empty_read(monkeypatch):
|
|
"""A zero-byte read is EOF on the pty."""
|
|
log = runlog.setup(1)
|
|
real_read = runlog._read_pty
|
|
calls = {"n": 0}
|
|
|
|
def once_then_eof(fd, size):
|
|
calls["n"] += 1
|
|
if calls["n"] == 1:
|
|
return real_read(fd, size)
|
|
return b""
|
|
|
|
monkeypatch.setattr(runlog, "_read_pty", once_then_eof)
|
|
|
|
returncode, _ = runlog.run_logged(["bash", "-c", "echo hi"], log.logger)
|
|
assert returncode == 0
|
|
|
|
|
|
def test_run_logged_survives_a_child_that_writes_nothing():
|
|
"""Exercises the poll()-based exit when the pty never becomes readable."""
|
|
log = runlog.setup(1)
|
|
returncode, output = runlog.run_logged(["true"], log.logger)
|
|
assert returncode == 0
|
|
assert output == ""
|