1
0
mirror of https://github.com/golang/go synced 2024-11-11 22:40:22 -07:00

io/fs: implement FileInfoToDirEntry

Implements FileInfoToDirEntry which converts an fs.FileInfo to fs.DirEntry.

Fixes #42387.

Change-Id: Ie723b6ed583c6c5ecf22bbe64e3b6496f5114254
Reviewed-on: https://go-review.googlesource.com/c/go/+/293649
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
This commit is contained in:
Michel Levieux 2021-02-18 15:53:46 +01:00 committed by Ian Lance Taylor
parent 11b4aee05b
commit 374b190475
2 changed files with 80 additions and 0 deletions

View File

@ -45,3 +45,33 @@ func ReadDir(fsys FS, name string) ([]DirEntry, error) {
sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
return list, err
}
// dirInfo is a DirEntry based on a FileInfo.
type dirInfo struct {
fileInfo FileInfo
}
func (di dirInfo) IsDir() bool {
return di.fileInfo.IsDir()
}
func (di dirInfo) Type() FileMode {
return di.fileInfo.Mode().Type()
}
func (di dirInfo) Info() (FileInfo, error) {
return di.fileInfo, nil
}
func (di dirInfo) Name() string {
return di.fileInfo.Name()
}
// FileInfoToDirEntry returns a DirEntry that returns information from info.
// If info is nil, FileInfoToDirEntry returns nil.
func FileInfoToDirEntry(info FileInfo) DirEntry {
if info == nil {
return nil
}
return dirInfo{fileInfo: info}
}

View File

@ -6,7 +6,10 @@ package fs_test
import (
. "io/fs"
"os"
"testing"
"testing/fstest"
"time"
)
type readDirOnly struct{ ReadDirFS }
@ -41,3 +44,50 @@ func TestReadDir(t *testing.T) {
dirs, err = ReadDir(sub, ".")
check("sub(.)", dirs, err)
}
func TestFileInfoToDirEntry(t *testing.T) {
testFs := fstest.MapFS{
"notadir.txt": {
Data: []byte("hello, world"),
Mode: 0,
ModTime: time.Now(),
Sys: &sysValue,
},
"adir": {
Data: nil,
Mode: os.ModeDir,
ModTime: time.Now(),
Sys: &sysValue,
},
}
tests := []struct {
path string
wantMode FileMode
wantDir bool
}{
{path: "notadir.txt", wantMode: 0, wantDir: false},
{path: "adir", wantMode: os.ModeDir, wantDir: true},
}
for _, test := range tests {
test := test
t.Run(test.path, func(t *testing.T) {
fi, err := Stat(testFs, test.path)
if err != nil {
t.Fatal(err)
}
dirEntry := FileInfoToDirEntry(fi)
if g, w := dirEntry.Type(), test.wantMode; g != w {
t.Errorf("FileMode mismatch: got=%v, want=%v", g, w)
}
if g, w := dirEntry.Name(), test.path; g != w {
t.Errorf("Name mismatch: got=%v, want=%v", g, w)
}
if g, w := dirEntry.IsDir(), test.wantDir; g != w {
t.Errorf("IsDir mismatch: got=%v, want=%v", g, w)
}
})
}
}