diff --git a/copyline.go b/copyline.go index 2d7e62f..4c66828 100644 --- a/copyline.go +++ b/copyline.go @@ -22,9 +22,9 @@ import ( // Note that ‘n64’ represents how many bytes were written, not read. func CopyLine(writer io.Writer, reader io.Reader) (n64 int64, err error) { - for { - var eof bool + var eof bool + for { var r0 rune var size0 int { @@ -113,5 +113,9 @@ func CopyLine(writer io.Writer, reader io.Reader) (n64 int64, err error) { break } + if eof && n64 <= 0 { + return n64, io.EOF + } + return n64, nil } diff --git a/copyline_test.go b/copyline_test.go index 8c71e4b..58e8ce5 100644 --- a/copyline_test.go +++ b/copyline_test.go @@ -191,3 +191,41 @@ func TestCopyLine(t *testing.T) { } } } + +func TestCopyLine_empty(t *testing.T) { + + var reader io.Reader = strings.NewReader("") + + var actualStorage strings.Builder + + actualN, err := netln.CopyLine(&actualStorage, reader) + if nil == err { + t.Error("Expected an error but did not actually get one.") + t.Logf("ERROR: (%T) %q", err, err) + return + } + + { + var expected error = io.EOF + var actual error = err + + if expected != actual { + t.Errorf("The actual error was not what was expeceted.") + t.Logf("EXPECTED-ERROR: (%T) %q", expected, expected) + t.Logf("ACTUAL-ERROR: (%T) %q", actual, actual) + return + } + } + + { + var expected int64 = 0 + var actual int64 = actualN + + if expected != actual { + t.Error("The actual number of bytes written is not what was expected.") + t.Logf("EXPECTED: %d bytes", expected) + t.Logf("ACTUAL: %d bytes", actual) + return + } + } +}