120 lines
6.3 KiB
Python
120 lines
6.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)
|
|
# go test's report of a failed assertion: ` pick_test.go:42: got 3, want 4`, under the `--- FAIL: TestPick (0.00s)` of its test.
|
|
ASSERTION = re.compile(r"^( *)\S*_test\.go:\d+:(.*)$")
|
|
FAILED_TEST = re.compile(r"^\s*--- FAIL: (\S+)", re.MULTILINE)
|
|
|
|
|
|
class Mutant(typing.NamedTuple):
|
|
"""A mutant Gremlins' tests killed: 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 names the line it adds after, and one that only removes the line the gap is after.
|
|
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 message of the first assertion that failed in go test's output, without the test's
|
|
name, file or line, so it says what goes wrong and not where. None where no assertion failed, as on a panic, a build that
|
|
broke or a passing run, or where its message says nothing beyond the test."""
|
|
lines = output.splitlines()
|
|
# 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_TEST.findall(output) for part in (name, name.partition("/")[0])}
|
|
for index, line in enumerate(lines):
|
|
if found := ASSERTION.match(line):
|
|
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(test in text for test in tests)]
|
|
return "\n".join(kept) or None
|
|
return None
|