Debian Codegraph @

finder_test.go

raw
1 package locafero
2
3 import (
4 "path/filepath"
5 "runtime"
6 "testing"
7
8 "github.com/spf13/afero"
9 )
10
11 func toAbsOsPath(s string) string {
12 if runtime.GOOS == "windows" {
13 return filepath.Join("C:", s)
14 }
15
16 return s
17 }
18
19 func eachToAbsOsPath(paths []string) []string {
20 if paths == nil {
21 return nil
22 }
23
24 newPaths := make([]string, len(paths))
25
26 for i, p := range paths {
27 newPaths[i] = toAbsOsPath(p)
28 }
29
30 return newPaths
31 }
32
33 func toOsPath(s string) string {
34 if runtime.GOOS == "windows" {
35 return filepath.Clean(s)
36 }
37
38 return s
39 }
40
41 func eachToOsPath(paths []string) []string {
42 if paths == nil {
43 return nil
44 }
45
46 newPaths := make([]string, len(paths))
47
48 for i, p := range paths {
49 newPaths[i] = toOsPath(p)
50 }
51
52 return newPaths
53 }
54
55 func TestFinder_Find(t *testing.T) {
56 fsys := afero.NewMemMapFs()
57
58 files := []string{
59 "/home/user/.config/app/config.yaml",
60 "/home/user/app/config.yaml",
61 "/home/user/config.json",
62 "/home/user/config.yaml",
63 "/home/user/config/app.yaml",
64 "/home/user/config/config.yaml",
65 "/etc/app/config.yaml",
66 "/etc/config.json",
67 "/etc/config.yaml",
68 "/etc/config/app.yaml",
69 "/etc/config/config.yaml",
70 }
71
72 for _, file := range files {
73 dir := filepath.Dir(toAbsOsPath(file))
74
75 err := fsys.MkdirAll(dir, 0o777)
76 mustNotBeError(t, err)
77
78 _, err = fsys.Create(toAbsOsPath(file))
79 mustNotBeError(t, err)
80 }
81
82 testCases := []struct {
83 name string
84 finder Finder
85 results []string
86 }{
87 {
88 name: "nothing to find",
89 finder: Finder{},
90 results: nil,
91 },
92 {
93 name: "no names to find",
94 finder: Finder{
95 Paths: []string{"/home/user"},
96 },
97 results: nil,
98 },
99 {
100 name: "no paths to find in",
101 finder: Finder{
102 Names: []string{"config.yaml"},
103 },
104 results: nil,
105 },
106 {
107 name: "find in path",
108 finder: Finder{
109 Paths: []string{"/home/user"},
110 Names: []string{"config.yaml"},
111 },
112 results: []string{
113 "/home/user/config.yaml",
114 },
115 },
116 {
117 name: "find in multiple paths",
118 finder: Finder{
119 Paths: []string{"/home/user", "/etc"},
120 Names: []string{"config.yaml"},
121 },
122 results: []string{
123 "/home/user/config.yaml",
124 "/etc/config.yaml",
125 },
126 },
127 {
128 name: "find multiple names in multiple paths",
129 finder: Finder{
130 Paths: []string{"/home/user", "/etc"},
131 Names: []string{"config", "config.yaml"},
132 },
133 results: []string{
134 "/home/user/config",
135 "/home/user/config.yaml",
136 "/etc/config",
137 "/etc/config.yaml",
138 },
139 },
140 {
141 name: "find in subdirs of each other",
142 finder: Finder{
143 Paths: []string{"/home/user", "/home/user/app"},
144 Names: []string{"config.yaml"},
145 },
146 results: []string{
147 "/home/user/config.yaml",
148 "/home/user/app/config.yaml",
149 },
150 },
151 {
152 name: "find files only",
153 finder: Finder{
154 Paths: []string{"/home/user", "/etc"},
155 Names: []string{"config", "config.yaml"},
156 Type: FileTypeFile,
157 },
158 results: []string{
159 "/home/user/config.yaml",
160 "/etc/config.yaml",
161 },
162 },
163 {
164 name: "find dirs only",
165 finder: Finder{
166 Paths: []string{"/home/user", "/etc"},
167 Names: []string{"config", "config.yaml"},
168 Type: FileTypeDir,
169 },
170 results: []string{
171 "/home/user/config",
172 "/etc/config",
173 },
174 },
175 {
176 name: "glob match",
177 finder: Finder{
178 Paths: []string{"/home/user", "/etc"},
179 Names: []string{"config*"},
180 },
181 results: []string{
182 "/home/user/config",
183 "/home/user/config.json",
184 "/home/user/config.yaml",
185 "/etc/config",
186 "/etc/config.json",
187 "/etc/config.yaml",
188 },
189 },
190 {
191 name: "glob match",
192 finder: Finder{
193 Paths: []string{"/home/user", "/etc"},
194 Names: []string{"config.*"},
195 },
196 results: []string{
197 "/home/user/config.json",
198 "/home/user/config.yaml",
199 "/etc/config.json",
200 "/etc/config.yaml",
201 },
202 },
203 {
204 name: "glob match files",
205 finder: Finder{
206 Paths: []string{"/home/user", "/etc"},
207 Names: []string{"config*"},
208 Type: FileTypeFile,
209 },
210 results: []string{
211 "/home/user/config.json",
212 "/home/user/config.yaml",
213 "/etc/config.json",
214 "/etc/config.yaml",
215 },
216 },
217 {
218 name: "glob match dirs",
219 finder: Finder{
220 Paths: []string{"/home/user", "/etc"},
221 Names: []string{"config*"},
222 Type: FileTypeDir,
223 },
224 results: []string{
225 "/home/user/config",
226 "/etc/config",
227 },
228 },
229 {
230 name: "glob match in subdirs of each other",
231 finder: Finder{
232 Paths: []string{"/home/user", "/home/user/config", "/etc", "/etc/config"},
233 Names: []string{"config*"},
234 },
235 results: []string{
236 "/home/user/config",
237 "/home/user/config.json",
238 "/home/user/config.yaml",
239 "/home/user/config/config.yaml",
240 "/etc/config",
241 "/etc/config.json",
242 "/etc/config.yaml",
243 "/etc/config/config.yaml",
244 },
245 },
246 {
247 name: "glob match files in subdirs of each other",
248 finder: Finder{
249 Paths: []string{"/home/user", "/home/user/config", "/etc", "/etc/config"},
250 Names: []string{"config*"},
251 Type: FileTypeFile,
252 },
253 results: []string{
254 "/home/user/config.json",
255 "/home/user/config.yaml",
256 "/home/user/config/config.yaml",
257 "/etc/config.json",
258 "/etc/config.yaml",
259 "/etc/config/config.yaml",
260 },
261 },
262 {
263 name: "glob match dirs in subdirs of each other",
264 finder: Finder{
265 Paths: []string{"/home/user", "/home/user/config", "/etc", "/etc/config"},
266 Names: []string{"config*"},
267 Type: FileTypeDir,
268 },
269 results: []string{
270 "/home/user/config",
271 "/etc/config",
272 },
273 },
274 }
275
276 for _, testCase := range testCases {
277 t.Run(testCase.name, func(t *testing.T) {
278 testCase.finder.Paths = eachToAbsOsPath(testCase.finder.Paths)
279
280 results, err := testCase.finder.Find(fsys)
281 mustNotBeError(t, err)
282
283 beEqual(t, eachToAbsOsPath(testCase.results), results)
284 })
285 }
286 }
287
288 func TestFinder_Find_RelativePaths(t *testing.T) {
289 fsys := afero.NewOsFs()
290
291 finder := Finder{
292 Paths: []string{
293 "testdata/home/user",
294 "testdata/etc",
295 },
296 Names: []string{"config.*"},
297 Type: FileTypeFile,
298 }
299
300 results, err := finder.Find(fsys)
301 mustNotBeError(t, err)
302
303 expected := []string{
304 "testdata/home/user/config.yaml",
305 "testdata/etc/config.yaml",
306 }
307
308 beEqual(t, eachToOsPath(expected), results)
309 }
310
311 func TestFinder_Find_AbsolutePaths(t *testing.T) {
312 abs := func(t *testing.T, s string) string {
313 t.Helper()
314
315 a, err := filepath.Abs(s)
316 mustNotBeError(t, err)
317
318 return a
319 }
320
321 fsys := afero.NewOsFs()
322
323 finder := Finder{
324 Paths: []string{
325 abs(t, "testdata/home/user"),
326 abs(t, "testdata/etc"),
327 },
328 Names: []string{"config.*"},
329 Type: FileTypeFile,
330 }
331
332 results, err := finder.Find(fsys)
333 mustNotBeError(t, err)
334
335 expected := []string{
336 abs(t, "testdata/home/user/config.yaml"),
337 abs(t, "testdata/etc/config.yaml"),
338 }
339
340 beEqual(t, expected, results)
341 }
342
343 func FuzzFinder_Find(f *testing.F) {
344 f.Add("test") // A simple pattern
345 f.Add("*") // A wildcard
346 f.Add("???[abc]") // Something with pattern syntax
347
348 fsys := afero.NewMemMapFs()
349
350 _ = afero.WriteFile(fsys, "foo.txt", []byte("Hello world"), 0o644)
351 _ = afero.WriteFile(fsys, "bar.txt", []byte("Hello again"), 0o644)
352
353 f.Fuzz(func(_ *testing.T, pattern string) {
354 finder := Finder{
355 Paths: []string{""},
356 Names: []string{pattern},
357 Type: FileTypeFile,
358 }
359
360 _, _ = finder.Find(fsys)
361 })
362 }
363
364 func beEqual[T comparable](t *testing.T, expected, actual []T) {
365 t.Helper()
366
367 if len(expected) != len(actual) {
368 t.Errorf(
369 "expected both lists to be the same length\nwant: %d\ngot: %d",
370 len(expected),
371 len(actual),
372 )
373 }
374
375 for i := range expected {
376 if expected[i] != actual[i] {
377 t.Errorf(
378 "expected %d. element to be equal\nwant: %v\ngot: %v",
379 i+1,
380 expected[i],
381 actual[i],
382 )
383 }
384 }
385 }
386
387 func mustNotBeError(t *testing.T, err error) {
388 t.Helper()
389
390 if err != nil {
391 t.Fatalf("expected no error, got %v", err)
392 }
393 }