mirror of https://github.com/spf13/afero.git
Test that a read of nonzero bytes from an InMemFile does not return EOF in the same read op.
This commit is contained in:
parent
96c189ab50
commit
33b62e7d34
21
fs_test.go
21
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"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue