Normalize CRLF to LF

This commit is contained in:
Count Infinity 2025-12-16 01:25:49 -07:00
parent d6d3fd3424
commit cdd0b400cc
2 changed files with 31 additions and 0 deletions

View file

@ -235,6 +235,9 @@ def read_batchfile(pythonpath, file_ending=".py"):
if not text and decoderr:
raise UnicodeDecodeError("\n".join(decoderr), bytearray(), 0, 0, "")
if text:
text = text.replace("\r\n", "\n").replace("\r", "\n")
return text

View file

@ -93,6 +93,34 @@ class TestBatchCommandProcessor(TestCase):
[mock.call("foopath", file_ending=".ev"), mock.call("x", file_ending=".ev")],
)
class TestReadBatchFile(TestCase):
"""Test read_batchfile line ending normalization."""
@mock.patch.object(utils, "pypath_to_realpath", return_value=["testpath"])
@mock.patch.object(codecs, "open")
def test_normalizes_crlf_to_lf(self, mocked_open, _):
"""Test that CRLF line endings are normalized to LF.
See: https://github.com/evennia/evennia/issues/3847
"""
mocked_file = mock.MagicMock()
mocked_file.read.return_value = "@create sky\r\n#INSERT another.ev\r\n"
mocked_open.return_value.__enter__.return_value = mocked_file
result = batchprocessors.read_batchfile("foopath")
self.assertEqual(result, "@create sky\n#INSERT another.ev\n")
@mock.patch.object(utils, "pypath_to_realpath", return_value=["testpath"])
@mock.patch.object(codecs, "open")
def test_normalizes_cr_to_lf(self, mocked_open, _):
"""Test that old Mac CR line endings are normalized to LF."""
mocked_file = mock.MagicMock()
mocked_file.read.return_value = "@create sky\r#INSERT another.ev\r"
mocked_open.return_value.__enter__.return_value = mocked_file
result = batchprocessors.read_batchfile("foopath")
self.assertEqual(result, "@create sky\n#INSERT another.ev\n")
class TestBatchCodeProcessor(TestCase):
@mock.patch.object(batchprocessors, "read_batchfile")