:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
"""Per-team census of the repo's test files, resolved through the distributed owners.yaml
map. A static read of the tree, so it carries the denominator the signal-only CI spans cannot."""
import re
from collections.abc import Iterable
from dataclasses import dataclass
from pathlib import Path, PurePosixPath
from .resolver import OwnersResolver, Resolution
UNOWNED_TEAM = "unowned"
_PYTEST_FILE = re.compile(r"(^|/)test_[^/]*\.py$|_test\.py$")
_JEST_FILE = re.compile(r"\.(test|spec)\.[jt]sx?$")
def first_team_owner(owners: list[str] | None) -> str:
"""First owner that is a team slug; an ``@handle`` is a person, not a team."""
return next((owner for owner in owners or [] if not owner.startswith("@")), "")
def owning_team(resolution: Resolution) -> str | None:
"""``None`` for an ``owners: null`` exemption (SPEC 3.1), ``unowned`` when no team owns the file."""
if resolution.unowned_by_design:
return None
return first_team_owner(resolution.owners) or UNOWNED_TEAM
def runner_for_path(path: str) -> str | None:
if _PYTEST_FILE.search(path):
return "pytest"
if _JEST_FILE.search(path):
return "jest"
return None
def unowned_test_directories(resolver: OwnersResolver, paths: Iterable[str]) -> set[str]:
"""Directories, not files, so the baseline holds while files come and go. A file owned only by
``@handles`` counts: a person is not a routing target."""
return {
str(PurePosixPath(path).parent)
for path in paths
if runner_for_path(path) and owning_team(resolver.resolve(path)) == UNOWNED_TEAM
}
@dataclass(frozen=True, kw_only=True, slots=True)
class TeamTestCensus:
owner_team: str
pytest_file_count: int
jest_file_count: int
@property
def test_file_count(self) -> int:
return self.pytest_file_count + self.jest_file_count
def as_payload(self) -> dict[str, int | str]:
"""The wire shape shared by ``owners:census --json`` and the census events."""
return {
"owner_team": self.owner_team,
"pytest_file_count": self.pytest_file_count,
"jest_file_count": self.jest_file_count,
"test_file_count": self.test_file_count,
}
def census(paths: Iterable[str], repo_root: Path) -> list[TeamTestCensus]:
"""Count test files per owning team over repo-relative ``paths``, most tests first.
Uncovered files and files owned only by ``@handles`` count under ``unowned``, and files that
``owners: null`` exempts are left out."""
resolver = OwnersResolver(repo_root)
counts: dict[str, dict[str, int]] = {}
for path in paths:
runner = runner_for_path(path)
if runner is None:
continue
team = owning_team(resolver.resolve(path))
if team is None:
continue
counts.setdefault(team, {"pytest": 0, "jest": 0})[runner] += 1
return sorted(
(
TeamTestCensus(
owner_team=team,
pytest_file_count=by_runner["pytest"],
jest_file_count=by_runner["jest"],
)
for team, by_runner in counts.items()
),
key=lambda c: (-c.test_file_count, c.owner_team),
)