Skip to content

Commit

Permalink
Fix ForEachLineInFile to not lose the last line if it doesn't end wit…
Browse files Browse the repository at this point in the history
…h a LF
  • Loading branch information
stefanhaller committed Oct 9, 2024
1 parent b71aa5e commit 696e78f
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
},
})
4 changes: 2 additions & 2 deletions pkg/utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions pkg/utils/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"},
},
}

Expand Down

0 comments on commit 696e78f

Please sign in to comment.