Skip to content

Commit eeb75bb

Browse files
committed
test: cover bufferedWriter error paths for WriteAt, CopyTo, Sync
Add tests for IO error scenarios when the underlying temp file is closed or broken: - TestBufferedWriterWriteAtFlushError: Flush fails in WriteAt - TestBufferedWriterCopyToFlushError: Flush fails in CopyTo - TestBufferedWriterCopyToSeekError: Seek fails in CopyTo - TestBufferedWriterSyncWriteToError: WriteTo fails in Sync This brings diff coverage from 95.56% to 100%.
1 parent 1216ed8 commit eeb75bb

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

stream_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,57 @@ func TestNewStreamWriterOptions(t *testing.T) {
705705
assert.NoError(t, err)
706706
assert.Equal(t, 1024, sw3.rawData.flushSize)
707707
}
708+
709+
func TestBufferedWriterWriteAtFlushError(t *testing.T) {
710+
// Test WriteAt temp file path where Flush fails (line 901)
711+
bw := &bufferedWriter{flushSize: 1, bioSize: 4096}
712+
bw.WriteString("AAABBBCCC")
713+
_ = bw.Sync()
714+
// Write more data so bio has unflushed content
715+
bw.bio.WriteString("extra")
716+
// Close the temp file to cause Flush (bio.Flush) to fail
717+
bw.tmp.Close()
718+
err := bw.WriteAt([]byte("YYY"), 3)
719+
assert.Error(t, err)
720+
}
721+
722+
func TestBufferedWriterCopyToFlushError(t *testing.T) {
723+
// Test CopyTo temp file path where Flush fails (line 915)
724+
bw := &bufferedWriter{flushSize: 1, bioSize: 4096}
725+
bw.WriteString("test data")
726+
_ = bw.Sync()
727+
bw.WriteString(" more")
728+
// Close file to cause Flush to fail
729+
bw.tmp.Close()
730+
var dst bytes.Buffer
731+
_, err := bw.CopyTo(&dst)
732+
assert.Error(t, err)
733+
}
734+
735+
func TestBufferedWriterCopyToSeekError(t *testing.T) {
736+
// Test CopyTo temp file path where Seek fails (line 918)
737+
bw := &bufferedWriter{flushSize: 1, bioSize: 4096}
738+
bw.WriteString("test data")
739+
_ = bw.Sync()
740+
// Close file so Flush succeeds (bio is nil after sync with no writes) but Seek fails
741+
// We need bio to be nil so Flush() is a no-op, then Seek will fail on closed file
742+
bw.bio = nil
743+
bw.tmp.Close()
744+
var dst bytes.Buffer
745+
_, err := bw.CopyTo(&dst)
746+
assert.Error(t, err)
747+
}
748+
749+
func TestBufferedWriterSyncWriteToError(t *testing.T) {
750+
// Test Sync where buf.WriteTo(tmp) fails (line 970)
751+
bw := &bufferedWriter{flushSize: 1, bioSize: 4096}
752+
bw.WriteString("initial")
753+
// Sync to create temp file
754+
_ = bw.Sync()
755+
// Now reset state to have data in buf and tmp exists but is closed
756+
bw.bio = nil
757+
bw.buf.WriteString("more data")
758+
bw.tmp.Close()
759+
err := bw.Sync()
760+
assert.Error(t, err)
761+
}

0 commit comments

Comments
 (0)