Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes io.ReaderAt implementation of mem.File #353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion iofs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions mem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 12 additions & 1 deletion mem/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down