steam-backlog-enforcer/steam_installer/tests/test_config.py
Krzysztof kuhy Rudnicki a76e726e36 Merge steam-game-installer into the repo
The installer ships as the steam_installer package beside
steam_backlog_enforcer, with its tests, docs and Proton lock. The
enforcer's fast install now starts the installer from this checkout.

Both appinfo.vdf readers share one key-value decoder
(steam_installer.appinfo_kv), and the duplicated test fixtures and
helpers are folded into conftest and _cli_helpers so the whole-tree
duplicate-code check is clean. check_requirements_sync treats sibling
packages as first-party.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZCxnNWLtpVwFLVk9w
2026-11-22 20:25:39 +01:00

120 lines
4.4 KiB
Python

"""Username persistence and its precedence order."""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from steam_installer import config
if TYPE_CHECKING:
from pathlib import Path
@pytest.fixture(autouse=True)
def isolated_config(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""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: pytest.MonkeyPatch,
) -> None:
"""Explicit username wins over everything."""
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: pytest.MonkeyPatch) -> None:
"""Env wins over the saved file."""
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: pytest.MonkeyPatch,
) -> None:
"""A blank env var does not shadow the saved file."""
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() -> None:
"""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() -> None:
"""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() -> None:
"""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() -> None:
"""Load returns none when nothing is saved."""
assert config.load_username() is None
def test_load_ignores_other_keys_and_malformed_lines() -> None:
"""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() -> None:
"""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: pytest.MonkeyPatch) -> None:
"""Prompting saves the answer."""
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: pytest.MonkeyPatch) -> None:
"""An empty answer is refused."""
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() -> None:
"""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() -> None:
"""`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() -> None:
"""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