-
Notifications
You must be signed in to change notification settings - Fork 191
/
find_test.go
103 lines (83 loc) · 3.05 KB
/
find_test.go
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
package fsutil_test
import (
"io/fs"
"strings"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/testutil/fakeobj"
)
func TestMatchFirst(t *testing.T) {
assert.Eq(t, "testdata", fsutil.MatchFirst([]string{"testdata"}, fsutil.IsDir, ""))
assert.Eq(t, "testdata", fsutil.FirstExists("not-exists", "testdata"))
assert.Eq(t, "testdata", fsutil.FirstExistsDir("not-exists", "testdata"))
assert.Eq(t, "testdata/test.jpg", fsutil.FirstExistsFile("not-exists", "testdata/test.jpg"))
ps := fsutil.MatchPaths([]string{"testdata", "testdata/test.jpg"}, fsutil.IsDir)
assert.Eq(t, []string{"testdata"}, ps)
}
func TestSearchNameUp(t *testing.T) {
p := fsutil.SearchNameUp("testdata", "finder")
assert.NotEmpty(t, p)
assert.True(t, strings.HasSuffix(p, "fsutil"))
p = fsutil.SearchNameUp("testdata", ".dotdir")
assert.NotEmpty(t, p)
assert.True(t, strings.HasSuffix(p, "testdata"))
p = fsutil.SearchNameUp("testdata", "test.jpg")
assert.NotEmpty(t, p)
assert.True(t, strings.HasSuffix(p, "testdata"))
p = fsutil.SearchNameUp("testdata", "not-exists")
assert.Empty(t, p)
}
func TestGlobWithFunc(t *testing.T) {
assert.NotEmpty(t, fsutil.Glob("testdata/*"))
assert.NotEmpty(t, fsutil.Glob("testdata/*", func(s string) bool {
return s[0] != '.'
}))
var paths []string
err := fsutil.GlobWithFunc("testdata/*", func(fpath string) error {
paths = append(paths, fpath)
return nil
})
assert.NoErr(t, err)
assert.NotEmpty(t, paths)
}
func TestApplyFilters(t *testing.T) {
e1 := &fakeobj.DirEntry{Nam: "some-backup"}
f1 := fsutil.ExcludeSuffix("-backup")
assert.False(t, f1("", e1))
assert.True(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{f1}))
assert.True(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{fsutil.OnlyFindDir}))
assert.False(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{fsutil.OnlyFindFile}))
assert.False(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{fsutil.ExcludeDotFile}))
assert.False(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{fsutil.IncludeSuffix("-backup")}))
assert.True(t, fsutil.ApplyFilters("", e1, []fsutil.FilterFunc{fsutil.ExcludeNames("some-backup")}))
}
func TestFindInDir(t *testing.T) {
err := fsutil.FindInDir("path-not-exist", nil)
assert.NoErr(t, err)
err = fsutil.FindInDir("testdata/test.jpg", nil)
assert.NoErr(t, err)
files := make([]string, 0, 8)
err = fsutil.FindInDir("testdata", func(fPath string, de fs.DirEntry) error {
files = append(files, fPath)
return nil
})
dump.P(files)
assert.NoErr(t, err)
assert.True(t, len(files) > 0)
files = files[:0]
err = fsutil.FindInDir("testdata", func(fPath string, de fs.DirEntry) error {
files = append(files, fPath)
return nil
}, func(fPath string, de fs.DirEntry) bool {
return !strings.HasPrefix(de.Name(), ".")
})
assert.NoErr(t, err)
assert.True(t, len(files) > 0)
err = fsutil.FindInDir("testdata", func(fPath string, de fs.DirEntry) error {
return errorx.Raw("handle error")
})
assert.Err(t, err)
}