mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-07-04 19:43:11 +02:00
- meta/.pre-commit-config.yaml: move pytest-coverage hook to pre-commit stage - scripts/pytest_changed_packages.py: single batched pytest -n auto invocation with one --cov flag per affected python_pkg subpackage, wrapped in systemd-run --user --scope -p MemoryMax=4G -p MemorySwapMax=0 when available - python_pkg/steam_backlog_enforcer/tests/conftest.py: new autouse _no_real_sleep fixture patches time.sleep across game_install / library_hider / steam_api / _enforce_loop. Removes 3x 15s real sleeps in TestFinalizeCompletion that fired through _ensure_steam_running steam_backlog_enforcer test wall time: 33.97s -> 5.61s (xdist, no-cov) 5-package batched run: 732 tests in 1.37s @ 668% CPU Coverage stays at 100% on all affected packages. Evidence: docs/superpowers/evidence/pre-commit-pytest-batch-2026-05-14.json
137 lines
4.6 KiB
Python
137 lines
4.6 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
|
|
"""
|
|
|
|
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(
|
|
"python_pkg.steam_backlog_enforcer.config.CONFIG_DIR",
|
|
fake_config,
|
|
),
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.config.CONFIG_FILE",
|
|
fake_config / "config.json",
|
|
),
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.config.STATE_FILE",
|
|
fake_config / "state.json",
|
|
),
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.config.SNAPSHOT_FILE",
|
|
fake_config / "snapshot.json",
|
|
),
|
|
# Steam game manifests / install dirs
|
|
patch(
|
|
"python_pkg.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(
|
|
"python_pkg.steam_backlog_enforcer._hltb_types.HLTB_CACHE_FILE",
|
|
fake_config / "hltb_cache.json",
|
|
),
|
|
# /etc/hosts (store blocker)
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.store_blocker.HOSTS_FILE",
|
|
fake_hosts,
|
|
),
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.config.HOSTS_FILE",
|
|
fake_hosts,
|
|
),
|
|
):
|
|
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(
|
|
"python_pkg.steam_backlog_enforcer.game_install.subprocess.run",
|
|
noop_run,
|
|
),
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.game_install.subprocess.Popen",
|
|
noop_popen,
|
|
),
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.enforcer.subprocess.run",
|
|
noop_run,
|
|
),
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.store_blocker.subprocess.run",
|
|
noop_run,
|
|
),
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.library_hider.subprocess.run",
|
|
noop_run,
|
|
),
|
|
patch(
|
|
"python_pkg.steam_backlog_enforcer.library_hider.subprocess.Popen",
|
|
noop_popen,
|
|
),
|
|
):
|
|
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("python_pkg.steam_backlog_enforcer.game_install.time.sleep", noop),
|
|
patch("python_pkg.steam_backlog_enforcer.library_hider.time.sleep", noop),
|
|
patch("python_pkg.steam_backlog_enforcer.steam_api.time.sleep", noop),
|
|
patch("python_pkg.steam_backlog_enforcer._enforce_loop.time.sleep", noop),
|
|
):
|
|
yield
|