diff --git a/afero_test.go b/afero_test.go index 526afa9..e2c1362 100644 --- a/afero_test.go +++ b/afero_test.go @@ -570,6 +570,25 @@ func TestReaddir(t *testing.T) { } } +// https://github.com/spf13/afero/issues/169 +func TestReaddirRegularFile(t *testing.T) { + defer removeAllTestFiles(t) + for _, fs := range Fss { + f := tmpFile(fs) + defer f.Close() + + _, err := f.Readdirnames(-1) + if err == nil { + t.Fatal("Expected error") + } + + _, err = f.Readdir(-1) + if err == nil { + t.Fatal("Expected error") + } + } +} + type myFileInfo []os.FileInfo func (m myFileInfo) String() string { diff --git a/mem/file.go b/mem/file.go index 885e554..7af2fb5 100644 --- a/mem/file.go +++ b/mem/file.go @@ -131,6 +131,9 @@ func (f *File) Sync() error { } func (f *File) Readdir(count int) (res []os.FileInfo, err error) { + if !f.fileData.dir { + return nil, &os.PathError{Op: "readdir", Path: f.fileData.name, Err: errors.New("not a dir")} + } var outLength int64 f.fileData.Lock()