Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(tests): run @SpringBootTest integration tests with every selection (#3636)
Swati Ahuja7a3791e31d42
diff --git a/docs/start/CI.md b/docs/start/CI.mdindex e881e406a..9fc64f9f7 100644--- a/docs/start/CI.md+++ b/docs/start/CI.md@@ -261,7 +261,7 @@ a per-test coverage map (`repowise coverage add`) makes it more precise.- It also runs everything for: a missing or unreadable index; a per-test map at its row cap; a deleted file no known test used.- An index built at an older commit than the change's base is used as is. Files changed in between whose imports or module-name strings moved (any change beyond whitespace, outside Python and JavaScript/TypeScript) add the tests reaching them. It runs everything instead when that commit is not in the clone, or more than 1,000 files changed in between, or one of them is a lockfile, manifest, build or test config or `.repowise/config.yaml`. CI config and test data changed in between no longer force a full run: neither is imported.- Only `docs/` and root README-like files (README, CHANGELOG, LICENSE, CONTRIBUTING) are skipped, and only when no code names them (a test that reads `README.md` names it, and then runs); an empty line means only those changed.-- Tests the graph cannot see into run with every subset, and the reasons say how many and why: a test not indexed or importing nothing it resolves; a Python, JavaScript or TypeScript test that lists and reads files under a source directory (the working directory, a module's location, or its own location climbed out of the test directory; never its fixtures, sibling data or a temporary directory); one that runs the project's own command, `python -m` module, code importing the project, or package `bin` in a child process. The indexer finds them once from each test's source and stores the reason, so selection reads it without opening test files. An index built before this check runs everything until `repowise update` checks its tests. A Python module named like a test but outside every test directory is not a test when the nearest pytest config's `testpaths` or `python_files` leaves it out, as a bare `pytest` does: it is selected through like any other code. The rule follows a bare `pytest` run, so a test that CI only names explicitly on the command line outside `testpaths` is not seen as one: list it in `tests.always_run`.+- Tests the graph cannot see into run with every subset, and the reasons say how many and why: a test not indexed or importing nothing it resolves; a Python, JavaScript or TypeScript test that lists and reads files under a source directory (the working directory, a module's location, or its own location climbed out of the test directory; never its fixtures, sibling data or a temporary directory); one that runs the project's own command, `python -m` module, code importing the project, or package `bin` in a child process; a Java or Kotlin `@SpringBootTest`, which loads the whole application context (a slice such as `@WebMvcTest` is selected through its imports). A test that inherits `@SpringBootTest` from a base class or carries it through a custom meta-annotation is not detected and is selected through its imports only. The indexer finds them once from each test's source and stores the reason, so selection reads it without opening test files. An index built before this check runs everything until `repowise update` checks its tests. A Python module named like a test but outside every test directory is not a test when the nearest pytest config's `testpaths` or `python_files` leaves it out, as a bare `pytest` does: it is selected through like any other code. The rule follows a bare `pytest` run, so a test that CI only names explicitly on the command line outside `testpaths` is not seen as one: list it in `tests.always_run`.- A changed `conftest.py`, test package `__init__.py` or imported test helper selects the tests under or importing it, not everything.- A change that reaches a `conftest.py` only through its imports selects the tests that ask for a fixture using that change (directly, through other fixtures, `usefixtures` or `getfixturevalue`), plus one test under the conftest as an import check.- Every test under the conftest still runs when an autouse fixture, a `pytest_*` hook or module-level code uses the change, or when the conftest imports it only for side effects.diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.pyindex 136e0cceb..eafc49e2c 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py@@ -1837,8 +1837,8 @@ def parser_changed(repo_path: Path) -> bool:checkout. A store that cannot be read, has no repository row, or has noedges to reconcile, is not.- Also true when a test file was never checked for walking the source tree- (an index built before that check): selection would run everything until+ Also true when a test file was never checked for reaching code outside its+ imports (an index built before that check): selection would run everything untilthe re-parse stamps it, so a quiet repo must not take the shortcut."""from repowise.cli.helpers import get_db_url_for_repodiff --git a/packages/core/src/repowise/core/analysis/security_gate.py b/packages/core/src/repowise/core/analysis/security_gate.pyindex 4a694670d..638d5c993 100644--- a/packages/core/src/repowise/core/analysis/security_gate.py+++ b/packages/core/src/repowise/core/analysis/security_gate.py@@ -44,8 +44,8 @@ from .changed_lines import DIFF_ARGS, parse_unified_difffrom .security_scan import (SECRET_KINDS,_detect_secrets_allowlisted,- _mask_comments_and_strings,marker_silences,+ mask_comments_and_strings,masked_snippet,scan_source,source_lines,@@ -432,7 +432,7 @@ def _scoped(]custom, long_lines = _custom_entries(text, lines)uncommented = (- source_lines(_mask_comments_and_strings(source, strings=False))+ source_lines(mask_comments_and_strings(source, strings=False))if "pragma: allowlist" in sourceelse text.lines)diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.pyindex 33ba204b6..ea71ad762 100644--- a/packages/core/src/repowise/core/analysis/security_scan.py+++ b/packages/core/src/repowise/core/analysis/security_scan.py@@ -922,7 +922,7 @@ _STRING_STOP = {_NOT_NEWLINE = re.compile(r"[^\n]")-def _mask_comments_and_strings(source: str, *, strings: bool = True) -> str:+def mask_comments_and_strings(source: str, *, strings: bool = True) -> str:"""Blank common comments (and strings, unless *strings* is false), keeping offsets.Strings are still tracked when they are kept, so a ``#`` or ``//`` inside@@ -1127,7 +1127,7 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lisdef mask(strings: bool = True) -> tuple[str, list[str]]:if strings not in masks:- text = source if is_prose else _mask_comments_and_strings(source, strings=strings)+ text = source if is_prose else mask_comments_and_strings(source, strings=strings)masks[strings] = (text, lines if is_prose else source_lines(text))return masks[strings]@@ -1296,7 +1296,7 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis)uncommented = (- source_lines(_mask_comments_and_strings(source, strings=False))+ source_lines(mask_comments_and_strings(source, strings=False))if "pragma: allowlist" in sourceelse lines)diff --git a/packages/core/src/repowise/core/analysis/test_collection.py b/packages/core/src/repowise/core/analysis/test_collection.pyindex b624ae0e2..aadafad84 100644--- a/packages/core/src/repowise/core/analysis/test_collection.py+++ b/packages/core/src/repowise/core/analysis/test_collection.py@@ -288,12 +288,12 @@ async def _place_tests(session, repo_id: str, out: dict) -> None:except Exception as exc:out["graph_error"] = f"{type(exc).__name__}: {exc}"return- # An index older than the check cannot say which tests walk the tree, so- # it is out of date like any other; unplaced tests run anyway.+ # An index older than the check cannot say which tests reach past their+ # imports, so it is out of date like any other; unplaced tests run anyway.if unscanned and out["index_problem"] is None:out["index_problem"] = (- f"The index has not checked {len(unscanned)} test file(s) for walking the source "- f"tree or running the project (e.g. {unscanned[0]}); run `repowise update`."+ f"The index has not checked {len(unscanned)} test file(s) for reaching code "+ f"outside their imports (e.g. {unscanned[0]}); run `repowise update`.")diff --git a/packages/core/src/repowise/core/analysis/test_selection.py b/packages/core/src/repowise/core/analysis/test_selection.pyindex 3fcac089e..8a2507b16 100644--- a/packages/core/src/repowise/core/analysis/test_selection.py+++ b/packages/core/src/repowise/core/analysis/test_selection.py@@ -505,8 +505,9 @@ class SelectionInput:(:func:`doc_readers`); *plugin_loader* is a file loading pytest plugins byname (:func:`plugin_loader`); *unplaced_tests* are tests the graph cannotsee into (not indexed, or with no resolved edge), and *always_run_tests*- maps each test the indexer found walking the source tree or running the- project in a child process to why; every subset runs both.+ maps each test the indexer found reaching code outside its imports (a tree+ walk, a child process, a Spring application context) to why; every subset+ runs both."""changed: Collection[str]@@ -796,8 +797,8 @@ def _notes()if detected:out.append(- f"{len(detected)} test file(s) that list source files or run the project in "- f"a child process run with every selection (e.g. {detected[0]}: "+ f"{len(detected)} test file(s) that reach code outside their imports run "+ f"with every selection (e.g. {detected[0]}: "f"{inp.always_run_tests[detected[0]]}).")if not inp.map_current and inp.tiers.get("covered"):diff --git a/packages/core/src/repowise/core/ingestion/always_run.py b/packages/core/src/repowise/core/ingestion/always_run.pyindex 1aaa5bc30..58248e296 100644--- a/packages/core/src/repowise/core/ingestion/always_run.py+++ b/packages/core/src/repowise/core/ingestion/always_run.py@@ -10,7 +10,9 @@ while still importing something the graph resolves:* a **self-running** test starts the project's own command or module in achild process (a console script, ``sys.executable -m <own module>``, codepassed to ``-c`` that imports the project, the package's ``bin``), so the- code it exercises is reached through a process, not an import.+ code it exercises is reached through a process, not an import;+* a **whole-application** test (Java or Kotlin ``@SpringBootTest``) loads the+ Spring application context and reaches handlers by URL and bean wiring.:func:`always_run_reason` names which, or returns ``None``. It reads onlythe text it is given, so the indexer runs it once per test file while the@@ -29,8 +31,9 @@ directory never counts. The file read must sit in the walk's own function orblock, or in a block naming the walk's function or variable. Ceiling: thecheck is textual and per file, so a walk done by a shared helper the testcalls (``list_tracked_files()``) is not seen; detecting walks in helpers and-marking their importers is the upgrade path. Python, JavaScript and-TypeScript only.+marking their importers is the upgrade path. Walks and self-runs are judged+in Python, JavaScript and TypeScript only; Java and Kotlin tests only for+``@SpringBootTest`` (a slice such as ``@WebMvcTest`` is reached by its imports)."""from __future__ import annotations@@ -44,6 +47,11 @@ from typing import Any_PY = ".py"_JS = (".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts")+_JVM = (".java", ".kt")+# Simple or fully qualified; the word boundary keeps ``@SpringBootTestSupport`` out.+_SPRING_BOOT_TEST = re.compile(+ r"@(?:org\.springframework\.boot\.test\.context\.)?SpringBootTest\b"+)@dataclass(frozen=True)@@ -224,7 +232,7 @@ def is_judged_test(path: str) -> bool:"""A runnable test in a language :func:`always_run_reason` reads."""from ..analysis.test_selection import is_runnable_test- return path.endswith((_PY, *_JS)) and is_runnable_test(path)+ return path.endswith((_PY, *_JS, *_JVM)) and is_runnable_test(path)def _beside_code(path: str) -> bool:@@ -236,6 +244,8 @@ def _beside_code(path: str) -> bool:def always_run_reason(path: str, text: str, own: OwnCode) -> str | None:"""Why the test at *path* must run with every subset, or ``None``."""+ if path.endswith(_JVM):+ return _loads_spring_context(text)if path.endswith(_PY):lang = _PY_LANGelif path.endswith(_JS):@@ -249,6 +259,19 @@ def always_run_reason(path: str, text: str, own: OwnCode) -> str | None:return None+def _loads_spring_context(text: str) -> str | None:+ # Ceiling: per file, so ``@SpringBootTest`` inherited from a base class or+ # carried by a custom meta-annotation is not seen; resolving annotations+ # through the class graph is the upgrade path.+ if "SpringBootTest" not in text:+ return None+ from ..analysis.security_scan import mask_comments_and_strings++ if _SPRING_BOOT_TEST.search(mask_comments_and_strings(text)):+ return "it loads the whole Spring application context (@SpringBootTest)"+ return None++def _walks_source(text: str, lang: _Lang, *, local_is_source: bool = False) -> bool:if not (_has(text, lang.walk_tokens) and _has(text, lang.read_tokens)):return Falsediff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.pyindex 5a708172b..ce6281993 100644--- a/tests/unit/analysis/test_security_scan.py+++ b/tests/unit/analysis/test_security_scan.py@@ -232,10 +232,10 @@ class TestScanFile:def test_masking_blanks_comments_and_strings_in_place(self, source: str, code: str, uncommented: str) -> None:- from repowise.core.analysis.security_scan import _mask_comments_and_strings+ from repowise.core.analysis.security_scan import mask_comments_and_strings- assert _mask_comments_and_strings(source) == code- assert _mask_comments_and_strings(source, strings=False) == uncommented+ assert mask_comments_and_strings(source) == code+ assert mask_comments_and_strings(source, strings=False) == uncommenteddef test_file_without_a_call_word_skips_the_call_scan(self) -> None:from repowise.core.analysis.security_scan import _call_findingsdiff --git a/tests/unit/analysis/test_test_selection.py b/tests/unit/analysis/test_test_selection.pyindex 5d21b65a6..774ba3c80 100644--- a/tests/unit/analysis/test_test_selection.py+++ b/tests/unit/analysis/test_test_selection.py@@ -863,9 +863,9 @@ def test_a_test_found_walking_the_tree_runs_with_every_subset_and_says_why() -># A detected test the checkout no longer has is left out.assert sel.tests == ("tests/test_a.py", "tests/test_no_copies.py")assert (- "1 test file(s) that list source files or run the project in a child process run "- "with every selection (e.g. tests/test_no_copies.py: it lists and reads files under "- "a source directory)." in sel.reasons+ "1 test file(s) that reach code outside their imports run with every selection "+ "(e.g. tests/test_no_copies.py: it lists and reads files under a source directory)."+ in sel.reasons)assert sel.why == {"tests/test_a.py": "src/a.py changed (import-graph)",@@ -875,6 +875,23 @@ def test_a_test_found_walking_the_tree_runs_with_every_subset_and_says_why() ->}+def test_a_spring_boot_test_runs_when_a_controller_changes() -> None:+ controller = "src/main/java/org/x/owner/OwnerController.java"+ slice_test = "src/test/java/org/x/owner/OwnerControllerTests.java"+ boot_test = "src/test/java/org/x/PetClinicIntegrationTests.java"+ spring = "it loads the whole Spring application context (@SpringBootTest)"+ tiers = _tiers(inferred=[_inferred(controller, slice_test, "import-graph")])+ sel = _select(+ [controller],+ tiers,+ always_run_tests={boot_test: spring},+ known_tests=[slice_test, boot_test],+ )+ assert not sel.run_all+ assert sel.tests == (slice_test, boot_test)+ assert sel.why[boot_test] == f"runs with every subset: {spring}"++def test_explain_says_what_selected_a_test_or_that_nothing_did() -> None:from repowise.core.analysis.test_selection import explain_testdiff --git a/tests/unit/cli/test_impacted_tests_cmd.py b/tests/unit/cli/test_impacted_tests_cmd.pyindex f6ec819d8..bdb669687 100644--- a/tests/unit/cli/test_impacted_tests_cmd.py+++ b/tests/unit/cli/test_impacted_tests_cmd.py@@ -409,9 +409,9 @@ def test_a_test_the_indexer_found_walking_the_tree_runs_with_every_subset(repo)# src/a.py changed: tests/test_b.py stays out, the tree walker comes in.assert result.stdout == "tests/test_a.py tests/test_lint.py\n"assert (- "1 test file(s) that list source files or run the project in a child process run with "- "every selection (e.g. tests/test_lint.py: it lists and reads files under a source "- "directory)." in _err(result)+ "1 test file(s) that reach code outside their imports run with every selection "+ "(e.g. tests/test_lint.py: it lists and reads files under a source directory)."+ in _err(result))@@ -420,8 +420,8 @@ def test_an_index_that_never_checked_its_tests_runs_everything(repo) -> None:result = _run(repo, "main...feat", "--format", "args")assert result.stdout == ":all\n"assert (- "The index has not checked 1 test file(s) for walking the source tree or running "- "the project (e.g. tests/test_b.py); run `repowise update`." in _err(result)+ "The index has not checked 1 test file(s) for reaching code outside their imports "+ "(e.g. tests/test_b.py); run `repowise update`." in _err(result))diff --git a/tests/unit/ingestion/test_always_run.py b/tests/unit/ingestion/test_always_run.pyindex 2d4534496..5ec492ddf 100644--- a/tests/unit/ingestion/test_always_run.py+++ b/tests/unit/ingestion/test_always_run.py@@ -9,6 +9,7 @@ import pytestfrom repowise.core.ingestion.always_run import (OwnCode,always_run_reason,+ is_judged_test,own_code,stamp_always_run,)@@ -244,6 +245,58 @@ def test_other_languages_are_not_judged() -> None:assert _reason("pkg/a_test.go", 'exec.Command("tool")') is None+_SPRING = "it loads the whole Spring application context (@SpringBootTest)"+++ ("path", "text"),+ [+ (+ "src/test/java/org/x/PetClinicIntegrationTests.java",+ "import org.springframework.boot.test.context.SpringBootTest;\n"+ "@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)\n"+ "class PetClinicIntegrationTests {\n"+ ' @Test void findAll() { template.getForEntity("/owners/1", String.class); }\n}\n',+ ),+ (+ "src/test/java/org/x/AppTests.java",+ "@org.springframework.boot.test.context.SpringBootTest\nclass AppTests {}\n",+ ),+ (+ "src/test/kotlin/org/x/ApiTest.kt",+ "@SpringBootTest\n@AutoConfigureMockMvc\nclass ApiTest(@Autowired val mvc: MockMvc) {}\n",+ ),+ ],+)+def test_a_spring_boot_test_runs_with_every_subset(path, text) -> None:+ assert _reason(path, text) == _SPRING+++ "text",+ [+ # Plain JUnit.+ "import org.junit.jupiter.api.Test;\nclass OwnerTests { @Test void x() {} }\n",+ # A slice is reached through the class it names.+ "@WebMvcTest(OwnerController.class)\nclass OwnerControllerTests {}\n",+ "@DataJpaTest\nclass ClinicServiceTests {}\n",+ # Commented out, in a string, or a longer annotation name.+ "// @SpringBootTest\n/* @SpringBootTest */\nclass A {}\n",+ "/**\n * Unlike {@code @SpringBootTest}, loads one slice.\n */\nclass A {}\n",+ 'class A { String s = "@SpringBootTest"; String t = """\n@SpringBootTest\n"""; }\n',+ "@SpringBootTestSupport\nclass A {}\n",+ ],+)+def test_other_jvm_tests_have_no_reason(text) -> None:+ assert _reason("src/test/java/org/x/SomeTests.java", text) is None+++def test_jvm_tests_are_judged() -> None:+ assert is_judged_test("src/test/java/org/x/OwnerTests.java")+ assert is_judged_test("src/test/kotlin/org/x/ApiTest.kt")+ assert not is_judged_test("src/main/java/org/x/Owner.java")++def test_an_unclosed_string_full_of_backslashes_is_fast() -> None:text = "import subprocess\nsubprocess.run(['x', '" + "\\\\" * 5000 + "\n"start = time.perf_counter()@@ -275,6 +328,8 @@ def test_stamp_sets_the_reason_on_test_nodes_only() -> None:)sources = {"tests/test_lint.py": lint, "tests/test_a.py": "def test_a():\n pass\n"}sources["src/walker.py"] = lint+ sources["tests/AppTests.java"] = "@SpringBootTest\nclass AppTests {}\n"+ sources["tests/OwnerTests.java"] = "class OwnerTests { @Test void x() {} }\n"graph = nx.DiGraph()parsed = {}for path, text in sources.items():@@ -293,8 +348,10 @@ def test_stamp_sets_the_reason_on_test_nodes_only() -> None:)parsed[path] = ParsedFile(file_info=info, symbols=[], imports=[], exports=[])stamped = stamp_always_run(graph, parsed, {p: t.encode() for p, t in sources.items()})- assert stamped == 1+ assert stamped == 2assert graph.nodes["tests/test_lint.py"]["always_run_reason"] == _WALK+ assert graph.nodes["tests/AppTests.java"]["always_run_reason"] == _SPRING# Scanned and ordinary is "", apart from never scanned (None).assert graph.nodes["tests/test_a.py"]["always_run_reason"] == ""+ assert graph.nodes["tests/OwnerTests.java"]["always_run_reason"] == ""assert "always_run_reason" not in graph.nodes["src/walker.py"]diff --git a/tests/unit/persistence/test_impacted_tests_resolution.py b/tests/unit/persistence/test_impacted_tests_resolution.pyindex c12423f92..00a3ddae9 100644--- a/tests/unit/persistence/test_impacted_tests_resolution.py+++ b/tests/unit/persistence/test_impacted_tests_resolution.py@@ -663,3 +663,54 @@ async def test_the_ranking_orders_by_the_hops_the_collection_measures(async_sess("tests/test_near.py", "direct", 1),("tests/test_far.py", "transitive", 2),]+++_CONTROLLER = "src/main/java/org/x/owner/OwnerController.java"+_SLICE_TEST = "src/test/java/org/x/owner/OwnerControllerTests.java"+_BOOT_TEST = "src/test/java/org/x/PetClinicIntegrationTests.java"+_SPRING = "it loads the whole Spring application context (@SpringBootTest)"+++async def _spring_graph(session, repo_id, *, slice_reason, boot_reason) -> dict:+ """A slice test importing the controller; a boot test reaching it only by URL."""+ from sqlalchemy import update++ from repowise.core.analysis.test_collection import _place_tests+ from repowise.core.persistence.models import GraphNode++ await _graph(+ session,+ repo_id,+ tests={_SLICE_TEST, _BOOT_TEST},+ files={_CONTROLLER, "src/main/java/org/x/App.java"},+ imports=[(_SLICE_TEST, _CONTROLLER), (_BOOT_TEST, "src/main/java/org/x/App.java")],+ )+ for node_id, reason in ((_SLICE_TEST, slice_reason), (_BOOT_TEST, boot_reason)):+ await session.execute(+ update(GraphNode)+ .where(GraphNode.repository_id == repo_id, GraphNode.node_id == node_id)+ .values(always_run_reason=reason)+ )+ await session.commit()+ out = _empty_result(1)+ await _resolve_impacted(session, repo_id, {_CONTROLLER: {1}}, set(), out)+ await _place_tests(session, repo_id, out)+ return out+++async def test_a_spring_boot_test_is_read_back_as_always_run(async_session) -> None:+ repo = await insert_repo(async_session)+ out = await _spring_graph(async_session, repo.id, slice_reason="", boot_reason=_SPRING)+ assert _pairs(out, _CONTROLLER) == {_SLICE_TEST: "import-graph"}+ assert out["always_run_tests"] == {_BOOT_TEST: _SPRING}+ assert out["index_problem"] is None+++async def test_an_index_that_never_judged_its_jvm_tests_asks_for_an_update(async_session) -> None:+ repo = await insert_repo(async_session)+ out = await _spring_graph(async_session, repo.id, slice_reason=None, boot_reason=None)+ assert out["always_run_tests"] == {}+ assert out["index_problem"] == (+ f"The index has not checked 2 test file(s) for reaching code outside their imports "+ f"(e.g. {_BOOT_TEST}); run `repowise update`."+ )