Files

134 lines
7.3 KiB
Python

"""Pick the bugs task-bench plants: Gremlins' killed mutants in the Go files that hold the places the comprehension seats found hard,
re-applied from its report, each with the failing assertion's message that tells Haiku what goes wrong."""
import os
import random
import re
import typing
from failure import Failure
from mutate_changes.in_diff_tools import gremlins_mutants
from mutate_changes.changed_lines import is_test
from mutate_changes.tool_process import Run
# A place as `larv-review places` prints it: `<path>:<line>` in backticks.
PLACE = re.compile(r"`([^`\s:]+):\d+`")
# What each of Gremlins v0.6.0's mutators writes in place of a token: `tokenMutations` in its internal/engine/mappings.go, under the
# names its report gives the mutators, from internal/mutator/mutator.go.
MUTATIONS: dict[str, dict[bytes, bytes]] = {
"ARITHMETIC_BASE": {b"+": b"-", b"*": b"/", b"/": b"*", b"%": b"*", b"-": b"+"},
"CONDITIONALS_BOUNDARY": {b">=": b">", b">": b">=", b"<=": b"<", b"<": b"<="},
"CONDITIONALS_NEGATION": {b"==": b"!=", b">=": b"<", b">": b"<=", b"<=": b">", b"<": b">=", b"!=": b"=="},
"INCREMENT_DECREMENT": {b"--": b"++", b"++": b"--"},
"INVERT_ASSIGNMENTS": {b"+=": b"-=", b"*=": b"/=", b"/=": b"*=", b"-=": b"+="},
"INVERT_BITWISE": {b"&": b"|", b"|": b"&", b"^": b"&", b"&^": b"&", b"<<": b">>", b">>": b"<<"},
"INVERT_BWASSIGN": {b"&=": b"|=", b"|=": b"&=", b"^=": b"&=", b"&^=": b"&=", b"<<=": b">>=", b">>=": b"<<="},
"INVERT_LOGICAL": {b"&&": b"||", b"||": b"&&"},
"INVERT_LOOPCTRL": {b"break": b"continue", b"continue": b"break"},
"INVERT_NEGATIVES": {b"-": b"+"},
"REMOVE_SELF_ASSIGNMENTS": dict.fromkeys((b"+=", b"&=", b"&^=", b"*=", b"|=", b"/=", b"%=", b"<<=", b">>=", b"-=", b"^="), b"="),
}
HUNK = re.compile(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@", re.MULTILINE)
# What a test printed, as go test shows it: ` pick_test.go:42: got 3, want 4`. A failed assertion and a log line look the same.
PRINTED = re.compile(r"^( *)\S*_test\.go:\d+:(.*)$")
# The lines that say whose output follows: `=== RUN TestPick` with -v, and `--- FAIL: TestPick (0.00s)` with and without it.
TEST_MARK = re.compile(r"^( *)(?:=== (?:RUN|CONT|NAME)|--- (?:FAIL|PASS|SKIP):)\s+(\S+)")
FAILED_TEST = re.compile(r"^\s*--- FAIL: (\S+)", re.MULTILINE)
class Mutant(typing.NamedTuple):
"""A mutant the project's tests killed, as Gremlins reports it: the mutator and the token it changed, by its line and its column in bytes, both from 1."""
path: str
line: int
column: int
mutator: str
def place_files(places: str) -> list[str]:
"""The Go files that hold a place in what `larv-review places` prints, test files left out, in the order of their paths."""
return sorted({path for path in PLACE.findall(places) if path.endswith(".go") and not is_test(path)})
def killed_mutants(top: str, files: list[str], run: Run, jobs: int) -> list[Mutant] | Failure:
"""The mutants in `files` that the tests of the tree at top kill, as Gremlins finds them there.
Gremlins takes no list of files to mutate, only patterns to leave out, so it gets one for each other Go file of the tree."""
others = sorted(path for path in go_files(top) if path not in files)
found = gremlins_mutants(top, [argument for path in others for argument in ("--exclude-files", f"^{re.escape(path)}$")], run, jobs)
if isinstance(found, Failure):
return found
return [Mutant(mutant.path, mutant.line, mutant.column, mutant.mutator) for mutant in found if mutant.status == "KILLED" and mutant.path in files]
def go_files(top: str) -> list[str]:
"""Each Go file under top that Gremlins would mutate, by its path from top."""
paths = (os.path.relpath(os.path.join(folder, name), top) for folder, _, names in os.walk(top) for name in names)
return [path for path in paths if path.endswith(".go") and not is_test(path)]
def drawn(mutants: list[Mutant], seed: int) -> list[Mutant]:
"""The mutants in the order the seed draws them, whatever order Gremlins listed them in."""
return random.Random(seed).sample(sorted(set(mutants)), len(set(mutants)))
def mutated(source: bytes, mutant: Mutant) -> bytes | None:
"""A file's source with the mutant applied, or None where its line and column hold no token its mutator changes."""
lines = source.split(b"\n")
if not 1 <= mutant.line <= len(lines):
return None
line = lines[mutant.line - 1]
rest = line[mutant.column - 1:]
# The longest token first: `>=` holds `>`, and Gremlins names the mutant by the whole token.
token = max((token for token in MUTATIONS.get(mutant.mutator, {}) if rest.startswith(token)), key=len, default=None)
if token is None:
return None
lines[mutant.line - 1] = line[:mutant.column - 1] + MUTATIONS[mutant.mutator][token] + rest[len(token):]
return b"\n".join(lines)
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
def task_text(output: str) -> str | None:
"""What Haiku is told of a planted bug: the failing assertion's message in go test's output, without the test's name, file or
line, so it says what goes wrong and not where. That is the last message of the first failed test that printed one, since a
test logs before it asserts; a test with a failed subtest is left out, since it fails with the subtest. None where no failed test printed a message, as on a panic, a build that broke or a passing run,
or where the message only names the test."""
lines = output.splitlines()
failed = set(FAILED_TEST.findall(output))
# A subtest is named `TestPick/empty_input`; a message may name the whole or the test it belongs to.
tests = {part for name in failed for part in (name, name.partition("/")[0])}
marks: list[tuple[int, str]] = []
last: dict[str, tuple[int, re.Match[str]]] = {}
for index, line in enumerate(lines):
if mark := TEST_MARK.match(line):
marks.append((len(mark[1]), mark[2]))
elif printed := PRINTED.match(line):
# A test's output is indented deeper than its mark, so a line after a subtest's may be its parent's.
test = next((name for indent, name in reversed(marks) if indent < len(printed[1])), None)
if test is not None and test in failed and not any(other.startswith(f"{test}/") for other in failed):
last[test] = index, printed
if not last:
return None
index, found = next(iter(last.values()))
indent = len(found[1])
# A message's later lines are indented deeper than its first.
later = lines[index + 1:]
ends = next((at for at, text in enumerate(later) if len(text) - len(text.lstrip(" ")) <= indent), len(later))
message = [text.strip() for text in (found[2], *later[:ends])]
kept = [text for text in message if text and "_test.go" not in text and not any(name in text for name in tests)]
return "\n".join(kept) or None