Test that a read of nonzero bytes from an InMemFile does not return EOF in the same read op.

This commit is contained in:
Jamie Wilkinson 2015-03-22 11:34:14 +11:00 committed by spf13
parent 96c189ab50
commit 33b62e7d34
3 changed files with 25 additions and 28 deletions

View File

@ -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) { func TestRename(t *testing.T) {
for _, fs := range Fss { for _, fs := range Fss {
from, to := testDir+"/renamefrom", testDir+"/renameto" from, to := testDir+"/renamefrom", testDir+"/renameto"

View File

@ -105,12 +105,15 @@ func (f *InMemoryFile) Read(b []byte) (n int, err error) {
if f.closed == true { if f.closed == true {
return 0, ErrFileClosed 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) { if len(f.data)-int(f.at) >= len(b) {
n = len(b) n = len(b)
} else { } else {
n = len(f.data) - int(f.at) n = len(f.data) - int(f.at)
err = io.EOF
} }
copy(b, f.data[f.at:f.at+int64(n)]) copy(b, f.data[f.at:f.at+int64(n)])
atomic.AddInt64(&f.at, int64(n)) atomic.AddInt64(&f.at, int64(n))
return return

View File

@ -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)
}
}