Take a CRLF file's line end from its first line, with a test of a mixed file
CI / tests (pull_request) Successful in 4m13s

This commit is contained in:
2026-11-23 23:32:20 +01:00
parent 4620365931
commit a45279fe54
2 changed files with 6 additions and 2 deletions
+2 -2
View File
@@ -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. """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 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") lines = source.split(b"\n")
first = mutant.line - 1 first = mutant.line - 1
end = first + len(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: if not 0 <= first <= len(lines) or tuple(line.removesuffix(b"\r") for line in lines[first:end]) != mutant.was:
return None return None
# The file's last line has no line end, so none of its lines takes one. # 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) lines[first:end] = (line + line_end for line in mutant.now)
return b"\n".join(lines) return b"\n".join(lines)
+4
View File
@@ -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.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",)))) 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): 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") self.assertEqual(mutated(b"a\r\nb + c", Mutant("calc.py", 2, "x", (b"b + c",), (b"b - c",))), b"a\r\nb - c")