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(tests): let next see tests that reach a file through a package barrel (#3651)

Swati Ahuja14c00d728dd2
diff --git a/packages/core/src/repowise/core/analysis/test_reachability.py b/packages/core/src/repowise/core/analysis/test_reachability.py
index 752ecc675..f91ae8698 100644
--- a/packages/core/src/repowise/core/analysis/test_reachability.py
+++ b/packages/core/src/repowise/core/analysis/test_reachability.py
@@ -505,13 +505,16 @@ async def any_tests_reaching(
async def tests_reaching_through_helpers(
session: AsyncSession, repo_id: str, targets: list[str], test_files: set[str]
) -> dict[str, ReachedBy]:
- """Tests that import a file only tests import, which imports the target.
+ """Tests that import a file only tests import, or a barrel, which imports the target.
A second import hop, kept off hubs: a blanket second hop was measured and
rejected (see Depth), but a module nothing outside the test suite imports
is test support whatever its name (``attempt.spawn-workspace.test-support.ts``
loading ``attempt.ts`` for 30-odd tests), so walking through it claims
- nothing a hub would.
+ nothing a hub would. A barrel (``index.ts``, ``__init__.py``) passes on
+ what it imports even when source imports it too, as
+ :func:`files_with_paired_tests` already reads it: a package's tests
+ import its entry, not each module behind it.
"""
edge_types = sorted(FILE_DEPENDENCY_EDGE_TYPES)
middles: dict[str, set[str]] = {}
@@ -525,9 +528,10 @@ async def tests_reaching_through_helpers(
importers.setdefault(middle, set()).add(dependent)
reached: dict[str, set[str]] = {}
for middle, who in importers.items():
- if who <= test_files:
+ tests = who & test_files
+ if tests and (tests == who or PurePosixPath(middle).name in BARREL_FILENAMES):
for target in middles[middle]:
- reached.setdefault(target, set()).update(who)
+ reached.setdefault(target, set()).update(tests)
out: dict[str, ReachedBy] = {}
for target, tests in reached.items():
ordered = tuple(rank_tests(target, tests))
diff --git a/tests/unit/persistence/test_test_reachability_walk.py b/tests/unit/persistence/test_test_reachability_walk.py
index c1a8b130d..89b53f695 100644
--- a/tests/unit/persistence/test_test_reachability_walk.py
+++ b/tests/unit/persistence/test_test_reachability_walk.py
@@ -678,3 +678,47 @@ async def test_the_import_walk_from_memory_matches_the_walk_from_the_database(as
"tests/helper.py": 3,
"tests/test_h.py": 4,
}
+
+
+_KY_LIKE = {
+ "src/core/Ky.ts": "export class Ky {\n static create(input: string) { return new Ky(); }\n}\n",
+ # Three calls deep before the test's own call: past the call walk's depth.
+ "src/index.ts": (
+ "import {Ky} from './core/Ky';\n"
+ "const createRequest = (input: string) => Ky.create(input);\n"
+ "const createInstance = () => (input: string) => createRequest(input);\n"
+ "const ky = createInstance();\n"
+ "export default ky;\n"
+ "export type {Hooks} from './types/hooks';\n"
+ ),
+ # A type import back into the package entry: index.ts is not test-only.
+ "src/types/hooks.ts": "import type ky from '../index';\nexport type Hooks = typeof ky;\n",
+ "test/x.test.ts": (
+ "import ky from '../src/index';\nimport {pool} from '../src/pool';\n"
+ "ky('https://example.com');\n"
+ ),
+ # Source imports pool.ts too and it is no barrel, so it passes nothing on.
+ "src/pool.ts": "import {retry} from './retry';\nexport const pool = [retry];\n",
+ "src/retry.ts": "export const retry = 1;\n",
+ "src/agent.ts": "import {pool} from './pool';\nexport const agent = pool;\n",
+}
+
+
+async def test_any_reach_passes_through_the_package_entry_tests_import(tmp_path, async_session):
+ """A test importing a package's ``index.ts`` reaches the files it imports."""
+ from repowise.core.analysis.test_reachability import any_tests_reaching, clear_test_map_cache
+ from repowise.core.persistence.crud.graph import batch_upsert_graph_edges
+ from repowise.core.pipeline.persist import _edge_row, persist_graph_nodes
+ from tests.unit.analysis.test_public_api_flag import build_fixture
+
+ builder = build_fixture(tmp_path, _KY_LIKE)
+ repo = await insert_repo(async_session)
+ await persist_graph_nodes(async_session, repo.id, builder, ep_scores={})
+ edges = [_edge_row(u, v, d) for u, v, d in builder.graph().edges(data=True)]
+ await batch_upsert_graph_edges(async_session, repo.id, edges)
+ clear_test_map_cache()
+
+ found = await any_tests_reaching(async_session, repo.id, ["src/core/Ky.ts", "src/retry.ts"])
+ assert found["src/core/Ky.ts"].tests == ["test/x.test.ts"]
+ assert found["src/core/Ky.ts"].via == "import-graph"
+ assert "src/retry.ts" not in found