steam-backlog-enforcer/_installer_import/tests/test_config.py
Krzysztof kuhy Rudnicki 15f647f639 Install any game from an appid, a URL or a name; adopt Steam's partial downloads
`./run.sh <anything>` is now the entry point. It installs its own dependencies
(steamcmd via yay, DepotDownloader from the SteamRE release, a local .venv) and
works out what the argument is: an appid, a Steam URL, an exact title, or part
of a title. Every route is confirmed through steamcmd's own app_info before
anything acts on it.

Name search moves to steamcommunity.com/actions/SearchApps, which works, with
the storefront API kept as a fallback -- it TLS-rejects scripted clients here,
so search had been effectively dead. DNS on this box fails transiently about one
lookup in three, so each backend is retried before giving up. An exact title now
wins over Steam's own ranking, which puts "Portal 2" above "Portal"; an
ambiguous name shows a picker, or exits 2 rather than guessing.

Two problems found while building this, both now handled:

Anonymous steamcmd has no licence for a paid app and does not say so. It
transfers nothing, prints "Success! App '284160' fully installed.", exits 0, and
writes StateFlags=4 with SizeOnDisk=0 and no depots. A running Steam client then
reads that, decides the app is installed but stale, and downloads the whole game
by itself. `ghost.py` detects that state -- validated read-only against all ten
manifests in a real library, where the nine genuine installs are untouched and
only the fabricated one matches -- and `--repair-ghosts` clears it away. Repair
uses rmdir, never rmtree, so a non-empty directory survives a wrong diagnosis.

And Steam's own in-progress downloads are no longer wasted. `takeover.py`
hardlinks `steamapps/downloading/<appid>/` into the install directory before
depotdownloader runs; depotdownloader hash-checks what is already on disk
(ContentDownloader.cs skips a file when neededChunks is empty) and fetches only
the remainder. Nothing is deleted up front -- the staging tree is cleared only
after the install is verified -- so a failed run still leaves Steam able to
resume. The trees share inodes, so an in-place chunk patch corrects both.

cli.py drops from 311 to 244 lines, back under the repo's 250-line cap, by
moving the install flow to flow.py and the manifest finish to finalize.py.

install.sh had a latent bug: `pip install -e .[dev]` only warns about a missing
extra and exits 0, so the `|| fallback` that installed pytest and coverage never
ran. A fresh venv had no test tools at all; the existing one only worked because
they had been installed by hand earlier. Now named outright and asserted.

246 tests, 100% statement and branch coverage. Verified live: all four input
forms resolve to 284160, an exact "Portal" resolves to 400 and not 620, an
ambiguous name exits 2, ghost detection flags nothing on nine real installs, and
a from-scratch venv bootstraps and runs.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sgkb3wH6MU9fYquX328ovT
2026-09-11 21:38:16 +02:00

96 lines
3.4 KiB
Python

"""Username persistence and its precedence order."""
import pytest
from steam_installer import config
@pytest.fixture(autouse=True)
def isolated_config(tmp_path, monkeypatch):
"""Never read or write the real ~/.config during a test run."""
monkeypatch.setattr(config, "CONFIG_DIR", tmp_path / "cfg")
monkeypatch.setattr(config, "CONFIG_PATH", tmp_path / "cfg" / "config")
monkeypatch.delenv(config.ENV_VAR, raising=False)
def test_explicit_username_wins_over_everything(monkeypatch):
monkeypatch.setenv(config.ENV_VAR, "from_env")
config.save_username("from_file")
assert config.resolve_username("explicit", interactive=False) == "explicit"
def test_env_wins_over_the_saved_file(monkeypatch):
monkeypatch.setenv(config.ENV_VAR, "from_env")
config.save_username("from_file")
assert config.resolve_username(None, interactive=False) == "from_env"
def test_a_blank_env_var_does_not_shadow_the_saved_file(monkeypatch):
monkeypatch.setenv(config.ENV_VAR, " ")
config.save_username("from_file")
assert config.resolve_username(None, interactive=False) == "from_file"
def test_the_saved_file_is_used_when_nothing_else_is_given():
config.save_username("saved")
assert config.resolve_username(None, interactive=False) == "saved"
def test_saving_creates_the_directory_and_locks_the_file_down():
config.save_username("someone")
assert config.CONFIG_PATH.read_text() == "username=someone\n"
assert config.CONFIG_PATH.stat().st_mode & 0o777 == 0o600
def test_saving_twice_replaces_rather_than_appends():
config.save_username("first")
config.save_username("second")
assert config.load_username() == "second"
def test_load_returns_none_when_nothing_is_saved():
assert config.load_username() is None
def test_load_ignores_other_keys_and_malformed_lines():
config.CONFIG_DIR.mkdir(parents=True)
config.CONFIG_PATH.write_text("# a comment\nnot-a-pair\nother=x\nusername=real\n")
assert config.load_username() == "real"
def test_an_empty_username_value_reads_as_unset():
config.CONFIG_DIR.mkdir(parents=True)
config.CONFIG_PATH.write_text("username=\n")
assert config.load_username() is None
def test_prompting_saves_the_answer(monkeypatch):
monkeypatch.setattr("builtins.input", lambda *a: " typed ")
assert config.resolve_username(None, interactive=True) == "typed"
assert config.load_username() == "typed"
def test_an_empty_answer_is_refused(monkeypatch):
monkeypatch.setattr("builtins.input", lambda *a: "")
with pytest.raises(config.ConfigError, match="No Steam username entered"):
config.resolve_username(None, interactive=True)
def test_non_interactive_with_nothing_available_explains_the_options():
with pytest.raises(config.ConfigError, match="--username"):
config.resolve_username(None, interactive=False)
def test_a_line_with_no_separator_is_skipped_before_a_valid_one():
"""`partition` returns an empty separator for a bare line; that must not be
mistaken for a key with an empty value."""
config.CONFIG_DIR.mkdir(parents=True)
config.CONFIG_PATH.write_text("username\nusername=real\n")
assert config.load_username() == "real"
def test_a_config_file_with_no_username_key_reads_as_unset():
config.CONFIG_DIR.mkdir(parents=True)
config.CONFIG_PATH.write_text("other=x\n# nothing relevant here\n")
assert config.load_username() is None