Repository: spf13/afero
Pull request #371 (2022-07-18 13:51:57)

Make mem.File implement fs.ReadDirFile


Patch:
diff --git a/internal/common/adapters.go b/internal/common/adapters.go
new file mode 100644
index 0000000..60685ca
--- /dev/null
+++ b/internal/common/adapters.go
@@ -0,0 +1,27 @@
+// Copyright © 2022 Steve Francia <spf@spf13.com>.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package common
+
+import "io/fs"
+
+// FileInfoDirEntry provides an adapter from os.FileInfo to fs.DirEntry
+type FileInfoDirEntry struct {
+	fs.FileInfo
+}
+
+var _ fs.DirEntry = FileInfoDirEntry{}
+
+func (d FileInfoDirEntry) Type() fs.FileMode { return d.FileInfo.Mode().Type() }
+
+func (d FileInfoDirEntry) Info() (fs.FileInfo, error) { return d.FileInfo, nil }
diff --git a/iofs.go b/iofs.go
index d422bcc..938b931 100644
--- a/iofs.go
+++ b/iofs.go
@@ -10,6 +10,8 @@ import (
 	"path"
 	"sort"
 	"time"
+
+	"github.com/spf13/afero/internal/common"
 )
 
 // IOFS adopts afero.Fs to stdlib io/fs.FS
@@ -92,7 +94,7 @@ func (iofs IOFS) ReadDir(name string) ([]fs.DirEntry, error) {
 
 	ret := make([]fs.DirEntry, len(items))
 	for i := range items {
-		ret[i] = dirEntry{items[i]}
+		ret[i] = common.FileInfoDirEntry{FileInfo: items[i]}
 	}
 
 	return ret, nil
@@ -127,17 +129,6 @@ func (IOFS) wrapError(op, path string, err error) error {
 	}
 }
 
-// dirEntry provides adapter from os.FileInfo to fs.DirEntry
-type dirEntry struct {
-	fs.FileInfo
-}
-
-var _ fs.DirEntry = dirEntry{}
-
-func (d dirEntry) Type() fs.FileMode { return d.FileInfo.Mode().Type() }
-
-func (d dirEntry) Info() (fs.FileInfo, error) { return d.FileInfo, nil }
-
 // readDirFile provides adapter from afero.File to fs.ReadDirFile needed for correct Open
 type readDirFile struct {
 	File
@@ -153,7 +144,7 @@ func (r readDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
 
 	ret := make([]fs.DirEntry, len(items))
 	for i := range items {
-		ret[i] = dirEntry{items[i]}
+		ret[i] = common.FileInfoDirEntry{FileInfo: items[i]}
 	}
 
 	return ret, nil
diff --git a/mem/file.go b/mem/file.go
index 258880b..3cf4693 100644
--- a/mem/file.go
+++ b/mem/file.go
@@ -18,15 +18,20 @@ import (
 	"bytes"
 	"errors"
 	"io"
+	"io/fs"
 	"os"
 	"path/filepath"
 	"sync"
 	"sync/atomic"
 	"time"
+
+	"github.com/spf13/afero/internal/common"
 )
 
 const FilePathSeparator = string(filepath.Separator)
 
+var _ fs.ReadDirFile = &File{}
+
 type File struct {
 	// atomic requires 64-bit alignment for struct field access
 	at           int64
@@ -183,6 +188,19 @@ func (f *File) Readdirnames(n int) (names []string, err error) {
 	return names, err
 }
 
+// Implements fs.ReadDirFile
+func (f *File) ReadDir(n int) ([]fs.DirEntry, error) {
+	fi, err := f.Readdir(n)
+	if err != nil {
+		return nil, err
+	}
+	di := make([]fs.DirEntry, len(fi))
+	for i, f := range fi {
+		di[i] = common.FileInfoDirEntry{FileInfo: f}
+	}
+	return di, nil
+}
+
 func (f *File) Read(b []byte) (n int, err error) {
 	f.fileData.Lock()
 	defer f.fileData.Unlock()


Test patch:
diff --git a/afero_test.go b/afero_test.go
index 5cd0a1a..f875ae1 100644
--- a/afero_test.go
+++ b/afero_test.go
@@ -18,6 +18,7 @@ import (
 	"bytes"
 	"fmt"
 	"io"
+	iofs "io/fs"
 	"io/ioutil"
 	"os"
 	"path/filepath"
@@ -530,22 +531,43 @@ func TestReaddirSimple(t *testing.T) {
 
 func TestReaddir(t *testing.T) {
 	defer removeAllTestFiles(t)
-	for num := 0; num < 6; num++ {
+	const nums = 6
+	for num := 0; num < nums; num++ {
 		outputs := make([]string, len(Fss))
 		infos := make([]string, len(Fss))
 		for i, fs := range Fss {
 			testSubDir := setupTestDir(t, fs)
-			//tDir := filepath.Dir(testSubDir)
 			root, err := fs.Open(testSubDir)
 			if err != nil {
 				t.Fatal(err)
 			}
-			defer root.Close()
 
-			for j := 0; j < 6; j++ {
+			infosn := make([]string, nums)
+
+			for j := 0; j < nums; j++ {
 				info, err := root.Readdir(num)
 				outputs[i] += fmt.Sprintf("%v  Error: %v\n", myFileInfo(info), err)
-				infos[i] += fmt.Sprintln(len(info), err)
+				s := fmt.Sprintln(len(info), err)
+				infosn[j] = s
+				infos[i] += s
+			}
+			root.Close()
+
+			// Also check fs.ReadDirFile interface if implemented
+			if _, ok := root.(iofs.ReadDirFile); ok {
+				root, err = fs.Open(testSubDir)
+				if err != nil {
+					t.Fatal(err)
+				}
+				defer root.Close()
+
+				for j := 0; j < nums; j++ {
+					dirEntries, err := root.(iofs.ReadDirFile).ReadDir(num)
+					s := fmt.Sprintln(len(dirEntries), err)
+					if s != infosn[j] {
+						t.Fatalf("%s: %s != %s", fs.Name(), s, infosn[j])
+					}
+				}
 			}
 		}
 
diff --git a/iofs_test.go b/iofs_test.go
index 66c0c5e..579345e 100644
--- a/iofs_test.go
+++ b/iofs_test.go
@@ -16,6 +16,8 @@ import (
 	"testing"
 	"testing/fstest"
 	"time"
+
+	"github.com/spf13/afero/internal/common"
 )
 
 func TestIOFS(t *testing.T) {
@@ -105,7 +107,7 @@ func TestIOFSNativeDirEntryWhenPossible(t *testing.T) {
 			t.Fatalf("expected %d, got %d", numFiles, len(entries))
 		}
 		for i, entry := range entries {
-			if _, ok := entry.(dirEntry); ok {
+			if _, ok := entry.(common.FileInfoDirEntry); ok {
 				t.Fatal("DirEntry not native")
 			}
 			if ordered && entry.Name() != fmt.Sprintf("test%d.txt", i) {
@@ -138,7 +140,7 @@ func TestIOFSNativeDirEntryWhenPossible(t *testing.T) {
 			fileCount++
 		}
 
-		if _, ok := d.(dirEntry); ok {
+		if _, ok := d.(common.FileInfoDirEntry); ok {
 			t.Fatal("DirEntry not native")
 		}