76 lines
3.5 KiB
Python
76 lines
3.5 KiB
Python
"""A mutant task-bench plants as a bug, and how it is re-applied at the base and at the head: as the same change to the same lines."""
|
|
import re
|
|
import typing
|
|
|
|
from task_bench.figures import Side
|
|
|
|
HUNK = re.compile(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@", re.MULTILINE)
|
|
|
|
|
|
class Mutant(typing.NamedTuple):
|
|
"""A mutant the project's tests killed, as the lines it changes in the tree its tool ran in: the number of the first, from 1,
|
|
those lines as the tree has them, and what the mutant has in their place. Where it only adds lines, the number is that of the
|
|
line they go in before. name is what the report calls the bug it becomes: the path and line, then what the tool tells the
|
|
mutant by, which is Gremlins' column and mutator, or mutmut's name."""
|
|
|
|
path: str
|
|
line: int
|
|
name: str
|
|
was: tuple[bytes, ...]
|
|
now: tuple[bytes, ...]
|
|
|
|
|
|
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 mutant's lines hold no `\\r` where its tool read the file as text, as mutmut does, and hold it where the tool split the bytes on
|
|
`\\n`, as Gremlins' planter does. Both match a file's lines, and a new line without `\\r` gets the line end of the file's first line."""
|
|
lines = source.split(b"\n")
|
|
first = mutant.line - 1
|
|
end = first + len(mutant.was)
|
|
if not 0 <= first <= len(lines) or tuple(line.removesuffix(b"\r") for line in lines[first:end]) != tuple(line.removesuffix(b"\r") for line in mutant.was):
|
|
return None
|
|
line_end = b"\r" if lines[0].endswith(b"\r") else b""
|
|
new = [line if line.endswith(b"\r") else line + line_end for line in mutant.now]
|
|
# Where the mutant's lines end the file, the last of them has no line end.
|
|
if new and end >= len(lines):
|
|
new[-1] = mutant.now[-1]
|
|
lines[first:end] = new
|
|
return b"\n".join(lines)
|
|
|
|
|
|
def bug_at_sides(mutant: Mutant, diff: str, sources: dict[Side, bytes | None]) -> dict[Side, bytes] | None:
|
|
"""The file of mutant with the bug in it, at the base and at the head. None where the mutant doesn't become a bug at one side
|
|
or the other.
|
|
|
|
sources holds the file at each side, None where the side lacks it; diff is the file's diff from base to head.
|
|
|
|
The bug is the same change to the same lines at both sides, so only the code around it differs between them."""
|
|
at_head = line_at_head(diff, mutant.line)
|
|
if at_head is None:
|
|
return None
|
|
bugs: dict[Side, bytes] = {}
|
|
placings: tuple[tuple[Side, Mutant], ...] = (("base", mutant), ("head", mutant._replace(line=at_head)))
|
|
for side, placed in placings:
|
|
changed = None if (source := sources[side]) is None else mutated(source, placed)
|
|
if changed is None:
|
|
return None
|
|
bugs[side] = changed
|
|
return bugs
|
|
|
|
|
|
def line_at_head(diff: str, line: int) -> int | None:
|
|
"""The number at the head of a file's line `line` at the base, or None where the head changed or removed that line.
|
|
|
|
diff is `git diff -U0 <base> <head> -- <file>`, so each hunk holds changed lines only."""
|
|
moved = 0
|
|
for old_start, old_count, new_start, new_count in ((int(a), int(b or 1), int(c), int(d or 1)) for a, b, c, d in HUNK.findall(diff)):
|
|
# A hunk that only adds lines names the line it adds them after. One that only removes lines names the line the gap follows.
|
|
old_end = old_start + old_count if old_count else old_start + 1
|
|
new_end = new_start + new_count if new_count else new_start + 1
|
|
if old_count and old_start <= line < old_end:
|
|
return None
|
|
if line >= old_end:
|
|
moved = new_end - old_end
|
|
return line + moved
|