From 5bed8e3927204f284b565f20b474d6bfc913491b Mon Sep 17 00:00:00 2001 From: Christian Bieg Date: Sun, 1 Oct 2023 17:50:31 +0200 Subject: [PATCH] Fixed issues with cr lf lines in log files --- embark/embark/logviewer.py | 3 +++ embark/embark/tests/test_linecache.py | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/embark/embark/logviewer.py b/embark/embark/logviewer.py index 7a66cb18..3ccee478 100644 --- a/embark/embark/logviewer.py +++ b/embark/embark/logviewer.py @@ -168,6 +168,9 @@ def refresh(self) -> None: if len(line) > 0 and line[-1:] == b'\n': line_ending = line_ending - 1 + if len(line) > 1 and line[-2:] == b'\r\n': + line_ending = line_ending - 1 + self.line_beginnings.append(line_beginning) self.line_endings.append(line_ending) diff --git a/embark/embark/tests/test_linecache.py b/embark/embark/tests/test_linecache.py index 448f7a96..b4006444 100644 --- a/embark/embark/tests/test_linecache.py +++ b/embark/embark/tests/test_linecache.py @@ -17,6 +17,18 @@ def test_default(self): self.assertEqual(b'10: ggggggggg\n11: hhhhhhhhhhhhh\n', line_cache.read_lines(0, 2), 'The line cache did not return the correct value.') self.assertEqual(b'9: ffffffffff\n10: ggggggggg\n11: hhhhhhhhhhhhh', line_cache.read_lines(1, 3), 'The line cache did not return the correct value.') + def test_cr_lf(self): + line_cache = LineCache('./test/logviewer/line_cache_test_cr_lf.log') + + for _ in range(0, 2): + self.assertEqual(12, line_cache.num_lines(), 'Incorrect number of lines.') + self.assertEqual(len(line_cache.line_endings), len(line_cache.line_beginnings), 'The number of line beginnings and line endings do not match.') + self.assertEqual([0, 7, 10, 22, 30, 40, 49, 52, 55, 69, 83, 102], line_cache.line_beginnings, 'The line beginning cache is not valid.') + line_cache.refresh() + + self.assertEqual(b'10: ggggggggg\n11: hhhhhhhhhhhhh\r\n', line_cache.read_lines(0, 2), 'The line cache did not return the correct value.') + self.assertEqual(b'9: ffffffffff\n10: ggggggggg\n11: hhhhhhhhhhhhh', line_cache.read_lines(1, 3), 'The line cache did not return the correct value.') + def test_no_newline_end(self): line_cache = LineCache('./test/logviewer/line_cache_test_no_newline.log')