Test task-bench's docker calls, command line and planter where mutants survived
CI / tests (pull_request) Successful in 4m38s

This commit is contained in:
2026-11-21 01:39:12 +01:00
parent fed84cc510
commit ebbe926ab7
7 changed files with 316 additions and 35 deletions
+73 -1
View File
@@ -1,11 +1,15 @@
"""task-bench's planter: which files it mutates, which mutants it draws, how it re-applies one and what it tells Haiku."""
import json
import pathlib
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from task_bench.plant import drawn, line_at_head, Mutant, mutated, place_files, task_text
from task_bench.plant import drawn, go_files, killed_mutants, line_at_head, Mutant, mutated, place_files, task_text
from failure import Failure
from mutate_changes.tool_process import Ran
PLACES = """## 2026-11-19T22:33:15Z, PR #646 at a357cc7b
@@ -128,3 +132,71 @@ class LineAtHeadTest(unittest.TestCase):
def test_no_diff_keeps_every_line(self):
self.assertEqual(line_at_head("", 12), 12)
class KilledMutantsTest(unittest.TestCase):
def setUp(self):
folder = tempfile.TemporaryDirectory()
self.addCleanup(folder.cleanup)
self.top = pathlib.Path(folder.name)
for path in ("calc.go", "calc_test.go", "render/pick.go", "render/a+b.go", "render/notes.md", "cli/main.go"):
(self.top / path).parent.mkdir(parents=True, exist_ok=True)
(self.top / path).write_text("package x\n")
self.ran: list[tuple[list[str], str]] = []
def gremlins(self, report: dict | None, code: int = 0):
def run(argv: list[str], cwd: str) -> Ran:
self.ran.append((argv, cwd))
if report is not None:
pathlib.Path(cwd, argv[argv.index("--output") + 1]).write_text(json.dumps(report))
return Ran(code, "gremlins said so")
return run
def test_names_each_go_file_gremlins_would_mutate_by_its_path_from_the_top(self):
self.assertEqual(sorted(go_files(str(self.top))), ["calc.go", "cli/main.go", "render/a+b.go", "render/pick.go"])
def test_has_gremlins_leave_out_every_other_go_file_by_its_whole_path(self):
killed_mutants(str(self.top), ["render/pick.go"], self.gremlins({"files": []}), 3)
argv, cwd = self.ran[0]
self.assertEqual(cwd, str(self.top))
self.assertEqual(argv[:8], ["gremlins", "unleash", "--exclude-files", r"^calc\.go$", "--exclude-files", r"^cli/main\.go$", "--exclude-files", r"^render/a\+b\.go$"])
self.assertEqual((argv[8], argv[10:]), ("--output", ["--workers", "3"]))
def test_keeps_the_killed_mutants_of_the_files_asked_for(self):
report = {"files": [
{"file_name": "render/pick.go", "mutations": [{"line": 4, "column": 7, "status": "KILLED", "type": "ARITHMETIC_BASE"}, {"line": 5, "column": 2, "status": "LIVED", "type": "INVERT_LOGICAL"}, {"line": 6, "column": 2, "status": "NOT COVERED", "type": "INVERT_LOGICAL"}]},
{"file_name": "calc.go", "mutations": [{"line": 1, "column": 1, "status": "KILLED", "type": "ARITHMETIC_BASE"}]},
]}
self.assertEqual(killed_mutants(str(self.top), ["render/pick.go"], self.gremlins(report), 3), [Mutant("render/pick.go", 4, 7, "ARITHMETIC_BASE")])
def test_a_gremlins_that_fails_is_a_failure_with_what_it_printed(self):
found = killed_mutants(str(self.top), ["render/pick.go"], self.gremlins(None, code=1), 3)
self.assertIsInstance(found, Failure)
self.assertIn("gremlins said so", found.reason)
class EdgesTest(unittest.TestCase):
def test_mutates_the_last_line_of_a_file(self):
self.assertEqual(mutated(b"a\nb + c", Mutant("calc.go", 2, 3, "ARITHMETIC_BASE")), b"a\nb - c")
self.assertIsNone(mutated(b"a\nb + c", Mutant("calc.go", 3, 3, "ARITHMETIC_BASE")))
self.assertIsNone(mutated(b"a\nb + c", Mutant("calc.go", 0, 3, "ARITHMETIC_BASE")))
def test_takes_the_longest_token_at_the_column(self):
self.assertEqual(mutated(b"a >= b", Mutant("calc.go", 1, 3, "CONDITIONALS_BOUNDARY")), b"a > b")
self.assertEqual(mutated(b"a &^ b", Mutant("calc.go", 1, 3, "INVERT_BITWISE")), b"a & b")
def test_a_message_ends_at_the_next_line_indented_as_its_first(self):
output = "--- FAIL: TestPick/empty_input (0.00s)\n pick_test.go:42: got 3\n want 4\n pick_test.go:50: got 5\n"
self.assertEqual(task_text(output), "got 3\nwant 4")
def test_a_message_runs_to_the_end_of_the_output(self):
self.assertEqual(task_text("--- FAIL: TestPick (0.00s)\n pick_test.go:42: got 3\n want 4"), "got 3\nwant 4")
def test_a_message_may_name_a_part_of_a_subtests_name_that_is_no_test(self):
output = "--- FAIL: TestPick/empty (0.00s)\n pick_test.go:42: empty input: got 3\n in TestPick\n in TestPick/empty\n"
self.assertEqual(task_text(output), "empty input: got 3")
if __name__ == "__main__":
unittest.main()