steam-backlog-enforcer/steam_installer/tests/test_steamctl.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

71 lines
2.2 KiB
Python

"""Closing and reopening the Steam client.
Steam is only ever asked to leave politely. A hung client is a reason to stop
and say so, never to escalate to a signal: SIGKILL mid-write is how a manifest
ends up truncated.
"""
from __future__ import annotations
import logging
import pytest
from steam_installer import recognize, steamctl
LOGGER = logging.getLogger("test-steamctl")
def test_stop_is_a_no_op_when_steam_is_not_running(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Stop is a no op when steam is not running."""
monkeypatch.setattr(recognize, "steam_client_pids", list)
called = []
monkeypatch.setattr(steamctl.subprocess, "run", lambda *a, **k: called.append(a))
assert steamctl.stop(LOGGER) is False
assert called == []
def test_stop_asks_steam_to_shut_down_and_waits_for_it(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Stop asks steam to shut down and waits for it."""
pids = iter([[42], [42], [], []])
monkeypatch.setattr(recognize, "steam_client_pids", lambda: next(pids))
seen = {}
monkeypatch.setattr(
steamctl.subprocess, "run", lambda cmd, **k: seen.setdefault("cmd", cmd)
)
assert steamctl.stop(LOGGER, sleep=lambda _: None) is True
assert seen["cmd"] == [steamctl.STEAM, "-shutdown"]
def test_stop_gives_up_rather_than_killing_a_client_that_will_not_exit(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Stop gives up rather than killing a client that will not exit."""
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42])
monkeypatch.setattr(steamctl.subprocess, "run", lambda *a, **k: None)
with pytest.raises(steamctl.SteamStillRunningError, match="42"):
steamctl.stop(LOGGER, timeout=1.0, sleep=lambda _: None)
def test_start_launches_steam_detached(monkeypatch: pytest.MonkeyPatch) -> None:
"""Start launches steam detached."""
seen = {}
def fake_popen(cmd: list[str], **kwargs: object) -> None:
seen["cmd"] = cmd
seen["kwargs"] = kwargs
monkeypatch.setattr(steamctl.subprocess, "Popen", fake_popen)
steamctl.start(LOGGER)
assert seen["cmd"] == [steamctl.STEAM]
assert seen["kwargs"]["start_new_session"] is True