From c2f4040accd60f829642fe0aabf16618934cf0b4 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Fri, 27 Nov 2026 12:45:39 +0100 Subject: [PATCH] Test the requirements --testpaths closure Bug-class-check: linux_configuration/tests/test_requirements_closure.py Co-Authored-By: Claude Opus 5.5 --- .../requirements-test-closure-2026-11-27.json | 8 ++- .../tests/test_requirements_closure.py | 58 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 linux_configuration/tests/test_requirements_closure.py diff --git a/docs/superpowers/evidence/requirements-test-closure-2026-11-27.json b/docs/superpowers/evidence/requirements-test-closure-2026-11-27.json index ea7b61ee..fed6c8d0 100644 --- a/docs/superpowers/evidence/requirements-test-closure-2026-11-27.json +++ b/docs/superpowers/evidence/requirements-test-closure-2026-11-27.json @@ -4,7 +4,8 @@ "meta/requirements-test.txt and meta/requirements.txt: pin hypothesis==6.170.17 and jeepney==0.9.0 (newest stable)", "meta/scripts/check_requirements_sync.py: new --testpaths mode; meta/scripts/_requirements_closure.py walks the import closure", ".pre-commit-config.yaml: requirements-test-sync hook uses --testpaths and triggers on linux_configuration and meta/scripts .py files", - "Non-goal: the --package mode used by requirements-sync is unchanged" + "Non-goal: the --package mode used by requirements-sync is unchanged", + "linux_configuration/tests/test_requirements_closure.py: regression test for the --testpaths mode" ], "changes": [ "The old check skipped tests/ directories and never looked at linux_configuration, so a test-only import (hypothesis in phone_lease tests) and a dependency of a script that linux_configuration/tests loads (jeepney via game_rec_dbus) passed locally and failed CI", @@ -25,6 +26,11 @@ "command": "check_requirements_sync.py --package python_pkg (with and without --requirements meta/requirements-test.txt)", "result": "pass", "evidence": "exit 0 both, old mode unchanged" + }, + { + "command": "python3 -m pytest -q linux_configuration/tests/test_requirements_closure.py", + "result": "pass", + "evidence": "2 passed; with the TYPE_CHECKING skip disabled both fail (red first)" } ], "risks": [ diff --git a/linux_configuration/tests/test_requirements_closure.py b/linux_configuration/tests/test_requirements_closure.py new file mode 100644 index 00000000..4d590dad --- /dev/null +++ b/linux_configuration/tests/test_requirements_closure.py @@ -0,0 +1,58 @@ +"""Tests for check_requirements_sync.py --testpaths (the pytest import closure). + +The tree mimics the repo: a package whose tests import a test-only library, +and a script directory whose tests load a sibling helper that imports a +library lazily. Both must be reported; a TYPE_CHECKING-only import must not. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from _requirements_closure import import_closure +from check_requirements_sync import main + +if TYPE_CHECKING: + from pathlib import Path + +_TESTPATHS = ["pkg", "lc/tests"] + + +def _tree(root: Path) -> None: + """Write the fake repo under ``root``.""" + files = { + "pkg/__init__.py": "", + "pkg/core.py": "import requests\n", + "pkg/tests/test_core.py": "import hyp\nfrom pkg import core\n", + "lc/tests/test_tool.py": ( + "from typing import TYPE_CHECKING\n" + "if TYPE_CHECKING:\n" + " import typeonly\n" + "import helper\n" + ), + "lc/tools/helper.py": "def run():\n import lazydep\n", + "lc/tools/unused.py": "import notreached\n", + } + for rel, text in files.items(): + path = root / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text, encoding="utf-8") + + +def test_closure_follows_local_imports_and_skips_type_checking( + tmp_path: Path, +) -> None: + """Report test-only and transitive imports; skip unreached and typing-only.""" + _tree(tmp_path) + assert import_closure(tmp_path, _TESTPATHS) == {"hyp", "lazydep", "requests"} + + +def test_main_fails_until_every_import_is_declared(tmp_path: Path) -> None: + """Exit 1 while a closure import is undeclared, 0 once all are pinned.""" + _tree(tmp_path) + reqs = tmp_path / "req.txt" + reqs.write_text("requests==1\n", encoding="utf-8") + argv = ["--root", str(tmp_path), "--requirements", "req.txt", "--testpaths"] + assert main([*argv, *_TESTPATHS]) == 1 + reqs.write_text("requests==1\nhyp==1\nlazydep==1\n", encoding="utf-8") + assert main([*argv, *_TESTPATHS]) == 0