68 lines
3.0 KiB
Python
68 lines
3.0 KiB
Python
"""Pick the bugs task-bench plants: the mutants the tests kill in the files that hold the places the comprehension seats found
|
|
hard, each with the failing test's message that tells Haiku what goes wrong. Each language has a module that runs its mutation
|
|
tool and reads its test runner's output."""
|
|
import random
|
|
import re
|
|
import typing
|
|
|
|
from failure import Failure
|
|
from task_bench import go_bugs, python_bugs
|
|
from mutate_changes.changed_lines import is_test
|
|
from task_bench.planted_mutant import Mutant
|
|
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+`")
|
|
|
|
|
|
class Language(typing.NamedTuple):
|
|
"""A language the bench plants bugs in, by its files' suffixes. killed takes a tree's top, the files to mutate there, what
|
|
runs the mutation tool and how many tests it may run at once, and returns the mutants the tree's tests kill. told takes the
|
|
test command's output over a planted bug, and returns the failing test's message, or None where it printed none."""
|
|
|
|
name: str
|
|
suffixes: tuple[str, ...]
|
|
killed: typing.Callable[[str, list[str], Run, int], list[Mutant] | Failure]
|
|
told: typing.Callable[[str], str | None]
|
|
|
|
|
|
LANGUAGES = (
|
|
Language("Go", (".go",), go_bugs.killed_mutants, go_bugs.task_text),
|
|
Language("Python", (".py",), python_bugs.killed_mutants, python_bugs.task_text),
|
|
)
|
|
|
|
|
|
def language_of(path: str, languages: tuple[Language, ...] = LANGUAGES) -> Language | None:
|
|
return next((language for language in languages if path.endswith(language.suffixes)), None)
|
|
|
|
|
|
def place_files(places: str) -> list[str]:
|
|
"""The files that hold a place in what `larv-review places` prints and are in a language of LANGUAGES, test files left out, in
|
|
the order of their paths."""
|
|
return sorted({path for path in PLACE.findall(places) if language_of(path) and not is_test(path)})
|
|
|
|
|
|
def killed_mutants(top: str, files: list[str], run: Run, jobs: int, languages: tuple[Language, ...] = LANGUAGES) -> list[Mutant] | Failure:
|
|
"""The mutants in `files`, which place_files named, that the tests of the tree at top kill: each language's tool runs over
|
|
the files in that language."""
|
|
killed: list[Mutant] = []
|
|
for language in languages:
|
|
own = [path for path in files if language_of(path, languages) is language]
|
|
found = language.killed(top, own, run, jobs) if own else []
|
|
if isinstance(found, Failure):
|
|
return found
|
|
killed += found
|
|
return killed
|
|
|
|
|
|
def drawn(mutants: list[Mutant], seed: int) -> list[Mutant]:
|
|
"""The mutants in the order the seed draws them, whatever order the tools listed them in."""
|
|
return random.Random(seed).sample(sorted(set(mutants)), len(set(mutants)))
|
|
|
|
|
|
def task_text(path: str, output: str, languages: tuple[Language, ...] = LANGUAGES) -> str | None:
|
|
"""What Haiku is told of a bug planted in the file at path, one place_files named. output is what the test command printed;
|
|
the file's language picks the failing test's message out of it. None where it finds none."""
|
|
language = language_of(path, languages)
|
|
return language.told(output) if language else None
|