From 24ef172f2ba3a082357d4dabaa0e37d884a6c341 Mon Sep 17 00:00:00 2001 From: Bruno Reis Date: Tue, 10 May 2022 13:19:00 -0700 Subject: [PATCH] Fixes #352. Add tests: - ``*mem.File` implementation of `io.ReaderAt` must return non-nil error when reading less than length of give slice. - `MemMapFs` must pass `fstest.TestFS` tests for non-empty files. Fixes the bug. --- iofs_test.go | 10 +++++++++- mem/file.go | 5 +++++ mem/file_test.go | 13 ++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/iofs_test.go b/iofs_test.go index 1d310e5..e7795d0 100644 --- a/iofs_test.go +++ b/iofs_test.go @@ -29,7 +29,15 @@ func TestIOFS(t *testing.T) { t.Fatal("OpenFile (O_CREATE) failed:", err) } - f.Close() + _, err = f.WriteString("a") + if err != nil { + t.Fatal("WriteString failed:", err) + } + + err = f.Close() + if err != nil { + t.Fatal("Close failed:", err) + } if err := fstest.TestFS(NewIOFS(mmfs), "dir1/dir2/test.txt"); err != nil { t.Error(err) diff --git a/mem/file.go b/mem/file.go index 5ef8b6a..853f4ed 100644 --- a/mem/file.go +++ b/mem/file.go @@ -210,6 +210,11 @@ func (f *File) ReadAt(b []byte, off int64) (n int, err error) { atomic.StoreInt64(&f.at, off) n, err = f.Read(b) atomic.StoreInt64(&f.at, prev) + if int64(n) == int64(len(f.fileData.data))-off { + if n < len(b) { + err = io.EOF + } + } return } diff --git a/mem/file_test.go b/mem/file_test.go index 998a5d0..e84b44c 100644 --- a/mem/file_test.go +++ b/mem/file_test.go @@ -232,7 +232,7 @@ func TestFileReadAtSeekOffset(t *testing.T) { t.Fatal("expected 0") } - b := make([]byte, 4) + b := make([]byte, 4, 5) n, err := f.ReadAt(b, 0) if err != nil { t.Fatal(err) @@ -244,6 +244,17 @@ func TestFileReadAtSeekOffset(t *testing.T) { t.Fail() } + n, err = f.ReadAt(b[:cap(b)], 0) + if err != io.EOF { + t.Fatal(err) + } + if n != 4 { + t.Fail() + } + if string(b) != "TEST" { + t.Fail() + } + offsetAfterReadAt, err := f.Seek(0, io.SeekCurrent) if err != nil { t.Fatal(err)