Debian Codegraph @

tools/dir_walker_test.go

raw symbols SCIP
1 package tools
2
3 import (
4 "errors"
5 "fmt"
6 "os"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 )
12
13 type newDirWalkerForFileTestCase struct {
14 filePath string
15 expectedDirPath string
16 }
17
18 func (c *newDirWalkerForFileTestCase) Assert(t *testing.T) {
19 w := NewDirWalkerForFile("", c.filePath, nil)
20 assert.Equal(t, c.expectedDirPath, w.path)
21 }
22
23 func TestNewDirWalkerForFile(t *testing.T) {
24 for desc, c := range map[string]*newDirWalkerForFileTestCase{
25 "filename only": {"foo.bin", ""},
26 "path with one dir": {"abc/foo.bin", "abc"},
27 "path with two dirs": {"abc/def/foo.bin", "abc/def"},
28 "path with leading slash": {"/foo.bin", ""},
29 "path with trailing slash": {"abc/", "abc"},
30 "bare slash": {"/", ""},
31 "empty path": {"", ""},
32 } {
33 t.Run(desc, c.Assert)
34 }
35 }
36
37 type dirWalkerTestConfig struct{}
38
39 func (c *dirWalkerTestConfig) RepositoryPermissions(executable bool) os.FileMode {
40 return os.FileMode(0755)
41 }
42
43 type dirWalkerWalkTestCase struct {
44 parentPath string
45 path string
46 create bool
47
48 existsPath string
49 existsFile string
50 existsLink string
51
52 expectedParentPath string
53 expectedPath string
54 expectedErr error
55
56 walker *DirWalker
57 }
58
59 func (c *dirWalkerWalkTestCase) prependParentPath(path string) string {
60 if path == "" {
61 return c.parentPath
62 } else if c.parentPath == "" {
63 return path
64 } else if path[0] == '/' {
65 return "/" + c.parentPath + path
66 } else {
67 return c.parentPath + "/" + path
68 }
69 }
70
71 func (c *dirWalkerWalkTestCase) setupPaths(t *testing.T, parentPath string) error {
72 c.parentPath = parentPath
73
74 if parentPath != "" {
75 if err := os.MkdirAll(parentPath, 0755); err != nil {
76 return fmt.Errorf("unable to create path: %w", err)
77 }
78 }
79
80 if c.existsPath != "" {
81 c.existsPath = c.prependParentPath(c.existsPath)
82 if err := os.MkdirAll(c.existsPath, 0755); err != nil {
83 return fmt.Errorf("unable to create path: %w", err)
84 }
85 }
86
87 if c.existsFile != "" {
88 c.existsFile = c.prependParentPath(c.existsFile)
89 f, err := os.Create(c.existsFile)
90 if err != nil {
91 return fmt.Errorf("unable to create file: %w", err)
92 }
93 f.Close()
94 }
95
96 if c.existsLink != "" {
97 c.existsLink = c.prependParentPath(c.existsLink)
98 if err := os.Symlink(t.TempDir(), c.existsLink); err != nil {
99 return fmt.Errorf("unable to create symbolic link: %w", err)
100 }
101 }
102
103 c.expectedParentPath = c.prependParentPath(c.expectedParentPath)
104
105 return nil
106 }
107
108 func (c *dirWalkerWalkTestCase) Assert(t *testing.T) {
109 c.walker.parentPath = c.parentPath
110 c.walker.path = c.path
111
112 err := c.walker.walk(c.create)
113
114 assert.Equal(t, c.expectedParentPath, c.walker.parentPath, "found path does not match")
115 assert.Equal(t, c.expectedPath, c.walker.path, "missing path does not match")
116 if c.expectedErr == nil {
117 assert.NoError(t, err)
118 } else {
119 assert.Error(t, err)
120 assert.True(t, errors.Is(err, c.expectedErr), "wrong error type")
121 }
122 }
123
124 func TestDirWalkerWalk(t *testing.T) {
125 wd, err := os.Getwd()
126 require.NoError(t, err)
127
128 defer os.Chdir(wd)
129
130 for desc, c := range map[string]*dirWalkerWalkTestCase{
131 "empty path": {},
132 "one extant dir": {
133 path: "abc",
134 existsPath: "abc",
135 expectedParentPath: "abc",
136 },
137 "one missing dir": {
138 path: "abc",
139 expectedPath: "abc",
140 expectedErr: os.ErrNotExist,
141 },
142 "two extant dirs": {
143 path: "abc/def",
144 existsPath: "abc/def",
145 expectedParentPath: "abc/def",
146 },
147 "two missing dirs": {
148 path: "abc/def",
149 expectedPath: "abc/def",
150 expectedErr: os.ErrNotExist,
151 },
152 "three extant dirs": {
153 path: "abc/def/ghi",
154 existsPath: "abc/def/ghi",
155 expectedParentPath: "abc/def/ghi",
156 },
157 "three missing dirs": {
158 path: "abc/def/ghi",
159 expectedPath: "abc/def/ghi",
160 expectedErr: os.ErrNotExist,
161 },
162 "one extant dir and one missing dir": {
163 path: "abc/def",
164 existsPath: "abc",
165 expectedParentPath: "abc",
166 expectedPath: "def",
167 expectedErr: os.ErrNotExist,
168 },
169 "one extant dir and two missing dirs": {
170 path: "abc/def/ghi",
171 existsPath: "abc",
172 expectedParentPath: "abc",
173 expectedPath: "def/ghi",
174 expectedErr: os.ErrNotExist,
175 },
176 "two extant dirs and one missing dir": {
177 path: "abc/def/ghi",
178 existsPath: "abc/def",
179 expectedParentPath: "abc/def",
180 expectedPath: "ghi",
181 expectedErr: os.ErrNotExist,
182 },
183 "one missing dir with trailing slash": {
184 path: "abc/",
185 expectedPath: "abc/",
186 expectedErr: os.ErrNotExist,
187 },
188 "one extant dir with trailing slash": {
189 path: "abc/",
190 existsPath: "abc",
191 expectedParentPath: "abc",
192 },
193 "two extant dirs with trailing slash": {
194 path: "abc/def/",
195 existsPath: "abc/def",
196 expectedParentPath: "abc/def",
197 },
198 "one extant dir and one missing dir with trailing slash": {
199 path: "abc/def/",
200 existsPath: "abc",
201 expectedParentPath: "abc",
202 expectedPath: "def/",
203 expectedErr: os.ErrNotExist,
204 },
205 "one conflicting file": {
206 path: "abc",
207 existsFile: "abc",
208 expectedPath: "abc",
209 expectedErr: errNotDir,
210 },
211 "one extant dir and one conflicting file": {
212 path: "abc/def",
213 existsPath: "abc",
214 existsFile: "abc/def",
215 expectedParentPath: "abc",
216 expectedPath: "def",
217 expectedErr: errNotDir,
218 },
219 "two extant dirs and one conflicting file": {
220 path: "abc/def/ghi",
221 existsPath: "abc/def",
222 existsFile: "abc/def/ghi",
223 expectedParentPath: "abc/def",
224 expectedPath: "ghi",
225 expectedErr: errNotDir,
226 },
227 "one extant dir, one conflicting file, and one missing dir": {
228 path: "abc/def/ghi",
229 existsPath: "abc",
230 existsFile: "abc/def",
231 expectedParentPath: "abc",
232 expectedPath: "def/ghi",
233 expectedErr: errNotDir,
234 },
235 "one conflicting symlink": {
236 path: "abc",
237 existsLink: "abc",
238 expectedPath: "abc",
239 expectedErr: errNotDir,
240 },
241 "one extant dir and one conflicting symlink": {
242 path: "abc/def",
243 existsPath: "abc",
244 existsLink: "abc/def",
245 expectedParentPath: "abc",
246 expectedPath: "def",
247 expectedErr: errNotDir,
248 },
249 "two extant dirs and one conflicting symlink": {
250 path: "abc/def/ghi",
251 existsPath: "abc/def",
252 existsLink: "abc/def/ghi",
253 expectedParentPath: "abc/def",
254 expectedPath: "ghi",
255 expectedErr: errNotDir,
256 },
257 "one extant dir, one conflicting symlink, and one missing dir": {
258 path: "abc/def/ghi",
259 existsPath: "abc",
260 existsLink: "abc/def",
261 expectedParentPath: "abc",
262 expectedPath: "def/ghi",
263 expectedErr: errNotDir,
264 },
265 "one extant dir (not modified)": {
266 path: "abc",
267 create: true,
268 existsPath: "abc",
269 expectedParentPath: "abc",
270 },
271 "one created dir": {
272 path: "abc",
273 create: true,
274 expectedParentPath: "abc",
275 },
276 "two extant dirs (not modified)": {
277 path: "abc/def",
278 create: true,
279 existsPath: "abc/def",
280 expectedParentPath: "abc/def",
281 },
282 "two created dirs": {
283 path: "abc/def",
284 create: true,
285 expectedParentPath: "abc/def",
286 },
287 "three extant dirs (not modified)": {
288 path: "abc/def/ghi",
289 create: true,
290 existsPath: "abc/def/ghi",
291 expectedParentPath: "abc/def/ghi",
292 },
293 "three created dirs": {
294 path: "abc/def/ghi",
295 create: true,
296 expectedParentPath: "abc/def/ghi",
297 },
298 "one extant dir and one created dir": {
299 path: "abc/def",
300 create: true,
301 existsPath: "abc",
302 expectedParentPath: "abc/def",
303 },
304 "one extant dir and two created dirs": {
305 path: "abc/def/ghi",
306 create: true,
307 existsPath: "abc",
308 expectedParentPath: "abc/def/ghi",
309 },
310 "two extant dirs and one created dir": {
311 path: "abc/def/ghi",
312 create: true,
313 existsPath: "abc/def",
314 expectedParentPath: "abc/def/ghi",
315 },
316 "one created dir with trailing slash": {
317 path: "abc/",
318 create: true,
319 expectedParentPath: "abc",
320 },
321 "one extant dir with trailing slash (not modified)": {
322 path: "abc/",
323 create: true,
324 existsPath: "abc",
325 expectedParentPath: "abc",
326 },
327 "two extant dirs with trailing slash (not modified)": {
328 path: "abc/def/",
329 create: true,
330 existsPath: "abc/def",
331 expectedParentPath: "abc/def",
332 },
333 "one extant dir and one created dir with trailing slash": {
334 path: "abc/def/",
335 create: true,
336 existsPath: "abc",
337 expectedParentPath: "abc/def",
338 },
339 "one conflicting file (not modified)": {
340 path: "abc",
341 create: true,
342 existsFile: "abc",
343 expectedPath: "abc",
344 expectedErr: errNotDir,
345 },
346 "one extant dir and one conflicting file (not modified)": {
347 path: "abc/def",
348 create: true,
349 existsPath: "abc",
350 existsFile: "abc/def",
351 expectedParentPath: "abc",
352 expectedPath: "def",
353 expectedErr: errNotDir,
354 },
355 "two extant dirs and one conflicting file (not modified)": {
356 path: "abc/def/ghi",
357 create: true,
358 existsPath: "abc/def",
359 existsFile: "abc/def/ghi",
360 expectedParentPath: "abc/def",
361 expectedPath: "ghi",
362 expectedErr: errNotDir,
363 },
364 "one extant dir, one conflicting file, and one missing dir (not modified)": {
365 path: "abc/def/ghi",
366 create: true,
367 existsPath: "abc",
368 existsFile: "abc/def",
369 expectedParentPath: "abc",
370 expectedPath: "def/ghi",
371 expectedErr: errNotDir,
372 },
373 "one conflicting symlink (not modified)": {
374 path: "abc",
375 create: true,
376 existsLink: "abc",
377 expectedPath: "abc",
378 expectedErr: errNotDir,
379 },
380 "one extant dir and one conflicting symlink (not modified)": {
381 path: "abc/def",
382 create: true,
383 existsPath: "abc",
384 existsLink: "abc/def",
385 expectedParentPath: "abc",
386 expectedPath: "def",
387 expectedErr: errNotDir,
388 },
389 "two extant dirs and one conflicting symlink (not modified)": {
390 path: "abc/def/ghi",
391 create: true,
392 existsPath: "abc/def",
393 existsLink: "abc/def/ghi",
394 expectedParentPath: "abc/def",
395 expectedPath: "ghi",
396 expectedErr: errNotDir,
397 },
398 "one extant dir, one conflicting symlink, and one missing dir (not modified)": {
399 path: "abc/def/ghi",
400 create: true,
401 existsPath: "abc",
402 existsLink: "abc/def",
403 expectedParentPath: "abc",
404 expectedPath: "def/ghi",
405 expectedErr: errNotDir,
406 },
407 "invalid bare slash": {
408 path: "/",
409 expectedPath: "/",
410 expectedErr: errInvalidDir,
411 },
412 "invalid multiple slashes": {
413 path: "abc//def",
414 existsPath: "abc",
415 expectedParentPath: "abc",
416 expectedPath: "/def",
417 expectedErr: errInvalidDir,
418 },
419 "invalid leading slash": {
420 path: "/abc",
421 existsPath: "abc",
422 expectedPath: "/abc",
423 expectedErr: errInvalidDir,
424 },
425 "invalid bare dot component": {
426 path: ".",
427 expectedPath: ".",
428 expectedErr: errInvalidDir,
429 },
430 "invalid dot component": {
431 path: "abc/./def",
432 existsPath: "abc/def",
433 expectedParentPath: "abc",
434 expectedPath: "./def",
435 expectedErr: errInvalidDir,
436 },
437 "invalid bare double-dot component": {
438 path: "..",
439 expectedPath: "..",
440 expectedErr: errInvalidDir,
441 },
442 "invalid double-dot component": {
443 path: "abc/../def",
444 existsPath: "abc",
445 expectedParentPath: "abc",
446 expectedPath: "../def",
447 expectedErr: errInvalidDir,
448 },
449 } {
450 if err := os.Chdir(t.TempDir()); err != nil {
451 t.Errorf("unable to change directory: %s", err)
452 }
453
454 c.walker = &DirWalker{
455 config: &dirWalkerTestConfig{},
456 }
457
458 if err := c.setupPaths(t, ""); err != nil {
459 t.Error(err)
460 continue
461 }
462
463 t.Run(desc, c.Assert)
464
465 // retest with parent path; note that this alters the test case
466 if err := c.setupPaths(t, "foo/bar"); err != nil {
467 t.Error(err)
468 continue
469 }
470
471 t.Run(desc+" with parent path", c.Assert)
472 }
473 }