steam-backlog-enforcer/steam_backlog_enforcer/tests/test_main_entrypoint.py
Krzysztof kuhy Rudnicki 45ff4eb7c2 Split main.py (1076 lines) into a main/ package
main.py became main/ with __main__.py, so the invocation string baked into
run.sh and the systemd unit (`python -m steam_backlog_enforcer.main`) stays
byte-identical and nothing needs redeploying. Commands are grouped into
sibling modules (status, install, picks, abandon, misc), with _shared.py as
a leaf holding the lock helpers and constants both need, and _registry.py
holding the command tables. main/__init__.py keeps every public and private
name importable, so `from steam_backlog_enforcer.main import X` is unchanged.

The subtle part is patch targets. The test suite builds them as
f"{PKG}.{name}", so a grep for the literal module path found nothing -- but
once a command moves to a submodule it resolves its dependencies from that
module's globals, and patching the main package's re-export silently stops
biting. Measured which of the 36 patched symbols moved: 8 could have passed
vacuously. Rather than repair PKG per class, the five test_main*.py files
(all cap violators anyway) were regrouped to mirror the production modules,
so each file has exactly one correct PKG. Verified by mutation: breaking a
line in status/misc/picks/_shared each makes the corresponding tests fail.

test_web_server.py patched main_mod.serve, which no longer intercepts
cmd_serve; repointed at main.misc and confirmed it fails when the call is
removed. Added tests for the __main__ shim, which coverage would otherwise
report at 0% under fail_under = 100.

Shared test builders live in tests/_main_helpers.py, reachable because the
Phase 0 name-tests-test exclude allows tests/_*.py.

1290 tests pass; every main/ module reports 100% coverage; `main`, `status`,
`bogus` and `--status` produce byte-identical output and exit codes to
pre-refactor. 55 -> 49 files over the cap.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PPpmMpTSkp8UziRc5u8B7P
2026-08-23 19:56:34 +02:00

36 lines
1.3 KiB
Python

"""Tests for the ``python -m steam_backlog_enforcer.main`` entry point.
The shim exists so the invocation string baked into ``run.sh`` and the systemd
unit keeps working now that ``main`` is a package rather than a module.
"""
from __future__ import annotations
import importlib
from pathlib import Path
from unittest.mock import patch
class TestMainModule:
"""The ``__main__`` shim that keeps ``python -m ...main`` working."""
def test_module_is_importable(self) -> None:
"""Importing the shim must not execute main()."""
with patch("steam_backlog_enforcer.main.main") as mock_main:
entry = importlib.import_module("steam_backlog_enforcer.main.__main__")
assert entry.main is not None
mock_main.assert_not_called()
def test_guard_is_present(self) -> None:
"""The shim calls main() behind a ``__main__`` guard.
Executing the shim here would call ``main()`` for real, so the guard is
asserted on the source instead -- the runtime behaviour it produces is
covered end-to-end by the CLI's own tests.
"""
source = (
Path(__file__).resolve().parents[1] / "main" / "__main__.py"
).read_text()
assert 'if __name__ == "__main__":' in source
assert "main()" in source