-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinder_test.go
137 lines (126 loc) · 2.49 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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"testing"
)
func assertExists(t *testing.T, basePath string, paths ...string) {
t.Helper()
for _, p := range paths {
_, err := os.Stat(path.Join(basePath, p))
assertEqual(t, err, nil)
}
}
func makeTree(t *testing.T, basePath string, files ...string) {
for _, f := range files {
f = JoinPaths(basePath, f)
assertEqual(t, os.MkdirAll(filepath.Dir(f), 0755), nil)
if f[len(f)-1] != os.PathSeparator {
assertEqual(t, os.WriteFile(f, []byte(fmt.Sprintf("The %s file", f)), 0644), nil)
}
}
}
func TestBasename(t *testing.T) {
assertEqual(t, baseName("abc"), "abc")
assertEqual(t, baseName("abc.txt"), "abc.txt")
assertEqual(t, baseName(path.Join("123", "abc.yaml")), "abc.yaml")
assertEqual(t, baseName("abc"+string(os.PathSeparator)), "")
assertEqual(t, baseName(""), "")
assertEqual(t, baseName("."), "")
}
func TestFindFiles(t *testing.T) {
defer resetConfig()
Cfg = &Config{Verbose: false}
searchDir := t.TempDir()
paths := []string{
"check.txt",
"check.md",
"/check/hi.md",
"/check/hi.yaml",
"/check2/b/cart.yaml",
"/abc/def/cart.yaml",
}
makeTree(t, searchDir, paths...)
cases := []struct {
tag string
searchDir string
search string
exclude []string
prepend string
res []string
}{
{
tag: "t1",
res: []string{
"abc/",
"check/",
"check",
"check.txt",
"check2/",
}},
{
tag: "t2",
exclude: []string{"abc", "check2", "check.txt"},
res: []string{
"check/",
"check",
}},
{
tag: "t3",
exclude: []string{"abc", "check2", "check.txt"},
prepend: "abc",
res: []string{
"abc/check/",
"abc/check",
}},
{
tag: "t4",
search: "abc",
res: []string{
"abc/",
},
},
{
tag: "t5",
search: "check",
res: []string{
"check/",
"check",
"check.txt",
"check2/",
},
},
{
tag: "t6",
searchDir: path.Join(searchDir, "abc"),
res: []string{
"def/",
},
},
{
tag: "t7",
searchDir: path.Join(searchDir, "abc/def"),
search: "ca",
res: []string{"cart.yaml"},
},
{
tag: "t8",
searchDir: path.Join(searchDir, "abc/def"),
search: "check",
},
}
// Search
for _, c := range cases {
t.Run(c.tag, func(t *testing.T) {
root := c.searchDir
if root == "" {
root = searchDir
}
res, err := findFiles(root, c.search, c.exclude, c.prepend)
assertEqual(t, err, nil)
assertEqualSlice(t, res, c.res)
})
}
}