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(refactoring): test commands for Go, Rust, JVM and Ruby plans (#3585)

Raghav Chamadiyad63e6f8d1bd0
diff --git a/docs/layers/REFACTORING.md b/docs/layers/REFACTORING.md
index ad1ee49b4..0318ff02a 100644
--- a/docs/layers/REFACTORING.md
+++ b/docs/layers/REFACTORING.md
@@ -73,6 +73,10 @@ but none reaches the changed function.
A JavaScript or TypeScript test runs from the package whose `package.json`
declares its `test` script (`npm --prefix ui-tui test -- src/a.test.ts`), and a
test that no package can run gets no command.
+Go, Rust, Java, Kotlin and Ruby tests get a command too, for the package that owns
+them: `go test ./internal/cart`, `cargo test -p parser`, `./mvnw -pl core -am test`
+or `./gradlew :app:test`, and `bundle exec rspec` or `bundle exec rake test`. A test
+whose build file or runner the repository does not have gets no command.
In a plan with several steps, such as a performance fix at several call sites,
plan detail adds a `verify` to each step whose tests differ from the plan's: the
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/batches.py b/packages/core/src/repowise/core/analysis/health/refactoring/batches.py
index cb5d6f857..517fa7fd1 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/batches.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/batches.py
@@ -74,10 +74,14 @@ def stage_batches(
return None
lead = [s["n"] for s in steps if s["n"] < numbers[0]]
tests, total = _batch_tests(detail)
- # Each JS/TS test runs from the package the plan stored for it.
+ # Each test runs from the package the plan stored for it.
packages = _validation(detail).get("js_packages")
+ runs = _validation(detail).get("test_runs")
commands = commands_for_tests(
- tests, total=total, packages=packages if isinstance(packages, Mapping) else None
+ tests,
+ total=total,
+ packages=packages if isinstance(packages, Mapping) else None,
+ runs=runs if isinstance(runs, Mapping) else None,
)
check = _check(tests, detail)
sizes = _sizes(len(numbers))
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/commands.py b/packages/core/src/repowise/core/analysis/health/refactoring/commands.py
index 1c521e185..742413433 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/commands.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/commands.py
@@ -3,18 +3,40 @@
from __future__ import annotations
import json
+import posixpath
import re
-from collections.abc import Iterable, Mapping
+from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence
+from dataclasses import dataclass, field
+from functools import cached_property
from pathlib import Path
+from typing import Any
+from ....ingestion.external_systems.maven_model import load_maven_reactor
from ....ingestion.package_roots import owning_package
+from ....ingestion.resolvers.jvm_gradle import parse_settings
+from ....ingestion.workspace_members import (
+ declared_workspace_members,
+ manifest_package_name,
+ read_toml,
+)
from ...test_selection import JS_SUFFIXES, PYTHON_SUFFIXES
#: Each JS/TS test file's package: the directory (``""`` is the repository
#: root) whose package.json declares a ``test`` script, or None when no
#: package.json above the file does. A file the map does not hold is None too.
+#: JS keeps this map because it predates :data:`TestRuns`; folding it in is a
+#: follow-up that must keep reading plans stored with it.
JsPackages = Mapping[str, str | None]
+#: Each test (an id, or the file a capped list collapses to) of a language in
+#: :data:`_RUNNERS`, mapped to ``[run, arg]``: the command that runs its package
+#: from the repository root, and the argument narrowing it to the test's target,
+#: or None to run the whole package. Args of one run are joined with spaces,
+#: except ``--flag=value`` args, whose values merge into one ``--flag "a|b"``
+#: for a runner that keeps only its last such flag. A test the map does not hold
+#: gets no command.
+TestRuns = Mapping[str, Sequence[str | None]]
+
def _file(test: str) -> str:
return test.split("::", 1)[0]
@@ -31,6 +53,65 @@ def _arg(path: str) -> str:
return f'"{path}"' if re.search(r"\s", path) and not re.search(r'["$`]', path) else path
+def _relative(path: str, owner: str) -> str:
+ return path[len(owner) + 1 :] if owner else path
+
+
+@dataclass
+class _Checkout:
+ """One repository's package manifests, each extra file read at most once."""
+
+ root: Path
+ manifests: Mapping[str, frozenset[str]]
+ _memo: dict[tuple[Hashable, ...], Any] = field(default_factory=dict)
+
+ def memo(self, read: Callable[[Path], Any], path: Path) -> Any:
+ """*read* of *path*, once per checkout: every test of a package asks again."""
+ key = (read, path)
+ if key not in self._memo:
+ self._memo[key] = read(path)
+ return self._memo[key]
+
+ def has(self, directory: str, name: str) -> bool:
+ return self.memo(Path.is_file, self.root / directory / name)
+
+ def owner(
+ self, path: str, name: str, accepts: Callable[[str], bool] | None = None
+ ) -> str | None:
+ # The scan leaves the root manifest out, so the default checks the disk.
+ return owning_package(
+ path, self.manifests, name, accepts or (lambda directory: self.has(directory, name))
+ )
+
+ @cached_property
+ def cargo_members(self) -> set[str]:
+ return declared_workspace_members(self.root).get("Cargo.toml", set())
+
+ @cached_property
+ def maven_reactor(self) -> frozenset[str]:
+ """Module directories the root POM reaches through ``<modules>``."""
+ poms = [self.root / "pom.xml"] + [
+ self.root / directory / "pom.xml"
+ for directory, names in self.manifests.items()
+ if "pom.xml" in names
+ ]
+ reactor = load_maven_reactor(self.root, poms).declared_manifests
+ return frozenset(posixpath.dirname(manifest) for manifest in reactor)
+
+ @cached_property
+ def gradle_projects(self) -> dict[str, str]:
+ """Each project directory the root settings file includes, to its project path."""
+ for name in ("settings.gradle.kts", "settings.gradle"):
+ if self.has("", name):
+ modules, overrides = parse_settings(self.root / name)
+ projects: dict[str, str] = {}
+ for module in modules:
+ directory = overrides.get(module) or module.replace(":", "/")
+ projects[posixpath.normpath(directory)] = f":{module}"
+ return projects
+ return {}
+
+
def _declares_test(manifest: Path) -> bool:
try:
scripts = json.loads(manifest.read_text(encoding="utf-8")).get("scripts")
@@ -48,15 +129,13 @@ def js_test_packages(
root (*manifests*, from :func:`scan_package_manifests`) whose package.json
declares a test script. Blocking file reads: an async caller uses a thread.
"""
- declares: dict[str, bool] = {}
+ repo = _Checkout(root, manifests)
def accepts(directory: str) -> bool:
- if directory not in declares:
- declares[directory] = _declares_test(root / directory / "package.json")
- return declares[directory]
+ return repo.memo(_declares_test, root / directory / "package.json")
return {
- path: owning_package(path, manifests, "package.json", accepts)
+ path: repo.owner(path, "package.json", accepts)
for path in {_file(test) for test in tests if _is_js(test)}
}
@@ -69,6 +148,219 @@ def plan_packages(tests: Iterable[str], packages: JsPackages | None) -> dict[str
return {path: packages.get(path) for path in {_file(test) for test in tests if _is_js(test)}}
+Run = tuple[str, str | None]
+
+
+def _go_run(repo: _Checkout, test: str) -> Run | None:
+ """``go test`` on the test's package directory, inside the module owning it."""
+ path = _file(test)
+ module = repo.owner(path, "go.mod")
+ if module is None:
+ return None
+ package = posixpath.dirname(_relative(path, module))
+ run = f"go -C {_arg(module)} test" if module else "go test"
+ return run, _arg(f"./{package}" if package else ".")
+
+
+def _declared_target(data: Mapping[str, Any], rel: str) -> str | None:
+ """The ``[[test]]`` target of the crate's manifest *data* whose root is *rel*."""
+ declared = data.get("test")
+ for target in declared if isinstance(declared, list) else ():
+ name = target.get("name") if isinstance(target, dict) else None
+ if not isinstance(name, str):
+ continue
+ if posixpath.normpath(target.get("path") or f"tests/{name}.rs") == rel:
+ return name
+ return None
+
+
+def _discovered_target(rel: str) -> str | None:
+ """The target Cargo makes of ``tests/<name>.rs`` or ``tests/<name>/main.rs``."""
+ parts = rel.split("/")
+ if len(parts) == 2 and parts[0] == "tests" and parts[1].endswith(".rs"):
+ return parts[1][:-3]
+ return parts[1] if len(parts) == 3 and parts[0] == "tests" and parts[2] == "main.rs" else None
+
+
+def _rust_test_target(data: Mapping[str, Any], rel: str) -> str | None:
+ """The integration-test target whose root is *rel* (relative to its crate)."""
+ package = data.get("package")
+ autotests = not (isinstance(package, dict) and package.get("autotests") is False)
+ return _declared_target(data, rel) or (_discovered_target(rel) if autotests else None)
+
+
+def _rust_run(repo: _Checkout, test: str) -> Run | None:
+ """``cargo test`` on the crate owning the test; a virtual workspace root is no crate."""
+ path = _file(test)
+
+ def name(directory: str) -> str | None:
+ return repo.memo(manifest_package_name, repo.root / directory / "Cargo.toml")
+
+ crate = repo.owner(path, "Cargo.toml", lambda directory: name(directory) is not None)
+ if crate is None:
+ return None
+ data = repo.memo(read_toml, repo.root / crate / "Cargo.toml")
+ if not crate or crate in repo.cargo_members:
+ run = f"cargo test -p {name(crate)}"
+ elif "workspace" in data or "workspace" not in repo.memo(read_toml, repo.root / "Cargo.toml"):
+ # ``-p`` finds only crates of the workspace at the root; any other needs its manifest.
+ run = f"cargo test --manifest-path {_arg(crate + '/Cargo.toml')}"
+ else:
+ # Cargo refuses a crate inside a workspace that does not list it (or excludes it).
+ return None
+ target = _rust_test_target(data, _relative(path, crate))
+ return run, f"--test {target}" if target else None
+
+
+def _maven_run(repo: _Checkout, module: str) -> Run:
+ # Ceiling: ``./mvnw`` is the sh spelling; cmd runs ``mvnw.cmd``, like ``&&`` in _ruby_run.
+ mvn = "./mvnw" if repo.has("", "mvnw") else "mvn"
+ if not module:
+ return f"{mvn} test", None
+ if module in repo.maven_reactor:
+ # ``-am`` builds the modules it depends on; bare ``-pl`` would look for
+ # them in the local repository and fail on a fresh checkout.
+ return f"{mvn} -pl {_arg(module)} -am test", None
+ return f"{mvn} -f {_arg(module + '/pom.xml')} test", None
+
+
+def _gradle_run(repo: _Checkout, module: str) -> Run | None:
+ gradle = "./gradlew" if repo.has("", "gradlew") else "gradle"
+ if not module:
+ return f"{gradle} test", None
+ project = repo.gradle_projects.get(module)
+ return (f"{gradle} {project}:test", None) if project else None
+
+
+_GRADLE_SCRIPTS = ("build.gradle.kts", "build.gradle")
+
+
+def _jvm_run(repo: _Checkout, test: str) -> Run | None:
+ """Maven or Gradle on the deepest module owning the test."""
+ path = _file(test)
+ maven = repo.owner(path, "pom.xml")
+ gradle = max(
+ (owner for name in _GRADLE_SCRIPTS if (owner := repo.owner(path, name)) is not None),
+ key=len,
+ default=None,
+ )
+ # Both own the same directory when a repo ships both builds; Maven then runs.
+ if maven is not None and (gradle is None or len(maven) >= len(gradle)):
+ return _maven_run(repo, maven)
+ return _gradle_run(repo, gradle) if gradle is not None else None
+
+
+_RAKE_TEST = re.compile(r"""TestTask|\btask\s*\(?\s*:?['"]?test\b""")
+_RSPEC = re.compile(r"""\brspec\b""")
+
+
+def _declares_rake_test(rakefile: Path) -> bool:
+ try:
+ return bool(_RAKE_TEST.search(rakefile.read_text(encoding="utf-8", errors="ignore")))
+ except OSError:
+ return False
+
+
+def _uses_rspec(directory: Path) -> bool:
+ """Whether the gem in *directory* runs RSpec: a ``.rspec`` file, or rspec
+ named in its Gemfile or gemspec."""
+ if (directory / ".rspec").is_file():
+ return True
+ for manifest in [directory / "Gemfile", *directory.glob("*.gemspec")]:
+ try:
+ if _RSPEC.search(manifest.read_text(encoding="utf-8", errors="ignore")):
+ return True
+ except OSError:
+ continue
+ return False
+
+
+def _ruby_run(repo: _Checkout, test: str) -> Run | None:
+ """RSpec for a spec file of a gem that uses RSpec, else the Rakefile's test task."""
+ path = _file(test)
+ owner = repo.owner(path, "Gemfile")
+ if owner is None:
+ return None
+ rel = _relative(path, owner)
+ # Ceiling: ``&&`` fails in Windows PowerShell 5.1, as quoting does in _arg.
+ cd = f"cd {_arg(owner)} && " if owner else ""
+ if rel.startswith("spec/") and repo.memo(_uses_rspec, repo.root / owner):
+ # A spec helper is no example file, so the whole suite runs.
+ return f"{cd}bundle exec rspec", _arg(rel) if rel.endswith("_spec.rb") else None
+ if repo.memo(_declares_rake_test, repo.root / owner / "Rakefile"):
+ return f"{cd}bundle exec rake test", None
+ return None
+
+
+#: Test-file suffixes and the function naming the run, per language whose
+#: runner works per package. A runner gets the whole test id, ``::`` name
+#: included, so one that can select a single test can narrow to it.
+_RUNNERS: tuple[tuple[tuple[str, ...], Callable[[_Checkout, str], Run | None]], ...] = (
+ ((".go",), _go_run),
+ ((".rs",), _rust_run),
+ ((".java", ".kt"), _jvm_run),
+ ((".rb",), _ruby_run),
+)
+
+
+def runs_for_tests(
+ root: Path, manifests: Mapping[str, frozenset[str]], tests: Iterable[str]
+) -> dict[str, list[str | None]]:
+ """The run (:data:`TestRuns`) of each test in *tests* whose language is in
+ :data:`_RUNNERS`, from the package roots in *manifests*. A test whose runner
+ or manifest the repository lacks is left out. Blocking file reads.
+ """
+ repo = _Checkout(root, manifests)
+ runs: dict[str, list[str | None]] = {}
+ for test in sorted(set(tests)):
+ lowered = _file(test).lower()
+ runner = next((run for suffixes, run in _RUNNERS if lowered.endswith(suffixes)), None)
+ if runner is not None and (found := runner(repo, test)) is not None:
+ runs[test] = list(found)
+ return runs
+
+
+def _run_key(runs: TestRuns, test: str) -> str | None:
+ """The key *runs* holds for *test*: the test itself, else its file."""
+ return test if test in runs else _file(test) if _file(test) in runs else None
+
+
+def plan_runs(tests: Iterable[str], runs: TestRuns | None) -> dict[str, list[str | None]]:
+ """The part of *runs* a plan's *tests* use, stored like :func:`plan_packages`."""
+ if runs is None:
+ return {}
+ keys = {key for test in tests if (key := _run_key(runs, test)) is not None}
+ return {key: list(runs[key]) for key in keys}
+
+
+def _merged(args: Iterable[str]) -> list[str]:
+ """*args* with every ``--flag=value`` sharing a flag merged into one
+ ``--flag "a|b"``; any other arg is kept as is."""
+ plain: set[str] = set()
+ valued: dict[str, set[str]] = {}
+ for arg in args:
+ flag, equals, value = arg.partition("=")
+ if equals and flag.startswith("-") and " " not in flag:
+ valued.setdefault(flag, set()).add(value)
+ else:
+ plain.add(arg)
+ merged = [f'{flag} "{"|".join(sorted(values))}"' for flag, values in sorted(valued.items())]
+ return sorted(plain) + merged
+
+
+def _package_commands(tests: list[str], runs: TestRuns) -> list[str]:
+ """One command per run naming every target its tests need; a test that
+ needs its whole package widens the run to the whole package."""
+ groups: dict[str, set[str] | None] = {}
+ for key in {key for test in tests if (key := _run_key(runs, test)) is not None}:
+ run, arg = runs[key]
+ if arg is None:
+ groups[run] = None
+ elif (args := groups.setdefault(run, set())) is not None:
+ args.add(arg)
+ return [" ".join([run, *_merged(args or ())]) for run, args in sorted(groups.items())]
+
+
def _js_commands(tests: list[str], packages: JsPackages | None) -> list[str]:
"""``npm test`` from the package each test belongs to; a test no package
can run gets no command. Without *packages* every test runs from the root."""
@@ -85,7 +377,11 @@ def _js_commands(tests: list[str], packages: JsPackages | None) -> list[str]:
def commands_for_tests(
- tests: list[str], *, total: int | None = None, packages: JsPackages | None = None
+ tests: list[str],
+ *,
+ total: int | None = None,
+ packages: JsPackages | None = None,
+ runs: TestRuns | None = None,
) -> list[str]:
"""A command that runs at least everything the plan says validates it.
@@ -98,8 +394,10 @@ def commands_for_tests(
its tests: with none found, a bare ``pytest`` or ``npm test`` would run a
whole suite as if it guarded the change. A JS/TS test runs from the package
whose ``test`` script owns it (*packages*), with its path relative to that
- package; a test no package.json can run gets no command. A language other
- than Python or JS/TS gets none in place of a guess that would fail when run.
+ package; a test no package.json can run gets no command. A Go, Rust, JVM or
+ Ruby test runs its package, or the narrower target *runs* names for it. Any
+ other test, or one *runs* does not hold, gets none in place of a guess that
+ would fail when run.
"""
if total is not None and total > len(tests):
tests = sorted({_file(test) for test in tests})
@@ -109,7 +407,17 @@ def commands_for_tests(
if python_tests:
commands.append("pytest " + " ".join(map(_arg, python_tests)))
commands.extend(_js_commands(js_tests, packages))
+ if runs:
+ commands.extend(_package_commands(tests, runs))
return commands
-__all__ = ["JsPackages", "commands_for_tests", "js_test_packages", "plan_packages"]
+__all__ = [
+ "JsPackages",
+ "TestRuns",
+ "commands_for_tests",
+ "js_test_packages",
+ "plan_packages",
+ "plan_runs",
+ "runs_for_tests",
+]
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
index 263913560..d4cda67fa 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
@@ -41,7 +41,7 @@ from repowise.core.code_origin import ship_rank
from repowise.core.test_paths import is_test_support_path, names_test_for, paired_test_names
from .annotations import PlanAnnotations, partner_rows
-from .commands import JsPackages, commands_for_tests, plan_packages
+from .commands import JsPackages, TestRuns, commands_for_tests, plan_packages, plan_runs
from .models import RefactoringSuggestion
from .render import list_plan
@@ -312,6 +312,8 @@ class ValidationPlan:
# The package each shown JS/TS test runs from (``commands.JsPackages``),
# so a later read builds the same commands without a checkout.
js_packages: dict[str, str | None] = field(default_factory=dict)
+ # How each shown Go, Rust, JVM or Ruby test runs (``commands.TestRuns``).
+ test_runs: dict[str, list[str | None]] = field(default_factory=dict)
def as_dict(self) -> dict[str, Any]:
return {
@@ -327,6 +329,7 @@ class ValidationPlan:
"reasons": self.reasons,
"prerequisite": self.prerequisite,
**({"js_packages": self.js_packages} if self.js_packages else {}),
+ **({"test_runs": self.test_runs} if self.test_runs else {}),
}
@@ -345,6 +348,7 @@ class ValidationEvidence:
``hubs`` are the files no reach walk passes through (:func:`hub_files`), and
``siblings`` each directory's files, which decide whose test a name is.
``js_packages`` is the package each JS/TS test runs from; absent, from the root.
+ ``test_runs`` is how each Go, Rust, JVM or Ruby test runs; absent, it gets no command.
"""
symbols: Mapping[str, Sequence[SymbolSpan]] = field(default_factory=dict)
@@ -354,6 +358,7 @@ class ValidationEvidence:
hubs: frozenset[str] = frozenset()
siblings: Mapping[str, tuple[str, ...]] = field(default_factory=dict)
js_packages: JsPackages | None = None
+ test_runs: TestRuns | None = None
@dataclass(frozen=True, slots=True)
@@ -913,12 +918,16 @@ def build_validation_plan(
# Each quoted test's file, whole: shorter than naming every function,
# and it runs at least the tests the plan quotes.
commands = commands_for_tests(
- sorted({test.split("::", 1)[0] for test in shown}), packages=facts.js_packages
+ sorted({test.split("::", 1)[0] for test in shown}),
+ packages=facts.js_packages,
+ runs=facts.test_runs,
)
else:
shown = ordered_tests[:cap]
reasons = {test: best[test][1] for test in shown}
- commands = commands_for_tests(shown, total=aggregate_total, packages=facts.js_packages)
+ commands = commands_for_tests(
+ shown, total=aggregate_total, packages=facts.js_packages, runs=facts.test_runs
+ )
files = affected_files(suggestion)
return ValidationPlan(
basis=aggregate_basis,
@@ -932,6 +941,7 @@ def build_validation_plan(
targets=target_rows,
reasons=reasons if order_tests else {},
js_packages=plan_packages(shown, facts.js_packages),
+ test_runs=plan_runs(shown, facts.test_runs),
# Also when tests reach the file but none reaches a function it changes.
prerequisite=(
_characterization_step(suggestion)
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py b/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py
index cd728d7a8..d2b7e93cf 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py
@@ -131,7 +131,7 @@ class JvmGradleIndex:
# Settings parsing
# ---------------------------------------------------------------------------
-def _parse_settings(settings_file: Path) -> tuple[list[str], dict[str, str]]:
+def parse_settings(settings_file: Path) -> tuple[list[str], dict[str, str]]:
"""Return (module_names, project_dir_overrides) from settings.gradle(.kts)."""
if not settings_file.is_file():
return [], {}
@@ -354,7 +354,7 @@ def build_jvm_gradle_index(
modules: list[str] = []
project_dir_overrides: dict[str, str] = {}
for sp in settings_paths:
- mods, ovr = _parse_settings(sp)
+ mods, ovr = parse_settings(sp)
modules.extend(mods)
project_dir_overrides.update(ovr)
diff --git a/packages/core/src/repowise/core/ingestion/workspace_members.py b/packages/core/src/repowise/core/ingestion/workspace_members.py
index 484ed21e1..4064c9c28 100644
--- a/packages/core/src/repowise/core/ingestion/workspace_members.py
+++ b/packages/core/src/repowise/core/ingestion/workspace_members.py
@@ -53,7 +53,7 @@ def _globbed(repo_root: Path, includes: list, excludes: list) -> set[str]:
return out
-def _toml(path: Path) -> dict:
+def read_toml(path: Path) -> dict:
try:
with open(path, "rb") as f:
return tomllib.load(f)
@@ -67,7 +67,7 @@ def _toml_workspace(repo_root: Path, manifest: str, *table: str) -> set[str] | N
None without a ``members`` list: Cargo then makes the root's path
dependencies members implicitly, which this does not read.
"""
- data: object = _toml(repo_root / manifest)
+ data: object = read_toml(repo_root / manifest)
for key in table:
data = data.get(key) if isinstance(data, dict) else None
if not isinstance(data, dict):
@@ -126,7 +126,7 @@ def _manifest_field(manifest: Path, key: str) -> str | None:
return None
value = data.get(key) if isinstance(data, dict) else None
elif manifest.name in ("pyproject.toml", "Cargo.toml"):
- data = _toml(manifest)
+ data = read_toml(manifest)
section = data.get("project") or data.get("package")
if not isinstance(section, dict) or not section.get(key):
tool = data.get("tool")
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_recommendations.py b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_recommendations.py
index 91fdab1a2..8cf915df6 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_recommendations.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_recommendations.py
@@ -17,7 +17,12 @@ from typing import Any
from sqlalchemy import or_, select
from sqlalchemy.ext.asyncio import AsyncSession
-from repowise.core.analysis.health.refactoring.commands import JsPackages, js_test_packages
+from repowise.core.analysis.health.refactoring.commands import (
+ JsPackages,
+ TestRuns,
+ js_test_packages,
+ runs_for_tests,
+)
from repowise.core.analysis.health.refactoring.models import RefactoringSuggestion
from repowise.core.analysis.health.refactoring.recommendations import (
DEFAULT_TEST_LIMIT,
@@ -208,6 +213,7 @@ async def validation_inputs(
path: _expand_scopes(path, reached, narrowed[path]) for path, reached in inferred.items()
}
hub_targets = [path for path in target_files if path in hubs]
+ js_packages, runs = await _test_packages(checkout, test_files)
evidence = await _validation_evidence(
session,
repository_id,
@@ -221,7 +227,8 @@ async def validation_inputs(
siblings=_siblings(in_degree),
symbols=await _symbol_spans(session, repository_id, target_files),
function_tests=await tests_in_files(session, repository_id, target_files),
- js_packages=await _js_packages(checkout, test_files),
+ js_packages=js_packages,
+ test_runs=runs,
),
)
return ValidationInputs(
@@ -267,13 +274,19 @@ async def repository_checkout(session: AsyncSession, repository_id: str) -> str
return await session.scalar(select(Repository.local_path).where(Repository.id == repository_id))
-async def _js_packages(checkout: str | None, test_files: set[str]) -> JsPackages | None:
+async def _test_packages(
+ checkout: str | None, test_files: set[str]
+) -> tuple[JsPackages | None, TestRuns | None]:
+ """Where each test runs, from one scan of the checkout's package roots."""
root = Path(checkout) if checkout else None
if root is None or not root.is_dir():
- return None
+ return None, None
# The health pass already scanned this commit's package roots; this reuses it.
manifests = await asyncio.to_thread(scan_package_manifests, root)
- return await asyncio.to_thread(js_test_packages, root, manifests, test_files)
+ return (
+ await asyncio.to_thread(js_test_packages, root, manifests, test_files),
+ await asyncio.to_thread(runs_for_tests, root, manifests, test_files),
+ )
async def _symbol_spans(
diff --git a/packages/types/src/refactoring.ts b/packages/types/src/refactoring.ts
index f8b632dd5..7e65fc1b0 100644
--- a/packages/types/src/refactoring.ts
+++ b/packages/types/src/refactoring.ts
@@ -47,6 +47,11 @@ export interface RecommendationValidation {
* null when none can run it). Absent when the plan was validated without
* a checkout or names no JS/TS test. */
js_packages?: Record<string, string | null>;
+ /** How each shown Go, Rust, JVM or Ruby test runs: the command that runs its
+ * package from the repository root and the argument narrowing it to the
+ * file's target (null runs the whole package). Absent when the plan was
+ * validated without a checkout or names no such test. */
+ test_runs?: Record<string, [string, string | null]>;
}
/** How a step's tests were found: coverage runs its lines, the graph reaches
diff --git a/tests/unit/health/test_refactoring_package_commands.py b/tests/unit/health/test_refactoring_package_commands.py
new file mode 100644
index 000000000..2c4c3aa7a
--- /dev/null
+++ b/tests/unit/health/test_refactoring_package_commands.py
@@ -0,0 +1,291 @@
+"""A Go, Rust, JVM or Ruby plan's test command runs the package that owns the test."""
+
+from __future__ import annotations
+
+import re
+from pathlib import Path
+
+from repowise.core.analysis.health.refactoring import commands
+from repowise.core.analysis.health.refactoring.batches import stage_batches
+from repowise.core.analysis.health.refactoring.commands import commands_for_tests, runs_for_tests
+from repowise.core.analysis.health.refactoring.recommendations import (
+ ValidationEvidence,
+ ValidationPlan,
+ build_validation_plan,
+)
+from repowise.core.analysis.health.refactoring.serving import validation_from_profile
+from repowise.core.analysis.test_reachability import ReachedBy
+from repowise.core.ingestion.package_roots import scan_package_manifests
+from tests.unit.health.test_recommendations import _plan
+
+
+def _write(root: Path, files: dict[str, str]) -> None:
+ for path, text in files.items():
+ (root / path).parent.mkdir(parents=True, exist_ok=True)
+ (root / path).write_text(text, encoding="utf-8")
+
+
+def _commands(root: Path, tests: list[str]) -> list[str]:
+ return commands_for_tests(tests, runs=runs_for_tests(root, scan_package_manifests(root), tests))
+
+
+def test_go_runs_each_test_directory_inside_its_module(tmp_path: Path) -> None:
+ _write(
+ tmp_path,
+ {
+ "go.mod": "module example.com/shop\n",
+ "internal/cart/cart_test.go": "package cart\n",
+ "internal/tax/tax_test.go": "package tax\n",
+ "services/api/go.mod": "module example.com/api\n",
+ "services/api/handler/h_test.go": "package handler\n",
+ "services/api/root_test.go": "package api\n",
+ },
+ )
+ tests = [
+ "internal/cart/cart_test.go::TestTotal",
+ "internal/tax/tax_test.go",
+ "services/api/handler/h_test.go::TestOk",
+ "services/api/root_test.go",
+ ]
+ assert _commands(tmp_path, tests) == [
+ "go -C services/api test . ./handler",
+ "go test ./internal/cart ./internal/tax",
+ ]
+
+
+def test_go_without_a_module_gets_no_command(tmp_path: Path) -> None:
+ _write(tmp_path, {"cart/cart_test.go": "package cart\n"})
+ assert _commands(tmp_path, ["cart/cart_test.go"]) == []
+
+
+def test_rust_runs_the_owning_crate_or_its_integration_targets(tmp_path: Path) -> None:
+ _write(
+ tmp_path,
+ {
+ "Cargo.toml": '[workspace]\nmembers = ["crates/parser"]\n',
+ "crates/parser/Cargo.toml": '[package]\nname = "parser"\n',
+ "crates/parser/src/lib.rs": "",
+ "crates/parser/tests/api.rs": "",
+ "crates/parser/tests/words/main.rs": "",
+ "tools/x/Cargo.toml": '[package]\nname = "x"\n',
+ "tools/x/src/lib.rs": "",
+ },
+ )
+ integration = ["crates/parser/tests/api.rs::parses_word", "crates/parser/tests/words/main.rs"]
+ assert _commands(tmp_path, integration) == ["cargo test -p parser --test api --test words"]
+ # A unit test lives in the crate's own targets, so the whole crate runs.
+ # Cargo refuses a crate inside the root workspace that does not list it.
+ unit = "crates/parser/src/lib.rs::tests::parses_empty"
+ assert _commands(tmp_path, [*integration, unit, "tools/x/src/lib.rs::tests::y"]) == [
+ "cargo test -p parser"
+ ]
+
+
+def test_rust_crate_outside_any_workspace_is_found_by_its_manifest(tmp_path: Path) -> None:
+ _write(
+ tmp_path,
+ {
+ "tools/x/Cargo.toml": '[package]\nname = "x"\n',
+ "tools/x/src/lib.rs": "",
+ "Cargo.toml": '[workspace]\nmembers = ["a"]\n',
+ "nested/y/Cargo.toml": '[package]\nname = "y"\n\n[workspace]\n',
+ "nested/y/src/lib.rs": "",
+ },
+ )
+ # Its own [workspace] makes a nested crate a root cargo accepts.
+ assert _commands(tmp_path, ["nested/y/src/lib.rs"]) == [
+ "cargo test --manifest-path nested/y/Cargo.toml"
+ ]
+ (tmp_path / "Cargo.toml").unlink()
+ assert _commands(tmp_path, ["tools/x/src/lib.rs"]) == [
+ "cargo test --manifest-path tools/x/Cargo.toml"
+ ]
+
+
+def test_rust_reads_declared_test_targets(tmp_path: Path) -> None:
+ _write(
+ tmp_path,
+ {
+ "Cargo.toml": (
+ '[package]\nname = "rg"\nautotests = false\n\n'
+ '[[test]]\nname = "integration"\npath = "tests/tests.rs"\n'
+ ),
+ "tests/tests.rs": "",
+ "tests/util.rs": "",
+ },
+ )
+ assert _commands(tmp_path, ["tests/tests.rs"]) == ["cargo test -p rg --test integration"]
+ # A module the target includes is no target of its own.
+ assert _commands(tmp_path, ["tests/util.rs"]) == ["cargo test -p rg"]
+
+
+def test_rust_outside_every_crate_gets_no_command(tmp_path: Path) -> None:
+ _write(tmp_path, {"Cargo.toml": "[workspace]\nmembers = []\n", "scripts/a.rs": ""})
+ assert _commands(tmp_path, ["scripts/a.rs"]) == []
+
+
+def _pom(artifact: str, body: str = "") -> str:
+ return f"<project><groupId>x</groupId><artifactId>{artifact}</artifactId>{body}</project>"
+
+
+def test_maven_runs_the_reactor_module_with_the_wrapper(tmp_path: Path) -> None:
+ modules = "<packaging>pom</packaging><modules><module>core</module></modules>"
+ _write(
+ tmp_path,
+ {
+ "pom.xml": _pom("root", modules),
+ "mvnw": "",
+ "core/pom.xml": _pom("core"),
+ "core/src/test/java/x/CartTest.java": "",
+ "tools/pom.xml": _pom("tools"),
+ "tools/src/test/java/x/ToolTest.java": "",
+ },
+ )
+ tests = [
+ "core/src/test/java/x/CartTest.java::CartTest::total",
+ "tools/src/test/java/x/ToolTest.java",
+ ]
+ assert _commands(tmp_path, tests) == [
+ "./mvnw -f tools/pom.xml test",
+ "./mvnw -pl core -am test",
+ ]
+
+
+def test_a_root_with_both_builds_runs_maven(tmp_path: Path) -> None:
+ _write(tmp_path, {"pom.xml": _pom("app"), "build.gradle": "", "src/test/java/ATest.java": ""})
+ assert _commands(tmp_path, ["src/test/java/ATest.java"]) == ["mvn test"]
+
+
+def test_gradle_runs_the_project_the_settings_include(tmp_path: Path) -> None:
+ _write(
+ tmp_path,
+ {
+ "settings.gradle.kts": (
+ 'include("app", "lib:core")\n'
+ 'project(":lib:core").projectDir = file("libs/core")\n'
+ ),
+ "gradlew": "",
+ "app/build.gradle.kts": "",
+ "app/src/test/kotlin/x/GreeterTest.kt": "",
+ "libs/core/build.gradle": "",
+ "libs/core/src/test/java/x/CoreTest.java": "",
+ "examples/demo/build.gradle.kts": "",
+ "examples/demo/src/test/kotlin/DemoTest.kt": "",
+ },
+ )
+ tests = [
+ "app/src/test/kotlin/x/GreeterTest.kt::GreeterTest::saysHi",
+ "libs/core/src/test/java/x/CoreTest.java",
+ # A build the root settings do not include gets no command.
+ "examples/demo/src/test/kotlin/DemoTest.kt",
+ ]
+ assert _commands(tmp_path, tests) == ["./gradlew :app:test", "./gradlew :lib:core:test"]
+
+
+def test_ruby_runs_spec_files_or_the_rake_test_task(tmp_path: Path) -> None:
+ _write(
+ tmp_path,
+ {
+ "Gemfile": 'gem "minitest"\n',
+ "Rakefile": "Minitest::TestTask.create\n",
+ "test/routing_test.rb": "",
+ "engines/billing/Gemfile": 'gem "rspec"\n',
+ "engines/billing/spec/invoice_spec.rb": "",
+ "engines/billing/spec/price_spec.rb": "",
+ "engines/billing/spec/support/helper.rb": "",
+ },
+ )
+ specs = ["engines/billing/spec/invoice_spec.rb", "engines/billing/spec/price_spec.rb"]
+ assert _commands(tmp_path, ["test/routing_test.rb::RoutingTest::test_get", *specs]) == [
+ "bundle exec rake test",
+ "cd engines/billing && bundle exec rspec spec/invoice_spec.rb spec/price_spec.rb",
+ ]
+ # A spec helper is no example file, so the whole suite runs.
+ helper = "engines/billing/spec/support/helper.rb"
+ assert _commands(tmp_path, [*specs, helper]) == ["cd engines/billing && bundle exec rspec"]
+
+
+def test_ruby_without_a_gemfile_or_test_task_gets_no_command(tmp_path: Path) -> None:
+ _write(tmp_path, {"test/a_test.rb": "", "lib/Gemfile": "", "lib/test/b_test.rb": ""})
+ assert _commands(tmp_path, ["test/a_test.rb", "lib/test/b_test.rb"]) == []
+
+
+def test_ruby_runs_rspec_only_for_a_gem_that_uses_it(tmp_path: Path) -> None:
+ spec = "spec/cart_spec.rb"
+ _write(tmp_path, {"Gemfile": 'gem "minitest"\n', spec: ""})
+ assert _commands(tmp_path, [spec]) == []
+ (tmp_path / "Rakefile").write_text("task :test do\nend\n", encoding="utf-8")
+ assert _commands(tmp_path, [spec]) == ["bundle exec rake test"]
+ (tmp_path / "shop.gemspec").write_text('s.add_development_dependency "rspec"\n', "utf-8")
+ assert _commands(tmp_path, [spec]) == [f"bundle exec rspec {spec}"]
+ (tmp_path / "shop.gemspec").unlink()
+ (tmp_path / ".rspec").write_text("--require spec_helper\n", encoding="utf-8")
+ assert _commands(tmp_path, [spec]) == [f"bundle exec rspec {spec}"]
+
+
+def test_args_sharing_a_flag_merge_their_values(monkeypatch, tmp_path: Path) -> None:
+ def per_test(repo, test: str):
+ # A runner sees the whole id, so it can name the single test.
+ return "dotnet test t.csproj", f"--filter={test.rsplit('::', 1)[-1]}"
+
+ monkeypatch.setattr(commands, "_RUNNERS", (((".cs",), per_test),))
+ tests = ["t/CartTests.cs::Shop.CartTests.Total", "t/CartTests.cs::Shop.CartTests.Empty"]
+ runs = commands.runs_for_tests(tmp_path, {}, tests)
+ assert commands_for_tests(tests, runs=runs) == [
+ 'dotnet test t.csproj --filter "Shop.CartTests.Empty|Shop.CartTests.Total"'
+ ]
+ assert commands_for_tests(tests[:1], runs=runs) == [
+ 'dotnet test t.csproj --filter "Shop.CartTests.Total"'
+ ]
+ # Repeated flags without ``=`` stay repeated, as cargo's ``--test`` needs.
+ repeated = {"a.rs": ["cargo test -p a", "--test x"], "b.rs": ["cargo test -p a", "--test y"]}
+ assert commands_for_tests(["a.rs", "b.rs"], runs=repeated) == [
+ "cargo test -p a --test x --test y"
+ ]
+
+
+def test_the_wire_type_lists_every_stored_validation_field() -> None:
+ source = (
+ Path(__file__).resolve().parents[3] / "packages" / "types" / "src" / "refactoring.ts"
+ ).read_text(encoding="utf-8")
+ match = re.search(r"interface RecommendationValidation \{(.*?)\n\}", source, re.DOTALL)
+ assert match, "RecommendationValidation is not declared in refactoring.ts"
+ body = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.DOTALL)
+ fields = set(re.findall(r"^\s*(\w+)\??:", body, re.MULTILINE))
+ plan = ValidationPlan(
+ "unknown", None, 0, [], False, [], [], [],
+ js_packages={"a.test.ts": ""}, test_runs={"a_test.go": ["go test", "."]},
+ )
+ assert fields == set(plan.as_dict())
+
+
+def test_python_and_js_commands_are_unchanged_beside_other_languages() -> None:
+ runs = {"a/a_test.go": ["go test", "./a"]}
+ tests = ["tests/test_a.py", "web/a.test.ts", "a/a_test.go"]
+ assert commands_for_tests(tests, packages={"web/a.test.ts": ""}, runs=runs) == [
+ "pytest tests/test_a.py",
+ "npm test -- web/a.test.ts",
+ "go test ./a",
+ ]
+
+
+def test_the_plan_stores_its_runs_and_batches_reuse_them() -> None:
+ test = "internal/cart/cart_test.go::TestTotal"
+ runs = {"internal/cart/cart_test.go": ["go test", "./internal/cart"]}
+ suggestion = _plan("Total", file_path="internal/cart/cart.go")
+ inferred = {suggestion.file_path: ReachedBy([test], "import-graph", 1)}
+ plan = build_validation_plan(
+ suggestion, {}, inferred, evidence=ValidationEvidence(test_runs=runs)
+ )
+ assert plan.commands == ["go test ./internal/cart"]
+ stored = plan.as_dict()
+ assert stored["test_runs"] == runs
+ assert validation_from_profile(stored).test_runs == runs
+ assert "test_runs" not in build_validation_plan(suggestion, {}, inferred).as_dict()
+ detail = {
+ "refactoring_type": "extract_method",
+ "validation": {"tests": [test], "total": 1, "test_runs": runs},
+ }
+ batches = stage_batches(detail, [{"n": n, "action": "extract"} for n in range(1, 8)])
+ assert batches is not None
+ assert batches[0]["commands"] == ["go test ./internal/cart"]