forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinder_test.go
162 lines (134 loc) · 3.36 KB
/
finder_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package finder_test
import (
"fmt"
"os"
"testing"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/fsutil/finder"
"github.com/gookit/goutil/testutil/assert"
)
func TestMain(m *testing.M) {
_, _ = fsutil.PutContents("./testdata/test.txt", "hello, in test.txt")
m.Run()
}
func TestFinder_findFile(t *testing.T) {
f := finder.EmptyFinder().
ScanDir("./testdata").
NoDotFile().
NoDotDir().
WithoutExt(".jpg").
CacheResult()
assert.Nil(t, f.Err())
assert.NotEmpty(t, f.String())
assert.NotEmpty(t, f.Config())
assert.Eq(t, 0, f.CacheNum())
// find paths
assert.NotEmpty(t, f.FindPaths())
assert.Gt(t, f.CacheNum(), 0)
assert.NotEmpty(t, f.Caches())
f.Each(func(elem finder.Elem) {
fmt.Println(elem)
})
t.Run("each elem", func(t *testing.T) {
f.EachElem(func(elem finder.Elem) {
fmt.Println(elem)
})
})
t.Run("each file", func(t *testing.T) {
f.EachFile(func(file *os.File) {
fmt.Println(file.Name())
})
})
t.Run("each path", func(t *testing.T) {
f.EachPath(func(filePath string) {
fmt.Println(filePath)
})
})
t.Run("each stat", func(t *testing.T) {
f.EachStat(func(fi os.FileInfo, filePath string) {
fmt.Println(filePath, "=>", fi.ModTime())
})
})
t.Run("reset", func(t *testing.T) {
f.Reset()
assert.Empty(t, f.Caches())
assert.NotEmpty(t, f.FindPaths())
f.EachElem(func(elem finder.Elem) {
fmt.Println(elem)
})
})
}
func TestFinder_OnlyFindDir(t *testing.T) {
ff := finder.NewFinder("./../../").
OnlyFindDir().
UseAbsPath().
WithoutDotDir().
WithDirName("testdata")
ff.EachPath(func(filePath string) {
fmt.Println(filePath)
})
assert.Gt(t, ff.Num(), 0)
assert.Eq(t, 0, ff.CacheNum())
t.Run("each elem", func(t *testing.T) {
ff.Each(func(elem finder.Elem) {
fmt.Println(elem)
})
})
ff.ResetResult()
assert.Eq(t, 0, ff.Num())
assert.Eq(t, 0, ff.CacheNum())
t.Run("max depth", func(t *testing.T) {
ff.WithMaxDepth(2)
ff.EachPath(func(filePath string) {
fmt.Println(filePath)
})
assert.Gt(t, ff.Num(), 0)
})
}
func TestFileFinder_NoDotFile(t *testing.T) {
f := finder.NewEmpty().
CacheResult().
ScanDir("./testdata")
assert.NotEmpty(t, f.String())
fileName := ".env"
assert.NotEmpty(t, f.FindPaths())
assert.Contains(t, f.FindNames(), fileName)
f = finder.EmptyFinder().
ScanDir("./testdata").
NoDotFile()
assert.NotContains(t, f.FindNames(), fileName)
t.Run("Not MatchDotFile", func(t *testing.T) {
f = finder.EmptyFinder().
ScanDir("./testdata").
Not(finder.MatchDotFile())
assert.NotContains(t, f.FindNames(), fileName)
})
}
func TestFileFinder_IncludeName(t *testing.T) {
f := finder.NewFinder(".").
IncludeName("elem.go").
WithNames([]string{"not-exist.file"})
names := f.FindNames()
assert.Len(t, names, 1)
assert.Contains(t, names, "elem.go")
assert.NotContains(t, names, "not-exist.file")
f.Reset()
t.Run("name in subdir", func(t *testing.T) {
f.WithFileName("test.jpg")
names = f.FindNames()
assert.Len(t, names, 1)
assert.Contains(t, names, "test.jpg")
})
}
func TestFileFinder_ExcludeName(t *testing.T) {
f := finder.NewEmpty().
AddScanDir(".").
WithMaxDepth(1).
ExcludeName("elem.go").
WithoutNames([]string{"config.go"})
f.Exclude(finder.MatchSuffix("_test.go"), finder.MatchExt(".md"))
names := f.FindNames()
fmt.Println(names)
assert.Contains(t, names, "matcher.go")
assert.NotContains(t, names, "elem.go")
}