Plant a mutant in a Python file whose lines end in CRLF #705

Merged
lilleman merged 7 commits from item-530 into main 2026-11-24 00:17:03 +01:00
5 changed files with 45 additions and 14 deletions
+1
View File
@@ -2,6 +2,7 @@
## Unreleased
- `task-bench` plants a Python mutant in a file whose lines end in CRLF, which it could not re-apply before, and a Go mutant there keeps applying.
- This repo documents its own `task-bench` command in the README → Test, with `task-bench-image/Dockerfile`, an image that holds pytest, git and tmux, so each scoring run in `larv-review` benches claude-config and records its figures.
- The stability reviewer now checks a lookup key built by joining path segments, or matched by prefix, for two inputs that give one key, such as a column named like its own table or the namespaces `ns` and `ns.sub`.
- The release review now goes over a project's bench features: it reapplies each reference patch and rewrites one that no longer applies, deletes a feature that no longer breaks a goal, and adds features until each place the comprehension seats find hardest has one.
+12 -3
View File
@@ -21,12 +21,21 @@ class Mutant(typing.NamedTuple):
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 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
if not 0 <= first <= len(lines) or tuple(lines[first:first + len(mutant.was)]) != mutant.was:
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
lines[first:first + len(mutant.was)] = mutant.now
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)
+23
View File
@@ -29,6 +29,29 @@ class MutatedTest(unittest.TestCase):
self.assertEqual(mutated(b"a\nb + c", Mutant("calc.go", 2, "x", (b"b + c",), (b"b - c",))), b"a\nb - c")
self.assertIsNone(mutated(b"a\nb + c", Mutant("calc.go", 3, "x", (b"b + c",), (b"b - c",))))
def test_a_crlf_file_matches_a_mutants_lines_without_cr_and_gets_its_new_lines_with_one(self):
source = b"a\r\n\tb + c\r\n\td,\r\n\te\r\n"
self.assertEqual(mutated(source, Mutant("calc.py", 2, "x", (b"\tb + c", b"\td,"), (b"\tb", b"\t- c", b"\td"))), b"a\r\n\tb\r\n\t- c\r\n\td\r\n\te\r\n")
self.assertEqual(mutated(source, Mutant("calc.py", 3, "x", (), (b"\tf,",))), b"a\r\n\tb + c\r\n\tf,\r\n\td,\r\n\te\r\n")
self.assertEqual(mutated(source, Mutant("calc.py", 3, "x", (b"\td,",), ())), b"a\r\n\tb + c\r\n\te\r\n")
self.assertIsNone(mutated(source, Mutant("calc.py", 2, "x", (b"\tb - c",), (b"z",))))
self.assertEqual(mutated(source, Mutant("calc.py", 2, "x", (b"\tb + c", b"\td,"), (b"x", b"y"))), b"a\r\nx\r\ny\r\n\te\r\n")
def test_a_mutant_whose_lines_keep_their_cr_matches_and_goes_in_as_it_is(self):
source = b"a\r\n\tb + c\r\n\te"
self.assertEqual(mutated(source, Mutant("calc.go", 2, "x", (b"\tb + c\r",), (b"\tb - c\r",))), b"a\r\n\tb - c\r\n\te")
self.assertEqual(mutated(source, Mutant("calc.go", 3, "x", (b"\te",), (b"\tf",))), b"a\r\n\tb + c\r\n\tf")
self.assertEqual(mutated(source, Mutant("calc.go", 3, "x", (), (b"\tf\r",))), b"a\r\n\tb + c\r\n\tf\r\n\te")
self.assertEqual(mutated(source, Mutant("calc.go", 3, "x", (), (b"\tf",))), b"a\r\n\tb + c\r\n\tf\r\n\te")
self.assertEqual(mutated(source, Mutant("calc.go", 2, "x", (b"\tb + c", b"\te"), (b"x", b"y"))), b"a\r\nx\r\ny")
def test_a_file_that_mixes_line_ends_takes_the_one_of_its_first_line(self):
self.assertEqual(mutated(b"a\r\nb\nc\n", Mutant("calc.py", 2, "x", (b"b",), (b"z",))), b"a\r\nz\r\nc\n")
self.assertEqual(mutated(b"a\nb\r\nc\r\n", Mutant("calc.py", 2, "x", (b"b",), (b"z",))), b"a\nz\nc\r\n")
def test_the_last_line_of_a_crlf_file_has_no_line_end_to_take(self):
self.assertEqual(mutated(b"a\r\nb + c", Mutant("calc.py", 2, "x", (b"b + c",), (b"b - c",))), b"a\r\nb - c")
class LineAtHeadTest(unittest.TestCase):
# Lines 3 and 4 became one, two lines went in after line 9, and line 20 went.
+9 -1
View File
@@ -8,7 +8,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from failure import Failure
from task_bench.python_bugs import killed_mutants, task_text
from task_bench.planted_mutant import Mutant
from task_bench.planted_mutant import Mutant, mutated
from mutate_changes.tool_process import Ran
CALC = "def double(n):\n\treturn n * 2\n\n\ndef triple(n):\n\treturn (\n\t\tn * 3\n\t)\n"
@@ -163,6 +163,14 @@ class KilledMutantsTest(unittest.TestCase):
killed_mutants(str(self.top), ["bin/pkg/ends.py"], self.mutmut(), 3)
self.assertEqual(self.ran[0][0][4:], ["pkg.ends.x_first*", "pkg.ends.x_last*"])
def test_a_crlf_file_gives_the_same_mutants_without_the_cr_and_they_apply_to_it(self):
(self.top / "bin/pkg/calc.py").write_bytes(CALC.replace("\n", "\r\n").encode())
found = killed_mutants(str(self.top), ["bin/pkg/calc.py"], self.mutmut(RESULTS.replace("calc.x_", "pkg.calc.x_")), 3)
self.assertEqual([(mutant.line, mutant.was, mutant.now) for mutant in found], [(2, (b"\treturn n * 2",), (b"\treturn n / 2",)), (6, (b"\treturn (", b"\t\tn * 3", b"\t)"), (b"\treturn [", b"\t\tn * 3", b"\t]"))])
changed = mutated((self.top / "bin/pkg/calc.py").read_bytes(), found[0])
self.assertIsNotNone(changed)
self.assertIn(b"\treturn n / 2\r\n", changed or b"")
def test_keeps_the_mutants_of_every_folder_mutmut_runs_in(self):
for path, text in (("tools/setup.cfg", "[tool:pytest]\n"), ("tools/pkg/calc.py", CALC)):
(self.top / path).parent.mkdir(parents=True, exist_ok=True)
-10
View File
@@ -18,7 +18,6 @@
|---|---|---|---|---|---|---|---|---|---|---|---|
| 507 | 0.16.0 | | owner | **Bench rebuilding a feature the project shipped: revert its commit at the head, hand Haiku its item tagline, and check with the tests that commit added.** | 3 | 2 | 1 | 4 | 5 | 1.1 by 3 | 0.7 |
| 524 | 0.16.0 | | owner | **Write fejkdata's first six bench features, each a spec, acceptance tests in Go and a reference patch that builds it, in the `bench-tasks` folder its config names.** | 3 | 2 | 1 | 4 | 6 | 1.1 by 3 | 0.5 |
| 530 | 0.16.0 | | owner | **Plant a mutant in a Python file whose lines end in CRLF.** | 1 | 1 | 1 | 1 | 2 | 1.1 by 1 | 0.4 |
| 526 | 0.16.0 | | owner | **Plant bugs for `task-bench` in Rust, from the mutants cargo-mutants kills.** | 3 | 2 | 1 | 4 | 4 | 1.1 by 1 | 0.3 |
| 527 | 0.16.0 | | owner | **Plant bugs for `task-bench` in TypeScript, from the mutants Stryker kills.** | 3 | 2 | 1 | 4 | 4 | 1.1 by 1 | 0.3 |
| 528 | 0.16.0 | | owner | **Plant bugs for `task-bench` in PHP, from the mutants Infection kills.** | 3 | 2 | 1 | 4 | 4 | 1.1 by 1 | 0.3 |
@@ -130,15 +129,6 @@ The second part of item 504, which says what a bench feature is and when a run c
Done when `task-bench` counts a run of each: its acceptance tests fail at fejkdata's base and head and pass with its reference patch. The six land in fejkdata's checkout or its config folder, wherever its `bench-tasks` setting points, so the item changes no file of this repo and carries no `Touches:` list. About 1,000 lines of Go and specs.
### 530. Plant a mutant in a Python file whose lines end in CRLF.
mutmut's planter reads a file as text, which turns `\r\n` into `\n`, and the planter re-applies a mutant to the file's bytes split on `\n` alone. So no mutant of a Python file with CRLF line ends ever re-applies, and the bench doesn't say why. A stability review of item 508 reproduced it. Compare the lines without their `\r`, and write the mutant's lines with the line end the file uses.
Touches:
- `bin/task_bench/planted_mutant.py`: `mutated`
- `bin/task_bench/python_bugs.py`: `killed_mutants`
- `bin/task_bench/test_python_bugs.py`
### 526. Plant bugs for `task-bench` in Rust, from the mutants cargo-mutants kills.
cargo-mutants runs over the changed lines only today. Run it over the files that hold the seats' places, at the tree's top without git, and turn each killed mutant into the lines it changes, read from the diffs it writes under `mutants.out`. Tell Haiku the failure `cargo test` prints, without the test's name or file. Check the module against the tool's real output on a small project in a container, since this repo holds no Rust.