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): give each runner only its own always-run tests and name the rest (#3241)

Raghav Chamadiya6e819cc05c01
diff --git a/docs/start/CI.md b/docs/start/CI.md
index 715cc091a..fbcda488f 100644
--- a/docs/start/CI.md
+++ b/docs/start/CI.md
@@ -260,7 +260,8 @@ a per-test coverage map (`repowise coverage add`) makes it more precise.
- 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`.
- A changed `conftest.py`, test package `__init__.py` or imported test helper selects the tests under or importing it, not everything.
- Without a range, in CI it reads the pull request's change; locally, the staged changes.
-- `tests.full_run_on` (gitignore patterns) adds run-everything paths; `tests.always_run` is appended to every selection as written.
+- `tests.full_run_on` (gitignore patterns) adds run-everything paths; `tests.always_run` is appended to every selection as written, for the runner whose language each entry is in.
+- With `--runner pytest`, `go` or `jest`, the arguments hold only that runner's tests, `tests.always_run` included. A repository with tests in several languages needs one job per runner: the tests a job leaves out are counted on stderr, and listed per runner under `left_out` with `--format json`. `--runner files` gets every test.
- `--explain <test>` says why one test file was or was not selected: the first changed file that reached it, the evidence (coverage, call graph, import graph) and the import route from the test to that file when there is one, or the rule that runs it. With `--format json` the same answer is added as `explain`, and `selected.why` holds it for every selected test.
- Ceiling: on the JVM and .NET, a same-package test linked only by an unresolved call is missed. So is a test that reaches the change through a plugin or registry loaded by a string name, a tree walk or child process done inside a shared helper it calls, or one in another language: list such tests in `tests.always_run`, and run the full suite on the default branch.
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 07a46312c..3e10a4618 100644
--- a/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py
@@ -688,11 +688,18 @@ def _render_explain(result: dict, test: str) -> None:
def _render_selection(result: dict, fmt: str, runner: str, explain: str | None = None) -> None:
"""``--format args`` (one line, reasons on stderr) or ``--format json``."""
- from repowise.core.analysis.test_selection import format_args, resolve_runner, runner_args
+ from repowise.core.analysis.test_selection import (
+ format_args,
+ left_out,
+ resolve_runner,
+ runner_args,
+ runner_notes,
+ )
selection = result["selection"]
resolved = resolve_runner(selection, runner)
args = runner_args(selection, resolved)
+ notes = runner_notes(selection, resolved)
if fmt == "args":
click.echo(format_args(args))
# Plain stderr, one reason per line: a CI log must not wrap or style them.
@@ -700,12 +707,12 @@ def _render_selection(result: dict, fmt: str, runner: str, explain: str | None =
click.echo("Run every test:", err=True)
else:
click.echo(f"{len(args)} argument(s) for {resolved}.", err=True)
- for reason in selection.reasons:
+ for reason in (*selection.reasons, *notes):
click.echo(f" {reason}", err=True)
return
selected = selection.to_dict()
- run_all, reasons = selected.pop("run_all"), selected.pop("reasons")
+ run_all, reasons = selected.pop("run_all"), selected.pop("reasons") + notes
extra = {"explain": _explanation(result, explain)} if explain else {}
emit_json(
{
@@ -731,6 +738,7 @@ def _render_selection(result: dict, fmt: str, runner: str, explain: str | None =
"selected": selected,
"runner": resolved,
"args": args,
+ "left_out": left_out(selection, resolved),
**extra,
}
)
diff --git a/packages/core/src/repowise/core/analysis/test_selection.py b/packages/core/src/repowise/core/analysis/test_selection.py
index 7d7425668..1cda446ad 100644
--- a/packages/core/src/repowise/core/analysis/test_selection.py
+++ b/packages/core/src/repowise/core/analysis/test_selection.py
@@ -1059,8 +1059,17 @@ def _node_id(test_id: str, test_file: str) -> str:
return f"{test_file}::{test_id.split('::', 1)[1]}"
+# The test files each runner takes; ``files`` takes every one.
+_RUNNER_SUFFIXES = {"pytest": _PYTHON, "go": (".go",), "jest": _JS}
+
+
+def _takes(runner: str, test: str) -> bool:
+ """Whether *test* (a file or node id) is in *runner*'s language; case-blind."""
+ return test.split("::", 1)[0].lower().endswith(_RUNNER_SUFFIXES[runner])
+
+
def _all_python(files: tuple[str, ...], packages: tuple[str, ...]) -> bool:
- return bool(files) and all(f.endswith(_PYTHON) for f in files)
+ return bool(files) and all(_takes("pytest", f) for f in files)
def _all_go(files: tuple[str, ...], packages: tuple[str, ...]) -> bool:
@@ -1069,7 +1078,7 @@ def _all_go(files: tuple[str, ...], packages: tuple[str, ...]) -> bool:
def _all_js(files: tuple[str, ...], packages: tuple[str, ...]) -> bool:
- return bool(files) and all(f.endswith(_JS) for f in files)
+ return bool(files) and all(_takes("jest", f) for f in files)
# ``auto`` picks the first runner every selected test file belongs to.
@@ -1085,7 +1094,7 @@ def resolve_runner(selection: Selection, runner: str) -> str:
def _pytest_args(selection: Selection) -> list[str]:
- return [t for t in selection.tests if t.split("::", 1)[0].endswith(_PYTHON)]
+ return [t for t in selection.tests if _takes("pytest", t)]
def _go_args(selection: Selection) -> list[str]:
@@ -1095,7 +1104,7 @@ def _go_args(selection: Selection) -> list[str]:
def _jest_args(selection: Selection) -> list[str]:
- return [f for f in selection.test_files if f.endswith(_JS)]
+ return [f for f in selection.test_files if _takes("jest", f)]
def _file_args(selection: Selection) -> list[str]:
@@ -1109,13 +1118,55 @@ def runner_args(selection: Selection, runner: str) -> list[str]:
"""Arguments for *runner* (already resolved); ``[RUN_ALL]`` for a full run.
Each runner gets only the tests it can run, so a job per language can share
- one selection. ``tests.always_run`` entries are appended as written. jest
- and vitest get file paths, meant for ``--runTestsByPath``.
+ one selection; :func:`runner_notes` names the rest. ``tests.always_run``
+ entries are appended as written, but one in another runner's language is
+ left to that runner. jest and vitest get file paths, meant for
+ ``--runTestsByPath``.
"""
if selection.run_all:
return [RUN_ALL]
args = _RUNNER_ARGS.get(runner, _file_args)(selection)
- return list(dict.fromkeys([*args, *selection.always_run]))
+ always = [t for t in selection.always_run if _runs(t, runner)]
+ return list(dict.fromkeys([*args, *always]))
+
+
+def _runs(test: str, runner: str) -> bool:
+ """Whether *runner* can run *test*; a directory or glob it cannot tell is kept."""
+ if runner not in _RUNNER_SUFFIXES or not is_code_file(test.split("::", 1)[0]):
+ return True
+ return _takes(runner, test)
+
+
+def left_out(selection: Selection, runner: str) -> dict[str, list[str]]:
+ """Selected tests *runner*'s arguments leave out, keyed by the runner that takes them.
+
+ A test no named runner takes (Kotlin, Swift) is keyed ``files``. Empty on a
+ full run, where every runner runs everything of its own.
+ """
+ if selection.run_all:
+ return {}
+ out: dict[str, list[str]] = {}
+ for test in dict.fromkeys((*selection.test_files, *selection.always_run)):
+ if not _runs(test, runner):
+ owner = next((r for r in _RUNNER_SUFFIXES if _takes(r, test)), "files")
+ out.setdefault(owner, []).append(test)
+ return out
+
+
+def runner_notes(selection: Selection, runner: str) -> list[str]:
+ """One line naming what :func:`left_out` found, so nothing is dropped silently.
+
+ A pipeline running one runner needs a job per runner for these.
+ """
+ left = left_out(selection, runner)
+ if not left:
+ return []
+ counts = ", ".join(f"{len(tests)} for {owner}" for owner, tests in sorted(left.items()))
+ first = next(iter(left.values()))[0]
+ return [
+ f"Selected tests for other runners are not in these {runner} arguments ({counts}; "
+ f"e.g. {first}); run them in a job per runner."
+ ]
def format_args(args: Iterable[str]) -> str:
diff --git a/tests/unit/analysis/test_test_selection.py b/tests/unit/analysis/test_test_selection.py
index 966baf51c..10e37ef9d 100644
--- a/tests/unit/analysis/test_test_selection.py
+++ b/tests/unit/analysis/test_test_selection.py
@@ -17,10 +17,12 @@ from repowise.core.analysis.test_selection import (
doc_readers,
format_args,
is_runnable_test,
+ left_out,
plan_gap,
plugin_loader,
resolve_runner,
runner_args,
+ runner_notes,
select_tests,
with_rewired,
)
@@ -827,3 +829,38 @@ def test_a_test_named_module_pytest_does_not_collect_is_not_runnable() -> None:
assert is_runnable_test("src/pkg/tests/test_b.py", roots)
# With no config the name decides, as it does for pytest.
assert is_runnable_test("src/pkg/test_paths.py")
+
+
+def test_a_runner_gets_only_its_own_always_run_tests_and_hears_of_the_rest() -> None:
+ config = TestSelectionConfig(
+ always_run=("tests/test_smoke.py", "apps/android/FooTest.kt", "e2e/")
+ )
+ tiers = _tiers(inferred=[_inferred("src/b.py", "tests/test_b.py", "call-graph")])
+ sel = _select(["src/b.py"], tiers, config=config, unplaced_tests=["ui/a.test.ts"])
+ assert runner_args(sel, "pytest") == ["tests/test_b.py", "tests/test_smoke.py", "e2e/"]
+ assert runner_args(sel, "jest") == ["ui/a.test.ts", "e2e/"]
+ assert runner_args(sel, "files") == [
+ "tests/test_b.py",
+ "ui/a.test.ts",
+ "tests/test_smoke.py",
+ "apps/android/FooTest.kt",
+ "e2e/",
+ ]
+ assert left_out(sel, "pytest") == {"files": ["apps/android/FooTest.kt"], "jest": ["ui/a.test.ts"]}
+ assert runner_notes(sel, "pytest") == [
+ "Selected tests for other runners are not in these pytest arguments (1 for files, "
+ "1 for jest; e.g. ui/a.test.ts); run them in a job per runner."
+ ]
+ # Tests the indexer found walking the tree are split the same way.
+ walked = _select(
+ ["src/b.py"], tiers, known_tests=["tests/test_b.py", "app/LintTest.kt"],
+ always_run_tests={"app/LintTest.kt": "it lists and reads files under a source directory"},
+ )
+ assert left_out(walked, "pytest") == {"files": ["app/LintTest.kt"]}
+ assert runner_args(walked, "pytest") == ["tests/test_b.py"]
+ # Case-blind: an upper-case extension is still the runner's own.
+ upper = _select(["src/b.py"], tiers, unplaced_tests=["ui/B.TEST.TS", "tests/TEST_C.PY"])
+ assert runner_args(upper, "jest") == ["ui/B.TEST.TS"]
+ assert runner_args(upper, "pytest") == ["tests/test_b.py", "tests/TEST_C.PY"]
+ assert runner_notes(sel, "files") == []
+ assert runner_notes(_select(["uv.lock"], config=config), "pytest") == []
diff --git a/tests/unit/cli/test_impacted_tests_cmd.py b/tests/unit/cli/test_impacted_tests_cmd.py
index 6df2d6fab..a9a475dcb 100644
--- a/tests/unit/cli/test_impacted_tests_cmd.py
+++ b/tests/unit/cli/test_impacted_tests_cmd.py
@@ -431,3 +431,17 @@ def test_explain_joins_the_json_report(repo) -> None:
"route": ["tests/test_a.py", "src/a.py"],
}
assert data["selected"]["why"] == {"tests/test_a.py": "src/a.py changed (import-graph)"}
+
+
+def test_a_runner_is_told_of_always_run_tests_for_another(repo) -> None:
+ (repo / ".repowise" / "config.yaml").write_text(
+ "tests:\n always_run: [web/app.test.ts]\n", encoding="utf-8"
+ )
+ result = _run(repo, "main...feat", "--format", "args", "--runner", "pytest")
+ assert result.exit_code == 0, result.output
+ assert result.stdout == "tests/test_a.py\n"
+ assert "not in these pytest arguments (1 for jest; e.g. web/app.test.ts)" in _err(result)
+ data = json.loads(_run(repo, "main...feat", "--format", "json", "--runner", "pytest").stdout)
+ assert data["left_out"] == {"jest": ["web/app.test.ts"]}
+ files = _run(repo, "main...feat", "--format", "args", "--runner", "files")
+ assert files.stdout == "tests/test_a.py web/app.test.ts\n"