Run mutation tests on a chunk's changed lines before review with bin/mutate-changes, and hand stability-reviewer the survivors
CI / tests (pull_request) Successful in 2m14s
CI / tests (pull_request) Successful in 2m14s
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""The setup.cfg that has mutmut mutate a branch's changed files, and the tests left out because they fail before any mutant."""
|
||||
import configparser
|
||||
import io
|
||||
import re
|
||||
|
||||
from failure import Failure
|
||||
|
||||
SUMMARY = re.compile(r"^=+ short test summary info =+$", re.MULTILINE)
|
||||
# A test pytest's short summary reports as failed or erroring; its message, where one fits, follows " - ".
|
||||
REPORTED = re.compile(r"^(?:ERROR|FAILED) (\S.*?)(?: - .*)?$", re.MULTILINE)
|
||||
|
||||
|
||||
class CaseKept(configparser.ConfigParser):
|
||||
"""Keeps each key as written, since the project's other sections are copied over unchanged."""
|
||||
|
||||
def optionxform(self, optionstr: str) -> str:
|
||||
return optionstr
|
||||
|
||||
|
||||
def mutmut_config(existing: str, sources: list[str], left: list[str]) -> str | Failure:
|
||||
"""The setup.cfg that has mutmut mutate the sources, copy the rest of the folder, which their tests import, and pass pytest the options that leave tests out.
|
||||
|
||||
existing is the project's own setup.cfg, empty where it has none. Its settings stay, though its comments go, and its [mutmut] pytest_add_cli_args come before the options that leave tests out."""
|
||||
config = CaseKept(interpolation=None)
|
||||
try:
|
||||
config.read_string(existing)
|
||||
except configparser.Error as failure:
|
||||
return Failure(f"could not read setup.cfg: {failure}")
|
||||
if not config.has_section("mutmut"):
|
||||
config.add_section("mutmut")
|
||||
section = config["mutmut"]
|
||||
section["source_paths"] = listed(sources)
|
||||
section["also_copy"] = "."
|
||||
if left:
|
||||
section["pytest_add_cli_args"] = "\n".join(filter(None, [section.get("pytest_add_cli_args", ""), listed(left)]))
|
||||
written = io.StringIO()
|
||||
config.write(written)
|
||||
return written.getvalue()
|
||||
|
||||
|
||||
def listed(items: list[str]) -> str:
|
||||
"""Items as a [mutmut] list, one per line, each % doubled: mutmut 3 reads setup.cfg with configparser's default interpolation."""
|
||||
return "\n".join(item.replace("%", "%%") for item in items)
|
||||
|
||||
|
||||
def left_out(output: str) -> list[str]:
|
||||
"""The pytest options that leave out each test a failed run's short summary reports: --deselect for a test, --ignore for a file that failed to collect."""
|
||||
summary = SUMMARY.split(output)
|
||||
if len(summary) < 2:
|
||||
return []
|
||||
return [f"--deselect={nodeid}" if "::" in nodeid else f"--ignore={nodeid}" for nodeid in REPORTED.findall(summary[-1])]
|
||||
Reference in New Issue
Block a user