Plant a mutant in a Python file whose lines end in CRLF
CI / tests (pull_request) Successful in 4m8s

This commit is contained in:
2026-11-23 23:14:36 +01:00
parent fb3e526989
commit 4620365931
4 changed files with 41 additions and 4 deletions
+9 -3
View File
@@ -21,12 +21,18 @@ class Mutant(typing.NamedTuple):
def mutated(source: bytes, mutant: Mutant) -> bytes | None:
"""A file's source with the mutant applied, or None where the lines the mutant changes don't stand at the mutant's line."""
"""A file's source with the mutant applied, or None where the lines the mutant changes don't stand at the mutant's line.
The mutant's lines come from a file read as text, so they hold no `\\r`: they match a file's lines without theirs, and go in with
the line end the file uses."""
lines = source.split(b"\n")
first = mutant.line - 1
if not 0 <= first <= len(lines) or tuple(lines[first:first + len(mutant.was)]) != mutant.was:
end = first + len(mutant.was)
if not 0 <= first <= len(lines) or tuple(line.removesuffix(b"\r") for line in lines[first:end]) != mutant.was:
return None
lines[first:first + len(mutant.was)] = mutant.now
# The file's last line has no line end, so none of its lines takes one.
line_end = b"\r" if len(lines) > 1 and lines[0].endswith(b"\r") and end < len(lines) else b""
lines[first:end] = (line + line_end for line in mutant.now)
return b"\n".join(lines)