Files
2026-11-20 23:24:21 +01:00

64 lines
3.0 KiB
Python

"""Which lines of a file a branch adds or changes, which mutation tool mutates the file, and in which folder."""
import os
import posixpath
import re
import typing
# A hunk header of `git diff -U0`. The new side's count is 0 where the hunk only deletes, and left out where it is 1.
HUNK = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@", re.MULTILINE)
STRYKER = (".cjs", ".cts", ".js", ".jsx", ".mjs", ".mts", ".ts", ".tsx")
TOOLS = {".go": "gremlins", ".php": "infection", ".py": "mutmut", ".rs": "cargo-mutants", **dict.fromkeys(STRYKER, "stryker")}
TEST_FOLDERS = frozenset({"__tests__", "test", "tests"})
TEST_NAME = re.compile(r"test_.*\.py|.*_test\.py|conftest\.py|.*_test\.go|.*\.(?:spec|test)\.[^.]+")
def added_lines(diff: str) -> frozenset[int]:
"""The line numbers, in the new file, that a `git diff -U0` of one file adds or changes."""
return frozenset(line for hunk in HUNK.finditer(diff) for line in range(int(hunk[1]), int(hunk[1]) + int(hunk[2] or 1)))
def runs(lines: typing.Iterable[int]) -> list[tuple[int, int]]:
"""The lines as runs of consecutive numbers, each as its first and last line, in order."""
found: list[tuple[int, int]] = []
for line in sorted(lines):
if found and found[-1][1] == line - 1:
found[-1] = (found[-1][0], line)
else:
found.append((line, line))
return found
def tool_for(path: str) -> str | None:
"""The tool that mutates a file, by its extension; None for a test file, or a language none of the tools covers."""
return None if is_test(path) else TOOLS.get(posixpath.splitext(path)[1])
def is_test(path: str) -> bool:
folders, name = posixpath.split(path)
return bool(TEST_FOLDERS & set(folders.split("/")) or TEST_NAME.fullmatch(name))
def not_mutated(paths: list[str]) -> list[str]:
"""A line naming the paths in a language no tool covers, then one naming the test files, leaving out a line that would name none."""
uncovered = [path for path in paths if tool_for(path) is None and not is_test(path)]
tests = [path for path in paths if is_test(path)]
lines = (("Not mutated, as no tool covers their language", uncovered), ("Not mutated, as test files", tests))
return [f"{text}: {', '.join(found)}." for text, found in lines if found]
def by_folder(top: str, changed: dict[str, frozenset[int]], configs: tuple[str, ...]) -> dict[str, dict[str, frozenset[int]]]:
"""The changed files by the nearest folder above each that holds one of configs, "." for top where none does, each file by its
path from that folder."""
grouped: dict[str, dict[str, frozenset[int]]] = {}
for path, lines in changed.items():
folder = posixpath.dirname(path)
while folder and not any(os.path.isfile(os.path.join(top, folder, name)) for name in configs):
folder = posixpath.dirname(folder)
grouped.setdefault(folder or ".", {})[posixpath.relpath(path, folder or ".")] = lines
return grouped
def label(folder: str) -> str:
"""A folder from the checkout's top, as a report names it."""
return "the checkout's top" if folder == "." else folder