1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
package fsutil
import (
"context"
gofs "io/fs"
"os"
"path/filepath"
"testing"
"github.com/containerd/continuity/fs/fstest"
"github.com/stretchr/testify/require"
"github.com/tonistiigi/fsutil/types"
)
func TestWalk(t *testing.T) {
tmpDir := t.TempDir()
apply := fstest.Apply(
fstest.CreateDir("dir", 0700),
fstest.CreateFile("dir/foo", []byte("contents"), 0600),
)
require.NoError(t, apply.Apply(tmpDir))
f, err := NewFS(tmpDir)
require.NoError(t, err)
paths := []string{}
files := []gofs.DirEntry{}
err = f.Walk(context.TODO(), "", func(path string, entry gofs.DirEntry, err error) error {
require.NoError(t, err)
paths = append(paths, path)
files = append(files, entry)
return nil
})
require.NoError(t, err)
require.Equal(t, []string{"dir", filepath.FromSlash("dir/foo")}, paths)
require.Len(t, files, 2)
require.Equal(t, "dir", files[0].Name())
require.Equal(t, "foo", files[1].Name())
fis := []gofs.FileInfo{}
for _, f := range files {
fi, err := f.Info()
require.NoError(t, err)
fis = append(fis, fi)
}
require.Equal(t, "dir", fis[0].Name())
require.Equal(t, "foo", fis[1].Name())
require.Equal(t, len("contents"), int(fis[1].Size()))
require.Equal(t, "dir", fis[0].(*StatInfo).Path)
require.Equal(t, filepath.FromSlash("dir/foo"), fis[1].(*StatInfo).Path)
}
func TestWalkDir(t *testing.T) {
tmpDir := t.TempDir()
apply := fstest.Apply(
fstest.CreateDir("dir", 0700),
fstest.CreateFile("dir/foo", []byte("contents"), 0600),
)
require.NoError(t, apply.Apply(tmpDir))
tmpfs, err := NewFS(tmpDir)
require.NoError(t, err)
tmpDir2 := t.TempDir()
apply = fstest.Apply(
fstest.CreateDir("dir", 0700),
fstest.CreateFile("dir/bar", []byte("contents2"), 0600),
)
require.NoError(t, apply.Apply(tmpDir2))
tmpfs2, err := NewFS(tmpDir2)
require.NoError(t, err)
f, err := SubDirFS([]Dir{
{
Stat: &types.Stat{
Mode: uint32(os.ModeDir | 0755),
Path: "1",
},
FS: tmpfs,
},
{
Stat: &types.Stat{
Mode: uint32(os.ModeDir | 0755),
Path: "2",
},
FS: tmpfs2,
},
})
require.NoError(t, err)
paths := []string{}
files := []gofs.DirEntry{}
err = f.Walk(context.TODO(), "", func(path string, entry gofs.DirEntry, err error) error {
require.NoError(t, err)
paths = append(paths, path)
files = append(files, entry)
return nil
})
require.NoError(t, err)
require.Equal(t, []string{"1", filepath.FromSlash("1/dir"), filepath.FromSlash("1/dir/foo"), "2", filepath.FromSlash("2/dir"), filepath.FromSlash("2/dir/bar")}, paths)
require.Equal(t, "1", files[0].Name())
require.Equal(t, "dir", files[1].Name())
require.Equal(t, "foo", files[2].Name())
}
|