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.

feat(impacted-tests): select a subset when the index lags the change's base (#3219)

Raghav Chamadiya0adbf2d0d50a
diff --git a/docs/start/CI.md b/docs/start/CI.md
index bf6ca9006..86b4f9dce 100644
--- a/docs/start/CI.md
+++ b/docs/start/CI.md
@@ -254,7 +254,8 @@ a per-test coverage map (`repowise coverage add`) makes it more precise.
- Branch on the run-all flag, never on `:all`: jest and vitest read it as a pattern and run nothing.
- It runs everything for: an empty change; a lockfile, manifest, build, test or CI config change; `.repowise/config.yaml`; a production package's `__init__.py`.
- It also runs everything for: non-code files in a test tree; a changed file with no known test or only a filename guess; a route through a test helper no test imports; any Python test helper while a conftest or pytest config loads plugins by name.
-- It also runs everything for: a missing, out-of-date or unreadable index; a per-test map at its row cap; a deleted file no known test used.
+- 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); an empty line means only those changed.
- Tests the graph cannot see into (not indexed, or importing nothing it resolves, such as a test that only runs a subprocess) run with every subset. 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.
diff --git a/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py b/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py
index bb8f11c3d..130e5dd0f 100644
--- a/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py
@@ -124,7 +124,7 @@ def impacted_tests_command(
cannot_evaluate(fmt, exc.code, str(exc))
checkout = _read_checkout(repo_path)
- result = run_async(_collect(repo_path, change, checkout.roots))
+ result = run_async(_collect(repo_path, change, checkout.roots, config))
result["diff"] = change.label
if config is not None:
result["selection"] = _select(repo_path, change, result, config, checkout)
@@ -173,8 +173,12 @@ def _read_checkout(repo_path) -> _Checkout:
return _Checkout(tracked, texts, roots)
-async def _collect(repo_path, change, roots: PytestRoots | None = None) -> dict:
- """Resolve the change's files to impacted tests + labelled fallbacks."""
+async def _collect(repo_path, change, roots: PytestRoots | None = None, config=None) -> dict:
+ """Resolve the change's files to impacted tests + labelled fallbacks.
+
+ With a selection *config*, files changed since the index was built are
+ walked too, so selection can add the tests reaching them.
+ """
from repowise.core.persistence.crud import (
get_health_metrics,
get_repository,
@@ -203,8 +207,9 @@ async def _collect(repo_path, change, roots: PytestRoots | None = None) -> dict:
# Repo file keys back the filename-pattern fallback (same source the
# aggregate coverage ingest resolves against).
repo_keys = {m.file_path for m in await get_health_metrics(session, repo_id)}
+ routes = [] if config is None else _gap_routes(repo_path, change, config, repo_keys, out)
await _resolve_impacted(
- session, repo_id, _query_lines(change, measured), repo_keys, out, roots
+ session, repo_id, _query_lines(change, measured), repo_keys, out, roots, routes
)
await _place_tests(session, repo_id, out)
@@ -241,6 +246,36 @@ def _indexed_commit(repo_path, row_commit: str | None, out: dict) -> None:
out["indexed_commit"] = state or row_commit
+def _gap_routes(repo_path, change, config, repo_keys: set[str], out: dict) -> list[str]:
+ """Record the files changed since the indexed commit, and return those to walk.
+
+ Only files whose edges may have moved are walked: each candidate is read at
+ the indexed commit and at the base in one batch and compared.
+ """
+ from repowise.core.analysis.change_health.sources import read_blobs
+ from repowise.core.analysis.changed_lines import index_gap
+ from repowise.core.analysis.import_drift import edges_may_differ
+ from repowise.core.analysis.test_selection import plan_gap, with_rewired
+
+ out["index_gap"] = gap = index_gap(str(repo_path), out["indexed_commit"], change)
+ if gap is None:
+ return []
+ plan = plan_gap(gap, config, [*change.files, *change.deleted])
+ if plan.candidates:
+ old, new = out["indexed_commit"], change.base
+ specs = [(rev, p) for p in plan.candidates for rev in (old, new)]
+ blobs = read_blobs(str(repo_path), specs)
+ # Unreadable means nothing was compared, so every candidate may have moved.
+ rewired = [
+ p
+ for p in plan.candidates
+ if blobs is None or edges_may_differ(p, blobs.get((old, p)), blobs.get((new, p)))
+ ]
+ plan = with_rewired(plan, rewired, repo_keys)
+ out["gap"] = plan
+ return list(plan.targets)
+
+
def _query_lines(change, measured: str | None) -> dict[str, set[int] | None]:
"""The lines to look up per file, on the side of the diff the map was measured at.
@@ -276,6 +311,8 @@ def _empty_result(changed_files: int) -> dict:
"map_truncated": False,
"indexed_commit": None,
"index_problem": None,
+ "index_gap": None,
+ "gap": None,
"graph_error": None,
"placed_tests": None,
"helper_importers": {},
@@ -293,6 +330,7 @@ async def _resolve_impacted(
repo_keys: set[str],
out: dict,
roots: PytestRoots | None = None,
+ routes: tuple[str, ...] | list[str] = (),
) -> dict:
"""Classify each changed file: covered tests, inferred tests, or unknown.
@@ -303,6 +341,10 @@ async def _resolve_impacted(
also asks the graph: coverage records what one run executed, so it adds to
the graph's answer and never replaces it.
+ *routes* (files changed since the index was built) are looked up the same
+ way, by file, but only to report the tests reaching them: none is guessed
+ from its name or listed as unknown.
+
Three tiers, and the output says which one answered for every file, because
they are not interchangeable:
@@ -323,13 +365,16 @@ async def _resolve_impacted(
Nothing said anything. Run the full suite.
"""
from repowise.core.analysis.health.coverage import paired_test_file
- from repowise.core.persistence.crud import tests_covering
+ from repowise.core.persistence.crud import tests_covering, tests_covering_files
covered: dict[str, dict] = out["covered"]
has_rows: set[str] = set()
- graph_targets = sorted(changed)
- for source_file, lines in sorted(changed.items()):
- rows = await tests_covering(session, repo_id, source_file, lines=lines)
+ route_only = [r for r in routes if r not in changed]
+ graph_targets = sorted({*changed, *route_only})
+ by_file = {f: await tests_covering(session, repo_id, f, lines=ls) for f, ls in changed.items()}
+ if route_only and not out.get("map_empty"):
+ by_file.update(await tests_covering_files(session, repo_id, set(route_only)))
+ for source_file, rows in sorted(by_file.items()):
for r in rows:
has_rows.add(source_file)
entry = covered.setdefault(
@@ -356,8 +401,8 @@ async def _resolve_impacted(
{"source_file": source_file, "test_file": t, "via": via} for t, via in found
)
continue
- if source_file in has_rows:
- continue # coverage answered; a name-shaped guess adds nothing
+ if source_file in has_rows or source_file not in changed:
+ continue # coverage answered, or a route; a name-shaped guess adds nothing
guess = paired_test_file(source_file, repo_keys)
if guess:
out["inferred"].append(
@@ -469,7 +514,6 @@ def _with_importers(picks: dict[str, str], parents: dict[str, set[str]]) -> list
def _select(repo_path, change, result: dict, config, checkout: _Checkout):
"""The run-all-or-subset decision for ``--format args`` / ``json``."""
- from repowise.core.analysis.changed_lines import index_gap
from repowise.core.analysis.test_selection import (
SelectionInput,
doc_readers,
@@ -499,7 +543,8 @@ def _select(repo_path, change, result: dict, config, checkout: _Checkout):
label=change.label,
map_current=result["map_current"],
map_truncated=result["map_truncated"],
- index_gap=index_gap(str(root), result["indexed_commit"], change),
+ index_gap=result["index_gap"],
+ gap=result["gap"],
index_problem=result["index_problem"],
graph_error=result["graph_error"],
missing={f for f in named if not (root / f).is_file()},
diff --git a/packages/core/src/repowise/core/analysis/change_health/sources.py b/packages/core/src/repowise/core/analysis/change_health/sources.py
index d164cd9eb..864bf2502 100644
--- a/packages/core/src/repowise/core/analysis/change_health/sources.py
+++ b/packages/core/src/repowise/core/analysis/change_health/sources.py
@@ -395,18 +395,27 @@ def _iter_name_status(raw: str) -> Iterator[tuple[str, str, str]]:
def _cat_file_batch(repo_path: str, sha: str, paths: list[str]) -> dict[str, bytes]:
"""One batch read; an object missing at *sha* is omitted rather than raising."""
- specs = "\n".join(f"{sha}:{path}" for path in paths) + "\n"
+ read = read_blobs(repo_path, [(sha, path) for path in paths])
+ return {path: blob for (_, path), blob in (read or {}).items()}
+
+
+def read_blobs(repo_path: str, specs: list[tuple[str, str]]) -> dict[tuple[str, str], bytes] | None:
+ """``{(rev, path): bytes}`` over one ``git cat-file --batch``; ``None`` if git failed.
+
+ An object missing at its revision is omitted rather than raising.
+ """
+ request = "\n".join(f"{rev}:{path}" for rev, path in specs) + "\n"
proc = subprocess.run(
["git", "-C", repo_path, "cat-file", "--batch"],
- input=specs.encode("utf-8"),
+ input=request.encode("utf-8"),
capture_output=True,
timeout=GIT_TIMEOUT_SECONDS,
)
if proc.returncode != 0:
- return {}
- out: dict[str, bytes] = {}
+ return None
+ out: dict[tuple[str, str], bytes] = {}
data, cursor = proc.stdout, 0
- for path in paths:
+ for spec in specs:
newline = data.find(b"\n", cursor)
if newline == -1:
break
@@ -421,6 +430,6 @@ def _cat_file_batch(repo_path: str, sha: str, paths: list[str]) -> dict[str, byt
break # unparseable header: stop rather than misread the stream
size = int(fields[2])
start = newline + 1
- out[path] = data[start : start + size]
+ out[spec] = data[start : start + size]
cursor = start + size + 1 # trailing newline
return out
diff --git a/packages/core/src/repowise/core/analysis/import_drift.py b/packages/core/src/repowise/core/analysis/import_drift.py
new file mode 100644
index 000000000..b1e39435c
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/import_drift.py
@@ -0,0 +1,147 @@
+"""Whether a file's graph edges can differ between two versions of its text.
+
+An index built at an older commit still describes every file whose edges did
+not move since. In Python and JavaScript / TypeScript a file reaches another
+only through an import the parser reads (``import``, ``from``, ``require``,
+``import()``, a re-export) or through a string a loader resolves (a dotted
+module name in a plugin table, a mocked or spawned path). Two versions with the
+same imports and the same module- or path-shaped string literals therefore
+have the same edges, whatever else changed in them.
+
+Cheapest check first: the strings are a regex away, and when no line that
+differs can be part of an import statement (one naming an import keyword, or
+inside a bracketed or continued statement that does) the imports cannot
+differ, so nothing is parsed. Otherwise both versions go through the parser's
+import pass alone. Any other language is assumed to have moved whenever its
+text changed beyond whitespace: Go, Java, C# and others reach same-package
+code with no import at all. Data and prose import nothing.
+
+Ceilings: Python reaching a submodule as an attribute of a package some other
+module imported (``import a`` then ``a.b.f()``) moves no import, and a data
+file a framework reads as wiring (a class named in YAML) is not compared.
+Storing each file's fingerprint at index time would save the old-side work.
+"""
+
+from __future__ import annotations
+
+import re
+from collections import Counter
+from datetime import UTC, datetime
+from pathlib import PurePosixPath
+
+from ..ingestion.models import EXTENSION_TO_LANGUAGE, FileInfo
+
+# Data and prose: they import nothing, so they cannot carry a route.
+_NO_EDGES = frozenset({"json", "yaml", "toml", "markdown", "asciidoc"})
+
+# A quoted string with no spaces holding a dot or a slash: a dotted module
+# name (what the Python dynamic-import hints resolve) or a file path.
+_NAME_LITERAL = re.compile(rb"""(["'`])([A-Za-z_@.$][\w.\-/@$]*)\1""")
+
+# Words that open or continue an import statement, per import-bound language.
+_JS_WORD = re.compile(rb"\b(?:import|export|require|from)\b")
+_IMPORT_WORD = {
+ "python": re.compile(rb"\b(?:import|from|__import__)\b"),
+ "typescript": _JS_WORD,
+ "javascript": _JS_WORD,
+}
+_JS_COMMENT = re.compile(rb"//.*")
+_LINE_COMMENT = {"python": re.compile(rb"#.*"), "typescript": _JS_COMMENT}
+_LINE_COMMENT["javascript"] = _JS_COMMENT
+_STRING = re.compile(rb"""(["'`])(?:\\.|(?!\1).)*\1""")
+_WHITESPACE = re.compile(rb"\s+")
+
+
+def _language(path: str) -> str | None:
+ return EXTENSION_TO_LANGUAGE.get(PurePosixPath(path).suffix.lower())
+
+
+def may_carry_edges(path: str) -> bool:
+ """Whether *path* is code the graph can hold edges from."""
+ language = _language(path)
+ return language is not None and language not in _NO_EDGES
+
+
+def edges_may_differ(path: str, before: bytes | None, after: bytes | None) -> bool:
+ """Whether *path*'s edges can differ between *before* and *after*.
+
+ ``None`` on a side means the file is absent there. A file added since has
+ no edges in the older index for a test to reach, and a deleted one only
+ loses edges, so neither moves a route; a file that cannot be parsed has.
+ """
+ if before is None or after is None or before == after:
+ return False
+ language = _language(path)
+ if language not in _IMPORT_WORD:
+ return _WHITESPACE.sub(b" ", before).strip() != _WHITESPACE.sub(b" ", after).strip()
+ if _names(before) != _names(after):
+ return True
+ if not _touches_imports(language, before, after):
+ return False
+ old = _imports(path, language, before)
+ return old is None or old != _imports(path, language, after)
+
+
+def _names(source: bytes) -> list[bytes]:
+ """The module- or path-shaped string literals of one version."""
+ found = {m.group(2) for m in _NAME_LITERAL.finditer(source)}
+ return sorted(n for n in found if b"." in n or b"/" in n)
+
+
+def _touches_imports(language: str, before: bytes, after: bytes) -> bool:
+ """Whether a line one version has more of than the other can be in an import.
+
+ Lines are compared as a multiset, so a moved line does not count.
+ """
+ old, new = before.splitlines(), after.splitlines()
+ old_count, new_count = Counter(old), Counter(new)
+ changed = {line for line in old_count | new_count if old_count[line] != new_count[line]}
+ return _import_line_changed(language, old, changed) or _import_line_changed(
+ language, new, changed
+ )
+
+
+def _import_line_changed(language: str, lines: list[bytes], changed: set[bytes]) -> bool:
+ word, comment = _IMPORT_WORD[language], _LINE_COMMENT[language]
+ depth, open_statement = 0, False
+ for line in lines:
+ code = comment.sub(b"", _STRING.sub(b'""', line))
+ starts = word.search(code) is not None
+ if line in changed and (starts or open_statement):
+ return True
+ if not (starts or open_statement):
+ continue
+ # Brackets outside strings and comments. A statement left open by a
+ # miscount only parses more, never less.
+ depth += sum(code.count(c) for c in b"([{") - sum(code.count(c) for c in b")]}")
+ open_statement = depth > 0 or code.rstrip().endswith(b"\\")
+ if not open_statement:
+ depth = 0
+ return False
+
+
+def _imports(path: str, language: str, source: bytes) -> list[tuple] | None:
+ """The imports the parser reads in one version, or ``None`` if it will not parse."""
+ from ..ingestion.parser import ASTParser
+
+ try:
+ imports = ASTParser().parse_imports(_file_info(path, language, source), source)
+ except Exception:
+ return None
+ return sorted((i.module_path, tuple(sorted(i.imported_names)), i.is_reexport) for i in imports)
+
+
+def _file_info(path: str, language: str, source: bytes) -> FileInfo:
+ # The parser reads only the path, language and bytes it is handed.
+ return FileInfo(
+ path=path,
+ abs_path=path,
+ language=language, # type: ignore[arg-type]
+ size_bytes=len(source),
+ git_hash="",
+ last_modified=datetime.now(UTC),
+ is_test=False,
+ is_config=False,
+ is_api_contract=False,
+ is_entry_point=False,
+ )
diff --git a/packages/core/src/repowise/core/analysis/test_selection.py b/packages/core/src/repowise/core/analysis/test_selection.py
index 912acc2cc..bfc6cf2a0 100644
--- a/packages/core/src/repowise/core/analysis/test_selection.py
+++ b/packages/core/src/repowise/core/analysis/test_selection.py
@@ -19,8 +19,9 @@ is chosen when any of these hold:
a route to it passes through a test helper no test imports, or any Python
test helper while a conftest or pytest config loads plugins by name (its
users are unknown);
-6. the index is missing, was built before other files changed, disagrees with
- itself about its commit, or its graph could not be read;
+6. the index is missing, disagrees with itself about its commit, or its graph
+ could not be read; or the files changed between its commit and the base are
+ unknown, too many to trace, or include a manifest, lockfile or build config;
7. the per-test map hit its stored row cap;
8. a deleted code file has no known test, or a test the index names is
missing from the checkout.
@@ -28,14 +29,15 @@ is chosen when any of these hold:
Otherwise the subset is the covering tests, the tests the graph shows reaching
the changed files (a changed test, the call graph, the import graph) and
``tests.always_run``, plus every test the graph cannot see into (not indexed,
-or with no resolved edge). A test package's ``__init__.py`` or a ``conftest.py``
-(changed, deleted, or on a route to a changed file) stands for every test under
-its directory. A helper module tests import stands for the tests that import
-it, directly or through other helpers (basis ``helper-importers``), which are the files that run it: Python runs the package file
-for each module in it, and pytest loads a conftest for each test at or below
-it. Only documentation (``docs/`` and the root README,
-CHANGELOG, LICENSE and the like, never code) that no code names is skipped
-without a test.
+or with no resolved edge), plus the tests reaching files whose imports moved
+between the indexed commit and the base (:func:`plan_gap`). A test package's
+``__init__.py`` or a ``conftest.py`` (changed, deleted, or on a route to a
+changed file) stands for every test under its directory. A helper module tests
+import stands for the tests that import it, directly or through other helpers
+(basis ``helper-importers``), which are the files that run it: Python runs the
+package file for each module in it, and pytest loads a conftest for each test
+at or below it. Only documentation (``docs/`` and the root README, CHANGELOG,
+LICENSE and the like, never code) that no code names is skipped without a test.
:func:`runner_args` renders a selection as arguments for one test runner; a
full run renders as the :data:`RUN_ALL` sentinel. pytest and go reject it as a
@@ -45,10 +47,11 @@ a pipeline must branch on the run-all flag rather than rely on the sentinel.
from __future__ import annotations
+import functools
import re
import shlex
from collections.abc import Collection, Iterable, Mapping
-from dataclasses import dataclass, field
+from dataclasses import dataclass, field, replace
from pathlib import PurePosixPath
from typing import Any
@@ -367,12 +370,17 @@ def is_runnable_test(path: str, roots: PytestRoots | None = None) -> bool:
)
[email protected]_cache(maxsize=8)
+def _extra_spec(patterns: tuple[str, ...]) -> pathspec.PathSpec:
+ """``tests.full_run_on`` compiled once per pattern list, not once per path."""
+ return pathspec.PathSpec.from_lines("gitwildmatch", patterns)
+
+
def full_run_reason(path: str, extra: Iterable[str] = ()) -> str | None:
"""Why a change to *path* can change any test, or ``None``."""
specs = list(_DEFAULT_SPECS)
if extra := tuple(extra):
- spec = pathspec.PathSpec.from_lines("gitwildmatch", extra)
- specs.append(("it matches tests.full_run_on", spec))
+ specs.append(("it matches tests.full_run_on", _extra_spec(extra)))
for why, spec in specs:
if spec.match_file(path):
return why
@@ -422,6 +430,9 @@ class SelectionInput:
doc_readers: Mapping[str, str] = field(default_factory=dict)
plugin_loader: str | None = None
unplaced_tests: Collection[str] = ()
+ # :func:`plan_gap` for *index_gap*, with the targets whose rows *tiers* also
+ # holds; None when the caller did not trace it, so any code there runs all.
+ gap: GapPlan | None = None
@dataclass
@@ -444,18 +455,22 @@ def select_tests(inp: SelectionInput) -> Selection:
evidence = _Evidence.of(inp, deleted)
per_file, file_reasons = _tests_per_file(triage.code, evidence)
+ traced = bool(triage.code and inp.gap and inp.gap.targets)
+ gap_tests, route_reasons = (
+ _gap_route_tests(inp.gap.targets, paths, evidence) if traced else ([], [])
+ )
if inp.index_available:
- run_all += file_reasons
+ run_all += file_reasons + route_reasons
basis = {**triage.basis, **{path: per_file[path][1] for path in triage.code}}
# The graph cannot say what an unplaced test reaches, so it always runs.
unplaced = [(t, t) for t in inp.unplaced_tests] if triage.code else []
tests, test_files = _runnable(
- [*(t for path in triage.code for t in per_file[path][0]), *unplaced]
+ [*(t for path in triage.code for t in per_file[path][0]), *gap_tests, *unplaced]
)
return Selection(
run_all=bool(run_all),
- reasons=tuple(run_all + _notes(inp, triage.skipped, bool(unplaced))),
+ reasons=tuple(run_all + _notes(inp, triage.skipped, bool(unplaced), traced)),
tests=tests,
test_files=test_files,
packages=_go_packages(triage.code, deleted, inp.go_test_dirs),
@@ -507,7 +522,7 @@ def _index_reasons(inp: SelectionInput, *, has_code: bool) -> list[str]:
"No index: nothing records which tests reach the changed code "
"(run `repowise init`, or restore a cached .repowise directory)."
]
- out = [inp.index_problem] if inp.index_problem else _gap_reasons(inp.index_gap)
+ out = [inp.index_problem] if inp.index_problem else _gap_reasons(inp)
if inp.graph_error:
out.append(
f"The graph could not be read ({inp.graph_error}), so the tests reaching "
@@ -521,9 +536,15 @@ def _index_reasons(inp: SelectionInput, *, has_code: bool) -> list[str]:
return out
-def _notes(inp: SelectionInput, skipped: list[str], unplaced: bool) -> list[str]:
+def _notes(inp: SelectionInput, skipped: list[str], unplaced: bool, traced: bool) -> list[str]:
"""Reasons that explain the selection without forcing a full run."""
out = []
+ if traced:
+ rewired = len(inp.gap.rewired) if inp.gap else 0
+ out.append(
+ f"The index predates {rewired} changed file(s) outside this change; "
+ "the tests reaching them run too."
+ )
if unplaced:
out.append(
f"{len(inp.unplaced_tests)} test file(s) the graph cannot see into run with "
@@ -551,13 +572,15 @@ def _go_packages(
return tuple(sorted(dirs & set(go_test_dirs)))
-def _gap_reasons(index_gap: Collection[str] | None) -> list[str]:
- if index_gap is None:
+def _gap_reasons(inp: SelectionInput) -> list[str]:
+ if inp.index_gap is None:
return [
"Cannot tell what changed since the index was built (its commit is not "
"recorded, or not in this clone); run `repowise update` before selecting."
]
- unseen = sorted(p for p in index_gap if not is_documentation(p))
+ if inp.gap is not None:
+ return list(inp.gap.reasons)
+ unseen = sorted(p for p in inp.index_gap if not is_documentation(p))
if not unseen:
return []
return [
@@ -567,6 +590,127 @@ def _gap_reasons(index_gap: Collection[str] | None) -> list[str]:
]
+# Past this many files changed since the index was built, tracing them would
+# select nearly every test, so a full run is cheaper to decide. Ceiling: a file
+# count, not a measure of how much of the suite they reach.
+MAX_INDEX_GAP = 1000
+
+# Read by tests or CI, never imported, so they cannot add a route to a change.
+_GAP_INERT = frozenset({"CI configuration changed", "shared test data can change any test"})
+_HELPER_REASON = "a shared test helper can change any test"
+_GAP_TRACED = (None, "route", "package")
+
+
+def _gap_kind(path: str, config: TestSelectionConfig) -> str | None:
+ """``"route"``, ``"package"``, ``None`` (cannot reach a change), or a full-run reason."""
+ if is_documentation(path):
+ return None
+ why = full_run_reason(path, config.full_run_on)
+ if why == _PACKAGE_INIT_REASON:
+ return "package"
+ if why is None or why == _HELPER_REASON or (why in _GAP_INERT and is_code_file(path)):
+ return "route"
+ return None if why in _GAP_INERT else why
+
+
+@dataclass(frozen=True)
+class GapPlan:
+ """What the files changed between the indexed commit and the base mean for selection.
+
+ *reasons* say why they force a full run, if they do. Otherwise
+ *candidates* are the code files among them whose edges may have moved,
+ *packages* the production ``__init__.py`` files among those, and, once
+ :func:`with_rewired` has run, *rewired* the candidates whose edges did move
+ and *targets* the files whose tests join the selection.
+ """
+
+ reasons: tuple[str, ...] = ()
+ candidates: tuple[str, ...] = ()
+ packages: frozenset[str] = frozenset()
+ rewired: tuple[str, ...] = ()
+ targets: tuple[str, ...] = ()
+
+
+def plan_gap(
+ index_gap: Collection[str], config: TestSelectionConfig, change: Collection[str] = ()
+) -> GapPlan:
+ """Classify each file changed since the index was built, once.
+
+ Nothing is traced when a file in *change* already forces a full run.
+
+ A full run is forced when there are more than :data:`MAX_INDEX_GAP` of them
+ (documentation aside), or when one is configuration that decides how every
+ import resolves or what is built (a manifest, a lockfile, build or test
+ config, ``tests.full_run_on``). Data and prose import nothing, so only code
+ is a candidate.
+ """
+ from .import_drift import may_carry_edges
+
+ counted = sorted(p for p in index_gap if not is_documentation(p))
+ if len(counted) > MAX_INDEX_GAP:
+ return GapPlan(
+ reasons=(
+ f"The index predates {len(counted)} changed file(s) outside this change "
+ f"(e.g. {counted[0]}), more than the {MAX_INDEX_GAP} worth tracing; "
+ "run `repowise update` before selecting.",
+ )
+ )
+ kinds = {p: _gap_kind(p, config) for p in counted}
+ triggers = [(p, k) for p, k in kinds.items() if k not in _GAP_TRACED]
+ if triggers:
+ path, why = triggers[0]
+ return GapPlan(
+ reasons=(
+ f"{path} changed after the index was built ({len(triggers)} such): {why}; "
+ "run `repowise update` before selecting.",
+ )
+ )
+ if any(full_run_reason(p, config.full_run_on) for p in change):
+ return GapPlan()
+ candidates = tuple(p for p, k in kinds.items() if k is not None and may_carry_edges(p))
+ return GapPlan(
+ candidates=candidates,
+ packages=frozenset(p for p in candidates if kinds[p] == "package"),
+ )
+
+
+def with_rewired(
+ plan: GapPlan, rewired: Collection[str], indexed_files: Collection[str]
+) -> GapPlan:
+ """*plan* with the candidates whose edges moved, and the files standing for them.
+
+ A route the index cannot see from a test to the change must pass through a
+ rewired file: up to the first such file on it, every file kept its edges,
+ so the index has that part of the route. Selecting the tests that reach
+ each rewired file therefore covers every new route. A production
+ ``__init__.py`` runs for every module under its directory, an import the
+ graph does not record, so it stands for all of them. Ceiling: an unchanged
+ file whose import starts resolving to a file added since is not traced.
+ """
+ out = set(rewired)
+ for path in set(rewired) & plan.packages:
+ out.update(_files_under(path, indexed_files))
+ return replace(plan, rewired=tuple(sorted(rewired)), targets=tuple(sorted(out)))
+
+
+def _files_under(init: str, files: Collection[str]) -> list[str]:
+ parent = str(PurePosixPath(init).parent)
+ return [f for f in files if parent == "." or f.startswith(f"{parent}/")]
+
+
+def _gap_route_tests(
+ targets: Collection[str], paths: list[str], ev: _Evidence
+) -> tuple[list[_TestRef], list[str]]:
+ """The tests reaching each gap target, and run-all reasons found on those routes."""
+ tests: list[_TestRef] = []
+ reasons: list[str] = []
+ for path in sorted(set(targets) - set(paths)):
+ found, _, why = _file_tests(path, ev, route_only=True)
+ tests += found
+ reasons += [f"Changed after the index was built: {r}" for r in why]
+ return tests, reasons
+
+
_TestRef = tuple[str, str | None] # (test id or file, the file it lives in)
# Bases that need no test of their own to be safe.
@@ -585,6 +729,7 @@ class _Evidence:
deleted: frozenset[str]
known_tests: tuple[str, ...]
plugin_loader: str | None
+ gap: frozenset[str] = frozenset()
@classmethod
def of(cls, inp: SelectionInput, deleted: set[str]) -> _Evidence:
@@ -604,6 +749,7 @@ class _Evidence:
deleted=frozenset(deleted),
known_tests=tuple(inp.known_tests),
plugin_loader=inp.plugin_loader,
+ gap=frozenset(inp.index_gap or ()),
)
def found(self, path: str) -> list[_TestRef]:
@@ -632,7 +778,8 @@ def _tests_per_file(
def _stale_index_reasons(ev: _Evidence) -> list[str]:
- stale = sorted(ev.missing - ev.deleted)
+ # A missing test that changed since the index was built was deleted then.
+ stale = sorted(ev.missing - ev.deleted - ev.gap)
if not stale:
return []
return [
@@ -641,8 +788,14 @@ def _stale_index_reasons(ev: _Evidence) -> list[str]:
]
-def _file_tests(path: str, ev: _Evidence) -> tuple[list[_TestRef], str, list[str]]:
- """One changed file's tests, the evidence behind them, and any run-all reasons."""
+def _file_tests(
+ path: str, ev: _Evidence, *, route_only: bool = False
+) -> tuple[list[_TestRef], str, list[str]]:
+ """One changed file's tests, the evidence behind them, and any run-all reasons.
+
+ With *route_only* the file only links tests to the change (it changed before
+ the base), so having no test of its own is not a reason.
+ """
found = ev.found(path)
tests = ev.present([*ev.covered.get(path, ()), *found])
basis = _basis(path, ev.covered, ev.inferred, ev.unknown)
@@ -660,7 +813,7 @@ def _file_tests(path: str, ev: _Evidence) -> tuple[list[_TestRef], str, list[str
# A deleted test needs no run of its own; the tests importing it do.
if path in ev.deleted and is_runnable_test(path):
basis = "deleted-test"
- if not (tests or helpers or basis in _SELF_SUFFICIENT):
+ if not (route_only or tests or helpers or basis in _SELF_SUFFICIENT):
reasons.append(_no_test_reason(path, basis, ev))
return tests, basis, reasons
diff --git a/packages/core/src/repowise/core/ingestion/parser.py b/packages/core/src/repowise/core/ingestion/parser.py
index c71b10f70..b80e6ea99 100644
--- a/packages/core/src/repowise/core/ingestion/parser.py
+++ b/packages/core/src/repowise/core/ingestion/parser.py
@@ -1314,6 +1314,58 @@ def _is_reexport_import(stmt_node: Node, raw: str, language: str) -> bool:
return language == "swift" and raw.startswith("@_exported")
+def _parse_tree(
+ lang: str, language: Language, grammar_tag: str, source: bytes
+) -> tuple[Node, list[str], str, Language]:
+ """``(root, parse errors, grammar tag, language)``; a JSX-bearing ``.ts`` retries as TSX."""
+ parser = Parser(language)
+ tree = parser.parse(source)
+ root = tree.root_node
+
+ parse_errors = _collect_error_nodes(root)
+
+ # Adaptive TSX grammar fallback: if a .ts file contains JSX markup,
+ # tree-sitter-typescript produces ERROR nodes. Re-parse using the TSX
+ # grammar ONLY IF:
+ # 1. Initial parse yielded error nodes (parse_errors is non-empty)
+ # 2. The file projects to TypeScript and is not already on tsx
+ # 3. Source contains JSX-specific closing tokens (b"/>" or b"</")
+ # A .vue render function may be written in JSX
+ # (``vnodes.push(<i class={c} />)``), which is the single TS parse
+ # failure across a 1,593-file .vue corpus. ``source`` here is the
+ # markup-blanked projection, so the template's own ``</`` and ``/>``
+ # are already spaces — the token test still keys on real JSX only.
+ # The swap is strictly safe: it only takes effect when TSX yields
+ # FEWER errors than the first parse.
+ if (
+ parse_errors
+ and lang in ("typescript", "vue")
+ and grammar_tag != "tsx"
+ and (b"/>" in source or b"</" in source)
+ ):
+ tsx_language = _get_language("tsx")
+ if tsx_language is not None:
+ tsx_tree = Parser(tsx_language).parse(source)
+ tsx_errors = _collect_error_nodes(tsx_tree.root_node)
+ if len(tsx_errors) < len(parse_errors):
+ tree = tsx_tree
+ root = tree.root_node
+ parse_errors = tsx_errors
+ # Both grammar_tag AND language must be reassigned.
+ # grammar_tag is consumed immediately below by
+ # the caller's _get_query(lang, language, grammar_tag), which
+ # appends tsx.scm to the base typescript.scm query.
+ # That append is what supplies the
+ # jsx_opening_element / jsx_self_closing_element captures
+ # that restore JSX component call-site edges.
+ # Reassigning only ``tree`` / ``root`` would fix parse
+ # errors but leave those edges missing — the dead-code
+ # false-positive would remain.
+ grammar_tag = "tsx"
+ language = tsx_language
+ return root, parse_errors, grammar_tag, language
+
+
class ASTParser:
"""Unified AST parser — works for all languages via .scm query files.
@@ -1331,6 +1383,31 @@ class ASTParser:
def __init__(self) -> None:
pass
+ def parse_imports(self, file_info: FileInfo, source: bytes) -> list[Import]:
+ """Only *source*'s imports: :meth:`parse_file`'s grammar, query and import pass.
+
+ Symbols, calls and references are not extracted, which is most of a
+ parse. A language without a tree-sitter path takes the full parse.
+ """
+ lang = file_info.language
+ config = LANGUAGE_CONFIGS.get(lang)
+ grammar_tag = grammar_tag_for(lang, file_info.path)
+ language = _get_language(grammar_tag)
+ signature_file = lang == "fsharp" and file_info.path.endswith(".fsi")
+ if (
+ lang in SPECIAL_HANDLER_LANGUAGES
+ or config is None
+ or language is None
+ or signature_file
+ ):
+ return self.parse_file(file_info, source).imports
+ source = prepare_source(lang, source, path=file_info.path)
+ root, _, grammar_tag, language = _parse_tree(lang, language, grammar_tag, source)
+ query = self._get_query(lang, language, grammar_tag)
+ matches = _run_query(query, root) if query is not None else []
+ src = source.decode("utf-8", errors="replace")
+ return self._extract_imports(matches, config, file_info, src)
+
def parse_file(self, file_info: FileInfo, source: bytes) -> ParsedFile:
"""Parse *source* bytes and return a fully populated ParsedFile."""
lang = file_info.language
@@ -1397,52 +1474,8 @@ class ASTParser:
original_source = source
source = prepare_source(lang, source, path=file_info.path)
- parser = Parser(language)
- tree = parser.parse(source)
+ root, parse_errors, grammar_tag, language = _parse_tree(lang, language, grammar_tag, source)
src = source.decode("utf-8", errors="replace")
- root = tree.root_node
-
- parse_errors = _collect_error_nodes(root)
-
- # Adaptive TSX grammar fallback: if a .ts file contains JSX markup,
- # tree-sitter-typescript produces ERROR nodes. Re-parse using the TSX
- # grammar ONLY IF:
- # 1. Initial parse yielded error nodes (parse_errors is non-empty)
- # 2. The file projects to TypeScript and is not already on tsx
- # 3. Source contains JSX-specific closing tokens (b"/>" or b"</")
- # A .vue render function may be written in JSX
- # (``vnodes.push(<i class={c} />)``), which is the single TS parse
- # failure across a 1,593-file .vue corpus. ``source`` here is the
- # markup-blanked projection, so the template's own ``</`` and ``/>``
- # are already spaces — the token test still keys on real JSX only.
- # The swap is strictly safe: it only takes effect when TSX yields
- # FEWER errors than the first parse.
- if (
- parse_errors
- and lang in ("typescript", "vue")
- and grammar_tag != "tsx"
- and (b"/>" in source or b"</" in source)
- ):
- tsx_language = _get_language("tsx")
- if tsx_language is not None:
- tsx_tree = Parser(tsx_language).parse(source)
- tsx_errors = _collect_error_nodes(tsx_tree.root_node)
- if len(tsx_errors) < len(parse_errors):
- tree = tsx_tree
- root = tree.root_node
- parse_errors = tsx_errors
- # Both grammar_tag AND language must be reassigned.
- # grammar_tag is consumed immediately below by
- # self._get_query(lang, language, grammar_tag), which
- # appends tsx.scm to the base typescript.scm query.
- # That append is what supplies the
- # jsx_opening_element / jsx_self_closing_element captures
- # that restore JSX component call-site edges.
- # Reassigning only ``tree`` / ``root`` would fix parse
- # errors but leave those edges missing — the dead-code
- # false-positive would remain.
- grammar_tag = "tsx"
- language = tsx_language
query = self._get_query(lang, language, grammar_tag)
diff --git a/tests/unit/analysis/test_import_drift.py b/tests/unit/analysis/test_import_drift.py
new file mode 100644
index 000000000..622f1c9e9
--- /dev/null
+++ b/tests/unit/analysis/test_import_drift.py
@@ -0,0 +1,73 @@
+"""Whether a file's edges can have moved between two versions of its text."""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.analysis.import_drift import edges_may_differ, may_carry_edges
+
+_PY = b"from a.b import (\n c,\n)\n\n\ndef f():\n return c(1)\n"
+_TS = b'import { a } from "./a";\n\nexport function f() {\n return a(1);\n}\n'
+
+
+ ("path", "before", "after"),
+ [
+ ("m.py", _PY, _PY.replace(b"c(1)", b"c(2) + 3")),
+ ("m.py", _PY, _PY + b'\nlog("done with this step")\n'),
+ ("m.ts", _TS, _TS.replace(b"a(1)", b"a(2)")),
+ ("m.ts", _TS, _TS + b"// a comment\n"),
+ ("m.py", _PY, _PY + b'\nNAMES = ["extra", "with spaces in it"]\n'),
+ ("m.go", b"package m\n\nfunc F() {}\n", b"package m\n\nfunc F() {}\n"),
+ # Reformatted, so parsed, but the same import.
+ ("m.ts", _TS, _TS.replace(b'import { a } from "./a";', b'import {\n a,\n} from "./a";')),
+ ],
+)
+def test_a_body_change_keeps_the_edges(path, before, after) -> None:
+ assert edges_may_differ(path, before, after) is False
+
+
+ ("path", "before", "after"),
+ [
+ ("m.py", _PY, _PY.replace(b" c,\n", b" c,\n d,\n")),
+ ("m.py", _PY, _PY + b"\ndef g():\n from x import y\n"),
+ ("m.py", _PY, _PY + b'\nPLUGINS = ["pkg.plugins.extra"]\n'),
+ # A bracket in a comment must not close the statement early.
+ (
+ "m.py",
+ b"from a import ( # )\n c,\n)\n",
+ b"from a import ( # )\n c,\n d,\n)\n",
+ ),
+ ("m.ts", b'import {\n a,\n} from "./a";\n', b'import {\n a,\n b,\n} from "./a";\n'),
+ ("m.ts", _TS, _TS + b'export * from "./b";\n'),
+ ("m.ts", _TS, _TS.replace(b"a(1)", b'(await import("./c")).a(1)')),
+ ("m.ts", _TS, _TS + b'vi.mock("./d");\n'),
+ ("m.go", b"package m\n", b"package m\n\nfunc F() { G() }\n"),
+ ],
+)
+def test_an_import_or_a_module_name_moves_the_edges(path, before, after) -> None:
+ assert edges_may_differ(path, before, after) is True
+
+
+def test_a_file_on_one_side_only_moves_no_route() -> None:
+ assert edges_may_differ("m.py", None, _PY) is False
+ assert edges_may_differ("m.py", _PY, None) is False
+ assert edges_may_differ("m.go", b"package m\n", b"package m\n") is False
+
+
+ ("path", "carries"),
+ [
+ ("a.py", True),
+ ("a.tsx", True),
+ ("a.go", True),
+ ("a.json", False),
+ ("a.yaml", False),
+ ("README.md", False),
+ ("app.css", False),
+ ("glossary.jsonl", False),
+ ],
+)
+def test_only_code_carries_edges(path, carries) -> None:
+ assert may_carry_edges(path) is carries
diff --git a/tests/unit/analysis/test_test_selection.py b/tests/unit/analysis/test_test_selection.py
index 9950bc190..60048494a 100644
--- a/tests/unit/analysis/test_test_selection.py
+++ b/tests/unit/analysis/test_test_selection.py
@@ -9,6 +9,7 @@ from __future__ import annotations
import pytest
from repowise.core.analysis.test_selection import (
+ MAX_INDEX_GAP,
RUN_ALL,
Selection,
SelectionInput,
@@ -16,10 +17,12 @@ from repowise.core.analysis.test_selection import (
doc_readers,
format_args,
is_runnable_test,
+ plan_gap,
plugin_loader,
resolve_runner,
runner_args,
select_tests,
+ with_rewired,
)
from repowise.core.pytest_roots import read_pytest_roots
@@ -347,7 +350,7 @@ def test_an_index_that_cannot_be_trusted_runs_everything(kwargs, start) -> None:
assert any(r.startswith(start) for r in sel.reasons), sel.reasons
-def test_an_index_built_before_other_files_changed_runs_everything() -> None:
+def test_an_index_gap_no_one_traced_runs_everything() -> None:
tiers = _tiers(inferred=[_inferred("src/a.py", "tests/test_a.py", "call-graph")])
assert _select(["src/a.py"], tiers, index_gap=["README.md"]).run_all is False
stale = _select(["src/a.py"], tiers, index_gap=["README.md", "src/b.py"])
@@ -360,6 +363,107 @@ def test_an_index_built_before_other_files_changed_runs_everything() -> None:
assert unknown.reasons[0].startswith("Cannot tell what changed since the index was built")
+# -- an index behind the base -------------------------------------------------
+
+
+def _behind(gap, inferred=(), **kwargs):
+ """src/a.py changed; the index predates *gap*, every candidate in it rewired."""
+ tiers = _tiers(inferred=[_inferred("src/a.py", "tests/test_a.py", "call-graph"), *inferred])
+ plan = plan_gap(gap, _NONE)
+ plan = with_rewired(plan, plan.candidates, kwargs.pop("indexed_files", ()))
+ return _select(["src/a.py"], tiers, index_gap=gap, gap=plan, **kwargs)
+
+
+def test_an_index_behind_the_base_adds_the_tests_reaching_the_gap() -> None:
+ sel = _behind(
+ ["src/b.py", "src/c.py", "README.md"],
+ inferred=[_inferred("src/b.py", "tests/test_b.py", "import-graph")],
+ )
+ assert sel.run_all is False
+ assert sel.test_files == ("tests/test_a.py", "tests/test_b.py")
+ # src/c.py has no test: it links none to the change, which is not a reason.
+ assert "The index predates 2 changed file(s) outside this change" in sel.reasons[0]
+
+
+ ("path", "why"),
+ [
+ ("uv.lock", "uv.lock changed after the index was built (1 such): dependencies"),
+ ("web/tsconfig.json", "build or test configuration can change any test"),
+ (".repowise/config.yaml", "Repowise's configuration changed"),
+ ],
+)
+def test_configuration_changed_after_the_index_runs_everything(path, why) -> None:
+ sel = _behind(["src/b.py", path])
+ assert sel.run_all
+ assert why in sel.reasons[0]
+ assert plan_gap(["src/b.py", path], _NONE).candidates == ()
+
+
+def test_nothing_is_traced_when_the_change_runs_everything_anyway() -> None:
+ assert plan_gap(["src/b.py"], _NONE, ["uv.lock", "src/a.py"]).candidates == ()
+ assert plan_gap(["src/b.py"], _NONE, ["src/a.py"]).candidates == ("src/b.py",)
+
+
[email protected]("path", [".github/workflows/ci.yml", "tests/fixtures/user.json"])
+def test_ci_config_and_test_data_after_the_index_cannot_reach_the_change(path) -> None:
+ assert _behind([path]).run_all is False
+ assert plan_gap([path, "src/b.py", "ui/app.css"], _NONE).candidates == ("src/b.py",)
+
+
+def test_a_production_package_init_after_the_index_stands_for_its_modules() -> None:
+ files = ["pkg/__init__.py", "pkg/sub/x.py", "pkg/y.py", "other/z.py"]
+ plan = plan_gap(["pkg/__init__.py", "other/z.py"], _NONE)
+ assert with_rewired(plan, plan.candidates, files).targets == (
+ "other/z.py",
+ "pkg/__init__.py",
+ "pkg/sub/x.py",
+ "pkg/y.py",
+ )
+ # An __init__.py whose imports did not move stands for nothing.
+ assert with_rewired(plan, ["other/z.py"], files).targets == ("other/z.py",)
+ sel = _behind(
+ ["pkg/__init__.py"],
+ inferred=[_inferred("pkg/sub/x.py", "tests/test_x.py", "import-graph")],
+ indexed_files=files,
+ )
+ assert sel.run_all is False and "tests/test_x.py" in sel.test_files
+
+
+def test_a_gap_too_large_to_trace_runs_everything() -> None:
+ gap = [f"src/m{i}.py" for i in range(MAX_INDEX_GAP + 1)]
+ sel = _behind(gap)
+ assert sel.run_all
+ assert f"more than the {MAX_INDEX_GAP} worth tracing" in sel.reasons[0]
+ assert plan_gap(gap, _NONE).candidates == ()
+
+
+def test_a_test_deleted_after_the_index_is_not_a_stale_index() -> None:
+ sel = _behind(
+ ["src/b.py", "tests/test_b.py"],
+ inferred=[_inferred("src/b.py", "tests/test_b.py", "import-graph")],
+ missing=["tests/test_b.py"],
+ )
+ assert sel.run_all is False
+ assert sel.test_files == ("tests/test_a.py",)
+
+
+def test_a_gap_route_through_an_unimported_helper_runs_everything() -> None:
+ sel = _behind(
+ ["src/b.py"], inferred=[_inferred("src/b.py", "tests/helpers.py", "import-graph")]
+ )
+ assert sel.run_all
+ assert sel.reasons[0].startswith(
+ "Changed after the index was built: src/b.py is reached through the test helper"
+ )
+
+
+def test_the_inert_gap_reasons_name_real_full_run_groups() -> None:
+ from repowise.core.analysis.test_selection import _FULL_RUN_GROUPS, _GAP_INERT
+
+ assert {why for why, _ in _FULL_RUN_GROUPS} >= _GAP_INERT
+
+
# -- the subset ---------------------------------------------------------------
diff --git a/tests/unit/cli/test_impacted_tests_cmd.py b/tests/unit/cli/test_impacted_tests_cmd.py
index 0a3685b55..6750df027 100644
--- a/tests/unit/cli/test_impacted_tests_cmd.py
+++ b/tests/unit/cli/test_impacted_tests_cmd.py
@@ -144,20 +144,33 @@ def test_no_index_runs_everything(repo) -> None:
assert "No index" in _err(result)
-def test_an_index_behind_the_base_runs_everything(repo) -> None:
- # main moves past the indexed commit, and the branch is rebased onto it.
+def _move_main(repo, files: dict[str, str]) -> None:
+ """main moves past the indexed commit, and the branch is rebased onto it."""
_git(repo, "switch", "-q", "main")
- _write(repo, {"src/b.py": "B = 2\n"})
- _git(repo, "commit", "-qam", "main moves")
+ _write(repo, files)
+ _git(repo, "add", "-A")
+ _git(repo, "commit", "-qm", "main moves")
_git(repo, "switch", "-q", "feat")
_git(repo, "rebase", "-q", "main")
+
+
+def test_an_index_behind_the_base_adds_the_tests_reaching_the_gap(repo) -> None:
+ # src/c.py was added since, so no test in the index reaches it.
+ _move_main(repo, {"src/b.py": "import os\n\nB = 2\n", "src/c.py": "C = 1\n"})
result = _run(repo, "main...feat", "--format", "args")
- assert result.stdout == ":all\n"
- assert "The index predates 1 changed file(s) outside this change (e.g. src/b.py)" in (
+ assert result.stdout == "tests/test_a.py\n"
+ assert "The index predates 1 changed file(s) outside this change; the tests reaching" in (
_err(result)
)
+def test_a_lockfile_changed_after_the_index_runs_everything(repo) -> None:
+ _move_main(repo, {"src/b.py": "B = 2\n", "uv.lock": "lock\n"})
+ result = _run(repo, "main...feat", "--format", "args")
+ assert result.stdout == ":all\n"
+ assert "uv.lock changed after the index was built (1 such): dependencies" in _err(result)
+
+
def test_in_ci_the_default_change_is_the_pull_requests(repo) -> None:
# Nothing is staged, so the local default would select nothing at all.
result = _run(repo, "--format", "args", env={"CI": "true"})
@@ -281,3 +294,39 @@ def test_a_changed_test_package_init_selects_the_tests_under_it(repo) -> None:
assert data["run_all"] is False, data["reasons"]
assert data["selected"]["basis"]["tests/__init__.py"] == "test-package"
assert data["args"] == ["tests/test_a.py"]
+
+
+async def _add_to_index(root: Path, test: str, source: str) -> None:
+ """*test* imports *source* in the graph, and *source* has health metrics."""
+ from repowise.core.persistence.models import GraphEdge, GraphNode, HealthFileMetric
+
+ db = root / ".repowise" / "wiki.db"
+ engine = create_async_engine(f"sqlite+aiosqlite:///{db.as_posix()}")
+ async with async_sessionmaker(engine, expire_on_commit=False)() as session:
+ session.add(GraphNode(repository_id="r1", node_id=test, node_type="file", is_test=True))
+ session.add(
+ GraphEdge(
+ repository_id="r1", source_node_id=test, target_node_id=source, edge_type="imports"
+ )
+ )
+ session.add(HealthFileMetric(repository_id="r1", file_path=source))
+ await session.commit()
+ await engine.dispose()
+
+
+ ("b_after", "expected"),
+ [
+ ("B = 2\n", "tests/test_a.py\n"),
+ ("import os\n\nB = 2\n", "tests/test_a.py tests/test_b.py\n"),
+ ],
+)
+def test_only_files_whose_imports_moved_since_the_index_add_their_tests(
+ repo, b_after, expected
+) -> None:
+ _land_on_main(repo, {"tests/test_b.py": "from src.b import B\n\ndef test_b():\n assert B\n"})
+ asyncio.run(_add_to_index(repo, "tests/test_b.py", "src/b.py"))
+ _move_main(repo, {"src/b.py": b_after})
+ result = _run(repo, "main...feat", "--format", "args", "--runner", "pytest")
+ assert result.exit_code == 0, result.output
+ assert result.stdout == expected