From d82243906a22a69126f371b8ecc4dfb6905b8cdf Mon Sep 17 00:00:00 2001 From: Jamie Wilkinson Date: Sun, 22 Mar 2015 11:34:14 +1100 Subject: [PATCH] Test that a read of nonzero bytes from an InMemFile does not return EOF in the same read op. --- fs_test.go | 21 +++++++++++++++++++++ memfile.go | 5 ++++- memfile_test.go | 27 --------------------------- 3 files changed, 25 insertions(+), 28 deletions(-) delete mode 100644 memfile_test.go 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) - } -}