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.

fix(core): a test-named module pytest does not collect is not a test (#3218)

Raghav Chamadiya8a7cd156ba51
diff --git a/docs/start/CI.md b/docs/start/CI.md
index 13b3e8df4..bf6ca9006 100644
--- a/docs/start/CI.md
+++ b/docs/start/CI.md
@@ -256,7 +256,7 @@ a per-test coverage map (`repowise coverage add`) makes it more precise.
- It also runs everything for: non-code files in a test tree; a changed file with no known test or only a filename guess; a route through a test helper no test imports; any Python test helper while a conftest or pytest config loads plugins by name.
- It also runs everything for: a missing, out-of-date or unreadable index; a per-test map at its row cap; a deleted file no known test used.
- Only `docs/` and root README-like files (README, CHANGELOG, LICENSE, CONTRIBUTING) are skipped, and only when no code names them (a test that reads `README.md` names it); an empty line means only those changed.
-- Tests the graph cannot see into (not indexed, or importing nothing it resolves, such as a test that only runs a subprocess) run with every subset. With `--runner pytest`, test-named modules outside `testpaths` and outside test directories are left out, as a bare `pytest` leaves them out.
+- Tests the graph cannot see into (not indexed, or importing nothing it resolves, such as a test that only runs a subprocess) run with every subset. A Python module named like a test but outside every test directory is not a test when the nearest pytest config's `testpaths` or `python_files` leaves it out, as a bare `pytest` does: it is selected through like any other code. The rule follows a bare `pytest` run, so a test that CI only names explicitly on the command line outside `testpaths` is not seen as one: list it in `tests.always_run`.
- A changed `conftest.py`, test package `__init__.py` or imported test helper selects the tests under or importing it, not everything.
- 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.
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 1a0f3dfcc..bb8f11c3d 100644
--- a/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py
@@ -31,6 +31,7 @@ from __future__ import annotations
import sys
from pathlib import Path
+from typing import TYPE_CHECKING, NamedTuple
import click
from rich.table import Table
@@ -54,6 +55,9 @@ from repowise.cli.helpers import (
from repowise.cli.output import emit_json
from repowise.core.analysis.test_selection import RUNNERS
+if TYPE_CHECKING:
+ from repowise.core.pytest_roots import PytestRoots
+
# The whole reverse-import closure: a test importing a module that imports the
# changed file runs it too. No depth limit: the walk ends when no node gains a
# new seed, so a cut-off can never drop a test.
@@ -119,10 +123,11 @@ def impacted_tests_command(
except CannotEvaluateError as exc:
cannot_evaluate(fmt, exc.code, str(exc))
- result = run_async(_collect(repo_path, change))
+ checkout = _read_checkout(repo_path)
+ result = run_async(_collect(repo_path, change, checkout.roots))
result["diff"] = change.label
if config is not None:
- result["selection"] = _select(repo_path, change, result, config)
+ result["selection"] = _select(repo_path, change, result, config, checkout)
_render(result, fmt, runner)
@@ -147,7 +152,28 @@ def _selection_config(repo_path):
raise CannotEvaluateError("config_invalid", str(exc)) from exc
-async def _collect(repo_path, change) -> dict:
+class _Checkout(NamedTuple):
+ """Tracked paths and the pytest files among them, each read once per run."""
+
+ tracked: list[str]
+ pytest_texts: list[tuple[str, str]]
+ roots: PytestRoots
+
+
+def _read_checkout(repo_path) -> _Checkout:
+ """Tracked files, their conftests and pytest configs, and pytest's collection roots."""
+ from repowise.core import git_refs
+ from repowise.core.analysis.test_selection import is_scan_source
+ from repowise.core.pytest_roots import PYTEST_CONFIG_NAMES, read_pytest_roots
+
+ root = Path(repo_path)
+ tracked = git_refs.tracked_paths(str(root))
+ texts = list(_texts(root, [p for p in tracked if is_scan_source(p)], pytest_only=True))
+ roots = read_pytest_roots((p, t) for p, t in texts if Path(p).name in PYTEST_CONFIG_NAMES)
+ return _Checkout(tracked, texts, roots)
+
+
+async def _collect(repo_path, change, roots: PytestRoots | None = None) -> dict:
"""Resolve the change's files to impacted tests + labelled fallbacks."""
from repowise.core.persistence.crud import (
get_health_metrics,
@@ -177,7 +203,9 @@ async def _collect(repo_path, change) -> dict:
# Repo file keys back the filename-pattern fallback (same source the
# aggregate coverage ingest resolves against).
repo_keys = {m.file_path for m in await get_health_metrics(session, repo_id)}
- await _resolve_impacted(session, repo_id, _query_lines(change, measured), repo_keys, out)
+ await _resolve_impacted(
+ session, repo_id, _query_lines(change, measured), repo_keys, out, roots
+ )
await _place_tests(session, repo_id, out)
return out
@@ -264,6 +292,7 @@ async def _resolve_impacted(
changed: dict[str, set[int] | None],
repo_keys: set[str],
out: dict,
+ roots: PytestRoots | None = None,
) -> dict:
"""Classify each changed file: covered tests, inferred tests, or unknown.
@@ -314,7 +343,7 @@ async def _resolve_impacted(
return out
try:
- candidates, importers = await _graph_candidates(session, repo_id, graph_targets)
+ candidates, importers = await _graph_candidates(session, repo_id, graph_targets, roots)
except Exception as exc:
# Nothing the graph said can be trusted; selection runs everything.
out["graph_error"] = f"{type(exc).__name__}: {exc}"
@@ -344,7 +373,7 @@ async def _resolve_impacted(
async def _graph_candidates(
- session, repo_id: str, targets: list[str]
+ session, repo_id: str, targets: list[str], roots: PytestRoots | None = None
) -> tuple[dict[str, list], dict[str, list[str]]]:
"""``{target: [(test file, via), ...]}`` from the graph, and each test file's importers.
@@ -363,7 +392,7 @@ async def _graph_candidates(
from repowise.core.analysis.test_selection import is_code_file
test_files = {f for f in await load_test_files(session, repo_id) if is_code_file(f)}
- found = await _tier_picks(session, repo_id, targets, test_files)
+ found = await _tier_picks(session, repo_id, targets, test_files, roots)
seeds = {t for picks in found.values() for t in picks}
parents = await _test_importers(session, repo_id, seeds, test_files)
out = {target: _with_importers(picks, parents) for target, picks in found.items()}
@@ -376,11 +405,11 @@ def _all_tests(reached) -> tuple[str, ...]:
async def _tier_picks(
- session, repo_id: str, targets: list[str], test_files: set[str]
+ session, repo_id: str, targets: list[str], test_files: set[str], roots: PytestRoots | None
) -> dict[str, dict[str, str]]:
"""``{target: {test file: via}}``: a changed test itself, then the call and import walks."""
from repowise.core.analysis.test_reachability import tests_reaching_by_tier
- from repowise.core.test_paths import is_test_path
+ from repowise.core.analysis.test_selection import is_runnable_test
reaching = await tests_reaching_by_tier(session, repo_id, targets, test_files=test_files)
importers = await tests_reaching_by_tier(
@@ -394,7 +423,8 @@ async def _tier_picks(
found: dict[str, dict[str, str]] = {}
for target in targets:
picks = found.setdefault(target, {})
- if target in test_files or is_test_path(target):
+ # A test the index has not seen yet (new in this change) is still its own pick.
+ if target in test_files or is_runnable_test(target, roots):
picks[target] = "changed-test"
for reached, via in ((reaching.get(target), None), (importers.get(target), "import-graph")):
for t in _all_tests(reached) if reached else ():
@@ -437,9 +467,8 @@ def _with_importers(picks: dict[str, str], parents: dict[str, set[str]]) -> list
return list(picks.items())
-def _select(repo_path, change, result: dict, config):
+def _select(repo_path, change, result: dict, config, checkout: _Checkout):
"""The run-all-or-subset decision for ``--format args`` / ``json``."""
- from repowise.core import git_refs
from repowise.core.analysis.changed_lines import index_gap
from repowise.core.analysis.test_selection import (
SelectionInput,
@@ -448,19 +477,18 @@ def _select(repo_path, change, result: dict, config):
is_runnable_test,
is_scan_source,
plugin_loader,
- pytest_testpaths,
select_tests,
)
named = {g["test_file"] for g in result["inferred"]}
named |= {i["test_file"] for i in result["covered"].values() if i["test_file"]}
root = Path(repo_path)
- tracked = git_refs.tracked_paths(str(root))
+ tracked = checkout.tracked
go_test_dirs = {str(Path(p).parent.as_posix()) for p in tracked if p.endswith("_test.go")}
- known_tests = sorted(p for p in tracked if is_runnable_test(p))
placed = result["placed_tests"]
docs = [p for p in (*change.files, *change.deleted) if is_documentation(p)]
sources = [p for p in tracked if is_scan_source(p)]
+ known_tests = sorted(p for p in tracked if is_runnable_test(p, checkout.roots))
return select_tests(
SelectionInput(
changed=change.files,
@@ -478,9 +506,8 @@ def _select(repo_path, change, result: dict, config):
go_test_dirs=go_test_dirs,
known_tests=known_tests,
doc_readers=doc_readers(docs, _texts(root, sources)) if docs else {},
- plugin_loader=plugin_loader(_texts(root, sources, pytest_only=True)),
+ plugin_loader=plugin_loader(checkout.pytest_texts),
unplaced_tests=[] if placed is None else [t for t in known_tests if t not in placed],
- pytest_testpaths=pytest_testpaths(lambda name: _text(root / name)),
)
)
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/base.py b/packages/core/src/repowise/core/analysis/health/biomarkers/base.py
index 6287efaaa..9f571d640 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/base.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/base.py
@@ -6,6 +6,7 @@ from dataclasses import dataclass, field
from typing import Any, Protocol
from ....ingestion.git_indexer.function_blame import BlameIndex
+from ....test_paths import is_test_related_path
from ...graph_view import HasEdge
from ..complexity import ClassComplexity, ErrorHandlingHit, FunctionComplexity, PerfHit
from ..duplication import ClonePair
@@ -26,6 +27,10 @@ class FileContext:
nloc: int
has_test_file: bool
module: str | None
+ # The ``is_test`` flag ingestion stamped (path, language and pytest config).
+ # ``None`` for a context built without it, which falls back to the path
+ # rules; read it through :attr:`is_test_material`.
+ is_test: bool | None = None
# Every walked function in this file, in document order. Not keyed by
# name: a name key drops all but one of a file's anonymous ``it``
# callbacks, and every same-named method on a second class beside it.
@@ -123,6 +128,13 @@ class FileContext:
# Empty without a call graph, which leaves the marker as it was.
cross_file_oracle_lines: frozenset[int] = frozenset()
+ @property
+ def is_test_material(self) -> bool:
+ """The stored ``is_test`` flag, or the path rules when none was given."""
+ if self.is_test is not None:
+ return self.is_test
+ return is_test_related_path(self.file_path, self.language)
+
# A repo whose trailing-90-day window has at most this many active human
# contributors is a "small team": concentrated ownership there is the normal
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/co_change_scatter.py b/packages/core/src/repowise/core/analysis/health/biomarkers/co_change_scatter.py
index 93c00b2f0..d54e7a45a 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/co_change_scatter.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/co_change_scatter.py
@@ -39,7 +39,6 @@ from pathlib import PurePosixPath
from typing import Any
from ....co_change import parse_partners
-from ....test_paths import is_test_related_path
from ...dead_code.file_reachability import BARREL_FILENAMES
from ..models import Severity
from ..semantics import format_top_percentile
@@ -83,7 +82,7 @@ class CoChangeScatterDetector:
def detect(self, ctx: FileContext) -> list[BiomarkerResult]:
meta: dict[str, Any] = ctx.git_meta or {}
- if is_test_related_path(ctx.file_path, ctx.language) or _is_barrel(ctx.file_path):
+ if ctx.is_test_material or _is_barrel(ctx.file_path):
return []
percentile = _as_float(meta.get("co_change_scatter_pct"))
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py
index 8addc7b31..426b7bef9 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py
@@ -13,7 +13,6 @@ does nothing — the absence-of-coverage case is the
from __future__ import annotations
-from ....test_paths import is_test_related_path
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -32,7 +31,7 @@ class CoverageGapDetector:
return []
if ctx.total_coverable_lines <= 0:
return []
- if is_test_related_path(ctx.file_path, ctx.language):
+ if ctx.is_test_material:
return []
cov = ctx.line_coverage_pct
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py
index 6a1410220..5db6467b4 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py
@@ -27,7 +27,6 @@ the has-tests / hotspot gates.
from __future__ import annotations
-from ....test_paths import is_test_related_path
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -59,7 +58,7 @@ class CoverageGradientDetector:
if cov is None:
# No coverage data -> silent. Absent is not the same as uncovered.
return []
- if is_test_related_path(ctx.file_path, ctx.language):
+ if ctx.is_test_material:
return []
uncovered_fraction = max(0.0, (100.0 - float(cov)) / 100.0)
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/hidden_coupling.py b/packages/core/src/repowise/core/analysis/health/biomarkers/hidden_coupling.py
index 9a20e402b..8bfd963c2 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/hidden_coupling.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/hidden_coupling.py
@@ -34,7 +34,7 @@ short-circuit is explicit so backfill behavior is testable.
from __future__ import annotations
from ....co_change import STRUCTURAL_UNEXPLAINED, parse_partners
-from ....test_paths import is_test_related_path, is_test_to_production_pair
+from ....test_paths import is_test_to_production_pair
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -89,7 +89,7 @@ class HiddenCouplingDetector:
# A test file has no undeclared coupling worth reporting: its
# production partners are expected to co-change, and two tests moving
# together is the suite tracking the code, not a hidden dependency.
- if is_test_related_path(ctx.file_path, ctx.language):
+ if ctx.is_test_material:
return []
meta = ctx.git_meta or {}
partners = parse_partners(meta.get("co_change_partners_json"))
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py
index 1b193742a..5cb8aed53 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py
@@ -24,7 +24,6 @@ is an accusation.
from __future__ import annotations
from ....ingestion.git_indexer.fix_shape import is_code_path
-from ....test_paths import is_test_related_path
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -60,7 +59,7 @@ class UntestedHotspotDetector:
# A test file is not undertested. Coverage reports rarely instrument
# the suite itself, so where coverage is ingested every test file reads
# as 0% covered and a churning one would be accused of it.
- if is_test_related_path(ctx.file_path, ctx.language):
+ if ctx.is_test_material:
return []
if not _has_something_to_test(ctx):
return []
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index 8d0fdac6c..0061adaef 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -48,7 +48,6 @@ from .asserts.oracle_reach import collect_cross_file_oracles
from .biomarkers import FileContext, detect_all
from .complexity import FileComplexity, FunctionComplexity, walk_file
from .complexity.languages import has_health_dialect
-from .coverage import is_test_file as _coverage_is_test_file
from .dataflow import FileDataflowCache
from .duplication import ClonePair, DuplicationReport
from .duplication.detector import clone_ranges, union_line_count
@@ -110,7 +109,14 @@ log = structlog.get_logger(__name__)
# Not a licence to move a calibrated scoring weight — those are frozen
# independently of this stamp.
#
-# Current stamp (v38): Split File's co-change edge reads per-function commit
+# Current stamp (v39): a Python module named like a test outside every test
+# directory is production code when the nearest pytest config's ``testpaths``
+# or ``python_files`` leaves it out (``core/test_selection.py``). Its
+# ``is_test`` flag flips, and the biomarkers and ``has_test_file`` now read that
+# stored flag instead of re-classifying the path, so such a module joins the
+# production scores and findings.
+#
+# v38: Split File's co-change edge reads per-function commit
# sets (the 50 newest distinct commits of each function, stored on
# ``git_function_blame``), and a class takes the union of its methods' sets
# instead of blame over its whole span. Group membership is a kernel input to
@@ -380,7 +386,7 @@ log = structlog.get_logger(__name__)
# forms. Files that were counted untested and are not become tested, which
# moves untested-hotspot findings and the scores that carry them, on every
# language with a prefix or spec convention rather than Ruby alone.
-HEALTH_ANALYZER_VERSION = 38
+HEALTH_ANALYZER_VERSION = 39
def _mark_deprecated(
@@ -1567,7 +1573,7 @@ class HealthAnalyzer:
)
clone_sources = (
_clone_sources(file_path, source_lines, clones, self._abs_paths, self.read_source)
- if source_lines is not None and _coverage_is_test_file(file_path)
+ if source_lines is not None and pf.file_info.is_test
else {}
)
@@ -1584,14 +1590,12 @@ class HealthAnalyzer:
language=pf.file_info.language,
nloc=nloc,
# ``pf.file_info.is_test`` is the decision ingestion already made
- # for this file, with its language in hand — read it rather than
- # re-derive from the path string (#1103). The coverage check stays
- # because it also sniffs framework imports out of the source, which
- # a path cannot tell you.
+ # for this file, with its language and pytest config in hand; read
+ # it rather than re-derive from the path string (#1103).
has_test_file=file_path in paired_tests
or pf.file_info.is_test
- or _coverage_is_test_file(file_path)
or fcx.has_inline_tests,
+ is_test=bool(pf.file_info.is_test),
# Kept separate from ``has_test_file`` on purpose. That flag means
# "a test imports this file, or is named for it"; this one means "the
# call graph records a test reaching this file". They disagree
diff --git a/packages/core/src/repowise/core/analysis/health/history_refresh.py b/packages/core/src/repowise/core/analysis/health/history_refresh.py
index db0d1204c..1faa3c728 100644
--- a/packages/core/src/repowise/core/analysis/health/history_refresh.py
+++ b/packages/core/src/repowise/core/analysis/health/history_refresh.py
@@ -126,6 +126,8 @@ def refresh_history(
nloc=int(field(metric, "nloc", 0) or 0),
has_test_file=bool(field(metric, "has_test_file", False)),
module=field(metric, "module"),
+ # The stored row's flag; ``None`` on an older row falls back to the path.
+ is_test=field(metric, "is_test", None),
git_meta=git_meta,
repo_active_contributors_90d=repo_active_contributors_90d,
)
diff --git a/packages/core/src/repowise/core/analysis/knowledge_graph.py b/packages/core/src/repowise/core/analysis/knowledge_graph.py
index 36789270d..259da1c3f 100644
--- a/packages/core/src/repowise/core/analysis/knowledge_graph.py
+++ b/packages/core/src/repowise/core/analysis/knowledge_graph.py
@@ -218,7 +218,9 @@ def _slugify(text: str) -> str:
# reaches the export instead of being dropped.
# "5": which files are tests changed (``repowise.core.test_paths``), moving the
# ``test`` tag on file nodes without moving a node or edge count.
-KG_BUILDER_VERSION = "5"
+# "6": a test-named Python module that pytest's config does not collect is no
+# longer a test, moving its ``test`` tag.
+KG_BUILDER_VERSION = "6"
# An unmapped type is dropped from the export entirely (see the
# `if not kg_type: continue` below), which is silent. Six real types used to be
diff --git a/packages/core/src/repowise/core/analysis/test_selection.py b/packages/core/src/repowise/core/analysis/test_selection.py
index 5fe17563d..912acc2cc 100644
--- a/packages/core/src/repowise/core/analysis/test_selection.py
+++ b/packages/core/src/repowise/core/analysis/test_selection.py
@@ -45,18 +45,16 @@ a pipeline must branch on the run-all flag rather than rely on the sentinel.
from __future__ import annotations
-import configparser
import re
import shlex
-import tomllib
-from collections.abc import Callable, Collection, Iterable, Mapping
+from collections.abc import Collection, Iterable, Mapping
from dataclasses import dataclass, field
-from fnmatch import fnmatchcase
from pathlib import PurePosixPath
from typing import Any
import pathspec
+from ..pytest_roots import PYTEST_CONFIG_NAMES, PytestRoots
from ..support_paths import DOC_EXTENSIONS
from ..test_paths import is_test_path, is_test_related_path, is_test_support_path
@@ -244,7 +242,6 @@ class Selection:
always_run: tuple[str, ...] = ()
skipped_files: tuple[str, ...] = ()
basis: Mapping[str, str] = field(default_factory=dict)
- pytest_testpaths: tuple[str, ...] = ()
def to_dict(self) -> dict[str, Any]:
return {
@@ -277,8 +274,6 @@ def is_documentation(path: str) -> bool:
)
-# pytest config a doctest glob or a plugin can be declared in.
-_PYTEST_CONFIGS = frozenset({"pyproject.toml", "setup.cfg", "pytest.ini", "tox.ini"})
# ``pytest_plugins = [...]`` or ``-p name`` (not ``-p no:name``, which disables one).
_PLUGIN_DECLARATION = re.compile(r"\bpytest_plugins\b|(?:^|[\s\"'=])-p\s*(?!no:)[A-Za-z_]")
@@ -286,7 +281,7 @@ _PLUGIN_DECLARATION = re.compile(r"\bpytest_plugins\b|(?:^|[\s\"'=])-p\s*(?!no:)
def is_scan_source(path: str) -> bool:
"""Code or pytest config: the files :func:`doc_readers` and :func:`plugin_loader` read."""
name = PurePosixPath(path).name
- return is_code_file(path) or name in _PYTEST_CONFIGS
+ return is_code_file(path) or name in PYTEST_CONFIG_NAMES
def doc_readers(docs: Collection[str], sources: Iterable[tuple[str, str]]) -> dict[str, str]:
@@ -319,7 +314,7 @@ def plugin_loader(sources: Iterable[tuple[str, str]]) -> str | None:
"""
for path, text in sources:
name = PurePosixPath(path).name
- pytest_file = name == "conftest.py" or name in _PYTEST_CONFIGS
+ pytest_file = name == "conftest.py" or name in PYTEST_CONFIG_NAMES
if pytest_file and _PLUGIN_DECLARATION.search(text):
return path
return None
@@ -358,10 +353,18 @@ def is_test_helper(path: str) -> bool:
)
-def is_runnable_test(path: str) -> bool:
- """A test-shaped file name with an extension a runner collects tests from."""
+def is_runnable_test(path: str, roots: PytestRoots | None = None) -> bool:
+ """A test-shaped file name with an extension a runner collects tests from.
+
+ With *roots*, a Python file pytest's config leaves out of collection
+ (``core/test_paths.py``) is not one: the same rule that stamps ``is_test``.
+ """
p = PurePosixPath(path)
- return p.suffix.lower() in _TEST_CODE_SUFFIXES and is_test_path(p.name)
+ return (
+ p.suffix.lower() in _TEST_CODE_SUFFIXES
+ and is_test_path(p.name)
+ and (roots is None or is_test_path(path, roots=roots))
+ )
def full_run_reason(path: str, extra: Iterable[str] = ()) -> str | None:
@@ -419,7 +422,6 @@ class SelectionInput:
doc_readers: Mapping[str, str] = field(default_factory=dict)
plugin_loader: str | None = None
unplaced_tests: Collection[str] = ()
- pytest_testpaths: tuple[str, ...] = ()
@dataclass
@@ -460,7 +462,6 @@ def select_tests(inp: SelectionInput) -> Selection:
always_run=inp.config.always_run,
skipped_files=tuple(triage.skipped),
basis=basis,
- pytest_testpaths=inp.pytest_testpaths,
)
@@ -842,67 +843,7 @@ def resolve_runner(selection: Selection, runner: str) -> str:
def _pytest_args(selection: Selection) -> list[str]:
- roots = selection.pytest_testpaths
- return [
- t
- for t in selection.tests
- if (f := t.split("::", 1)[0]).endswith(_PYTHON) and _pytest_collects(f, roots)
- ]
-
-
-def _pytest_collects(path: str, testpaths: tuple[str, ...]) -> bool:
- """Whether a bare ``pytest`` would run *path*, so passing it adds nothing new.
-
- A test-shaped module outside ``testpaths`` and outside any test tree is
- production code (``core/test_paths.py``) that pytest would fail to collect;
- a full run never runs it, so neither does a subset.
- """
- p = PurePosixPath(path)
- # A neutral file name asks about the directory alone: in a test tree, keep it.
- if not testpaths or is_test_related_path(str(p.parent / "__init__.py")):
- return True
- candidates = [path, *(str(parent) for parent in p.parents)]
- return any(fnmatchcase(c, root) for root in testpaths for c in candidates)
-
-
-# Where pytest reads ``testpaths`` from, in its own order: the first file that
-# holds the section wins, and a ``pytest.ini`` wins even without one.
-_PYTEST_SECTIONS = (
- ("pytest.ini", "pytest"),
- ("pyproject.toml", None),
- ("tox.ini", "pytest"),
- ("setup.cfg", "tool:pytest"),
-)
-
-
-def pytest_testpaths(read: Callable[[str], str | None]) -> tuple[str, ...]:
- """``testpaths`` from the root pytest config (*read* returns a file's text or None).
-
- ``()`` when unset or unreadable, which filters nothing.
- """
- for name, section in _PYTEST_SECTIONS:
- if (text := read(name)) is None:
- continue
- try:
- value = _testpaths_value(text, section, always=name == "pytest.ini")
- except (tomllib.TOMLDecodeError, configparser.Error):
- return ()
- if value is not None:
- paths = value.split() if isinstance(value, str) else value
- return tuple(str(v).strip("/").removeprefix("./") or "." for v in paths)
- return ()
-
-
-def _testpaths_value(text: str, section: str | None, *, always: bool) -> Any:
- """The raw ``testpaths`` of one config, ``None`` when it has no pytest section."""
- if section is None:
- block = tomllib.loads(text).get("tool", {}).get("pytest", {}).get("ini_options")
- return None if block is None else block.get("testpaths", ())
- parser = configparser.ConfigParser(interpolation=None)
- parser.read_string(text)
- if not parser.has_section(section):
- return "" if always else None
- return parser.get(section, "testpaths", fallback="")
+ return [t for t in selection.tests if t.split("::", 1)[0].endswith(_PYTHON)]
def _go_args(selection: Selection) -> list[str]:
diff --git a/packages/core/src/repowise/core/ingestion/traverser.py b/packages/core/src/repowise/core/ingestion/traverser.py
index 08a72e4a9..b65fb50cf 100644
--- a/packages/core/src/repowise/core/ingestion/traverser.py
+++ b/packages/core/src/repowise/core/ingestion/traverser.py
@@ -31,6 +31,7 @@ from pathspec.patterns.gitwildmatch import GitWildMatchPattern, GitWildMatchPatt
from ..code_origin import is_generated_header
from ..entry_candidacy import conventional_entry_stems, not_an_execution_start
+from ..pytest_roots import PYTEST_CONFIG_NAMES, PytestRoots, pytest_options, pytest_roots
from ..test_paths import is_test_related_path
from .languages.registry import REGISTRY as _LANG_REGISTRY
from .languages.specs.cpp import INCLUDE_FRAGMENT_EXTENSIONS
@@ -779,7 +780,9 @@ class FileTraverser:
_is_console_script_target(rel_str, self._console_script_modules)
or rel_str in self._distribution_inits
)
- is_test = is_test_related_path(rel_str, language)
+ is_test = is_test_related_path(
+ rel_str, language, self._console_script_tables().pytest_roots
+ )
entry = _is_entry_point(rel_str, abs_path, language) or manifest_entry
return FileInfo(
path=rel_str,
@@ -1114,7 +1117,7 @@ def _is_entry_point(rel_str: str, abs_path: Path, language: str) -> bool:
class ConsoleScriptTables(NamedTuple):
- """What one pass over the repo's ``pyproject.toml`` files yields."""
+ """What one pass over the repo's Python project configs yields."""
names: frozenset[str]
"""Launcher names an installer drops in the environment's script dir."""
@@ -1124,6 +1127,8 @@ class ConsoleScriptTables(NamedTuple):
"""``[project].name`` values — the distributions this repo installs as."""
package_inits: frozenset[str]
"""Repo-relative ``__init__.py`` of each distribution's own import package."""
+ pytest_roots: PytestRoots = PytestRoots()
+ """Where a bare pytest run collects tests, which decides a test-named module."""
def _collect_console_scripts(
@@ -1135,7 +1140,9 @@ def _collect_console_scripts(
``[project.entry-points.*]`` group from each ``pyproject.toml`` in the
repo. In ``name = "pkg.module:func"`` the key is the launcher and the part
before the colon is the module it imports; ``[project].name`` is the
- distribution. Best-effort: unparsable files are skipped.
+ distribution. Best-effort: unparsable files are skipped. The pytest
+ configs (``pytest.ini``, ``tox.ini``, ``setup.cfg`` and the same
+ ``pyproject.toml``) come off this one walk and one read each.
"""
from repowise.core.fs_walk import iter_glob
@@ -1143,14 +1150,17 @@ def _collect_console_scripts(
modules: set[str] = set()
distributions: set[str] = set()
inits: set[str] = set()
+ pytest_configs: list[tuple[str, dict]] = []
try:
config_files = list(
- iter_glob(repo_root, ("pyproject.toml",), prune_nested_git=prune_nested_git)
+ iter_glob(repo_root, tuple(PYTEST_CONFIG_NAMES), prune_nested_git=prune_nested_git)
)
except OSError:
return ConsoleScriptTables(frozenset(), frozenset(), frozenset(), frozenset())
for config_file in config_files:
- project = _pyproject_project_table(config_file)
+ project, options = _read_python_config(config_file)
+ if options is not None:
+ pytest_configs.append((config_file.relative_to(repo_root).as_posix(), options))
if project is None:
continue
dist = project.get("name")
@@ -1160,7 +1170,11 @@ def _collect_console_scripts(
inits.add(init)
_add_script_targets(project, names, modules)
return ConsoleScriptTables(
- frozenset(names), frozenset(modules), frozenset(distributions), frozenset(inits)
+ frozenset(names),
+ frozenset(modules),
+ frozenset(distributions),
+ frozenset(inits),
+ pytest_roots(pytest_configs),
)
@@ -1179,16 +1193,20 @@ def _distribution_init(repo_root: Path, base: Path, dist: str) -> str | None:
return init.relative_to(repo_root).as_posix()
-def _pyproject_project_table(config_file: Path) -> dict | None:
- """The ``[project]`` table of one pyproject.toml, or None if unreadable or absent."""
+def _read_python_config(config_file: Path) -> tuple[dict | None, dict | None]:
+ """``([project] table, pytest options)`` of one config, each None if absent or unreadable."""
import tomllib
try:
- data = tomllib.loads(config_file.read_text(encoding="utf-8"))
+ text = config_file.read_text(encoding="utf-8")
+ if config_file.name != "pyproject.toml":
+ return None, pytest_options(config_file.name, text)
+ data = tomllib.loads(text)
except Exception:
- return None
+ return None, None
project = data.get("project")
- return project if isinstance(project, dict) else None
+ options = pytest_options(config_file.name, toml=data)
+ return (project if isinstance(project, dict) else None), options
def _add_script_targets(project: dict, names: set[str], modules: set[str]) -> None:
diff --git a/packages/core/src/repowise/core/pytest_roots.py b/packages/core/src/repowise/core/pytest_roots.py
new file mode 100644
index 000000000..1f09e071d
--- /dev/null
+++ b/packages/core/src/repowise/core/pytest_roots.py
@@ -0,0 +1,152 @@
+"""Where a bare ``pytest`` run collects tests from, read from its config files.
+
+A Python module named like a test (``core/test_paths.py``, ``cli/approvals_test.py``)
+is production code when pytest's own configuration keeps it out of collection:
+``testpaths`` names the directories a bare run searches, and ``python_files`` the
+file names it collects. :mod:`.test_paths` asks :meth:`PytestRoots.collects`
+before trusting a test-shaped Python name outside any test directory.
+
+Pure: callers hand in config text (or the already-parsed ``pyproject.toml``
+table), so ingestion reads each config once in the walk it already makes and a
+server can call this with data it holds.
+
+Each config governs its own directory, nearest first, the way running ``pytest``
+inside a package picks up that package's config. Within one directory pytest's
+precedence applies: ``pytest.ini`` (even with no section), then
+``pyproject.toml``, ``tox.ini``, ``setup.cfg``. A nested config's ``testpaths``
+are read against its own directory and against the repository root, because a
+CI job may pass it with ``-c`` from the root (``pytest -c skills/pyproject.toml
+skills``), and a file any config above it collects (by ``testpaths`` and
+``python_files``) counts as collected, so a doubt keeps a test a test.
+
+The rule follows a bare ``pytest`` run. A file a CI job only names explicitly on
+the command line, outside every ``testpaths``, is not seen as a test; list such
+files in ``tests.always_run``. Ceiling: ``python_files`` patterns are matched
+against the file name only, and a pattern with a directory part is skipped
+rather than allowed to exclude a file; none seen so far.
+"""
+
+from __future__ import annotations
+
+import configparser
+import tomllib
+from collections.abc import Iterable, Mapping
+from dataclasses import dataclass, field
+from fnmatch import fnmatchcase
+from pathlib import PurePosixPath
+from typing import Any
+
+# The files pytest reads options from, in its own precedence order, with the
+# section that holds them (``None``: the ``[tool.pytest.ini_options]`` table).
+PYTEST_CONFIG_SECTIONS: tuple[tuple[str, str | None], ...] = (
+ ("pytest.ini", "pytest"),
+ ("pyproject.toml", None),
+ ("tox.ini", "pytest"),
+ ("setup.cfg", "tool:pytest"),
+)
+PYTEST_CONFIG_NAMES: frozenset[str] = frozenset(name for name, _ in PYTEST_CONFIG_SECTIONS)
+_PRECEDENCE = {name: rank for rank, (name, _) in enumerate(PYTEST_CONFIG_SECTIONS)}
+
+
+@dataclass(frozen=True, slots=True)
+class _Collection:
+ testpaths: tuple[str, ...]
+ python_files: tuple[str, ...]
+
+
+@dataclass(frozen=True, slots=True)
+class PytestRoots:
+ """``testpaths`` and ``python_files`` for each directory holding a pytest config."""
+
+ by_dir: Mapping[str, _Collection] = field(default_factory=dict)
+
+ def collects(self, path: str) -> bool | None:
+ """Whether a bare ``pytest`` run collects *path*; ``None`` when no config governs it.
+
+ Collected when any config at or above it collects it (see the module
+ docstring), so only a unanimous "no" demotes a test.
+ """
+ p = PurePosixPath(path)
+ verdicts = [
+ _collected(p, p.relative_to(parent), rules)
+ for parent in p.parents
+ if (rules := self.by_dir.get("" if str(parent) == "." else str(parent))) is not None
+ ]
+ return any(verdicts) if verdicts else None
+
+
+def _collected(path: PurePosixPath, rel: PurePosixPath, rules: _Collection) -> bool:
+ # A pattern with a directory part is skipped (see the module ceiling).
+ patterns = [pat for pat in rules.python_files if "/" not in pat]
+ if patterns and not any(fnmatchcase(path.name, pat) for pat in patterns):
+ return False
+ if not rules.testpaths or "." in rules.testpaths:
+ return True
+ candidates = {str(c) for p in (rel, path) for c in (p, *p.parents) if str(c) != "."}
+ return any(fnmatchcase(c, root) for root in rules.testpaths for c in candidates)
+
+
+def pytest_options(
+ name: str, text: str = "", *, toml: Mapping[str, Any] | None = None
+) -> dict[str, Any] | None:
+ """The pytest options of one config file, or ``None`` when it configures no pytest.
+
+ *toml* is ``pyproject.toml`` already parsed, so a caller that read it for
+ another table does not parse it twice. A file that does not parse yields
+ ``None``, as it would leave pytest to its defaults here.
+ """
+ section = dict(PYTEST_CONFIG_SECTIONS).get(name, "")
+ if section == "":
+ return None
+ try:
+ if section is None:
+ data = toml if toml is not None else tomllib.loads(text)
+ block = data.get("tool", {}).get("pytest", {}).get("ini_options")
+ return block if isinstance(block, dict) else None
+ parser = configparser.ConfigParser(interpolation=None)
+ parser.read_string(text)
+ except (tomllib.TOMLDecodeError, configparser.Error, AttributeError):
+ return None
+ if not parser.has_section(section):
+ return {} if name == "pytest.ini" else None
+ return dict(parser.items(section))
+
+
+def pytest_roots(configs: Iterable[tuple[str, Mapping[str, Any]]]) -> PytestRoots:
+ """Build from ``(config path, options)`` pairs (:func:`pytest_options`).
+
+ The highest-precedence config in each directory wins.
+ """
+ chosen: dict[str, tuple[int, Mapping[str, Any]]] = {}
+ for path, options in configs:
+ p = PurePosixPath(path)
+ key = "" if str(p.parent) == "." else str(p.parent)
+ rank = _PRECEDENCE.get(p.name, len(_PRECEDENCE))
+ if key not in chosen or rank < chosen[key][0]:
+ chosen[key] = (rank, options)
+ return PytestRoots(
+ {
+ key: _Collection(
+ tuple(v.strip("/").removeprefix("./") or "." for v in _words(o.get("testpaths"))),
+ _words(o.get("python_files")),
+ )
+ for key, (_, o) in chosen.items()
+ }
+ )
+
+
+def read_pytest_roots(texts: Iterable[tuple[str, str]]) -> PytestRoots:
+ """:func:`pytest_roots` from ``(config path, text)`` pairs."""
+ return pytest_roots(
+ (path, options)
+ for path, text in texts
+ if (options := pytest_options(PurePosixPath(path).name, text)) is not None
+ )
+
+
+def _words(value: Any) -> tuple[str, ...]:
+ """An ini value is whitespace-separated; a TOML one is a list (or a string)."""
+ if not value:
+ return ()
+ items = value.split() if isinstance(value, str) else value
+ return tuple(str(v) for v in items if str(v).strip())
diff --git a/packages/core/src/repowise/core/test_paths.py b/packages/core/src/repowise/core/test_paths.py
index 92f84bdf0..ad58e4e3e 100644
--- a/packages/core/src/repowise/core/test_paths.py
+++ b/packages/core/src/repowise/core/test_paths.py
@@ -42,6 +42,16 @@ are test trees while ``latest/`` and ``contest/`` stay single words that are not
**Filename rules are source rules.** ``test_``, ``_test`` and ``.test.`` name a
test only on a source-language file: ``tsconfig.test.json`` and
``workflows/release.test.yml`` are configuration that happens to share the word.
+
+**A Python test name needs pytest to agree.** ``test_x.py`` outside any test
+directory is a test only if pytest would collect it: when the nearest pytest
+config sets ``testpaths`` or ``python_files`` that leave it out, it is a
+production module named for what it does (``core/test_selection.py``). Pass
+*roots* (:mod:`.pytest_roots`) for that; without it the name decides, which is
+what pytest does with no config. Other languages keep their name rules: a Go
+``_test.go`` or a ``.test.ts`` is a test by its toolchain's own convention.
+Ingestion passes *roots* when it stamps ``is_test``; path-only callers that
+pass none answer by name, so read the stored flag where one is at hand.
"""
from __future__ import annotations
@@ -50,6 +60,10 @@ import re
from dataclasses import dataclass
from functools import cache
from pathlib import PurePath, PurePosixPath
+from typing import TYPE_CHECKING
+
+if TYPE_CHECKING:
+ from .pytest_roots import PytestRoots
# Directory segments that mark every file beneath them as test material,
# whatever the filename. ``__test__`` is the Jest variant of ``__tests__``;
@@ -360,7 +374,7 @@ def _has_wildcard_pair(
)
-def _classify(path: str, language: str | None) -> str:
+def _classify(path: str, language: str | None, roots: PytestRoots | None = None) -> str:
"""``"test"``, ``"support"``, or ``""`` for production code.
One traversal, so the two public predicates cannot disagree with each other
@@ -392,7 +406,9 @@ def _classify(path: str, language: str | None) -> str:
if _REPO_METADATA_DIR in segments:
return "test" if named_test else ""
- if not named_test and not _is_test_dir(segments, original_segments, filename, language):
+ # A test name outside a test directory still needs pytest to collect it.
+ needs_test_dir = not named_test or _pytest_skips(path, filename, roots)
+ if needs_test_dir and not _is_test_dir(segments, original_segments, filename, language):
return ""
# Inside test material. A test-shaped filename wins; otherwise a
@@ -402,29 +418,45 @@ def _classify(path: str, language: str | None) -> str:
return "test"
-def is_test_path(path: str, language: str | None = None) -> bool:
+def _pytest_skips(path: str, filename: str, roots: PytestRoots | None) -> bool:
+ """Whether pytest's config keeps a test-named Python file out of collection."""
+ return (
+ roots is not None
+ and PurePosixPath(filename).suffix.lower() == ".py"
+ and roots.collects(path.replace("\\", "/")) is False
+ )
+
+
+def is_test_path(
+ path: str, language: str | None = None, roots: PytestRoots | None = None
+) -> bool:
"""Whether *path* is a test.
Test *support* (``conftest.py``, ``tests/factories/user.py``) is
deliberately not a test here - see :func:`is_test_support_path`. Pass
*language* when it is known: it decides the ambiguous ``spec/``/``t/``
cases, which are RSpec for Ruby and a Perl-style test tree for Python,
- and specification or miscellaneous folders for everything else.
+ and specification or miscellaneous folders for everything else. Pass
+ *roots* to let pytest's config overrule a test-shaped Python name.
"""
- return _classify(path, language) == "test"
+ return _classify(path, language, roots) == "test"
-def is_test_support_path(path: str, language: str | None = None) -> bool:
+def is_test_support_path(
+ path: str, language: str | None = None, roots: PytestRoots | None = None
+) -> bool:
"""Whether *path* is test infrastructure rather than a test.
``conftest.py``, ``spec_helper.rb``, ``FooFixtures.java``, and the
scaffolding directories inside a test tree (``tests/factories/user.py``).
Never true at the same time as :func:`is_test_path`.
"""
- return _classify(path, language) == "support"
+ return _classify(path, language, roots) == "support"
-def is_test_related_path(path: str, language: str | None = None) -> bool:
+def is_test_related_path(
+ path: str, language: str | None = None, roots: PytestRoots | None = None
+) -> bool:
"""Whether *path* is a test **or** test support.
This is what stamps ``FileInfo.is_test`` at ingestion, and what callers
@@ -432,7 +464,7 @@ def is_test_related_path(path: str, language: str | None = None) -> bool:
skipping files, a health biomarker exempting them. Callers that rank or
search should prefer :func:`is_test_path`, so fixtures stay findable.
"""
- return _classify(path, language) != ""
+ return _classify(path, language, roots) != ""
def is_unambiguous_test_path(path: str, language: str | None = None) -> bool:
diff --git a/tests/unit/analysis/test_kg_skip_logic.py b/tests/unit/analysis/test_kg_skip_logic.py
index dce342555..fd771ef8c 100644
--- a/tests/unit/analysis/test_kg_skip_logic.py
+++ b/tests/unit/analysis/test_kg_skip_logic.py
@@ -210,7 +210,7 @@ class TestFingerprintDeterminism:
one: whoever bumps the constant updates this line and sees, in the
diff, that they are asking every existing store to re-curate.
"""
- assert knowledge_graph.KG_BUILDER_VERSION == "5"
+ assert knowledge_graph.KG_BUILDER_VERSION == "6"
# ---------------------------------------------------------------------------
diff --git a/tests/unit/analysis/test_test_selection.py b/tests/unit/analysis/test_test_selection.py
index 2c59239e7..9950bc190 100644
--- a/tests/unit/analysis/test_test_selection.py
+++ b/tests/unit/analysis/test_test_selection.py
@@ -15,12 +15,13 @@ from repowise.core.analysis.test_selection import (
TestSelectionConfig,
doc_readers,
format_args,
+ is_runnable_test,
plugin_loader,
- pytest_testpaths,
resolve_runner,
runner_args,
select_tests,
)
+from repowise.core.pytest_roots import read_pytest_roots
_NONE = TestSelectionConfig()
@@ -669,33 +670,12 @@ def test_a_test_the_graph_cannot_see_into_runs_with_every_subset() -> None:
assert _select(["docs/guide.md"], unplaced_tests=["tests/test_cli.py"]).tests == ()
-def test_pytest_is_not_given_production_modules_outside_testpaths() -> None:
+def test_a_test_named_module_pytest_does_not_collect_is_not_runnable() -> None:
"""``core/test_paths.py`` is test-shaped, but a bare pytest never collects it."""
- tests = ("tests/test_a.py", "src/pkg/test_paths.py", "src/pkg/tests/test_b.py")
- sel = Selection(run_all=False, reasons=(), tests=tests, pytest_testpaths=("tests",))
- assert runner_args(sel, "pytest") == ["tests/test_a.py", "src/pkg/tests/test_b.py"]
- assert runner_args(Selection(run_all=False, reasons=(), tests=tests), "pytest") == list(tests)
-
-
- ("files", "expected"),
- [
- (
- {"pyproject.toml": '[tool.pytest.ini_options]\ntestpaths = ["tests", "./it/"]\n'},
- ("tests", "it"),
- ),
- (
- {"setup.cfg": "[tool:pytest]\ntestpaths = tests\n integration\n"},
- ("tests", "integration"),
- ),
- # pytest.ini wins even with no section, over a pyproject that has one.
- (
- {"pytest.ini": "", "pyproject.toml": '[tool.pytest.ini_options]\ntestpaths = ["t"]\n'},
- (),
- ),
- ({"pyproject.toml": "not toml ["}, ()),
- ({}, ()),
- ],
-)
-def test_pytest_testpaths_reads_the_config_pytest_reads(files, expected) -> None:
- assert pytest_testpaths(files.get) == expected
+ config = '[tool.pytest.ini_options]\ntestpaths = ["tests"]\n'
+ roots = read_pytest_roots([("pyproject.toml", config)])
+ assert not is_runnable_test("src/pkg/test_paths.py", roots)
+ assert is_runnable_test("tests/test_a.py", roots)
+ 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")
diff --git a/tests/unit/cli/test_impacted_tests_cmd.py b/tests/unit/cli/test_impacted_tests_cmd.py
index 574d3700c..0a3685b55 100644
--- a/tests/unit/cli/test_impacted_tests_cmd.py
+++ b/tests/unit/cli/test_impacted_tests_cmd.py
@@ -268,7 +268,8 @@ def test_tests_the_graph_cannot_see_run_and_production_modules_do_not(repo) -> N
result = _run(repo, "main...feat", "--format", "args", "--runner", "pytest")
assert result.exit_code == 0, result.output
assert result.stdout == "tests/test_a.py tests/test_cli.py\n"
- assert "2 test file(s) the graph cannot see into" in _err(result)
+ # src/test_util.py is production code, not a test the graph cannot see into.
+ assert "1 test file(s) the graph cannot see into" in _err(result)
def test_a_changed_test_package_init_selects_the_tests_under_it(repo) -> None:
diff --git a/tests/unit/health/test_coverage_biomarkers.py b/tests/unit/health/test_coverage_biomarkers.py
index 249493daf..0d0ed3718 100644
--- a/tests/unit/health/test_coverage_biomarkers.py
+++ b/tests/unit/health/test_coverage_biomarkers.py
@@ -166,6 +166,13 @@ def test_coverage_gap_skips_test_files() -> None:
assert CoverageGapDetector().detect(ctx) == []
+def test_coverage_gap_reads_the_stored_flag_over_the_path() -> None:
+ """A test-named production module (pytest does not collect it) is still scored."""
+ ctx = _ctx(path="src/pkg/test_selection.py", line_cov=10.0, total_lines=200)
+ ctx.is_test = False
+ assert CoverageGapDetector().detect(ctx)
+
+
def test_coverage_gap_skips_when_no_coverage_data() -> None:
ctx = _ctx(line_cov=None, total_lines=0)
assert CoverageGapDetector().detect(ctx) == []
diff --git a/tests/unit/health/test_model_version_coupling.py b/tests/unit/health/test_model_version_coupling.py
index 32cde161c..b46bb4ac8 100644
--- a/tests/unit/health/test_model_version_coupling.py
+++ b/tests/unit/health/test_model_version_coupling.py
@@ -22,7 +22,7 @@ from repowise.core.analysis.health.refactoring.identity import (
# The three stamps as they stand together. Moving either identity model means
# moving the analyzer with it, and updating this tuple in the same commit.
-_STAMPS = (38, 3, 4)
+_STAMPS = (39, 3, 4)
def test_the_identity_models_and_the_analyzer_stamp_are_pinned_together() -> None:
diff --git a/tests/unit/ingestion/test_traverser.py b/tests/unit/ingestion/test_traverser.py
index 40b77483b..657651ae7 100644
--- a/tests/unit/ingestion/test_traverser.py
+++ b/tests/unit/ingestion/test_traverser.py
@@ -382,6 +382,22 @@ class TestFileTraverser:
main_file = next(p for p in files if p.endswith("main.py"))
assert files[main_file].is_test is False
+ def test_a_test_named_module_outside_pytest_testpaths_is_production(
+ self, tmp_path: Path
+ ) -> None:
+ (tmp_path / "pyproject.toml").write_text(
+ '[project]\nname = "demo"\n[tool.pytest.ini_options]\ntestpaths = ["tests"]\n'
+ )
+ for rel in ("demo/test_selection.py", "tests/test_demo.py", "tools/test_cli.py"):
+ (tmp_path / rel).parent.mkdir(parents=True, exist_ok=True)
+ (tmp_path / rel).write_text("x = 1\n")
+ # A nested config governs its own directory.
+ (tmp_path / "tools" / "pytest.ini").write_text("[pytest]\n")
+ files = {f.path: f.is_test for f in FileTraverser(tmp_path).traverse()}
+ assert files["demo/test_selection.py"] is False
+ assert files["tests/test_demo.py"] is True
+ assert files["tools/test_cli.py"] is True
+
def test_file_info_fields(self, tmp_path: Path) -> None:
(tmp_path / "calc.py").write_text("class Calc: pass")
traverser = FileTraverser(tmp_path)
diff --git a/tests/unit/test_pytest_roots.py b/tests/unit/test_pytest_roots.py
new file mode 100644
index 000000000..961801e6a
--- /dev/null
+++ b/tests/unit/test_pytest_roots.py
@@ -0,0 +1,74 @@
+"""Where a bare pytest run collects from, read from the configs pytest reads."""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.pytest_roots import pytest_options, read_pytest_roots
+
+_PYPROJECT = '[tool.pytest.ini_options]\ntestpaths = ["tests", "./it/"]\n'
+
+
+ ("files", "path", "expected"),
+ [
+ ({"pyproject.toml": _PYPROJECT}, "tests/unit/test_a.py", True),
+ ({"pyproject.toml": _PYPROJECT}, "it/test_b.py", True),
+ ({"pyproject.toml": _PYPROJECT}, "pkg/core/test_paths.py", False),
+ ({"setup.cfg": "[tool:pytest]\ntestpaths = tests\n integration\n"}, "integration/test_x.py", True),
+ ({"tox.ini": "[pytest]\ntestpaths = tests\n"}, "app/test_x.py", False),
+ # testpaths may be globs
+ ({"pytest.ini": "[pytest]\ntestpaths = packages/*/tests\n"}, "packages/a/tests/test_x.py", True),
+ # python_files narrows the names, testpaths unset
+ ({"pytest.ini": "[pytest]\npython_files = check_*.py\n"}, "pkg/test_x.py", False),
+ ({"pytest.ini": "[pytest]\npython_files = test_*.py\n"}, "pkg/test_x.py", True),
+ # pytest.ini wins even with no section, over a pyproject that has one
+ ({"pytest.ini": "", "pyproject.toml": _PYPROJECT}, "pkg/test_x.py", True),
+ # no pytest section anywhere, or a config that does not parse: no opinion
+ ({"pyproject.toml": "[project]\nname = 'x'\n"}, "pkg/test_x.py", None),
+ ({"pyproject.toml": "not toml ["}, "pkg/test_x.py", None),
+ ({}, "pkg/test_x.py", None),
+ ],
+)
+def test_collects_follows_the_root_config(files, path, expected) -> None:
+ assert read_pytest_roots(files.items()).collects(path) is expected
+
+
+def test_the_nearest_config_governs_its_own_directory() -> None:
+ roots = read_pytest_roots(
+ [
+ ("pyproject.toml", '[tool.pytest.ini_options]\ntestpaths = ["tests"]\n'),
+ ("packages/lib/pytest.ini", "[pytest]\n"),
+ ]
+ )
+ assert roots.collects("packages/lib/lib/test_core.py") is True
+ assert roots.collects("packages/app/app/test_core.py") is False
+
+
+def test_a_nested_config_passed_from_the_root_keeps_its_tests() -> None:
+ """``pytest -c skills/pyproject.toml skills`` reads ``testpaths`` from the root."""
+ config = '[tool.pytest.ini_options]\ntestpaths = ["skills"]\n'
+ roots = read_pytest_roots([("skills/pyproject.toml", config)])
+ assert roots.collects("skills/creator/scripts/test_init.py") is True
+
+
+def test_a_file_an_ancestor_config_collects_stays_collected() -> None:
+ """A nested ``python_files`` narrows names, but the root config still collects it."""
+ roots = read_pytest_roots(
+ [
+ ("pytest.ini", "[pytest]\n"),
+ ("pkg/tox.ini", "[pytest]\npython_files = check_*.py\n"),
+ ]
+ )
+ assert roots.collects("pkg/sub/test_x.py") is True
+
+
+def test_a_python_files_pattern_with_a_directory_never_excludes() -> None:
+ roots = read_pytest_roots([("pytest.ini", "[pytest]\npython_files = tests/*.py\n")])
+ assert roots.collects("pkg/test_x.py") is True
+
+
+def test_options_come_from_an_already_parsed_pyproject() -> None:
+ parsed = {"tool": {"pytest": {"ini_options": {"testpaths": ["t"]}}}}
+ assert pytest_options("pyproject.toml", toml=parsed) == {"testpaths": ["t"]}
+ assert pytest_options("README.md", "[pytest]") is None
diff --git a/tests/unit/test_test_paths.py b/tests/unit/test_test_paths.py
index b95414c4c..8902f9ef0 100644
--- a/tests/unit/test_test_paths.py
+++ b/tests/unit/test_test_paths.py
@@ -10,6 +10,7 @@ from __future__ import annotations
import pytest
+from repowise.core.pytest_roots import read_pytest_roots
from repowise.core.test_paths import (
is_test_path,
is_test_related_path,
@@ -330,3 +331,33 @@ def test_unambiguous_test_paths_need_more_than_a_test_shaped_name_under_src(
path: str, expected: bool
) -> None:
assert is_unambiguous_test_path(path) is expected
+
+
+_ROOTS = read_pytest_roots(
+ [("pyproject.toml", '[tool.pytest.ini_options]\ntestpaths = ["tests"]\n')]
+)
+
+
+ ("path", "expected"),
+ [
+ # production modules named for what they do, outside testpaths
+ ("packages/core/src/repowise/core/test_paths.py", ""),
+ ("packages/core/src/repowise/core/analysis/test_selection.py", ""),
+ ("mytool/cli/approvals_test.py", ""),
+ # a test directory still settles it, inside testpaths or not
+ ("tests/unit/test_engine.py", "test"),
+ ("packages/ui/tests/test_a.py", "test"),
+ ("pkg/tests/helpers/test_builders.py", "test"),
+ ("conftest.py", "support"),
+ # pytest config says nothing about other languages
+ ("src/app/format.test.ts", "test"),
+ ("pkg/server/handler_test.go", "test"),
+ ],
+)
+def test_pytest_config_overrules_a_test_shaped_python_name(path: str, expected: str) -> None:
+ got = "test" if is_test_path(path, roots=_ROOTS) else ""
+ got = "support" if is_test_support_path(path, roots=_ROOTS) else got
+ assert got == expected
+ # Without the config the name decides, as it does for a bare pytest.
+ assert is_test_related_path(path)