From 696e78fcc8941aa684421e20fea11b69b07206b8 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 9 Oct 2024 15:37:08 +0200 Subject: [PATCH] Fix ForEachLineInFile to not lose the last line if it doesn't end with a LF --- .../tests/conflicts/resolve_without_trailing_lf.go | 3 --- pkg/utils/io.go | 4 ++-- pkg/utils/io_test.go | 6 ------ 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/pkg/integration/tests/conflicts/resolve_without_trailing_lf.go b/pkg/integration/tests/conflicts/resolve_without_trailing_lf.go index c85c3ea48..2af1eac54 100644 --- a/pkg/integration/tests/conflicts/resolve_without_trailing_lf.go +++ b/pkg/integration/tests/conflicts/resolve_without_trailing_lf.go @@ -52,9 +52,6 @@ var ResolveWithoutTrailingLf = NewIntegrationTest(NewIntegrationTestArgs{ Contains("M file").IsSelected(), ) - /* EXPECTED: t.Views().Main().Content(Contains("-a1\n+a2\n").DoesNotContain("-no eol")) - ACTUAL: */ - t.Views().Main().Content(Contains("-a1\n+a2\n").Contains("-no eol")) }, }) diff --git a/pkg/utils/io.go b/pkg/utils/io.go index 98d026429..1d222746b 100644 --- a/pkg/utils/io.go +++ b/pkg/utils/io.go @@ -21,8 +21,8 @@ func ForEachLineInFile(path string, f func(string, int)) error { func forEachLineInStream(reader io.Reader, f func(string, int)) { bufferedReader := bufio.NewReader(reader) for i := 0; true; i++ { - line, err := bufferedReader.ReadString('\n') - if err != nil { + line, _ := bufferedReader.ReadString('\n') + if len(line) == 0 { break } f(line, i) diff --git a/pkg/utils/io_test.go b/pkg/utils/io_test.go index bf1d46ebe..b8dfa957c 100644 --- a/pkg/utils/io_test.go +++ b/pkg/utils/io_test.go @@ -26,10 +26,7 @@ func Test_forEachLineInStream(t *testing.T) { { name: "single line without line feed", input: "abc", - /* EXPECTED: expectedLines: []string{"abc"}, - ACTUAL: */ - expectedLines: []string{}, }, { name: "multiple lines", @@ -44,10 +41,7 @@ func Test_forEachLineInStream(t *testing.T) { { name: "multiple lines without linefeed at end of file", input: "abc\ndef\nghi", - /* EXPECTED: expectedLines: []string{"abc\n", "def\n", "ghi"}, - ACTUAL: */ - expectedLines: []string{"abc\n", "def\n"}, }, }