From a8c61770125160b240a5b30677490f9d476d4dc4 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Mon, 23 Nov 2026 13:49:14 +0100 Subject: [PATCH] Skip the coverage bar on -k subset runs pytest -q -k (the board's done_check form) exited 1 on green tests: fail_under = 100 judged a handful of tests against the whole package. A pytest_sessionstart plugin clears pytest-cov's fail_under when a keyword is given; whole-suite runs (commit gate, full_gate.sh, CI) select none and keep the bar. Co-Authored-By: Claude Opus 5.5 --- pyproject.toml | 3 ++ screen_locker/tests/_subset_coverage.py | 28 +++++++++++++++ screen_locker/tests/test_subset_coverage.py | 40 +++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 screen_locker/tests/_subset_coverage.py create mode 100644 screen_locker/tests/test_subset_coverage.py diff --git a/pyproject.toml b/pyproject.toml index 8668ecc..573a945 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -267,6 +267,9 @@ addopts = [ # repo's 400-line-per-file cap) -- see its own docstring. "-p", "screen_locker.tests.test_sync_fixtures", + # A `-k` run is a subset: only whole-suite runs need fail_under = 100. + "-p", + "screen_locker.tests._subset_coverage", ] filterwarnings = [ "error", diff --git a/screen_locker/tests/_subset_coverage.py b/screen_locker/tests/_subset_coverage.py new file mode 100644 index 0000000..80ac46d --- /dev/null +++ b/screen_locker/tests/_subset_coverage.py @@ -0,0 +1,28 @@ +"""Hold only whole-suite runs to pyproject's ``fail_under = 100``. + +A ``-k`` run selects a handful of tests, so its total coverage is always far +below 100% and pytest-cov turned every green ``pytest -q -k `` (the +board's done_check form) into exit 1. The commit gate, the nightly +``full_gate.sh`` and CI select no keyword, so they keep the bar. Path-selected +partial runs (``test_changed.sh``, ``push_gate.sh``) pass ``--no-cov`` +themselves and are untouched. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import pytest + + +def pytest_sessionstart(session: pytest.Session) -> None: + """Drop the coverage bar for a ``-k`` keyword-selected run. + + pytest-cov copies ``fail_under`` into its options when coverage starts + (before this hook) and checks it at the end; ``None`` skips the check. + """ + config = session.config + cov = config.pluginmanager.getplugin("_cov") + if cov is not None and config.option.keyword: + cov.options.cov_fail_under = None diff --git a/screen_locker/tests/test_subset_coverage.py b/screen_locker/tests/test_subset_coverage.py new file mode 100644 index 0000000..696e87a --- /dev/null +++ b/screen_locker/tests/test_subset_coverage.py @@ -0,0 +1,40 @@ +"""A ``-k`` run is a subset: the 100% coverage bar is for whole-suite runs. + +The board's done_checks are ``pytest -q -k ``; with ``fail_under = 100`` +in pyproject every one of them exited 1 on green tests (2026-11-23). +""" + +from __future__ import annotations + +from types import SimpleNamespace + +from screen_locker.tests import _subset_coverage + + +def _config(keyword: str, fail_under: float | None) -> SimpleNamespace: + cov = SimpleNamespace(options=SimpleNamespace(cov_fail_under=fail_under)) + manager = SimpleNamespace(getplugin=lambda name: cov if name == "_cov" else None) + return SimpleNamespace( + option=SimpleNamespace(keyword=keyword), pluginmanager=manager + ) + + +def test_keyword_run_drops_the_coverage_bar() -> None: + """``-k name`` selects a subset: its coverage is not held to fail_under.""" + config = _config("preview_review_sequence", 100) + _subset_coverage.pytest_sessionstart(SimpleNamespace(config=config)) + assert config.pluginmanager.getplugin("_cov").options.cov_fail_under is None + + +def test_whole_suite_keeps_the_coverage_bar() -> None: + """No ``-k``: the commit gate's full run still needs 100%.""" + config = _config("", 100) + _subset_coverage.pytest_sessionstart(SimpleNamespace(config=config)) + assert config.pluginmanager.getplugin("_cov").options.cov_fail_under == 100 + + +def test_no_coverage_plugin_is_left_alone() -> None: + """``--no-cov`` or ``-p no:cov``: nothing to relax, no crash.""" + manager = SimpleNamespace(getplugin=lambda _name: None) + config = SimpleNamespace(option=SimpleNamespace(keyword="x"), pluginmanager=manager) + _subset_coverage.pytest_sessionstart(SimpleNamespace(config=config))