Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests: add testing Readdir
  • Loading branch information
rymut committed Dec 20, 2024
commit 0bd01a6723c6bde1052384316f9395bb1121544a
53 changes: 9 additions & 44 deletions hugofs/rootmapping_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,49 +837,11 @@ func (f *rootMappingDir) Stat() (iofs.FileInfo, error) {
}

func (f *rootMappingDir) Readdir(count int) ([]os.FileInfo, error) {
//panic("not supported: use ReadDir")
/*
dirs, err := f.ReadDir(count)
fmt.Printf("collected files are : %s", dirs)
if err != nil {
return nil, err
}
return dirEntriesToFileInfo(dirs), nil
*/

/*
items, err := f.ReadDir(count)
fmt.Printf("collected files are : %s", items)
if err != nil {
return nil, fmt.Errorf("error readdirnames: %s", err)
}
files := make([]os.FileInfo, len(items))
for i, item := range items {
file, e := item.Info()
if e != nil {
// return nil, fmt.Errorf("error reading dir stat %s: %s", d, e)
}
files[i] = file
}
return files, nil
*/

dirnames, err := f.Readdirnames(count)
fmt.Printf("collected files are : %s\n", dirnames)
if err != nil {
return nil, fmt.Errorf("error readdirnames: %s\n", err)
}
files := make([]os.FileInfo, len(dirnames))
for i, d := range dirnames {
file, e := f.fs.Stat("layouts")
if e != nil {
fmt.Printf("cannot state file %s : %s\n", d, e)

// return nil, fmt.Errorf("error reading dir stat %s: %s", d, e)
}
files[i] = file
}
return files, nil
return dirEntriesToFileInfo(dirs)
}

// Note that Readdirnames preserves the order of the underlying filesystem(s),
Expand All @@ -892,13 +854,16 @@ func (f *rootMappingDir) Readdirnames(count int) ([]string, error) {
return dirEntriesToNames(dirs), nil
}

func dirEntriesToFileInfo(fis []iofs.DirEntry) []os.FileInfo {
names := make([]os.FileInfo, len(fis))
func dirEntriesToFileInfo(fis []iofs.DirEntry) ([]os.FileInfo, error) {
fileInfos := make([]os.FileInfo, len(fis))
for i, d := range fis {
info, _ := d.Info()
names[i] = info
fileInfo, err := d.Info()
if err != nil {
return nil, err
}
fileInfos[i] = fileInfo
}
return names
return fileInfos, nil
}

func dirEntriesToNames(fis []iofs.DirEntry) []string {
Expand Down
69 changes: 69 additions & 0 deletions hugofs/rootmapping_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gohugoio/hugo/hugofs/glob"

qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
"github.com/gohugoio/hugo/htesting"
"github.com/spf13/afero"
)
Expand Down Expand Up @@ -302,6 +303,74 @@ func TestRootMappingFsMount(t *testing.T) {
})
}

func TestReaddirRootMappingFsMountOverlap(t *testing.T) {
c := qt.New(t)
fs := NewBaseFileDecorator(afero.NewMemMapFs())

c.Assert(afero.WriteFile(fs, filepath.FromSlash("da/a.txt"), []byte("some no content"), 0o755), qt.IsNil)
c.Assert(afero.WriteFile(fs, filepath.FromSlash("db/b.txt"), []byte("some no content"), 0o755), qt.IsNil)
c.Assert(afero.WriteFile(fs, filepath.FromSlash("dc/c.txt"), []byte("some no content"), 0o755), qt.IsNil)
c.Assert(afero.WriteFile(fs, filepath.FromSlash("de/e.txt"), []byte("some no content"), 0o755), qt.IsNil)

rm := []RootMapping{
{
From: "static",
To: "da",
},
{
From: "static/b",
To: "db",
},
{
From: "static/b/c",
To: "dc",
},
{
From: "/static/e/",
To: "de",
},
}

rfs, err := NewRootMappingFs(fs, rm...)
c.Assert(err, qt.IsNil)

checkBasicFileInfos := func(name string, expect []iofs.FileInfo) {
c.Helper()
name = filepath.FromSlash(name)
f, err := rfs.Open(name)
c.Assert(err, qt.IsNil)
defer f.Close()
names, err := f.Readdir(-1)
c.Assert(err, qt.IsNil)

comp := func(x iofs.FileInfo, y iofs.FileInfo) bool {
if x == nil && y == nil {
return true
}
// &hugofs.dirNameOnlyFileInfo (does not implement Size && Sys is always nil)
return x != nil && y != nil &&
x.Name() == y.Name() &&
x.Mode() == y.Mode() &&
x.ModTime() == y.ModTime()
}
c.Assert(names, qt.CmpEquals(cmp.Comparer(comp)), expect, qt.Commentf(fmt.Sprintf("%#v", names)))
}
getExpectedFileInfos := func(paths []string) []iofs.FileInfo{
c.Helper()
fileInfos := make([]iofs.FileInfo, len(paths))
for i, path := range paths {
path = filepath.FromSlash(path)
fileInfo, err := rfs.Stat(path)
c.Assert(err, qt.IsNil)

fileInfos[i] = fileInfo
}
return fileInfos
}
checkBasicFileInfos("static", getExpectedFileInfos([]string{"static/a.txt", "static/b", "static/e"}))
checkBasicFileInfos("", getExpectedFileInfos([]string{"static"}))
}

func TestRootMappingFsMountOverlap(t *testing.T) {
c := qt.New(t)
fs := NewBaseFileDecorator(afero.NewMemMapFs())
Expand Down