diff --git a/m/reader_test.go b/m/reader_test.go index 4d1b43b..9ed9916 100644 --- a/m/reader_test.go +++ b/m/reader_test.go @@ -416,9 +416,49 @@ func TestReadUpdatingFile_InitiallyEmpty(t *testing.T) { assert.Equal(t, allLines.lines[0].Plain(nil), "Text") } -// NOTE: Consider testing file tailing with the first part not ending in a -// linefeed. In this case, the last line should become longer when new bytes -// show up. +// If people keep appending to the currently opened file we should display those +// changes. +// +// This test verifies it with the initial contents not ending with a linefeed. +func TestReadUpdatingFile_HalfLine(t *testing.T) { + // Make a temp file containing one line of text, ending with a newline + file, err := os.CreateTemp("", "moar-TestReadUpdatingFile-*.txt") + assert.NilError(t, err) + defer os.Remove(file.Name()) + + _, err = file.WriteString("Start") + assert.NilError(t, err) + + // Start a reader on that file + testMe, err := NewReaderFromFilename(file.Name(), *styles.Get("native"), formatters.TTY16m, nil) + assert.NilError(t, err) + + // Wait for the reader to finish reading + assert.NilError(t, testMe._wait()) + assert.Equal(t, int(testMe.bytesCount), len([]byte("Start"))) + + // Append the rest of the line + const secondLineString = ", end\n" + _, err = file.WriteString(secondLineString) + assert.NilError(t, err) + + // Give the reader some time to react + for i := 0; i < 20; i++ { + allLines, _ := testMe.GetLines(linenumbers.LineNumber{}, 10) + if len(allLines.lines) == 2 { + break + } + time.Sleep(100 * time.Millisecond) + } + + // Verify we got the two lines + allLines, _ := testMe.GetLines(linenumbers.LineNumber{}, 10) + assert.Equal(t, len(allLines.lines), 1, "Still expecting one line, got %d", len(allLines.lines)) + assert.Equal(t, testMe.GetLineCount(), 1) + assert.Equal(t, allLines.lines[0].Plain(nil), "Start, end") + + assert.Equal(t, int(testMe.bytesCount), len([]byte("Start, end\n"))) +} // NOTE: Consider testing file tailing with the first part ending ending in the // middle of an UTF-8 character. In this case, the new bytes should complete the