mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-20 08:32:18 +01:00
Accumulates time spent in Steam games and other launchers, and once the daily budget (default 8h) is spent, shuts Steam down, kills the games, and makes them unlaunchable until the next 06:00. The gaming day starts at 06:00 local rather than midnight so a late session counts against the day it began in; rollover is checked lazily against a stored day key, so it survives suspend, reboot and daemon restarts with no timer of its own. Elapsed time comes from wall-clock deltas clamped to two tick intervals, which keeps suspends, missed ticks and NTP steps honest. The block is a read-only bind mount of a refusal stub over all four launcher entry points -- /usr/bin/steam is only a two-line exec wrapper, so masking it alone leaves bin_steam.sh, Steam's own steam.sh and lutris live. A bind mount mutates no bytes, umount restores exactly, and a reboot clears it unconditionally; chmod/rename would leave Steam broken if the daemon died mid-block. /proc/self/mountinfo is the authority on what is masked, never the saved state, which is what makes a stale block release even if the state file was deleted. The mount is deferred one tick past the cutoff so `steam -shutdown` still works and games close cleanly. Launchers are matched by cmdline, not comm: /usr/bin/lutris is a python3 script, so the kernel records its comm as "python3" and the existing scan never sees it. The kill set is wider than the budget predicate, walking down from each launcher, because a Lutris-launched Wine game carries no SteamAppId. State is chattr +i'd -- ~/.config is user-writable, so root ownership alone would not stop `rm` from resetting the counter -- and a missing state file while mounts are live fails closed rather than lifting the block. Also fixes a pre-existing bug: do_enforce returned when no game was assigned, exiting the process. Since the budget is accounted for from inside that loop, it silently stopped enforcing whenever a game was finished but not rescanned. Verified on the desktop with a real game: AoE2 counted 18 processes, tripped at the 60s demo budget, all processes killed, all four paths masked and refusing, then gaming-unblock restored them byte-identical with `pacman -Qkk steam lutris` reporting 0 altered files. 1322 tests pass at 100.00% branch coverage; pacman validated the ALPM hook. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QbEaXapjMSkxyzgvbdkUFb
276 lines
10 KiB
Python
276 lines
10 KiB
Python
"""Safety conftest: prevent tests from touching real Steam/config files.
|
|
|
|
Redirects all filesystem paths used by the steam_backlog_enforcer package
|
|
to temporary directories. This stops tests from accidentally:
|
|
- Deleting real game files via uninstall_other_games / uninstall_game
|
|
- Overwriting ~/.config/steam_backlog_enforcer/state.json (losing the
|
|
user's current assignment)
|
|
- Reading real appmanifest files from ~/.local/share/Steam/steamapps
|
|
- Modifying /etc/hosts via the store blocker
|
|
- Corrupting the HLTB cache on disk
|
|
- Launching real Steam or calling real subprocess commands
|
|
- Deleting real ~/.steam, ~/.local/share/Steam, etc. via the total-block
|
|
Steam/Proton remnant purge
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Iterator
|
|
from pathlib import Path
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _isolate_filesystem(tmp_path: Path) -> Iterator[None]:
|
|
"""Redirect all real filesystem paths to a temporary directory.
|
|
|
|
Individual tests that also patch these paths will simply override
|
|
this fixture's patches for the duration of their own ``with`` block.
|
|
"""
|
|
fake_config = tmp_path / "config"
|
|
fake_config.mkdir()
|
|
fake_steamapps = tmp_path / "steamapps"
|
|
fake_steamapps.mkdir()
|
|
fake_hosts = tmp_path / "hosts"
|
|
|
|
with (
|
|
# Config / state / snapshot paths (used by State.save, Config.save, etc.)
|
|
patch(
|
|
"steam_backlog_enforcer.config.CONFIG_DIR",
|
|
fake_config,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.config.CONFIG_FILE",
|
|
fake_config / "config.json",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.config.STATE_FILE",
|
|
fake_config / "state.json",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.config.SNAPSHOT_FILE",
|
|
fake_config / "snapshot.json",
|
|
),
|
|
# Steam game manifests / install dirs. STEAMAPPS_PATH is canonical in
|
|
# _steam_state; game_install imports it, so both bindings need the
|
|
# patch (same gotcha as HOSTS_FILE below).
|
|
patch(
|
|
"steam_backlog_enforcer._steam_state.STEAMAPPS_PATH",
|
|
fake_steamapps,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.STEAMAPPS_PATH",
|
|
fake_steamapps,
|
|
),
|
|
# HLTB cache file (computed at import time from CONFIG_DIR, so
|
|
# patching CONFIG_DIR alone does not redirect it)
|
|
patch(
|
|
"steam_backlog_enforcer._hltb_types.HLTB_CACHE_FILE",
|
|
fake_config / "hltb_cache.json",
|
|
),
|
|
# /etc/hosts (store blocker + total block - each module has its own
|
|
# `from ... import HOSTS_FILE` binding, so each needs its own patch)
|
|
patch(
|
|
"steam_backlog_enforcer.store_blocker.HOSTS_FILE",
|
|
fake_hosts,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer._total_block.HOSTS_FILE",
|
|
fake_hosts,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.config.HOSTS_FILE",
|
|
fake_hosts,
|
|
),
|
|
# Total-block lock + IP cache (computed at import time from
|
|
# CONFIG_DIR, so patching CONFIG_DIR alone does not redirect them -
|
|
# same gotcha as HLTB_CACHE_FILE above. A real total-block lock may
|
|
# be active on the host machine; tests must never touch it.)
|
|
patch(
|
|
"steam_backlog_enforcer._total_block.TOTAL_BLOCK_LOCK_FILE",
|
|
fake_config / "total_block_lock.json",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer._total_block._IPTABLES_IP_CACHE_FILE",
|
|
fake_config / "total_block_ip_cache.json",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer._total_block._STEAM_PURGE_LOG_FILE",
|
|
fake_config / "total_block_purge_log.json",
|
|
),
|
|
# Steam/Proton remnant paths (real ~/.steam, ~/.local/share/Steam,
|
|
# etc. - _remove_steam_remnants() deletes these, so tests must never
|
|
# see the real ones)
|
|
patch(
|
|
"steam_backlog_enforcer._total_block._STEAM_REMNANT_PATHS",
|
|
(
|
|
tmp_path / "fake_home" / ".steam",
|
|
tmp_path / "fake_home" / "steam",
|
|
tmp_path / "fake_home" / ".local" / "share" / "Steam",
|
|
tmp_path / "fake_home" / ".steampath",
|
|
tmp_path / "fake_home" / ".steampid",
|
|
tmp_path / "fake_home" / ".config" / "steamtinkerlaunch",
|
|
tmp_path / "fake_home" / ".config" / "CSDSteamBuild",
|
|
),
|
|
),
|
|
# Whitelist exception files (_whitelist module-level constants)
|
|
patch(
|
|
"steam_backlog_enforcer._whitelist.APPROVED_EXCEPTIONS_FILE",
|
|
fake_config / "approved_exceptions.json",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer._whitelist.EXCEPTION_AUDIT_LOG",
|
|
fake_config / "exception_audit.log",
|
|
),
|
|
# _enforce_loop imports CONFIG_FILE directly; patch the local binding so
|
|
# lock_enforcement_files() uses the tmp path instead of the real one.
|
|
patch(
|
|
"steam_backlog_enforcer._enforce_loop.CONFIG_FILE",
|
|
fake_config / "config.json",
|
|
),
|
|
):
|
|
yield
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _isolate_playtime(tmp_path: Path) -> Iterator[None]:
|
|
"""Redirect the daily-gaming-budget paths to a temporary directory.
|
|
|
|
Separate from :func:`_isolate_filesystem` only because CPython caps a
|
|
``with`` statement at 20 statically nested blocks and that fixture is
|
|
already at the limit.
|
|
|
|
The ``BLOCK_TARGETS`` patch is the load-bearing one: unpatched, that tuple
|
|
names the real ``/usr/bin/steam``, and a test reaching the mount code would
|
|
mask the user's actual Steam install behind a refusal stub.
|
|
"""
|
|
# Namespaced names: an autouse fixture that creates bare "proc"/"targets"
|
|
# inside tmp_path would claim those names for every other test in the
|
|
# suite (test_enforcer builds its own tmp_path/"proc" with mkdir()).
|
|
fake_config = tmp_path / "config"
|
|
fake_config.mkdir(exist_ok=True)
|
|
fake_proc = tmp_path / "playtime_proc"
|
|
fake_proc.mkdir(exist_ok=True)
|
|
fake_targets = tmp_path / "playtime_targets"
|
|
fake_targets.mkdir(exist_ok=True)
|
|
|
|
with (
|
|
# Computed at import time from CONFIG_DIR, so patching CONFIG_DIR
|
|
# alone does not redirect them.
|
|
patch(
|
|
"steam_backlog_enforcer._playtime.PLAYTIME_STATE_FILE",
|
|
fake_config / "playtime_state.json",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer._playtime.PLAYTIME_DEMO_STATE_FILE",
|
|
fake_config / "playtime_demo_state.json",
|
|
),
|
|
patch("steam_backlog_enforcer._playtime._PROC", fake_proc),
|
|
# Never let a test chattr +i a file inside tmp_path — pytest could
|
|
# then not clean the directory up.
|
|
patch("steam_backlog_enforcer._playtime._try_set_immutable", MagicMock()),
|
|
patch("steam_backlog_enforcer._playtime.unlock_for_write", MagicMock()),
|
|
patch(
|
|
"steam_backlog_enforcer._playtime_block.BLOCK_TARGETS",
|
|
(
|
|
fake_targets / "steam",
|
|
fake_targets / "bin_steam.sh",
|
|
fake_targets / "steam.sh",
|
|
fake_targets / "lutris",
|
|
),
|
|
),
|
|
patch("steam_backlog_enforcer._playtime_block._STUB_DIR", tmp_path / "run"),
|
|
patch(
|
|
"steam_backlog_enforcer._playtime_block.STUB_PATH",
|
|
tmp_path / "run" / "gaming-blocked",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer._playtime_block.MOUNTINFO_PATH",
|
|
tmp_path / "mountinfo",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer._playtime_block._INIT_MOUNTINFO_PATH",
|
|
tmp_path / "init_mountinfo",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer._playtime_block.PACMAN_LOCK", tmp_path / "db.lck"
|
|
),
|
|
patch("steam_backlog_enforcer._playtime_block._PROC", fake_proc),
|
|
):
|
|
yield
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _block_real_subprocesses() -> Iterator[None]:
|
|
"""Block subprocess calls that could launch real Steam or modify system.
|
|
|
|
Individual tests that need to test subprocess behaviour should
|
|
patch the specific module's ``subprocess.run`` / ``subprocess.Popen``
|
|
themselves — their local patch will override this one.
|
|
"""
|
|
noop_run = MagicMock(return_value=MagicMock(returncode=1))
|
|
noop_popen = MagicMock()
|
|
|
|
with (
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.run",
|
|
noop_run,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.Popen",
|
|
noop_popen,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.enforcer.subprocess.run",
|
|
noop_run,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.store_blocker.subprocess.run",
|
|
noop_run,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer._total_block.subprocess.run",
|
|
noop_run,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.library_hider.subprocess.run",
|
|
noop_run,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.library_hider.subprocess.Popen",
|
|
noop_popen,
|
|
),
|
|
# The single most important patch in this file. _playtime_block._run
|
|
# executes `mount --bind` against paths that, unpatched, are the real
|
|
# /usr/bin/steam. A test that reached the real subprocess would mask
|
|
# the user's actual Steam install with a refusal stub.
|
|
patch(
|
|
"steam_backlog_enforcer._playtime_block.subprocess.run",
|
|
noop_run,
|
|
),
|
|
):
|
|
yield
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _no_real_sleep() -> Iterator[None]:
|
|
"""No-op every ``time.sleep`` used by the package.
|
|
|
|
Several modules call ``time.sleep`` for Steam-launch / install-retry /
|
|
rate-limit pacing. Individual tests that need to observe sleep
|
|
behaviour can override these patches inside their own ``with`` block.
|
|
"""
|
|
noop = MagicMock()
|
|
with (
|
|
patch("steam_backlog_enforcer.game_install.time.sleep", noop),
|
|
patch("steam_backlog_enforcer.library_hider.time.sleep", noop),
|
|
patch("steam_backlog_enforcer.steam_api.time.sleep", noop),
|
|
patch("steam_backlog_enforcer._enforce_loop.time.sleep", noop),
|
|
):
|
|
yield
|