testsAndMisc/python_pkg/brother_printer/tests/test_main_entry.py
Krzysztof kuhy Rudnicki 69242e4bb3
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Python tests / test (push) Waiting to run
fix(brother-printer): split oversized files, fix order-dependent entry test
a12e696 turned CI red two ways, both missed locally because the suite was
run for the brother_printer package alone while CI runs everything in a
random order.

The repo caps Python files at 500 lines and cups_service.py had reached
659. Rather than cut it arbitrarily, extract consumable life tracking -
page counters, replacement baselines, the scale migration, dropped-page
detection - into consumables.py. The seam is real: none of it depends on
CUPS service control, and cups_service needs only estimate_consumable_life
for its fallback query, so the dependency runs one way. The test files are
split along the same seams.

test_main_entry passed or failed depending on execution order: runpy warns
when the target module is already in sys.modules, a sibling test imports
it, and pytest is configured with filterwarnings=error. Drop the module
before running it so the test no longer depends on who ran first.

Structural only; no behaviour change. 692 tests, 100% branch coverage.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RG3eEn8rwZbcXaJrKJxdv8
2026-07-17 15:06:25 +02:00

36 lines
1.3 KiB
Python

"""Tests for brother_printer.__main__ module."""
from __future__ import annotations
import importlib
import runpy
import sys
from unittest.mock import MagicMock, patch
class TestMain:
def test_main_called_when_run_as_module(self) -> None:
"""python3 -m python_pkg.brother_printer runs main()."""
mock_main = MagicMock()
# runpy warns if __main__ is already imported, and the suite runs tests
# in a random order, so another test may have imported it first. The
# warning is an error here, so drop it and let runpy execute it fresh.
sys.modules.pop("python_pkg.brother_printer.__main__", None)
with patch(
"python_pkg.brother_printer.check_brother_printer.main",
mock_main,
):
runpy.run_module("python_pkg.brother_printer", run_name="__main__")
mock_main.assert_called_once()
def test_main_not_called_on_plain_import(self) -> None:
"""Importing __main__ must not fire off a status report as a side effect."""
mock_main = MagicMock()
with patch(
"python_pkg.brother_printer.check_brother_printer.main",
mock_main,
):
sys.modules.pop("python_pkg.brother_printer.__main__", None)
importlib.import_module("python_pkg.brother_printer.__main__")
mock_main.assert_not_called()