Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function: file.Walk #281

Merged
merged 11 commits into from
Apr 13, 2018
13 changes: 13 additions & 0 deletions funcs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,16 @@ func (f *FileFuncs) IsDir(path string) bool {
func (f *FileFuncs) ReadDir(path string) ([]string, error) {
return file.ReadDir(path)
}

// Walk -
func (f *FileFuncs) Walk(path string) ([]string, error) {
files := make([]string, 0)
afero.Walk(f.fs, path, func(subpath string, finfo os.FileInfo, err error) error {
if err != nil {
return err
}
files = append(files, subpath)
return nil
})
return files, nil
}
23 changes: 23 additions & 0 deletions funcs/file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package funcs

import (
"path/filepath"
"testing"

"github.com/spf13/afero"
Expand Down Expand Up @@ -30,3 +31,25 @@ func TestFileIsDir(t *testing.T) {
assert.True(t, ff.IsDir("/tmp"))
assert.False(t, ff.IsDir("/tmp/foo"))
}

func TestFileWalk(t *testing.T) {
fs := afero.NewMemMapFs()
ff := &FileFuncs{fs}

_ = fs.Mkdir("/tmp", 0777)
_ = fs.Mkdir("/tmp/bar", 0777)
_ = fs.Mkdir("/tmp/bar/baz", 0777)
f, _ := fs.Create("/tmp/bar/baz/foo")
_, _ = f.Write([]byte("foo"))

expectedLists := [][]string{{"tmp"}, {"tmp", "bar" }, {"tmp", "bar", "baz"}, {"tmp", "bar", "baz", "foo"}}
expectedPaths := make([]string, 0)
for _, path := range expectedLists {
expectedPaths = append(expectedPaths, string(filepath.Separator) + filepath.Join(path...))
}

actualPaths, err := ff.Walk(string(filepath.Separator) + "tmp")

assert.NoError(t, err)
assert.Equal(t, expectedPaths, actualPaths)
}