diff --git a/bin/task_bench/planted_mutant.py b/bin/task_bench/planted_mutant.py index f47cca80..46dbf24e 100644 --- a/bin/task_bench/planted_mutant.py +++ b/bin/task_bench/planted_mutant.py @@ -24,14 +24,14 @@ 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. 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.""" + the line end the file's first line has.""" lines = source.split(b"\n") first = mutant.line - 1 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 # 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"" + line_end = b"\r" if 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) diff --git a/bin/task_bench/test_planted_mutant.py b/bin/task_bench/test_planted_mutant.py index 187019cc..0937247e 100644 --- a/bin/task_bench/test_planted_mutant.py +++ b/bin/task_bench/test_planted_mutant.py @@ -36,6 +36,10 @@ class MutatedTest(unittest.TestCase): 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_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")