Plant a mutant in a Python file whose lines end in CRLF
CI / tests (pull_request) Successful in 4m8s
CI / tests (pull_request) Successful in 4m8s
This commit is contained in:
@@ -21,12 +21,18 @@ 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.
|
||||
|
||||
The mutant's lines come from a file read as text, so they hold no `\\r`: they match a file's lines without theirs, and go in with
|
||||
the line end the file uses."""
|
||||
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]) != mutant.was:
|
||||
return None
|
||||
lines[first:first + len(mutant.was)] = mutant.now
|
||||
# The file's last line has no line end, so none of its lines takes one.
|
||||
line_end = b"\r" if len(lines) > 1 and lines[0].endswith(b"\r") and end < len(lines) else b""
|
||||
lines[first:end] = (line + line_end for line in mutant.now)
|
||||
return b"\n".join(lines)
|
||||
|
||||
|
||||
|
||||
@@ -29,6 +29,16 @@ 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_file_with_crlf_line_ends_matches_the_lines_without_their_cr_and_gets_the_mutants_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\r",), (b"z",))))
|
||||
|
||||
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.
|
||||
|
||||
@@ -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_file_with_crlf_line_ends_gives_the_same_mutants_without_the_cr_and_applies_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)
|
||||
|
||||
@@ -3508,3 +3508,16 @@ Read most (opens and searches):
|
||||
- `bin/task_bench/runs.py` 1
|
||||
- `bin/task_bench/tasks.py` 1
|
||||
- `docs/score-anchors.md` 1
|
||||
|
||||
## Reading cost, chunk 33264f8a9040, finished 2026-11-23T22:13:25Z
|
||||
|
||||
64108 new input tokens before the first commit.
|
||||
|
||||
Read most (opens and searches):
|
||||
|
||||
- `README.md` 3
|
||||
- `docs/decisions.md` 3
|
||||
- `skills/larv-review/SKILL.md` 3
|
||||
- `bin/task_bench/main.py` 2
|
||||
- `CHANGELOG.md` 1
|
||||
- `todo.md` 1
|
||||
|
||||
Reference in New Issue
Block a user