diff --git a/fs_test.go b/fs_test.go index 4fe6afe..225100e 100644 --- a/fs_test.go +++ b/fs_test.go @@ -69,6 +69,27 @@ func TestRead0(t *testing.T) { } } +func TestMemFileRead(t *testing.T) { + f := MemFileCreate("testfile") + f.WriteString("abcd") + f.Seek(0, 0) + b := make([]byte, 8) + n, err := f.Read(b) + if n != 4 { + t.Errorf("didn't read all bytes: %v %v %v", n, err, b) + } + if err != nil { + t.Errorf("err is not nil: %v %v %v", n, err, b) + } + n, err = f.Read(b) + if n != 0 { + t.Errorf("read more bytes: %v %v %v", n, err, b) + } + if err != io.EOF { + t.Errorf("error is not EOF: %v %v %v", n, err, b) + } +} + func TestRename(t *testing.T) { for _, fs := range Fss { from, to := testDir+"/renamefrom", testDir+"/renameto" diff --git a/memfile.go b/memfile.go index 4815c6b..6f4edb4 100644 --- a/memfile.go +++ b/memfile.go @@ -105,12 +105,15 @@ func (f *InMemoryFile) Read(b []byte) (n int, err error) { if f.closed == true { return 0, ErrFileClosed } + if len(b) > 0 && int(f.at) == len(f.data) { + return 0, io.EOF + } if len(f.data)-int(f.at) >= len(b) { n = len(b) } else { n = len(f.data) - int(f.at) - err = io.EOF } + copy(b, f.data[f.at:f.at+int64(n)]) atomic.AddInt64(&f.at, int64(n)) return diff --git a/memfile_test.go b/memfile_test.go deleted file mode 100644 index b77ae8e..0000000 --- a/memfile_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package afero - -import ( - "io" - "testing" -) - -func TestMemFileRead(t *testing.T) { - f := MemFileCreate("testfile") - f.WriteString("abcd") - f.Seek(0, 0) - b := make([]byte, 8) - n, err := f.Read(b) - if n != 4 { - t.Errorf("didn't read all bytes: %v %v %v", n, err, b) - } - if err != nil { - t.Errorf("err is not nil: %v %v %v", n, err, b) - } - n, err = f.Read(b) - if n != 0 { - t.Errorf("read more bytes: %v %v %v", n, err, b) - } - if err != io.EOF { - t.Errorf("error is not EOF: %v %v %v", n, err, b) - } -}