github.com/wuhuizuo/gomplate@v3.5.0+incompatible/funcs/file_test.go (about) 1 package funcs 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/spf13/afero" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestFileExists(t *testing.T) { 12 fs := afero.NewMemMapFs() 13 ff := &FileFuncs{fs} 14 15 _ = fs.Mkdir("/tmp", 0777) 16 f, _ := fs.Create("/tmp/foo") 17 _, _ = f.Write([]byte("foo")) 18 19 assert.True(t, ff.Exists("/tmp/foo")) 20 assert.False(t, ff.Exists("/tmp/bar")) 21 } 22 23 func TestFileIsDir(t *testing.T) { 24 fs := afero.NewMemMapFs() 25 ff := &FileFuncs{fs} 26 27 _ = fs.Mkdir("/tmp", 0777) 28 f, _ := fs.Create("/tmp/foo") 29 _, _ = f.Write([]byte("foo")) 30 31 assert.True(t, ff.IsDir("/tmp")) 32 assert.False(t, ff.IsDir("/tmp/foo")) 33 } 34 35 func TestFileWalk(t *testing.T) { 36 fs := afero.NewMemMapFs() 37 ff := &FileFuncs{fs} 38 39 _ = fs.Mkdir("/tmp", 0777) 40 _ = fs.Mkdir("/tmp/bar", 0777) 41 _ = fs.Mkdir("/tmp/bar/baz", 0777) 42 f, _ := fs.Create("/tmp/bar/baz/foo") 43 _, _ = f.Write([]byte("foo")) 44 45 expectedLists := [][]string{{"tmp"}, {"tmp", "bar"}, {"tmp", "bar", "baz"}, {"tmp", "bar", "baz", "foo"}} 46 expectedPaths := make([]string, 0) 47 for _, path := range expectedLists { 48 expectedPaths = append(expectedPaths, string(filepath.Separator)+filepath.Join(path...)) 49 } 50 51 actualPaths, err := ff.Walk(string(filepath.Separator) + "tmp") 52 53 assert.NoError(t, err) 54 assert.Equal(t, expectedPaths, actualPaths) 55 }