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.

refactor(tests): split the test-map functions this week made too complex (#3548)

Raghav Chamadiya5bdb41453662
diff --git a/packages/core/src/repowise/core/analysis/conftest_routes.py b/packages/core/src/repowise/core/analysis/conftest_routes.py
index 210b7fe1d..cc6082008 100644
--- a/packages/core/src/repowise/core/analysis/conftest_routes.py
+++ b/packages/core/src/repowise/core/analysis/conftest_routes.py
@@ -125,6 +125,29 @@ def _names(module: tuple[str, ...], entry: tuple[str, ...], *, package: bool) ->
)
+def _first_named(
+ module: tuple[str, ...], entries: list[tuple[str, ...]], *, package: bool
+) -> tuple[str, ...] | None:
+ """The first of *entries* that *module* names (see :func:`_names`)."""
+ return next((e for e in entries if _names(module, e, package=package)), None)
+
+
+def _imported_entry(
+ parts: tuple[str, ...], entries: list[tuple[str, ...]]
+) -> tuple[str, ...] | None:
+ """The entry ``import a.b.c`` reaches: in or under the module, or a package above it."""
+ prefixes = [parts[:i] for i in range(1, len(parts))]
+ return _first_named(parts, entries, package=True) or next(
+ (h for p in prefixes if (h := _first_named(p, entries, package=False))), None
+ )
+
+
+def _from_module(node: ast.ImportFrom, here: tuple[str, ...]) -> tuple[str, ...]:
+ """The module a ``from`` import reads, its relative dots resolved against *here*."""
+ base = here[: len(here) - (node.level - 1)] if node.level else ()
+ return base + tuple((node.module or "").split(".")) if node.module else base
+
+
def _bindings(
node: ast.Import | ast.ImportFrom, here: tuple[str, ...], entries: list[tuple[str, ...]]
) -> tuple[dict[str, tuple[str, ...]], set[tuple[str, ...]], bool]:
@@ -132,30 +155,23 @@ def _bindings(
bound: dict[str, tuple[str, ...]] = {}
matched: set[tuple[str, ...]] = set()
star = False
-
- def hit(module: tuple[str, ...], *, package: bool) -> tuple[str, ...] | None:
- return next((e for e in entries if _names(module, e, package=package)), None)
-
if isinstance(node, ast.Import):
for alias in node.names:
parts = tuple(alias.name.split("."))
- prefixes = [parts[:i] for i in range(1, len(parts))]
- e = hit(parts, package=True) or next(
- (h for p in prefixes if (h := hit(p, package=False))), None
- )
- if e:
+ if e := _imported_entry(parts, entries):
bound[alias.asname or parts[0]] = e
matched.add(e)
return bound, matched, star
- base = here[: len(here) - (node.level - 1)] if node.level else ()
- module = base + tuple((node.module or "").split(".")) if node.module else base
+ module = _from_module(node, here)
for alias in node.names:
if alias.name == "*":
- e = hit(module, package=True)
+ e = _first_named(module, entries, package=True)
star = star or e is not None
continue
- e = hit((*module, alias.name), package=True) or hit(module, package=False)
+ e = _first_named((*module, alias.name), entries, package=True) or _first_named(
+ module, entries, package=False
+ )
if e:
bound[alias.asname or alias.name] = e
matched.add(e)
@@ -298,69 +314,59 @@ class ConftestFacts:
unit.fixture, unit.autouse = _fixture_info(node)
facts.units[node.name] = unit
for name, unit in facts.units.items():
- for sub in _body_nodes(unit.node):
- if isinstance(sub, (ast.Import, ast.ImportFrom)):
- unit.imports.append(sub)
- elif isinstance(sub, ast.Name) and isinstance(sub.ctx, ast.Load):
- unit.loads.append(sub)
- if sub.id in facts.units and sub.id != name:
- unit.refs.add(sub.id)
+ facts._read_unit_body(name, unit)
for node in code:
for sub in ast.walk(node):
- if isinstance(sub, ast.Name) and isinstance(sub.ctx, ast.Load):
- facts.code_loads.append(sub)
- if isinstance(sub, ast.Name) and sub.id == "pytest_plugins":
- facts.error = f"{path} loads pytest plugins"
- if isinstance(sub, ast.Name) and sub.id == "__all__":
- # Re-exported for the tests that import the conftest, which
- # the selection adds as its importers.
- value = getattr(facts.parents.get(id(sub)), "value", None)
- facts.exported |= {
- e.value
- for e in getattr(value, "elts", ())
- if isinstance(e, ast.Constant) and isinstance(e.value, str)
- }
+ if isinstance(sub, ast.Name):
+ facts._read_module_name(sub)
return facts
+ def _read_unit_body(self, name: str, unit: _Unit) -> None:
+ """Record the imports in *unit*'s body, the names it reads, and the units it names."""
+ for sub in _body_nodes(unit.node):
+ if isinstance(sub, (ast.Import, ast.ImportFrom)):
+ unit.imports.append(sub)
+ elif isinstance(sub, ast.Name) and isinstance(sub.ctx, ast.Load):
+ unit.loads.append(sub)
+ if sub.id in self.units and sub.id != name:
+ unit.refs.add(sub.id)
+
+ def _read_module_name(self, sub: ast.Name) -> None:
+ """Record what one name in module-level code tells: a read, plugin loading, ``__all__``."""
+ if isinstance(sub.ctx, ast.Load):
+ self.code_loads.append(sub)
+ if sub.id == "pytest_plugins":
+ self.error = f"{self.path} loads pytest plugins"
+ if sub.id == "__all__":
+ # Re-exported for the tests that import the conftest, which
+ # the selection adds as its importers.
+ value = getattr(self.parents.get(id(sub)), "value", None)
+ self.exported |= {
+ e.value
+ for e in getattr(value, "elts", ())
+ if isinstance(e, ast.Constant) and isinstance(e.value, str)
+ }
+
def use(self, entries: Collection[str]) -> ConftestUse:
"""What the conftest does with *entries*, the files it imports on a route."""
if self.error:
return ConftestUse(run_all=self.error)
# 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:
- bound, hits, star = _bindings(node, self.here, targets)
- if star:
- return ConftestUse(run_all=f"{self.path} star-imports a module on the route")
- hot.update(bound)
- matched |= hits
+ star_import = ConftestUse(run_all=f"{self.path} star-imports a module on the route")
+ hot, matched, star = self._bind(self.imports, targets)
+ if star:
+ return star_import
reach: dict[str, _Reach] = {}
loaded = set(self.exported)
for name, unit in self.units.items():
- r = reach[name] = _Reach()
- local: dict[str, tuple[str, ...]] = {}
- for node in unit.imports:
- bound, hits, star = _bindings(node, self.here, targets)
- if star:
- return ConftestUse(run_all=f"{self.path} star-imports a module on the route")
- local.update(bound)
- matched |= hits
- r.imports = next(iter(local), None)
- seen = {n.id for n in unit.loads}
- loaded |= seen
- for load in unit.loads:
- if load.id not in local and load.id not in hot:
- continue
- if _patch_target(load, self.parents):
- r.patch = r.patch or load.id
- else:
- r.run = r.run or load.id
- # A local import nothing reads is there for its side effects.
- if r.run is None and (unread := sorted(set(local) - seen)):
- r.run = unread[0]
+ local, hits, star = self._bind(unit.imports, targets)
+ if star:
+ return star_import
+ matched |= hits
+ reach[name] = self._unit_reach(unit, local, hot)
+ loaded |= {n.id for n in unit.loads}
if missed := [e for e in targets if e not in matched]:
return ConftestUse(
@@ -382,6 +388,40 @@ class ConftestFacts:
self._propagate(reach)
return self._decide(reach, module_refs)
+ def _bind(
+ self, nodes: list[Any], targets: list[tuple[str, ...]]
+ ) -> tuple[dict[str, tuple[str, ...]], set[tuple[str, ...]], bool]:
+ """``({bound name: entry}, entries matched, star-imports an entry)`` over *nodes*.
+
+ Stops at the first star import, which the caller answers on its own.
+ """
+ bound: dict[str, tuple[str, ...]] = {}
+ matched: set[tuple[str, ...]] = set()
+ for node in nodes:
+ names, hits, star = _bindings(node, self.here, targets)
+ if star:
+ return bound, matched, True
+ bound.update(names)
+ matched |= hits
+ return bound, matched, False
+
+ def _unit_reach(
+ self, unit: _Unit, local: Mapping[str, tuple[str, ...]], hot: Mapping[str, tuple[str, ...]]
+ ) -> _Reach:
+ """How *unit* uses the route, given its own (*local*) and module (*hot*) bindings."""
+ r = _Reach(imports=next(iter(local), None))
+ for load in unit.loads:
+ if load.id not in local and load.id not in hot:
+ continue
+ if _patch_target(load, self.parents):
+ r.patch = r.patch or load.id
+ else:
+ r.run = r.run or load.id
+ # A local import nothing reads is there for its side effects.
+ if r.run is None and (unread := sorted(set(local) - {n.id for n in unit.loads})):
+ r.run = unread[0]
+ return r
+
def _propagate(self, reach: dict[str, _Reach]) -> None:
"""A unit uses what the units it names and the fixtures it requests use."""
by_fixture = {u.fixture: n for n, u in self.units.items() if u.fixture}
@@ -679,16 +719,7 @@ def _narrow(
narrowed: list[str] = []
hidden_importer = scope.unresolved_conftest_import()
for conf, route in routes.items():
- reason = route.reason
- if reason is None and route.use is not None and hidden_importer:
- reason = (
- f"{hidden_importer} imports a conftest by a name the index could not "
- "resolve, so that importer is not known"
- )
- users: set[str] = set()
- if reason is None and route.use is not None:
- found = _fixture_users(conf, route.use, scope)
- reason, users = (found, set()) if isinstance(found, str) else (None, found)
+ reason, users = _route_users(conf, route, scope, hidden_importer)
if reason is not None:
_add_note(notes, (f"{conf} keeps every test under it for ", f": {reason}"), target)
continue
@@ -698,32 +729,57 @@ def _narrow(
continue
blocked.add(conf)
narrowed.append(conf)
- framework = {
- e.source
- for e in scope.edges
- if e.target == conf
- and e.edge_type == "framework"
- and e.hint in (CONFTEST_HINT, UNRECORDED_HINT)
- }
- importers = set(scope.parents.get(conf, ())) - framework - {conf}
- for test in sorted(users | importers):
+ for test in sorted(users | _test_importers(conf, scope)):
added.setdefault(test, "conftest-fixture")
kept = {t: v for t, v in picks.items() if t not in blocked}
for test, via in added.items():
kept.setdefault(test, via)
result = dict(with_importers(kept, scope.parents, blocked))
for conf in narrowed:
- prefix = _dir_prefix(conf)
- if any(t.startswith(prefix) and is_runnable_test(t) for t in result):
- check = None
- else:
- check = scope.first_test_under(prefix)
- if check:
- result.setdefault(check, "conftest-import-check")
+ check = _add_import_check(conf, result, scope)
_add_note(notes, _narrow_note(conf, routes[conf], check), target)
return list(result.items())
+def _route_users(
+ conf: str, route: _Route, scope: _Scope, hidden_importer: str | None
+) -> tuple[str | None, set[str]]:
+ """``(why the route cannot narrow, or None; the fixture users)`` for one conftest."""
+ reason = route.reason
+ if reason is None and route.use is not None and hidden_importer:
+ reason = (
+ f"{hidden_importer} imports a conftest by a name the index could not "
+ "resolve, so that importer is not known"
+ )
+ if reason is None and route.use is not None:
+ found = _fixture_users(conf, route.use, scope)
+ return (found, set()) if isinstance(found, str) else (None, found)
+ return reason, set()
+
+
+def _test_importers(conf: str, scope: _Scope) -> set[str]:
+ """The files importing *conf* itself, not the ones pytest links to it as their conftest."""
+ framework = {
+ e.source
+ for e in scope.edges
+ if e.target == conf
+ and e.edge_type == "framework"
+ and e.hint in (CONFTEST_HINT, UNRECORDED_HINT)
+ }
+ return set(scope.parents.get(conf, ())) - framework - {conf}
+
+
+def _add_import_check(conf: str, result: dict[str, str], scope: _Scope) -> str | None:
+ """Add a test under *conf* to import it when the selection holds none; the test added."""
+ prefix = _dir_prefix(conf)
+ if any(t.startswith(prefix) and is_runnable_test(t) for t in result):
+ return None
+ check = scope.first_test_under(prefix)
+ if check:
+ result.setdefault(check, "conftest-import-check")
+ return check
+
+
def _add_note(notes: dict[tuple[str, str], list[str]], key: tuple[str, str], target: str) -> None:
"""Record that *target* got the outcome *key* (the note's text around the target)."""
targets = notes.setdefault(key, [])
diff --git a/packages/core/src/repowise/core/analysis/test_impact.py b/packages/core/src/repowise/core/analysis/test_impact.py
index f9e38f872..0c315ca2a 100644
--- a/packages/core/src/repowise/core/analysis/test_impact.py
+++ b/packages/core/src/repowise/core/analysis/test_impact.py
@@ -84,9 +84,6 @@ async def analyze_test_impact(
Reads the coverage map and graph reachability for *changed_files*, then
hands both to :func:`assemble_test_impact`.
"""
- from repowise.core.analysis.test_reachability import load_test_files, tests_reaching_by_tier
- from repowise.core.persistence.crud import get_test_coverage_summary, tests_covering
-
changed = _changed_paths(changed_files, exclude_spec)
fold = {
"repository_id": repository_id,
@@ -97,6 +94,30 @@ async def analyze_test_impact(
if not changed:
return assemble_test_impact([], {}, {}, {}, **fold)
+ summary, measured, coverage_error = await _read_coverage(session, repository_id, changed)
+ indexed_commit, indexed_commit_error = await _read_indexed_commit(session, repository_id)
+ inferred, test_files, inference_error = await _read_inferred(session, repository_id, changed)
+
+ return assemble_test_impact(
+ changed,
+ measured,
+ inferred,
+ summary,
+ repository_test_files=test_files,
+ indexed_commit=indexed_commit,
+ indexed_commit_error=indexed_commit_error,
+ coverage_error=coverage_error,
+ inference_error=inference_error,
+ **fold,
+ )
+
+
+async def _read_coverage(
+ session: AsyncSession, repository_id: str, changed: list[str]
+) -> tuple[Mapping[str, Any], dict[str, list[Mapping[str, Any]]], str | None]:
+ """The coverage summary, each changed path's measured rows, and the last read error."""
+ from repowise.core.persistence.crud import get_test_coverage_summary, tests_covering
+
try:
summary = await get_test_coverage_summary(session, repository_id)
coverage_error: str | None = None
@@ -104,17 +125,6 @@ async def analyze_test_impact(
summary = {}
coverage_error = type(exc).__name__
- indexed_commit_error: str | None = None
- try:
- indexed_commit = (
- await session.execute(
- select(Repository.head_commit).where(Repository.id == repository_id)
- )
- ).scalar_one_or_none()
- except Exception as exc:
- indexed_commit = None
- indexed_commit_error = type(exc).__name__
-
measured: dict[str, list[Mapping[str, Any]]] = {}
if summary.get("pair_count", 0) > 0:
for path in changed:
@@ -124,6 +134,29 @@ async def analyze_test_impact(
)
except Exception as exc:
coverage_error = type(exc).__name__
+ return summary, measured, coverage_error
+
+
+async def _read_indexed_commit(
+ session: AsyncSession, repository_id: str
+) -> tuple[str | None, str | None]:
+ """The repository's indexed head commit, or ``None`` and the read error."""
+ try:
+ indexed_commit = (
+ await session.execute(
+ select(Repository.head_commit).where(Repository.id == repository_id)
+ )
+ ).scalar_one_or_none()
+ except Exception as exc:
+ return None, type(exc).__name__
+ return indexed_commit, None
+
+
+async def _read_inferred(
+ session: AsyncSession, repository_id: str, changed: list[str]
+) -> tuple[dict[str, dict[str, Any]], set[str], str | None]:
+ """The tests the graph walk reaches per changed path, the test files, and the walk error."""
+ from repowise.core.analysis.test_reachability import load_test_files, tests_reaching_by_tier
inference_error: str | None = None
test_files: set[str] = set()
@@ -146,19 +179,7 @@ async def analyze_test_impact(
for path, tests in (await narrow_scopes(session, repository_id, walked, test_files)).items():
if tests != walked[path]:
inferred[path] = {**inferred[path], "tests": tests, "narrowed": True}
-
- return assemble_test_impact(
- changed,
- measured,
- inferred,
- summary,
- repository_test_files=test_files,
- indexed_commit=indexed_commit,
- indexed_commit_error=indexed_commit_error,
- coverage_error=coverage_error,
- inference_error=inference_error,
- **fold,
- )
+ return inferred, test_files, inference_error
def _changed_paths(changed_files: Iterable[str], exclude_spec: Any) -> list[str]:
diff --git a/packages/core/src/repowise/core/analysis/test_reachability.py b/packages/core/src/repowise/core/analysis/test_reachability.py
index 18edafd61..752ecc675 100644
--- a/packages/core/src/repowise/core/analysis/test_reachability.py
+++ b/packages/core/src/repowise/core/analysis/test_reachability.py
@@ -768,24 +768,7 @@ async def _call_reaching(
seeds share the walk. The file tiers keep the historical batch semantics.
"""
# node -> seed -> call hops from the node to the seed.
- origins: dict[str, dict[str, int]] = {}
- # A caller that knows which symbol in the file it cares about enters there,
- # so a test reaching an unrelated symbol in the same file does not count.
- # An empty entry names no symbol, so that file is unseeded and keeps the
- # ``defines`` lookup; treating it as seeded would silence the file entirely.
- seeded = {
- seed: symbol_seeds[seed]
- for seed in seeds
- if symbol_seeds and symbol_seeds.get(seed)
- }
- for seed, symbol_ids in seeded.items():
- for symbol in symbol_ids:
- origins.setdefault(symbol, {})[seed] = 0
- unseeded = [seed for seed in seeds if seed not in seeded]
- if unseeded:
- declared = await _edges_from(session, repo_id, unseeded, ["defines"])
- for seed, symbol in declared:
- origins.setdefault(symbol, {})[seed] = 0
+ origins = await _call_origins(session, repo_id, seeds, symbol_seeds)
if not origins:
return {}
@@ -809,31 +792,15 @@ async def _call_reaching(
continue
owner = file_of_symbol(caller)
if owner in test_files:
- for seed, distance in carried.items():
- hops = distance + 1
- by_test = found.setdefault(seed, {})
- best = by_test.get(owner)
- if best is None or hops < best[0]:
- by_test[owner] = (hops, {caller})
- elif hops == best[0]:
- best[1].add(caller)
+ _record_test_hit(found, owner, caller, carried)
# A test is a leaf. Walking through one would let "test A calls
# shared helper B" drag B's unrelated targets in.
continue
if owner in avoid:
# A hub carries on only the seeds it declares itself.
carried = {s: d for s, d in carried.items() if s.split("::", 1)[0] == owner}
- if not carried:
- continue
- known = origins.setdefault(caller, {})
- closer = {
- seed: distance + 1
- for seed, distance in carried.items()
- if seed not in known or distance + 1 < known[seed]
- }
- if not closer:
+ if not _carry_closer(origins, caller, carried):
continue
- known.update(closer)
if caller not in queued:
queued.add(caller)
frontier.append(caller)
@@ -843,6 +810,67 @@ async def _call_reaching(
}
+async def _call_origins(
+ session: AsyncSession,
+ repo_id: str,
+ seeds: list[str],
+ symbol_seeds: Mapping[str, Collection[str]] | None,
+) -> dict[str, dict[str, int]]:
+ """The symbols each seed enters the call walk at, each at distance 0 from its seed."""
+ origins: dict[str, dict[str, int]] = {}
+ # A caller that knows which symbol in the file it cares about enters there,
+ # so a test reaching an unrelated symbol in the same file does not count.
+ # An empty entry names no symbol, so that file is unseeded and keeps the
+ # ``defines`` lookup; treating it as seeded would silence the file entirely.
+ seeded = {
+ seed: symbol_seeds[seed]
+ for seed in seeds
+ if symbol_seeds and symbol_seeds.get(seed)
+ }
+ for seed, symbol_ids in seeded.items():
+ for symbol in symbol_ids:
+ origins.setdefault(symbol, {})[seed] = 0
+ unseeded = [seed for seed in seeds if seed not in seeded]
+ if unseeded:
+ declared = await _edges_from(session, repo_id, unseeded, ["defines"])
+ for seed, symbol in declared:
+ origins.setdefault(symbol, {})[seed] = 0
+ return origins
+
+
+def _record_test_hit(
+ found: dict[str, dict[str, tuple[int, set[str]]]],
+ test: str,
+ caller: str,
+ carried: Mapping[str, int],
+) -> None:
+ """Keep the fewest hops *test* took to each carried seed, and its callers at that depth."""
+ for seed, distance in carried.items():
+ hops = distance + 1
+ by_test = found.setdefault(seed, {})
+ best = by_test.get(test)
+ if best is None or hops < best[0]:
+ by_test[test] = (hops, {caller})
+ elif hops == best[0]:
+ best[1].add(caller)
+
+
+def _carry_closer(
+ origins: dict[str, dict[str, int]], caller: str, carried: Mapping[str, int]
+) -> bool:
+ """Record the seeds *caller* reaches closer than before; True when any did."""
+ if not carried:
+ return False
+ known = origins.setdefault(caller, {})
+ closer = {
+ seed: distance + 1
+ for seed, distance in carried.items()
+ if seed not in known or distance + 1 < known[seed]
+ }
+ known.update(closer)
+ return bool(closer)
+
+
async def reach_into_symbols(
session: AsyncSession,
repo_id: str,
@@ -894,17 +922,28 @@ async def imported_names_by_test(
elif PurePosixPath(source).name in BARREL_FILENAMES:
barrels.setdefault(source, {})[target] = names
if barrels:
- for source, barrel, names in await _imported_names(session, repo_id, sorted(barrels)):
- if source not in test_files:
- continue
- for target, exported in barrels[barrel].items():
- # ``export *`` passes every name on; otherwise only the ones it re-exports.
- passed = names if not exported or "*" in exported else names & exported
- if passed or not names:
- _add_names(out, target, source, passed)
+ await _add_barrel_imports(session, repo_id, barrels, test_files, out)
return out
+async def _add_barrel_imports(
+ session: AsyncSession,
+ repo_id: str,
+ barrels: dict[str, dict[str, frozenset[str]]],
+ test_files: set[str],
+ out: dict[str, dict[str, frozenset[str]]],
+) -> None:
+ """Credit each test importing a barrel with the names the barrel passes on from each file."""
+ for source, barrel, names in await _imported_names(session, repo_id, sorted(barrels)):
+ if source not in test_files:
+ continue
+ for target, exported in barrels[barrel].items():
+ # ``export *`` passes every name on; otherwise only the ones it re-exports.
+ passed = names if not exported or "*" in exported else names & exported
+ if passed or not names:
+ _add_names(out, target, source, passed)
+
+
def _add_names(
out: dict[str, dict[str, frozenset[str]]], target: str, test: str, names: frozenset[str]
) -> None:
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py b/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py
index 7e6e773c7..7dfe083d7 100644
--- a/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py
@@ -291,23 +291,25 @@ def _add_conftest_edges(
conf_dir = Path(conf).parent.as_posix()
prefix = f"{conf_dir}/" if conf_dir != "." else ""
for p in path_set:
- if p == conf:
+ if p == conf or not _sees_conftest(graph, p, prefix, roots):
continue
- if roots is not None and not (
- Path(p).name == "conftest.py" or (p.endswith(".py") and roots.may_collect_name(p))
- ):
- continue
- node = graph.nodes.get(p, {})
- if not node.get("is_test", False):
- continue
- if (p.startswith(prefix) or (prefix == "" and "/" not in p)) and _add_edge_if_new(
- graph, p, conf
- ):
+ if _add_edge_if_new(graph, p, conf):
graph[p][conf]["hint_source"] = CONFTEST_HINT
count += 1
return count
+def _sees_conftest(graph: nx.DiGraph, p: str, prefix: str, roots: PytestRoots | None) -> bool:
+ """Whether *p* is a collected test file or conftest under the conftest directory *prefix*."""
+ if roots is not None and not (
+ Path(p).name == "conftest.py" or (p.endswith(".py") and roots.may_collect_name(p))
+ ):
+ return False
+ if not graph.nodes.get(p, {}).get("is_test", False):
+ return False
+ return p.startswith(prefix) or (prefix == "" and "/" not in p)
+
+
def fixture_declaration(name: str, decorators: Iterable[str]) -> tuple[str, bool] | None:
"""``(registered name, autouse)`` when *decorators* declare a pytest fixture.