Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): pair a file with the tests that import it (#2574)

Raghav Chamadiya4a942cb4f8be
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/base.py b/packages/core/src/repowise/core/analysis/health/biomarkers/base.py
index f25ed5276..d955a1880 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/base.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/base.py
@@ -43,8 +43,8 @@ class FileContext:
# Graph-derived signals.
# True when a test file can execute into this one along the call graph,
# within ``test_reachability.DEFAULT_CALL_DEPTH`` hops. Distinct from
- # ``has_test_file``, which is a filename convention: this is a recorded
- # edge, so it finds behaviour-named tests the convention cannot, and it
+ # ``has_test_file``, which is test *pairing* (a test importing the file, or
+ # named for it): this is an execution edge, and it
# over-claims, since control reaching a file is not a run exercising it.
# Sound as a floor ("something tests this"), never as a coverage quantity.
# ``False`` when no graph was available - the documented "no signal"
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py
index 988772f49..0be7742dc 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py
@@ -70,8 +70,8 @@ class UntestedHotspotDetector:
# ``reached_by_tests`` is the graph one, and it is what stops the
# long-standing false positive: a suite that names its tests for
# behaviour rather than for the file under test satisfies no naming
- # convention, so ``has_test_file`` was False and this fired on files
- # the graph records several test files importing.
+ # convention, so name-only pairing left ``has_test_file`` False and
+ # this fired on files the graph records several test files importing.
if ctx.has_test_file or ctx.reached_by_tests:
return []
cov_for_severity = 0.0
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index 74c2e71f1..c42e46211 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -38,7 +38,7 @@ from ...ingestion.package_roots import package_roots_from_paths as _package_root
from ...ingestion.package_roots import scan_package_roots as _scan_package_roots
from ...test_paths import paired_test_names
from ..graph_view import HasEdge, ImportEdgeView
-from ..test_reachability import files_reached_by_tests
+from ..test_reachability import files_reached_by_tests, files_with_paired_tests
from .asserts.lexicon import AssertVocabulary
from .asserts.oracle_reach import collect_cross_file_oracles
from .biomarkers import FileContext, detect_all
@@ -564,6 +564,21 @@ class HealthAnalyzer:
self._tests_reach_cache = files_reached_by_tests(index or CallGraphIndex(), test_files)
return self._tests_reach_cache
+ def _paired_tests(self, analyzed_paths: set[str]) -> set[str]:
+ """Analyzed files a test is paired with; see ``files_with_paired_tests``.
+
+ Test files come from the graph as well as this pass, so an incremental
+ run that re-parses only the changed files still sees every test.
+ """
+ test_files = {pf.file_info.path for pf in self.parsed_files if pf.file_info.is_test}
+ if self.graph is not None:
+ test_files.update(
+ node
+ for node, data in self.graph.nodes(data=True)
+ if data.get("is_test") and data.get("node_type") != "symbol"
+ )
+ return files_with_paired_tests(self.graph, analyzed_paths, test_files)
+
def _package_boundaries(self, analyzed_paths: set[str]) -> set[str]:
"""Package roots for this repo, decided once per analyzer.
@@ -645,7 +660,7 @@ class HealthAnalyzer:
# is symbol-level; we use file-level in-degree as the dependents
# signal (cheap, deterministic, conservative).
analyzed_paths = {pf.file_info.path for pf in self.parsed_files}
- path_basenames = _path_basenames(analyzed_paths)
+ paired_tests = self._paired_tests(analyzed_paths)
package_roots = self._package_boundaries(analyzed_paths)
graph_view: HasEdge | None = ImportEdgeView(self.graph) if self.graph is not None else None
@@ -753,7 +768,7 @@ class HealthAnalyzer:
file_metric, file_findings, file_suggestions = self._evaluate_file(
pf,
fcx,
- path_basenames,
+ paired_tests,
package_roots,
disabled=file_disabled,
dup_report=dup_report,
@@ -854,7 +869,7 @@ class HealthAnalyzer:
changed_set: set[str] | None = set(changed_files) if changed_files is not None else None
analyzed_paths = {pf.file_info.path for pf in self.parsed_files}
- path_basenames = _path_basenames(analyzed_paths)
+ paired_tests = self._paired_tests(analyzed_paths)
package_roots = self._package_boundaries(analyzed_paths)
graph_view: HasEdge | None = ImportEdgeView(self.graph) if self.graph is not None else None
@@ -963,7 +978,7 @@ class HealthAnalyzer:
file_metric, file_findings, file_suggestions = self._evaluate_file(
pf,
fcx,
- path_basenames,
+ paired_tests,
package_roots,
disabled=file_disabled,
dup_report=dup_report,
@@ -1245,7 +1260,7 @@ class HealthAnalyzer:
self,
pf: Any,
fcx: FileComplexity,
- path_basenames: set[str],
+ paired_tests: set[str],
package_roots: set[str],
*,
disabled: list[str],
@@ -1305,12 +1320,12 @@ class HealthAnalyzer:
# re-derive from the path string (#1103). The coverage check stays
# because it also sniffs framework imports out of the source, which
# a path cannot tell you.
- has_test_file=_has_paired_test_file(file_path, path_basenames)
+ has_test_file=file_path in paired_tests
or pf.file_info.is_test
or _coverage_is_test_file(file_path)
or fcx.has_inline_tests,
# Kept separate from ``has_test_file`` on purpose. That flag means
- # "a file named like this one's test exists"; this one means "the
+ # "a test imports this file, or is named for it"; this one means "the
# call graph records a test reaching this file". They disagree
# often, and collapsing them would leave no way to say which signal
# answered, or that one of them over-claims.
diff --git a/packages/core/src/repowise/core/analysis/test_reachability.py b/packages/core/src/repowise/core/analysis/test_reachability.py
index ab7b396ed..0cce03177 100644
--- a/packages/core/src/repowise/core/analysis/test_reachability.py
+++ b/packages/core/src/repowise/core/analysis/test_reachability.py
@@ -150,6 +150,7 @@ from __future__ import annotations
import json
from collections.abc import Collection, Mapping
from dataclasses import dataclass
+from pathlib import PurePosixPath
from typing import Any, Literal, TypeAlias
from sqlalchemy import select, text
@@ -164,6 +165,8 @@ from repowise.core.ingestion.models import EXECUTION_EDGE_TYPES, FILE_DEPENDENCY
from repowise.core.persistence.models import GraphNode
from repowise.core.test_paths import paired_test_names
+from .dead_code.file_reachability import BARREL_FILENAMES
+
# How many call hops a test may take and still be said to reach a file. The walk
# saturates here; see the Depth section above.
DEFAULT_CALL_DEPTH = 3
@@ -195,6 +198,7 @@ __all__ = [
"call_graph_from_db",
"call_graph_from_graph",
"files_reached_by_tests",
+ "files_with_paired_tests",
"load_test_files",
"rank_tests",
"tests_matching_by_name",
@@ -313,6 +317,69 @@ def files_reached_by_tests(
return {file_of_symbol(symbol) for symbol in reached} - test_files
+def _dependencies_of(graph: Any, path: str) -> set[str]:
+ try:
+ return {
+ dst
+ for _src, dst, data in graph.out_edges(path, data=True)
+ if data.get("edge_type") in FILE_DEPENDENCY_EDGE_TYPES
+ }
+ except Exception:
+ return set()
+
+
+def files_with_paired_tests(
+ graph: Any, paths: Collection[str], test_files: Collection[str]
+) -> set[str]:
+ """Files among *paths* that have a test of their own, import graph first.
+
+ A test file that imports a file directly is its test, wherever either
+ lives: ``tests/unit/cli/test_init.py`` importing ``cli/main.py`` pairs
+ them although no name says so. A test *named* for a file
+ (:func:`paired_test_names`) still pairs, but only when the graph agrees
+ or cannot say: the named test imports the file directly or through a
+ re-export barrel, has no resolved dependency at all, or there is no graph.
+ Otherwise the name is a collision - ``distill/test_engine.py`` tests the
+ distill engine, not ``health/engine.py`` - and does not pair.
+
+ One hop only, and a file-level "some test imports this", never a quantity.
+ The reach walk above stays call-graph-only; pairing is the looser question
+ "does this file have a test", and against this repository's stored line
+ coverage direct-import pairing finds 71.6% of covered files at 98.9%
+ precision where names alone found 34.6% at 100%.
+ """
+ paired: set[str] = set()
+ deps: dict[str, set[str]] = {}
+ if graph is not None:
+ for test in test_files:
+ deps[test] = _dependencies_of(graph, test)
+ paired.update(deps[test])
+ paired &= set(paths)
+
+ by_name: dict[str, list[str]] = {}
+ for path in {*paths, *test_files}:
+ by_name.setdefault(path.rsplit("/", 1)[-1], []).append(path)
+
+ def named_pair_holds(test: str, path: str) -> bool:
+ if graph is None:
+ return True
+ imported = deps[test] if test in deps else _dependencies_of(graph, test)
+ if not imported or path in imported:
+ return True
+ return any(
+ PurePosixPath(mid).name in BARREL_FILENAMES and path in _dependencies_of(graph, mid)
+ for mid in imported
+ )
+
+ for path in paths:
+ if path in paired:
+ continue
+ candidates = (c for name in paired_test_names(path) for c in by_name.get(name, ()))
+ if any(c != path and named_pair_holds(c, path) for c in candidates):
+ paired.add(path)
+ return paired
+
+
async def load_test_files(session: AsyncSession, repo_id: str) -> set[str]:
"""Paths of every file node ingestion classified as a test."""
res = await session.execute(
diff --git a/tests/unit/analysis/test_test_reachability.py b/tests/unit/analysis/test_test_reachability.py
index ec4a7ce0d..9d8218de4 100644
--- a/tests/unit/analysis/test_test_reachability.py
+++ b/tests/unit/analysis/test_test_reachability.py
@@ -9,6 +9,7 @@ lives in ``tests/unit/persistence`` beside the session fixture.
from __future__ import annotations
+import networkx as nx
import pytest
from repowise.core.analysis.test_reachability import (
@@ -16,6 +17,7 @@ from repowise.core.analysis.test_reachability import (
CallGraphView,
call_graph_from_graph,
files_reached_by_tests,
+ files_with_paired_tests,
)
@@ -154,3 +156,50 @@ def test_name_only_resolutions_are_dropped():
def test_missing_graph_is_no_signal_not_an_error():
view = call_graph_from_graph(None)
assert view.declares == {} and view.calls == {}
+
+
+# ---------------------------------------------------------------------------
+# files_with_paired_tests
+# ---------------------------------------------------------------------------
+
+
+def _dep_graph(*edges: tuple[str, str, str]) -> nx.DiGraph:
+ g = nx.DiGraph()
+ for src, dst, kind in edges:
+ g.add_edge(src, dst, edge_type=kind)
+ return g
+
+
+def test_a_test_importing_a_file_pairs_it_whatever_the_names() -> None:
+ g = _dep_graph(("tests/unit/cli/test_init.py", "src/cli/main.py", "imports"))
+ paths = {"src/cli/main.py", "tests/unit/cli/test_init.py"}
+ assert "src/cli/main.py" in files_with_paired_tests(g, paths, {"tests/unit/cli/test_init.py"})
+
+
+def test_a_name_collision_the_graph_contradicts_does_not_pair() -> None:
+ # ``distill/test_engine.py`` tests the distill engine, not health's.
+ g = _dep_graph(("tests/distill/test_engine.py", "src/distill/engine.py", "imports"))
+ paths = {"src/health/engine.py", "src/distill/engine.py", "tests/distill/test_engine.py"}
+ paired = files_with_paired_tests(g, paths, {"tests/distill/test_engine.py"})
+ assert "src/distill/engine.py" in paired
+ assert "src/health/engine.py" not in paired
+
+
+def test_a_name_match_holds_through_a_barrel_or_when_the_graph_is_silent() -> None:
+ g = _dep_graph(
+ ("tests/test_risk.py", "src/risk/__init__.py", "imports"),
+ ("src/risk/__init__.py", "src/risk/risk.py", "imports"),
+ ("tests/test_other.py", "src/unrelated.py", "co_changes"),
+ )
+ paths = {"src/risk/risk.py", "src/other.py", "src/lonely.py"}
+ tests = {"tests/test_risk.py", "tests/test_other.py"}
+ paired = files_with_paired_tests(g, paths, tests)
+ # Through the package barrel it imports.
+ assert "src/risk/risk.py" in paired
+ # ``test_other.py`` has no resolved dependency, so its name still counts.
+ assert "src/other.py" in paired
+ assert "src/lonely.py" not in paired
+ # With no graph at all, names are all there is.
+ assert files_with_paired_tests(None, {"src/lonely.py"}, {"tests/test_lonely.py"}) == {
+ "src/lonely.py"
+ }
diff --git a/tests/unit/health/test_file_nloc.py b/tests/unit/health/test_file_nloc.py
index 469c76f91..7cc8306ea 100644
--- a/tests/unit/health/test_file_nloc.py
+++ b/tests/unit/health/test_file_nloc.py
@@ -90,7 +90,7 @@ def test_health_metric_nloc_uses_file_nloc():
metric, _, _ = HealthAnalyzer(graph=None)._evaluate_file(
pf,
fcx,
- path_basenames={"route.js"},
+ paired_tests=set(),
package_roots=set(),
disabled=[],
dup_report=DuplicationReport(),
diff --git a/tests/unit/health/test_reachability_wiring.py b/tests/unit/health/test_reachability_wiring.py
index 7552902df..7ab9cf58f 100644
--- a/tests/unit/health/test_reachability_wiring.py
+++ b/tests/unit/health/test_reachability_wiring.py
@@ -78,20 +78,26 @@ def test_without_that_edge_the_same_tree_still_fires(tmp_path):
assert [f.file_path for f in fired if f.file_path == "src/parser.py"] == ["src/parser.py"]
[email protected]("edge_type", ["co_changes", "defines", "imports", "references"])
[email protected]("edge_type", ["co_changes", "defines", "references"])
def test_a_non_execution_edge_does_not_clear_it(tmp_path, edge_type):
- """Changing together, containment, importing and naming are not executing.
-
- ``imports`` is the one that changed: it clears the finding no longer. On the
- dogfooded slice unioning the import graph into this walk bought 0.6 points
- of recall for 16.8 of precision, and each false clear hides a real gap.
- """
+ """Changing together, containment and naming are not executing."""
parsed = _tree(tmp_path)
graph = _graph(extra=("tests/test_round_trips.py", "src/parser.py", edge_type))
fired = [f.file_path for f in _findings(parsed, graph) if f.file_path == "src/parser.py"]
assert fired == ["src/parser.py"]
+def test_a_test_importing_the_file_pairs_it_and_clears_it(tmp_path):
+ """Importing is not executing, so the reach walk still ignores it (on the
+ dogfooded slice unioning imports into that walk bought 0.6 points of recall
+ for 16.8 of precision). It clears the finding through the other signal: a
+ test that imports the file directly is that file's paired test, which names
+ alone got wrong both ways."""
+ parsed = _tree(tmp_path)
+ graph = _graph(extra=("tests/test_round_trips.py", "src/parser.py", "imports"))
+ assert [f for f in _findings(parsed, graph) if f.file_path == "src/parser.py"] == []
+
+
def test_no_graph_is_no_signal_not_a_crash(tmp_path):
"""The engine runs graphless on some paths; the walk must degrade, not raise.
diff --git a/tests/unit/health/test_severity_overrides_integration.py b/tests/unit/health/test_severity_overrides_integration.py
index 0d7bd4df9..425580edb 100644
--- a/tests/unit/health/test_severity_overrides_integration.py
+++ b/tests/unit/health/test_severity_overrides_integration.py
@@ -60,7 +60,7 @@ def _evaluate(severity_overrides):
metric, findings, _ = HealthAnalyzer(graph=None)._evaluate_file(
pf,
fcx,
- path_basenames=set(),
+ paired_tests=set(),
package_roots=set(),
disabled=[],
dup_report=DuplicationReport(),