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): name a manifest only where a written path leads to it (#3353)

Raghav Chamadiya65f13d634af0
diff --git a/docs/start/CI.md b/docs/start/CI.md
index 2d3b1060a..500fb405f 100644
--- a/docs/start/CI.md
+++ b/docs/start/CI.md
@@ -253,7 +253,7 @@ a per-test coverage map (`repowise coverage add`) makes it more precise.
- It exits `0` for a subset or for everything, and `2` only when it cannot read the change or the `tests.*` config.
- 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 Cargo, Make, CMake, Docker or CI file; tool and runtime config (dotfiles, `.tool-versions`, root-level `*.toml`, `*.yaml` and `*.json`, shell scripts); `.repowise/config.yaml`.
-- A lockfile, manifest or test runner config runs its own ecosystem's tests: for a nested `package.json`, lockfile or `go.mod`, the tests under its directory and those reaching code there; at the root, and for Python, Ruby, PHP and Gradle or Maven files (pip requirements included), `tsconfig` and jest or vitest configs, the whole ecosystem. Tests whose code names the file from outside that scope run too (a Python test reading `package.json`); if more than 50 non-test files outside the scope name it, everything runs.
+- A lockfile, manifest or test runner config runs its own ecosystem's tests: for a nested `package.json`, lockfile or `go.mod`, the tests under its directory and those reaching code there; at the root, and for Python, Ruby, PHP and Gradle or Maven files (pip requirements included), `tsconfig` and jest or vitest configs, the whole ecosystem. Tests whose code names the file from outside that scope run too (a Python test reading `package.json`); a mention written with a directory names only the files its path can mean (`packages/web/package.json` does not name `packages/ui/package.json`), a bare name, `../package.json` or a path built from parts names every file of that name. If more than 50 non-test files outside the scope name it, everything runs.
- A production package's `__init__.py` selects the tests reaching any module under it.
- A doc, template, query, stylesheet, image or nested data file selects the tests reaching the code that names it. When no code names it, a doc, template, stylesheet or image selects its package's tests, and a query or data file runs everything. Ceiling: a test that globs docs or templates in another package is not seen.
- 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, unless every known test is selected anyway (the reasons then say so); any Python test helper while a conftest or pytest config loads plugins by name.
diff --git a/packages/core/src/repowise/core/analysis/conftest_routes.py b/packages/core/src/repowise/core/analysis/conftest_routes.py
index 53df65525..210b7fe1d 100644
--- a/packages/core/src/repowise/core/analysis/conftest_routes.py
+++ b/packages/core/src/repowise/core/analysis/conftest_routes.py
@@ -326,7 +326,8 @@ class ConftestFacts:
"""What the conftest does with *entries*, the files it imports on a route."""
if self.error:
return ConftestUse(run_all=self.error)
- targets = [module_parts(e) for e in entries]
+ # Sorted: an import binds the first entry it names, so order must not vary.
+ targets = [module_parts(e) for e in sorted(entries)]
hot: dict[str, tuple[str, ...]] = {}
matched: set[tuple[str, ...]] = set()
for node in self.imports:
diff --git a/packages/core/src/repowise/core/analysis/namer_paths.py b/packages/core/src/repowise/core/analysis/namer_paths.py
new file mode 100644
index 000000000..ddd389d5f
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/namer_paths.py
@@ -0,0 +1,109 @@
+"""Whether a file's text names one particular file, not just a file of that name.
+
+A namer search matches a changed file's name (``package.json``) in source
+text, and every workspace has many files of that name. A mention written with
+its directory says which one it means. Each mention is reduced to its written
+path, with leading ``/``, ``./`` and ``../`` steps and inner ``a/../`` folded
+away (a relative path depends on the working directory, which the text does
+not give), and compared to the file's path, case-insensitively:
+
+- a written path names every file whose path ends in it
+ (``ui/package.json`` names ``packages/ui/package.json``), and every file
+ whose path it ends in (``/app/packages/ui/package.json``,
+ ``C:\\repo\\packages\\ui\\package.json``);
+- a bare name (``"package.json"``, ``../package.json``,
+ ``root + "/package.json"``), a glob or a template names every file of that
+ name.
+
+A mention that only continues the name (``mypackage.json``, ``package.json5``)
+is another file; an escape just before the name (``"\\npackage.json"``) is not
+part of it. Ceiling: a path built from separate parts
+(``ROOT / "ui" / "package.json"``) reads as the bare name, so it names every
+file of that name.
+"""
+
+from __future__ import annotations
+
+import posixpath
+import re
+from collections.abc import Iterable
+
+# Characters a written path is made of; a mention's directory part is the run
+# of them before the name.
+_PATH_CHAR = re.compile(r"[\w.\-/\\*?\[\]{}$%@~+]")
+# A directory part holding one of these is a glob or a template: any directory.
+_OPEN_CHARS = frozenset("*?[]{}$%@~+")
+# Continuing the name with one of these makes it another file's name.
+_NAME_CHAR = re.compile(r"[\w\-]")
+
+
+def names_file(text: str, target: str) -> bool:
+ """Whether *text* holds a mention that can mean *target*."""
+ return any_may_mean(mentions(text, posixpath.basename(target)), target)
+
+
+def mentions(text: str, name: str) -> list[str | None]:
+ """Each mention of *name* in *text* as its written path, ``None`` for any directory.
+
+ A mention that is only part of a longer name is left out.
+ """
+ out: list[str | None] = []
+ for match in re.finditer(re.escape(name), text):
+ if _continues(text, match.end()):
+ continue
+ prefix = _directory_part(text, match.start())
+ if prefix is None:
+ continue
+ written = _strip_relative(prefix + name)
+ out.append(None if _OPEN_CHARS & set(prefix) or written == name else written)
+ return out
+
+
+def any_may_mean(written: Iterable[str | None], target: str) -> bool:
+ """Whether any mention reduced by :func:`mentions` can mean *target*."""
+ path = target.casefold()
+ for mention in written:
+ if mention is None:
+ return True
+ tail = mention.casefold()
+ if tail == path or path.endswith(f"/{tail}") or tail.endswith(f"/{path}"):
+ return True
+ return False
+
+
+def _continues(text: str, end: int) -> bool:
+ """Whether the name goes on past *end* (``package.json5``, ``README.md.bak``)."""
+ if end < len(text) and _NAME_CHAR.match(text[end]):
+ return True
+ return text[end : end + 1] == "." and bool(_NAME_CHAR.match(text[end + 1 : end + 2]))
+
+
+def _directory_part(text: str, start: int) -> str | None:
+ """The written directory before the name at *start*, ``""`` when bare.
+
+ ``None`` when the name only ends a longer one (``mypackage.json``). An
+ escape such as ``\\n`` just before the name is a boundary, not a letter.
+ """
+ if start >= 2 and text[start - 2] == "\\" and text[start - 1].isalpha():
+ return ""
+ begin = start
+ while begin > 0 and _PATH_CHAR.match(text[begin - 1]):
+ begin -= 1
+ prefix = text[begin:start].replace("\\\\", "/").replace("\\", "/")
+ if prefix and not prefix.endswith("/"):
+ return None
+ return prefix
+
+
+def _strip_relative(written: str) -> str:
+ """*written* without leading ``/``, ``./`` or ``../`` steps, inner ``a/../`` folded."""
+ parts: list[str] = []
+ for part in written.split("/"):
+ if part in ("", "."):
+ continue
+ if part == "..":
+ if parts:
+ parts.pop()
+ continue
+ parts.append(part)
+ return "/".join(parts)
diff --git a/packages/core/src/repowise/core/analysis/selection_scopes.py b/packages/core/src/repowise/core/analysis/selection_scopes.py
index 1b720cdd9..abb8a1780 100644
--- a/packages/core/src/repowise/core/analysis/selection_scopes.py
+++ b/packages/core/src/repowise/core/analysis/selection_scopes.py
@@ -149,6 +149,11 @@ def needs_namers(path: str, config: TestSelectionConfig) -> bool:
return _ecosystem(path) is not None or (full_run_reason(path) is None and _is_asset(path))
+def is_manifest(path: str) -> bool:
+ """A manifest, lockfile or runner config: nothing includes it, so its namers write its path."""
+ return _ecosystem(path) is not None
+
+
def keeps_full_run(path: str, config: TestSelectionConfig) -> bool:
"""Whether *path* runs everything whatever names it, so no namer search can help."""
if full_run_reason(path, config.full_run_on) is None:
diff --git a/packages/core/src/repowise/core/analysis/test_collection.py b/packages/core/src/repowise/core/analysis/test_collection.py
index acd34d4ac..8527c7368 100644
--- a/packages/core/src/repowise/core/analysis/test_collection.py
+++ b/packages/core/src/repowise/core/analysis/test_collection.py
@@ -145,6 +145,7 @@ def plan_scopes(
reading the checkout.
"""
from .selection_scopes import (
+ is_manifest,
keeps_full_run,
needs_namers,
trigger_scopes,
@@ -157,7 +158,7 @@ def plan_scopes(
asked = [] if blocked else [p for p in paths if needs_namers(p, config)]
namers: dict[str, list[str]] = {}
if asked:
- namers = file_namers(asked, _scan_texts(checkout, asked, cancelled))
+ namers = file_namers(asked, _scan_texts(checkout, asked, cancelled), is_manifest)
scopes = trigger_scopes(paths, checkout.tracked, namers, config)
routes = sorted({r for scope in scopes.values() for r in scope.routes} - set(paths))
return Plan(namers, scopes, routes)
diff --git a/packages/core/src/repowise/core/analysis/test_selection.py b/packages/core/src/repowise/core/analysis/test_selection.py
index ecf7d4e81..f0fcb793a 100644
--- a/packages/core/src/repowise/core/analysis/test_selection.py
+++ b/packages/core/src/repowise/core/analysis/test_selection.py
@@ -60,7 +60,7 @@ from __future__ import annotations
import functools
import re
import shlex
-from collections.abc import Collection, Iterable, Mapping
+from collections.abc import Callable, Collection, Iterable, Mapping
from dataclasses import dataclass, field, replace
from pathlib import PurePosixPath
from typing import TYPE_CHECKING, Any
@@ -333,20 +333,27 @@ def doc_readers(docs: Collection[str], sources: Iterable[tuple[str, str]]) -> di
return {doc: names[0] for doc, names in file_namers(docs, sources).items()}
-def file_namers(files: Collection[str], sources: Iterable[tuple[str, str]]) -> dict[str, list[str]]:
+def file_namers(
+ files: Collection[str],
+ sources: Iterable[tuple[str, str]],
+ exact: Callable[[str], bool] | None = None,
+) -> dict[str, list[str]]:
"""``{file: the code naming it}`` for each of *files* some code or config names.
Code that reads a file names it (``ROOT / "README.md"``), and a
``--doctest-glob`` turns every doc into a test, so the config declaring
one names each doc (first). One substring test per distinct file name and
- source, and every namer is listed: a caller decides what too many means.
- Ceiling: code that globs a directory names no file.
+ source. A file *exact* accepts is named only by a mention whose written
+ path can mean it (:mod:`~.namer_paths`); any other is
+ named by its name alone, since another asset may include it (a stylesheet
+ ``@import``) where no code names its directory. Every namer is listed: a
+ caller decides what too many means. Ceiling: code that globs a directory
+ names no file.
"""
by_name: dict[str, list[str]] = {}
for f in files:
by_name.setdefault(PurePosixPath(f).name, []).append(f)
docs = [f for f in files if PurePosixPath(f).suffix.lower() in _DOC_SUFFIXES]
- found: dict[str, list[str]] = {}
out: dict[str, list[str]] = {}
for path, text in sources:
# Config only for the glob, code only for names: pyproject's
@@ -356,15 +363,37 @@ def file_namers(files: Collection[str], sources: Iterable[tuple[str, str]]) -> d
for doc in docs:
out.setdefault(doc, []).insert(0, path)
continue
- for name in by_name:
- if name in text:
- found.setdefault(name, []).append(path)
- for name, namers in found.items():
- for f in by_name[name]:
- out.setdefault(f, []).extend(n for n in namers if n != f)
+ for f in _named_in(text, path, by_name, exact):
+ out.setdefault(f, []).append(path)
return {f: n for f, n in out.items() if n}
+def _named_in(
+ text: str,
+ path: str,
+ by_name: Mapping[str, list[str]],
+ exact: Callable[[str], bool] | None,
+) -> list[str]:
+ """The files of *by_name* that *path*'s *text* names (see :func:`file_namers`).
+
+ The text is scanned once per name held, whatever the number of files of
+ that name.
+ """
+ from .namer_paths import any_may_mean, mentions
+
+ out: list[str] = []
+ for name, same in by_name.items():
+ if name not in text:
+ continue
+ written = mentions(text, name) if exact and any(exact(f) for f in same) else None
+ out += [
+ f
+ for f in same
+ if f != path and (written is None or not exact(f) or any_may_mean(written, f))
+ ]
+ return out
+
+
def plugin_loader(sources: Iterable[tuple[str, str]]) -> str | None:
"""The first conftest or pytest config that loads a plugin module by name.
diff --git a/tests/unit/analysis/test_namer_paths.py b/tests/unit/analysis/test_namer_paths.py
new file mode 100644
index 000000000..32eba8eac
--- /dev/null
+++ b/tests/unit/analysis/test_namer_paths.py
@@ -0,0 +1,60 @@
+"""A mention names one particular file only when its written path can mean that file."""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.analysis.namer_paths import names_file
+
+_UI = "packages/ui/package.json"
+
+
+ ("text", "names"),
+ [
+ # A written path names the files ending in it, and those it ends in.
+ ('ROOT / "packages/ui/package.json"', True),
+ ('base / "ui/package.json"', True),
+ (r'"packages\\ui\\package.json"', True),
+ ('"/app/packages/ui/package.json"', True),
+ (r'"C:\\repo\\packages\\ui\\package.json"', True),
+ (r"C:\repo\packages\ui\package.json", True),
+ ('"myrepo/packages/ui/package.json"', True),
+ ('"file:///abs/repo/packages/ui/package.json"', True),
+ ('"PACKAGES/UI/package.json"', True),
+ ('ROOT / "packages/web/package.json"', False),
+ ('"tests/fixtures/package.json"', False),
+ ('"/app/packages/web/package.json"', False),
+ # Relative steps are dropped: what is left decides.
+ ("import p from '../ui/package.json'", True),
+ ("import p from '../web/package.json'", False),
+ ('"packages/web/../ui/package.json"', True),
+ ("import p from '../package.json'", True),
+ ("readFileSync('./package.json')", True),
+ # Bare, joined to a computed base, a glob or a template: any such file.
+ ('MANIFESTS = ("package.json",)', True),
+ ('root + "/package.json"', True),
+ ('f"{d}/package.json"', True),
+ ('glob("packages/*/package.json")', True),
+ ("the package.json.", True),
+ ('"\\npackage.json"', True),
+ ('"\\tpackage.json"', True),
+ # Another file's name that only contains this one.
+ ('"mypackage.json"', False),
+ ('"package.json5"', False),
+ ('"package.json.bak"', False),
+ # One naming mention among others is enough.
+ ('"packages/web/package.json", "packages/ui/package.json"', True),
+ ],
+)
+def test_a_mention_names_the_file_its_written_path_can_mean(text, names) -> None:
+ assert names_file(text, _UI) is names
+
+
+def test_the_root_manifest_is_named_by_any_path_ending_in_its_name() -> None:
+ # A root checkout can sit anywhere, so every written path to a file of
+ # that name may be the root one.
+ assert names_file('"package.json"', "package.json")
+ assert names_file('"/abs/repo/package.json"', "package.json")
+ assert names_file('"packages/ui/package.json"', "package.json")
+ assert not names_file('"mypackage.json"', "package.json")
diff --git a/tests/unit/analysis/test_selection_scopes.py b/tests/unit/analysis/test_selection_scopes.py
index fda58ac80..c80f01dad 100644
--- a/tests/unit/analysis/test_selection_scopes.py
+++ b/tests/unit/analysis/test_selection_scopes.py
@@ -208,11 +208,20 @@ def test_file_namers_lists_every_file_naming_one() -> None:
("web/src/more.ts", '"layout.css"\n'),
]
files = ["web/src/styles/layout.css", "other/layout.css"]
+ # An asset may be included by another asset, so its name alone names it.
assert file_namers(files, sources)["other/layout.css"] == [
"web/src/app.ts",
"src/other.py",
"web/src/more.ts",
]
+ # A manifest is named only where a written directory can lead to it.
+ manifests = ["web/package.json", "other/package.json"]
+ code = [("web/src/app.ts", "open('./web/package.json')"), ("x.py", '"package.json"')]
+ exact = file_namers(manifests, code, lambda f: f.endswith("package.json"))
+ assert exact == {
+ "web/package.json": ["web/src/app.ts", "x.py"],
+ "other/package.json": ["x.py"],
+ }
def test_a_js_package_manifest_runs_its_tests_and_those_reaching_its_code() -> None:
@@ -344,7 +353,7 @@ def test_plan_scopes_names_the_same_files_from_git_grep_as_from_reading_them(tmp
"web/src/app.test.ts",
"web/src/app.ts",
]
- # "data.md" holds "a.md", so the file naming docs/data.md names docs/a.md too.
+ # "data.md" holds "a.md": a doc keeps name-only matching, so both are named.
assert grepped.namers["docs/a.md"][-1] == "tools/data.py"
assert grepped.namers["docs/data.md"][-1] == "tools/data.py"