blob: 60e5eb28167c7752fb6bc9127bfd5579fc9c5598 [file] [log] [blame]
Jeff Gastonf1fd45e2017-08-09 18:25:28 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package finder
16
17import (
18 "fmt"
19 "log"
20 "path/filepath"
21 "reflect"
22 "testing"
23
24 "sort"
25
26 "io/ioutil"
27
28 "android/soong/fs"
29 "runtime/debug"
30 "time"
31)
32
33// some utils for tests to use
34func newFs() *fs.MockFs {
35 return fs.NewMockFs(map[string][]byte{})
36}
37
38func newFinder(t *testing.T, filesystem *fs.MockFs, cacheParams CacheParams) *Finder {
39 cachePath := "/finder/finder-db"
40 cacheDir := filepath.Dir(cachePath)
41 filesystem.MkDirs(cacheDir)
42 if cacheParams.WorkingDirectory == "" {
43 cacheParams.WorkingDirectory = "/cwd"
44 }
45
46 logger := log.New(ioutil.Discard, "", 0)
47 finder := New(cacheParams, filesystem, logger, cachePath)
48 return finder
49}
50
51func finderWithSameParams(t *testing.T, original *Finder) *Finder {
52 return New(
53 original.cacheMetadata.Config.CacheParams,
54 original.filesystem,
55 original.logger,
56 original.DbPath)
57}
58
59func write(t *testing.T, path string, content string, filesystem *fs.MockFs) {
60 parent := filepath.Dir(path)
61 filesystem.MkDirs(parent)
62 err := filesystem.WriteFile(path, []byte(content), 0777)
63 if err != nil {
64 t.Fatal(err.Error())
65 }
66}
67
68func create(t *testing.T, path string, filesystem *fs.MockFs) {
69 write(t, path, "hi", filesystem)
70}
71
72func delete(t *testing.T, path string, filesystem *fs.MockFs) {
73 err := filesystem.Remove(path)
74 if err != nil {
75 t.Fatal(err.Error())
76 }
77}
78
79func removeAll(t *testing.T, path string, filesystem *fs.MockFs) {
80 err := filesystem.RemoveAll(path)
81 if err != nil {
82 t.Fatal(err.Error())
83 }
84}
85
86func move(t *testing.T, oldPath string, newPath string, filesystem *fs.MockFs) {
87 err := filesystem.Rename(oldPath, newPath)
88 if err != nil {
89 t.Fatal(err.Error())
90 }
91}
92
93func link(t *testing.T, newPath string, oldPath string, filesystem *fs.MockFs) {
94 parentPath := filepath.Dir(newPath)
95 err := filesystem.MkDirs(parentPath)
96 if err != nil {
97 t.Fatal(err.Error())
98 }
99 err = filesystem.Symlink(oldPath, newPath)
100 if err != nil {
101 t.Fatal(err.Error())
102 }
103}
104func read(t *testing.T, path string, filesystem *fs.MockFs) string {
105 reader, err := filesystem.Open(path)
106 if err != nil {
107 t.Fatalf(err.Error())
108 }
109 bytes, err := ioutil.ReadAll(reader)
110 if err != nil {
111 t.Fatal(err.Error())
112 }
113 return string(bytes)
114}
115func modTime(t *testing.T, path string, filesystem *fs.MockFs) time.Time {
116 stats, err := filesystem.Lstat(path)
117 if err != nil {
118 t.Fatal(err.Error())
119 }
120 return stats.ModTime()
121}
122func setReadable(t *testing.T, path string, readable bool, filesystem *fs.MockFs) {
123 err := filesystem.SetReadable(path, readable)
124 if err != nil {
125 t.Fatal(err.Error())
126 }
127}
128func fatal(t *testing.T, message string) {
129 t.Error(message)
130 debug.PrintStack()
131 t.FailNow()
132}
133func assertSameResponse(t *testing.T, actual []string, expected []string) {
134 sort.Strings(actual)
135 sort.Strings(expected)
136 if !reflect.DeepEqual(actual, expected) {
137 fatal(
138 t,
139 fmt.Sprintf(
140 "Expected Finder to return these %v paths:\n %v,\ninstead returned these %v paths: %v\n",
141 len(expected), expected, len(actual), actual),
142 )
143 }
144}
145
146func assertSameStatCalls(t *testing.T, actual []string, expected []string) {
147 sort.Strings(actual)
148 sort.Strings(expected)
149
150 if !reflect.DeepEqual(actual, expected) {
151 fatal(
152 t,
153 fmt.Sprintf(
154 "Finder made incorrect Stat calls.\n"+
155 "Actual:\n"+
156 "%v\n"+
157 "Expected:\n"+
158 "%v\n"+
159 "\n",
160 actual, expected),
161 )
162 }
163}
164func assertSameReadDirCalls(t *testing.T, actual []string, expected []string) {
165 sort.Strings(actual)
166 sort.Strings(expected)
167
168 if !reflect.DeepEqual(actual, expected) {
169 fatal(
170 t,
171 fmt.Sprintf(
172 "Finder made incorrect ReadDir calls.\n"+
173 "Actual:\n"+
174 "%v\n"+
175 "Expected:\n"+
176 "%v\n"+
177 "\n",
178 actual, expected),
179 )
180 }
181}
182
183// runSimpleTests creates a few files, searches for findme.txt, and checks for the expected matches
184func runSimpleTest(t *testing.T, existentPaths []string, expectedMatches []string) {
185 filesystem := newFs()
186 root := "/tmp"
187 filesystem.MkDirs(root)
188 for _, path := range existentPaths {
189 create(t, filepath.Join(root, path), filesystem)
190 }
191
192 finder := newFinder(t,
193 filesystem,
194 CacheParams{
195 "/cwd",
196 []string{root},
197 nil,
198 nil,
199 []string{"findme.txt", "skipme.txt"},
200 },
201 )
202 defer finder.Shutdown()
203
204 foundPaths := finder.FindNamedAt(root, "findme.txt")
205 absoluteMatches := []string{}
206 for i := range expectedMatches {
207 absoluteMatches = append(absoluteMatches, filepath.Join(root, expectedMatches[i]))
208 }
209 assertSameResponse(t, foundPaths, absoluteMatches)
210}
211
212// end of utils, start of individual tests
213
214func TestSingleFile(t *testing.T) {
215 runSimpleTest(t,
216 []string{"findme.txt"},
217 []string{"findme.txt"},
218 )
219}
220
221func TestIncludeFiles(t *testing.T) {
222 runSimpleTest(t,
223 []string{"findme.txt", "skipme.txt"},
224 []string{"findme.txt"},
225 )
226}
227
228func TestNestedDirectories(t *testing.T) {
229 runSimpleTest(t,
230 []string{"findme.txt", "skipme.txt", "subdir/findme.txt", "subdir/skipme.txt"},
231 []string{"findme.txt", "subdir/findme.txt"},
232 )
233}
234
235func TestEmptyDirectory(t *testing.T) {
236 runSimpleTest(t,
237 []string{},
238 []string{},
239 )
240}
241
242func TestEmptyPath(t *testing.T) {
243 filesystem := newFs()
244 root := "/tmp"
245 create(t, filepath.Join(root, "findme.txt"), filesystem)
246
247 finder := newFinder(
248 t,
249 filesystem,
250 CacheParams{
251 RootDirs: []string{root},
252 IncludeFiles: []string{"findme.txt", "skipme.txt"},
253 },
254 )
255 defer finder.Shutdown()
256
257 foundPaths := finder.FindNamedAt("", "findme.txt")
258
259 assertSameResponse(t, foundPaths, []string{})
260}
261
262func TestFilesystemRoot(t *testing.T) {
263 filesystem := newFs()
264 root := "/"
265 createdPath := "/findme.txt"
266 create(t, createdPath, filesystem)
267
268 finder := newFinder(
269 t,
270 filesystem,
271 CacheParams{
272 RootDirs: []string{root},
273 IncludeFiles: []string{"findme.txt", "skipme.txt"},
274 },
275 )
276 defer finder.Shutdown()
277
278 foundPaths := finder.FindNamedAt(root, "findme.txt")
279
280 assertSameResponse(t, foundPaths, []string{createdPath})
281}
282
283func TestNonexistentPath(t *testing.T) {
284 filesystem := newFs()
285 create(t, "/tmp/findme.txt", filesystem)
286
287 finder := newFinder(
288 t,
289 filesystem,
290 CacheParams{
291 RootDirs: []string{"/tmp/IDontExist"},
292 IncludeFiles: []string{"findme.txt", "skipme.txt"},
293 },
294 )
295 defer finder.Shutdown()
296
297 foundPaths := finder.FindNamedAt("/tmp/IAlsoDontExist", "findme.txt")
298
299 assertSameResponse(t, foundPaths, []string{})
300}
301
302func TestExcludeDirs(t *testing.T) {
303 filesystem := newFs()
304 create(t, "/tmp/exclude/findme.txt", filesystem)
305 create(t, "/tmp/exclude/subdir/findme.txt", filesystem)
306 create(t, "/tmp/subdir/exclude/findme.txt", filesystem)
307 create(t, "/tmp/subdir/subdir/findme.txt", filesystem)
308 create(t, "/tmp/subdir/findme.txt", filesystem)
309 create(t, "/tmp/findme.txt", filesystem)
310
311 finder := newFinder(
312 t,
313 filesystem,
314 CacheParams{
315 RootDirs: []string{"/tmp"},
316 ExcludeDirs: []string{"exclude"},
317 IncludeFiles: []string{"findme.txt", "skipme.txt"},
318 },
319 )
320 defer finder.Shutdown()
321
322 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
323
324 assertSameResponse(t, foundPaths,
325 []string{"/tmp/findme.txt",
326 "/tmp/subdir/findme.txt",
327 "/tmp/subdir/subdir/findme.txt"})
328}
329
330func TestPruneFiles(t *testing.T) {
331 filesystem := newFs()
332 create(t, "/tmp/out/findme.txt", filesystem)
333 create(t, "/tmp/out/.ignore-out-dir", filesystem)
334 create(t, "/tmp/out/child/findme.txt", filesystem)
335
336 create(t, "/tmp/out2/.ignore-out-dir", filesystem)
337 create(t, "/tmp/out2/sub/findme.txt", filesystem)
338
339 create(t, "/tmp/findme.txt", filesystem)
340 create(t, "/tmp/include/findme.txt", filesystem)
341
342 finder := newFinder(
343 t,
344 filesystem,
345 CacheParams{
346 RootDirs: []string{"/tmp"},
347 PruneFiles: []string{".ignore-out-dir"},
348 IncludeFiles: []string{"findme.txt"},
349 },
350 )
351 defer finder.Shutdown()
352
353 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
354
355 assertSameResponse(t, foundPaths,
356 []string{"/tmp/findme.txt",
357 "/tmp/include/findme.txt"})
358}
359
360func TestRootDir(t *testing.T) {
361 filesystem := newFs()
362 create(t, "/tmp/a/findme.txt", filesystem)
363 create(t, "/tmp/a/subdir/findme.txt", filesystem)
364 create(t, "/tmp/b/findme.txt", filesystem)
365 create(t, "/tmp/b/subdir/findme.txt", filesystem)
366
367 finder := newFinder(
368 t,
369 filesystem,
370 CacheParams{
371 RootDirs: []string{"/tmp/a"},
372 IncludeFiles: []string{"findme.txt"},
373 },
374 )
375 defer finder.Shutdown()
376
377 foundPaths := finder.FindNamedAt("/tmp/a", "findme.txt")
378
379 assertSameResponse(t, foundPaths,
380 []string{"/tmp/a/findme.txt",
381 "/tmp/a/subdir/findme.txt"})
382}
383
384func TestUncachedDir(t *testing.T) {
385 filesystem := newFs()
386 create(t, "/tmp/a/findme.txt", filesystem)
387 create(t, "/tmp/a/subdir/findme.txt", filesystem)
388 create(t, "/tmp/b/findme.txt", filesystem)
389 create(t, "/tmp/b/subdir/findme.txt", filesystem)
390
391 finder := newFinder(
392 t,
393 filesystem,
394 CacheParams{
395 RootDirs: []string{"/IDoNotExist"},
396 IncludeFiles: []string{"findme.txt"},
397 },
398 )
399
400 foundPaths := finder.FindNamedAt("/tmp/a", "findme.txt")
401 // If the caller queries for a file that is in the cache, then computing the
402 // correct answer won't be fast, and it would be easy for the caller to
403 // fail to notice its slowness. Instead, we only ever search the cache for files
404 // to return, which enforces that we can determine which files will be
405 // interesting upfront.
406 assertSameResponse(t, foundPaths, []string{})
407
408 finder.Shutdown()
409}
410
411func TestSearchingForFilesExcludedFromCache(t *testing.T) {
412 // setup filesystem
413 filesystem := newFs()
414 create(t, "/tmp/findme.txt", filesystem)
415 create(t, "/tmp/a/findme.txt", filesystem)
416 create(t, "/tmp/a/misc.txt", filesystem)
417
418 // set up the finder and run it
419 finder := newFinder(
420 t,
421 filesystem,
422 CacheParams{
423 RootDirs: []string{"/tmp"},
424 IncludeFiles: []string{"findme.txt"},
425 },
426 )
427 foundPaths := finder.FindNamedAt("/tmp", "misc.txt")
428 // If the caller queries for a file that is in the cache, then computing the
429 // correct answer won't be fast, and it would be easy for the caller to
430 // fail to notice its slowness. Instead, we only ever search the cache for files
431 // to return, which enforces that we can determine which files will be
432 // interesting upfront.
433 assertSameResponse(t, foundPaths, []string{})
434
435 finder.Shutdown()
436}
437
438func TestRelativeFilePaths(t *testing.T) {
439 filesystem := newFs()
440
441 create(t, "/tmp/ignore/hi.txt", filesystem)
442 create(t, "/tmp/include/hi.txt", filesystem)
443 create(t, "/cwd/hi.txt", filesystem)
444 create(t, "/cwd/a/hi.txt", filesystem)
445 create(t, "/cwd/a/a/hi.txt", filesystem)
446
447 finder := newFinder(
448 t,
449 filesystem,
450 CacheParams{
451 RootDirs: []string{"/cwd", "/tmp/include"},
452 IncludeFiles: []string{"hi.txt"},
453 },
454 )
455 defer finder.Shutdown()
456
457 foundPaths := finder.FindNamedAt("a", "hi.txt")
458 assertSameResponse(t, foundPaths,
459 []string{"a/hi.txt",
460 "a/a/hi.txt"})
461
462 foundPaths = finder.FindNamedAt("/tmp/include", "hi.txt")
463 assertSameResponse(t, foundPaths, []string{"/tmp/include/hi.txt"})
464
465 foundPaths = finder.FindNamedAt(".", "hi.txt")
466 assertSameResponse(t, foundPaths,
467 []string{"hi.txt",
468 "a/hi.txt",
469 "a/a/hi.txt"})
470
471 foundPaths = finder.FindNamedAt("/tmp/include", "hi.txt")
472 assertSameResponse(t, foundPaths, []string{"/tmp/include/hi.txt"})
473}
474
475// have to run this test with the race-detector (`go test -race src/android/soong/finder/*.go`)
476// for there to be much chance of the test actually detecting any error that may be present
477func TestRootDirsContainedInOtherRootDirs(t *testing.T) {
478 filesystem := newFs()
479
480 create(t, "/tmp/a/b/c/d/e/f/g/h/i/j/findme.txt", filesystem)
481
482 finder := newFinder(
483 t,
484 filesystem,
485 CacheParams{
486 RootDirs: []string{"/", "/a/b/c", "/a/b/c/d/e/f", "/a/b/c/d/e/f/g/h/i"},
487 IncludeFiles: []string{"findme.txt"},
488 },
489 )
490 defer finder.Shutdown()
491
492 foundPaths := finder.FindNamedAt("/tmp/a", "findme.txt")
493
494 assertSameResponse(t, foundPaths,
495 []string{"/tmp/a/b/c/d/e/f/g/h/i/j/findme.txt"})
496}
497
498func TestFindFirst(t *testing.T) {
499 filesystem := newFs()
500 create(t, "/tmp/a/hi.txt", filesystem)
501 create(t, "/tmp/b/hi.txt", filesystem)
502 create(t, "/tmp/b/a/hi.txt", filesystem)
503
504 finder := newFinder(
505 t,
506 filesystem,
507 CacheParams{
508 RootDirs: []string{"/tmp"},
509 IncludeFiles: []string{"hi.txt"},
510 },
511 )
512 defer finder.Shutdown()
513
514 foundPaths := finder.FindFirstNamed("hi.txt")
515
516 assertSameResponse(t, foundPaths,
517 []string{"/tmp/a/hi.txt",
518 "/tmp/b/hi.txt"},
519 )
520}
521
522func TestConcurrentFindSameDirectory(t *testing.T) {
523 filesystem := newFs()
524
525 // create a bunch of files and directories
526 paths := []string{}
527 for i := 0; i < 10; i++ {
528 parentDir := fmt.Sprintf("/tmp/%v", i)
529 for j := 0; j < 10; j++ {
530 filePath := filepath.Join(parentDir, fmt.Sprintf("%v/findme.txt", j))
531 paths = append(paths, filePath)
532 }
533 }
534 sort.Strings(paths)
535 for _, path := range paths {
536 create(t, path, filesystem)
537 }
538
539 // set up a finder
540 finder := newFinder(
541 t,
542 filesystem,
543 CacheParams{
544 RootDirs: []string{"/tmp"},
545 IncludeFiles: []string{"findme.txt"},
546 },
547 )
548 defer finder.Shutdown()
549
550 numTests := 20
551 results := make(chan []string, numTests)
552 // make several parallel calls to the finder
553 for i := 0; i < numTests; i++ {
554 go func() {
555 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
556 results <- foundPaths
557 }()
558 }
559
560 // check that each response was correct
561 for i := 0; i < numTests; i++ {
562 foundPaths := <-results
563 assertSameResponse(t, foundPaths, paths)
564 }
565}
566
567func TestConcurrentFindDifferentDirectories(t *testing.T) {
568 filesystem := newFs()
569
570 // create a bunch of files and directories
571 allFiles := []string{}
572 numSubdirs := 10
573 rootPaths := []string{}
574 queryAnswers := [][]string{}
575 for i := 0; i < numSubdirs; i++ {
576 parentDir := fmt.Sprintf("/tmp/%v", i)
577 rootPaths = append(rootPaths, parentDir)
578 queryAnswers = append(queryAnswers, []string{})
579 for j := 0; j < 10; j++ {
580 filePath := filepath.Join(parentDir, fmt.Sprintf("%v/findme.txt", j))
581 queryAnswers[i] = append(queryAnswers[i], filePath)
582 allFiles = append(allFiles, filePath)
583 }
584 sort.Strings(queryAnswers[i])
585 }
586 sort.Strings(allFiles)
587 for _, path := range allFiles {
588 create(t, path, filesystem)
589 }
590
591 // set up a finder
592 finder := newFinder(
593 t,
594 filesystem,
595
596 CacheParams{
597 RootDirs: []string{"/tmp"},
598 IncludeFiles: []string{"findme.txt"},
599 },
600 )
601 defer finder.Shutdown()
602
603 type testRun struct {
604 path string
605 foundMatches []string
606 correctMatches []string
607 }
608
609 numTests := numSubdirs + 1
610 testRuns := make(chan testRun, numTests)
611
612 searchAt := func(path string, correctMatches []string) {
613 foundPaths := finder.FindNamedAt(path, "findme.txt")
614 testRuns <- testRun{path, foundPaths, correctMatches}
615 }
616
617 // make several parallel calls to the finder
618 go searchAt("/tmp", allFiles)
619 for i := 0; i < len(rootPaths); i++ {
620 go searchAt(rootPaths[i], queryAnswers[i])
621 }
622
623 // check that each response was correct
624 for i := 0; i < numTests; i++ {
625 testRun := <-testRuns
626 assertSameResponse(t, testRun.foundMatches, testRun.correctMatches)
627 }
628}
629
630func TestStrangelyFormattedPaths(t *testing.T) {
631 filesystem := newFs()
632
633 create(t, "/tmp/findme.txt", filesystem)
634 create(t, "/tmp/a/findme.txt", filesystem)
635 create(t, "/tmp/b/findme.txt", filesystem)
636
637 finder := newFinder(
638 t,
639 filesystem,
640 CacheParams{
641 RootDirs: []string{"//tmp//a//.."},
642 IncludeFiles: []string{"findme.txt"},
643 },
644 )
645 defer finder.Shutdown()
646
647 foundPaths := finder.FindNamedAt("//tmp//a//..", "findme.txt")
648
649 assertSameResponse(t, foundPaths,
650 []string{"/tmp/a/findme.txt",
651 "/tmp/b/findme.txt",
652 "/tmp/findme.txt"})
653}
654
655func TestCorruptedCacheHeader(t *testing.T) {
656 filesystem := newFs()
657
658 create(t, "/tmp/findme.txt", filesystem)
659 create(t, "/tmp/a/findme.txt", filesystem)
660 write(t, "/finder/finder-db", "sample header", filesystem)
661
662 finder := newFinder(
663 t,
664 filesystem,
665 CacheParams{
666 RootDirs: []string{"/tmp"},
667 IncludeFiles: []string{"findme.txt"},
668 },
669 )
670 defer finder.Shutdown()
671
672 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
673
674 assertSameResponse(t, foundPaths,
675 []string{"/tmp/a/findme.txt",
676 "/tmp/findme.txt"})
677}
678
679func TestCanUseCache(t *testing.T) {
680 // setup filesystem
681 filesystem := newFs()
682 create(t, "/tmp/findme.txt", filesystem)
683 create(t, "/tmp/a/findme.txt", filesystem)
684
685 // run the first finder
686 finder := newFinder(
687 t,
688 filesystem,
689 CacheParams{
690 RootDirs: []string{"/tmp"},
691 IncludeFiles: []string{"findme.txt"},
692 },
693 )
694 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
695 // check the response of the first finder
696 correctResponse := []string{"/tmp/a/findme.txt",
697 "/tmp/findme.txt"}
698 assertSameResponse(t, foundPaths, correctResponse)
699 finder.Shutdown()
700
701 // check results
702 cacheText := read(t, finder.DbPath, filesystem)
703 if len(cacheText) < 1 {
704 t.Fatalf("saved cache db is empty\n")
705 }
706 if len(filesystem.StatCalls) == 0 {
707 t.Fatal("No Stat calls recorded by mock filesystem")
708 }
709 if len(filesystem.ReadDirCalls) == 0 {
710 t.Fatal("No ReadDir calls recorded by filesystem")
711 }
712 statCalls := filesystem.StatCalls
713 filesystem.ClearMetrics()
714
715 // run the second finder
716 finder2 := finderWithSameParams(t, finder)
717 foundPaths = finder2.FindNamedAt("/tmp", "findme.txt")
718 // check results
719 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{})
720 assertSameReadDirCalls(t, filesystem.StatCalls, statCalls)
721
722 finder2.Shutdown()
723}
724
725func TestCorruptedCacheBody(t *testing.T) {
726 // setup filesystem
727 filesystem := newFs()
728 create(t, "/tmp/findme.txt", filesystem)
729 create(t, "/tmp/a/findme.txt", filesystem)
730
731 // run the first finder
732 finder := newFinder(
733 t,
734 filesystem,
735 CacheParams{
736 RootDirs: []string{"/tmp"},
737 IncludeFiles: []string{"findme.txt"},
738 },
739 )
740 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
741 finder.Shutdown()
742
743 // check the response of the first finder
744 correctResponse := []string{"/tmp/a/findme.txt",
745 "/tmp/findme.txt"}
746 assertSameResponse(t, foundPaths, correctResponse)
747 numStatCalls := len(filesystem.StatCalls)
748 numReadDirCalls := len(filesystem.ReadDirCalls)
749
750 // load the cache file, corrupt it, and save it
751 cacheReader, err := filesystem.Open(finder.DbPath)
752 if err != nil {
753 t.Fatal(err)
754 }
755 cacheData, err := ioutil.ReadAll(cacheReader)
756 if err != nil {
757 t.Fatal(err)
758 }
759 cacheData = append(cacheData, []byte("DontMindMe")...)
760 filesystem.WriteFile(finder.DbPath, cacheData, 0777)
761 filesystem.ClearMetrics()
762
763 // run the second finder
764 finder2 := finderWithSameParams(t, finder)
765 foundPaths = finder2.FindNamedAt("/tmp", "findme.txt")
766 // check results
767 assertSameResponse(t, foundPaths, correctResponse)
768 numNewStatCalls := len(filesystem.StatCalls)
769 numNewReadDirCalls := len(filesystem.ReadDirCalls)
770 // It's permissable to make more Stat calls with a corrupted cache because
771 // the Finder may restart once it detects corruption.
772 // However, it may have already issued many Stat calls.
773 // Because a corrupted db is not expected to be a common (or even a supported case),
774 // we don't care to optimize it and don't cache the already-issued Stat calls
775 if numNewReadDirCalls < numReadDirCalls {
776 t.Fatalf(
777 "Finder made fewer ReadDir calls with a corrupted cache (%v calls) than with no cache"+
778 " (%v calls)",
779 numNewReadDirCalls, numReadDirCalls)
780 }
781 if numNewStatCalls < numStatCalls {
782 t.Fatalf(
783 "Finder made fewer Stat calls with a corrupted cache (%v calls) than with no cache (%v calls)",
784 numNewStatCalls, numStatCalls)
785 }
786 finder2.Shutdown()
787}
788
789func TestStatCalls(t *testing.T) {
790 // setup filesystem
791 filesystem := newFs()
792 create(t, "/tmp/a/findme.txt", filesystem)
793
794 // run finder
795 finder := newFinder(
796 t,
797 filesystem,
798 CacheParams{
799 RootDirs: []string{"/tmp"},
800 IncludeFiles: []string{"findme.txt"},
801 },
802 )
803 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
804 finder.Shutdown()
805
806 // check response
807 assertSameResponse(t, foundPaths, []string{"/tmp/a/findme.txt"})
808 assertSameStatCalls(t, filesystem.StatCalls, []string{"/tmp", "/tmp/a"})
809 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{"/tmp", "/tmp/a"})
810}
811
812func TestFileAdded(t *testing.T) {
813 // setup filesystem
814 filesystem := newFs()
815 create(t, "/tmp/ignoreme.txt", filesystem)
816 create(t, "/tmp/a/findme.txt", filesystem)
817 create(t, "/tmp/b/ignore.txt", filesystem)
818 create(t, "/tmp/b/c/nope.txt", filesystem)
819 create(t, "/tmp/b/c/d/irrelevant.txt", filesystem)
820
821 // run the first finder
822 finder := newFinder(
823 t,
824 filesystem,
825 CacheParams{
826 RootDirs: []string{"/tmp"},
827 IncludeFiles: []string{"findme.txt"},
828 },
829 )
830 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
831 filesystem.Clock.Tick()
832 finder.Shutdown()
833 // check the response of the first finder
834 assertSameResponse(t, foundPaths, []string{"/tmp/a/findme.txt"})
835
836 // modify the filesystem
837 filesystem.Clock.Tick()
838 create(t, "/tmp/b/c/findme.txt", filesystem)
839 filesystem.Clock.Tick()
840 filesystem.ClearMetrics()
841
842 // run the second finder
843 finder2 := finderWithSameParams(t, finder)
844 foundPaths = finder2.FindNamedAt("/tmp", "findme.txt")
845
846 // check results
847 assertSameResponse(t, foundPaths, []string{"/tmp/a/findme.txt", "/tmp/b/c/findme.txt"})
848 assertSameStatCalls(t, filesystem.StatCalls, []string{"/tmp", "/tmp/a", "/tmp/b", "/tmp/b/c", "/tmp/b/c/d"})
849 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{"/tmp/b/c"})
850 finder2.Shutdown()
851
852}
853
854func TestDirectoriesAdded(t *testing.T) {
855 // setup filesystem
856 filesystem := newFs()
857 create(t, "/tmp/ignoreme.txt", filesystem)
858 create(t, "/tmp/a/findme.txt", filesystem)
859 create(t, "/tmp/b/ignore.txt", filesystem)
860 create(t, "/tmp/b/c/nope.txt", filesystem)
861 create(t, "/tmp/b/c/d/irrelevant.txt", filesystem)
862
863 // run the first finder
864 finder := newFinder(
865 t,
866 filesystem,
867 CacheParams{
868 RootDirs: []string{"/tmp"},
869 IncludeFiles: []string{"findme.txt"},
870 },
871 )
872 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
873 finder.Shutdown()
874 // check the response of the first finder
875 assertSameResponse(t, foundPaths, []string{"/tmp/a/findme.txt"})
876
877 // modify the filesystem
878 filesystem.Clock.Tick()
879 create(t, "/tmp/b/c/new/findme.txt", filesystem)
880 create(t, "/tmp/b/c/new/new2/findme.txt", filesystem)
881 create(t, "/tmp/b/c/new/new2/ignoreme.txt", filesystem)
882 filesystem.ClearMetrics()
883
884 // run the second finder
885 finder2 := finderWithSameParams(t, finder)
886 foundPaths = finder2.FindNamedAt("/tmp", "findme.txt")
887
888 // check results
889 assertSameResponse(t, foundPaths,
890 []string{"/tmp/a/findme.txt", "/tmp/b/c/new/findme.txt", "/tmp/b/c/new/new2/findme.txt"})
891 assertSameStatCalls(t, filesystem.StatCalls,
892 []string{"/tmp", "/tmp/a", "/tmp/b", "/tmp/b/c", "/tmp/b/c/d", "/tmp/b/c/new", "/tmp/b/c/new/new2"})
893 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{"/tmp/b/c", "/tmp/b/c/new", "/tmp/b/c/new/new2"})
894
895 finder2.Shutdown()
896}
897
898func TestDirectoryAndSubdirectoryBothUpdated(t *testing.T) {
899 // setup filesystem
900 filesystem := newFs()
901 create(t, "/tmp/hi1.txt", filesystem)
902 create(t, "/tmp/a/hi1.txt", filesystem)
903
904 // run the first finder
905 finder := newFinder(
906 t,
907 filesystem,
908 CacheParams{
909 RootDirs: []string{"/tmp"},
910 IncludeFiles: []string{"hi1.txt", "hi2.txt"},
911 },
912 )
913 foundPaths := finder.FindNamedAt("/tmp", "hi1.txt")
914 finder.Shutdown()
915 // check the response of the first finder
916 assertSameResponse(t, foundPaths, []string{"/tmp/hi1.txt", "/tmp/a/hi1.txt"})
917
918 // modify the filesystem
919 filesystem.Clock.Tick()
920 create(t, "/tmp/hi2.txt", filesystem)
921 create(t, "/tmp/a/hi2.txt", filesystem)
922 filesystem.ClearMetrics()
923
924 // run the second finder
925 finder2 := finderWithSameParams(t, finder)
926 foundPaths = finder2.FindAll()
927
928 // check results
929 assertSameResponse(t, foundPaths,
930 []string{"/tmp/hi1.txt", "/tmp/hi2.txt", "/tmp/a/hi1.txt", "/tmp/a/hi2.txt"})
931 assertSameStatCalls(t, filesystem.StatCalls,
932 []string{"/tmp", "/tmp/a"})
933 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{"/tmp", "/tmp/a"})
934
935 finder2.Shutdown()
936}
937
938func TestFileDeleted(t *testing.T) {
939 // setup filesystem
940 filesystem := newFs()
941 create(t, "/tmp/ignoreme.txt", filesystem)
942 create(t, "/tmp/a/findme.txt", filesystem)
943 create(t, "/tmp/b/findme.txt", filesystem)
944 create(t, "/tmp/b/c/nope.txt", filesystem)
945 create(t, "/tmp/b/c/d/irrelevant.txt", filesystem)
946
947 // run the first finder
948 finder := newFinder(
949 t,
950 filesystem,
951 CacheParams{
952 RootDirs: []string{"/tmp"},
953 IncludeFiles: []string{"findme.txt"},
954 },
955 )
956 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
957 finder.Shutdown()
958 // check the response of the first finder
959 assertSameResponse(t, foundPaths, []string{"/tmp/a/findme.txt", "/tmp/b/findme.txt"})
960
961 // modify the filesystem
962 filesystem.Clock.Tick()
963 delete(t, "/tmp/b/findme.txt", filesystem)
964 filesystem.ClearMetrics()
965
966 // run the second finder
967 finder2 := finderWithSameParams(t, finder)
968 foundPaths = finder2.FindNamedAt("/tmp", "findme.txt")
969
970 // check results
971 assertSameResponse(t, foundPaths, []string{"/tmp/a/findme.txt"})
972 assertSameStatCalls(t, filesystem.StatCalls, []string{"/tmp", "/tmp/a", "/tmp/b", "/tmp/b/c", "/tmp/b/c/d"})
973 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{"/tmp/b"})
974
975 finder2.Shutdown()
976}
977
978func TestDirectoriesDeleted(t *testing.T) {
979 // setup filesystem
980 filesystem := newFs()
981 create(t, "/tmp/findme.txt", filesystem)
982 create(t, "/tmp/a/findme.txt", filesystem)
983 create(t, "/tmp/a/1/findme.txt", filesystem)
984 create(t, "/tmp/a/1/2/findme.txt", filesystem)
985 create(t, "/tmp/b/findme.txt", filesystem)
986
987 // run the first finder
988 finder := newFinder(
989 t,
990 filesystem,
991 CacheParams{
992 RootDirs: []string{"/tmp"},
993 IncludeFiles: []string{"findme.txt"},
994 },
995 )
996 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
997 finder.Shutdown()
998 // check the response of the first finder
999 assertSameResponse(t, foundPaths,
1000 []string{"/tmp/findme.txt",
1001 "/tmp/a/findme.txt",
1002 "/tmp/a/1/findme.txt",
1003 "/tmp/a/1/2/findme.txt",
1004 "/tmp/b/findme.txt"})
1005
1006 // modify the filesystem
1007 filesystem.Clock.Tick()
1008 removeAll(t, "/tmp/a/1", filesystem)
1009 filesystem.ClearMetrics()
1010
1011 // run the second finder
1012 finder2 := finderWithSameParams(t, finder)
1013 foundPaths = finder2.FindNamedAt("/tmp", "findme.txt")
1014
1015 // check results
1016 assertSameResponse(t, foundPaths,
1017 []string{"/tmp/findme.txt", "/tmp/a/findme.txt", "/tmp/b/findme.txt"})
1018 // Technically, we don't care whether /tmp/a/1/2 gets Statted or gets skipped
1019 // if the Finder detects the nonexistence of /tmp/a/1
1020 // However, when resuming from cache, we don't want the Finder to necessarily wait
1021 // to stat a directory until after statting its parent.
1022 // So here we just include /tmp/a/1/2 in the list.
1023 // The Finder is currently implemented to always restat every dir and
1024 // to not short-circuit due to nonexistence of parents (but it will remove
1025 // missing dirs from the cache for next time)
1026 assertSameStatCalls(t, filesystem.StatCalls,
1027 []string{"/tmp", "/tmp/a", "/tmp/a/1", "/tmp/a/1/2", "/tmp/b"})
1028 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{"/tmp/a"})
1029
1030 finder2.Shutdown()
1031}
1032
1033func TestDirectoriesMoved(t *testing.T) {
1034 // setup filesystem
1035 filesystem := newFs()
1036 create(t, "/tmp/findme.txt", filesystem)
1037 create(t, "/tmp/a/findme.txt", filesystem)
1038 create(t, "/tmp/a/1/findme.txt", filesystem)
1039 create(t, "/tmp/a/1/2/findme.txt", filesystem)
1040 create(t, "/tmp/b/findme.txt", filesystem)
1041
1042 // run the first finder
1043 finder := newFinder(
1044 t,
1045 filesystem,
1046 CacheParams{
1047 RootDirs: []string{"/tmp"},
1048 IncludeFiles: []string{"findme.txt"},
1049 },
1050 )
1051 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
1052 finder.Shutdown()
1053 // check the response of the first finder
1054 assertSameResponse(t, foundPaths,
1055 []string{"/tmp/findme.txt",
1056 "/tmp/a/findme.txt",
1057 "/tmp/a/1/findme.txt",
1058 "/tmp/a/1/2/findme.txt",
1059 "/tmp/b/findme.txt"})
1060
1061 // modify the filesystem
1062 filesystem.Clock.Tick()
1063 move(t, "/tmp/a", "/tmp/c", filesystem)
1064 filesystem.ClearMetrics()
1065
1066 // run the second finder
1067 finder2 := finderWithSameParams(t, finder)
1068 foundPaths = finder2.FindNamedAt("/tmp", "findme.txt")
1069
1070 // check results
1071 assertSameResponse(t, foundPaths,
1072 []string{"/tmp/findme.txt",
1073 "/tmp/b/findme.txt",
1074 "/tmp/c/findme.txt",
1075 "/tmp/c/1/findme.txt",
1076 "/tmp/c/1/2/findme.txt"})
1077 // Technically, we don't care whether /tmp/a/1/2 gets Statted or gets skipped
1078 // if the Finder detects the nonexistence of /tmp/a/1
1079 // However, when resuming from cache, we don't want the Finder to necessarily wait
1080 // to stat a directory until after statting its parent.
1081 // So here we just include /tmp/a/1/2 in the list.
1082 // The Finder is currently implemented to always restat every dir and
1083 // to not short-circuit due to nonexistence of parents (but it will remove
1084 // missing dirs from the cache for next time)
1085 assertSameStatCalls(t, filesystem.StatCalls,
1086 []string{"/tmp", "/tmp/a", "/tmp/a/1", "/tmp/a/1/2", "/tmp/b", "/tmp/c", "/tmp/c/1", "/tmp/c/1/2"})
1087 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{"/tmp", "/tmp/c", "/tmp/c/1", "/tmp/c/1/2"})
1088 finder2.Shutdown()
1089}
1090
1091func TestDirectoriesSwapped(t *testing.T) {
1092 // setup filesystem
1093 filesystem := newFs()
1094 create(t, "/tmp/findme.txt", filesystem)
1095 create(t, "/tmp/a/findme.txt", filesystem)
1096 create(t, "/tmp/a/1/findme.txt", filesystem)
1097 create(t, "/tmp/a/1/2/findme.txt", filesystem)
1098 create(t, "/tmp/b/findme.txt", filesystem)
1099
1100 // run the first finder
1101 finder := newFinder(
1102 t,
1103 filesystem,
1104 CacheParams{
1105 RootDirs: []string{"/tmp"},
1106 IncludeFiles: []string{"findme.txt"},
1107 },
1108 )
1109 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
1110 finder.Shutdown()
1111 // check the response of the first finder
1112 assertSameResponse(t, foundPaths,
1113 []string{"/tmp/findme.txt",
1114 "/tmp/a/findme.txt",
1115 "/tmp/a/1/findme.txt",
1116 "/tmp/a/1/2/findme.txt",
1117 "/tmp/b/findme.txt"})
1118
1119 // modify the filesystem
1120 filesystem.Clock.Tick()
1121 move(t, "/tmp/a", "/tmp/temp", filesystem)
1122 move(t, "/tmp/b", "/tmp/a", filesystem)
1123 move(t, "/tmp/temp", "/tmp/b", filesystem)
1124 filesystem.ClearMetrics()
1125
1126 // run the second finder
1127 finder2 := finderWithSameParams(t, finder)
1128 foundPaths = finder2.FindNamedAt("/tmp", "findme.txt")
1129
1130 // check results
1131 assertSameResponse(t, foundPaths,
1132 []string{"/tmp/findme.txt",
1133 "/tmp/a/findme.txt",
1134 "/tmp/b/findme.txt",
1135 "/tmp/b/1/findme.txt",
1136 "/tmp/b/1/2/findme.txt"})
1137 // Technically, we don't care whether /tmp/a/1/2 gets Statted or gets skipped
1138 // if the Finder detects the nonexistence of /tmp/a/1
1139 // However, when resuming from cache, we don't want the Finder to necessarily wait
1140 // to stat a directory until after statting its parent.
1141 // So here we just include /tmp/a/1/2 in the list.
1142 // The Finder is currently implemented to always restat every dir and
1143 // to not short-circuit due to nonexistence of parents (but it will remove
1144 // missing dirs from the cache for next time)
1145 assertSameStatCalls(t, filesystem.StatCalls,
1146 []string{"/tmp", "/tmp/a", "/tmp/a/1", "/tmp/a/1/2", "/tmp/b", "/tmp/b/1", "/tmp/b/1/2"})
1147 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{"/tmp", "/tmp/a", "/tmp/b", "/tmp/b/1", "/tmp/b/1/2"})
1148 finder2.Shutdown()
1149}
1150
1151// runFsReplacementTest tests a change modifying properties of the filesystem itself:
1152// runFsReplacementTest tests changing the user, the hostname, or the device number
1153// runFsReplacementTest is a helper method called by other tests
1154func runFsReplacementTest(t *testing.T, fs1 *fs.MockFs, fs2 *fs.MockFs) {
1155 // setup fs1
1156 create(t, "/tmp/findme.txt", fs1)
1157 create(t, "/tmp/a/findme.txt", fs1)
1158 create(t, "/tmp/a/a/findme.txt", fs1)
1159
1160 // setup fs2 to have the same directories but different files
1161 create(t, "/tmp/findme.txt", fs2)
1162 create(t, "/tmp/a/findme.txt", fs2)
1163 create(t, "/tmp/a/a/ignoreme.txt", fs2)
1164 create(t, "/tmp/a/b/findme.txt", fs2)
1165
1166 // run the first finder
1167 finder := newFinder(
1168 t,
1169 fs1,
1170 CacheParams{
1171 RootDirs: []string{"/tmp"},
1172 IncludeFiles: []string{"findme.txt"},
1173 },
1174 )
1175 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
1176 finder.Shutdown()
1177 // check the response of the first finder
1178 assertSameResponse(t, foundPaths,
1179 []string{"/tmp/findme.txt", "/tmp/a/findme.txt", "/tmp/a/a/findme.txt"})
1180
1181 // copy the cache data from the first filesystem to the second
1182 cacheContent := read(t, finder.DbPath, fs1)
1183 write(t, finder.DbPath, cacheContent, fs2)
1184
1185 // run the second finder, with the same config and same cache contents but a different filesystem
1186 finder2 := newFinder(
1187 t,
1188 fs2,
1189 CacheParams{
1190 RootDirs: []string{"/tmp"},
1191 IncludeFiles: []string{"findme.txt"},
1192 },
1193 )
1194 foundPaths = finder2.FindNamedAt("/tmp", "findme.txt")
1195
1196 // check results
1197 assertSameResponse(t, foundPaths,
1198 []string{"/tmp/findme.txt", "/tmp/a/findme.txt", "/tmp/a/b/findme.txt"})
1199 assertSameStatCalls(t, fs2.StatCalls,
1200 []string{"/tmp", "/tmp/a", "/tmp/a/a", "/tmp/a/b"})
1201 assertSameReadDirCalls(t, fs2.ReadDirCalls,
1202 []string{"/tmp", "/tmp/a", "/tmp/a/a", "/tmp/a/b"})
1203 finder2.Shutdown()
1204}
1205
1206func TestChangeOfDevice(t *testing.T) {
1207 fs1 := newFs()
1208 // not as fine-grained mounting controls as a real filesystem, but should be adequate
1209 fs1.SetDeviceNumber(0)
1210
1211 fs2 := newFs()
1212 fs2.SetDeviceNumber(1)
1213
1214 runFsReplacementTest(t, fs1, fs2)
1215}
1216
1217func TestChangeOfUserOrHost(t *testing.T) {
1218 fs1 := newFs()
1219 fs1.SetViewId("me@here")
1220
1221 fs2 := newFs()
1222 fs2.SetViewId("you@there")
1223
1224 runFsReplacementTest(t, fs1, fs2)
1225}
1226
1227func TestConsistentCacheOrdering(t *testing.T) {
1228 // setup filesystem
1229 filesystem := newFs()
1230 for i := 0; i < 5; i++ {
1231 create(t, fmt.Sprintf("/tmp/%v/findme.txt", i), filesystem)
1232 }
1233
1234 // run the first finder
1235 finder := newFinder(
1236 t,
1237 filesystem,
1238 CacheParams{
1239 RootDirs: []string{"/tmp"},
1240 IncludeFiles: []string{"findme.txt"},
1241 },
1242 )
1243 finder.FindNamedAt("/tmp", "findme.txt")
1244 finder.Shutdown()
1245
1246 // read db file
1247 string1 := read(t, finder.DbPath, filesystem)
1248
1249 err := filesystem.Remove(finder.DbPath)
1250 if err != nil {
1251 t.Fatal(err)
1252 }
1253
1254 // run another finder
1255 finder2 := finderWithSameParams(t, finder)
1256 finder2.FindNamedAt("/tmp", "findme.txt")
1257 finder2.Shutdown()
1258
1259 string2 := read(t, finder.DbPath, filesystem)
1260
1261 if string1 != string2 {
1262 t.Errorf("Running Finder twice generated two dbs not having identical contents.\n"+
1263 "Content of first file:\n"+
1264 "\n"+
1265 "%v"+
1266 "\n"+
1267 "\n"+
1268 "Content of second file:\n"+
1269 "\n"+
1270 "%v\n"+
1271 "\n",
1272 string1,
1273 string2,
1274 )
1275 }
1276
1277}
1278
1279func TestNumSyscallsOfSecondFind(t *testing.T) {
1280 // setup filesystem
1281 filesystem := newFs()
1282 create(t, "/tmp/findme.txt", filesystem)
1283 create(t, "/tmp/a/findme.txt", filesystem)
1284 create(t, "/tmp/a/misc.txt", filesystem)
1285
1286 // set up the finder and run it once
1287 finder := newFinder(
1288 t,
1289 filesystem,
1290 CacheParams{
1291 RootDirs: []string{"/tmp"},
1292 IncludeFiles: []string{"findme.txt"},
1293 },
1294 )
1295 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
1296 assertSameResponse(t, foundPaths, []string{"/tmp/findme.txt", "/tmp/a/findme.txt"})
1297
1298 filesystem.ClearMetrics()
1299
1300 // run the finder again and confirm it doesn't check the filesystem
1301 refoundPaths := finder.FindNamedAt("/tmp", "findme.txt")
1302 assertSameResponse(t, refoundPaths, foundPaths)
1303 assertSameStatCalls(t, filesystem.StatCalls, []string{})
1304 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{})
1305
1306 finder.Shutdown()
1307}
1308
1309func TestChangingParamsOfSecondFind(t *testing.T) {
1310 // setup filesystem
1311 filesystem := newFs()
1312 create(t, "/tmp/findme.txt", filesystem)
1313 create(t, "/tmp/a/findme.txt", filesystem)
1314 create(t, "/tmp/a/metoo.txt", filesystem)
1315
1316 // set up the finder and run it once
1317 finder := newFinder(
1318 t,
1319 filesystem,
1320 CacheParams{
1321 RootDirs: []string{"/tmp"},
1322 IncludeFiles: []string{"findme.txt", "metoo.txt"},
1323 },
1324 )
1325 foundPaths := finder.FindNamedAt("/tmp", "findme.txt")
1326 assertSameResponse(t, foundPaths, []string{"/tmp/findme.txt", "/tmp/a/findme.txt"})
1327
1328 filesystem.ClearMetrics()
1329
1330 // run the finder again and confirm it gets the right answer without asking the filesystem
1331 refoundPaths := finder.FindNamedAt("/tmp", "metoo.txt")
1332 assertSameResponse(t, refoundPaths, []string{"/tmp/a/metoo.txt"})
1333 assertSameStatCalls(t, filesystem.StatCalls, []string{})
1334 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{})
1335
1336 finder.Shutdown()
1337}
1338
1339func TestSymlinkPointingToFile(t *testing.T) {
1340 // setup filesystem
1341 filesystem := newFs()
1342 create(t, "/tmp/a/hi.txt", filesystem)
1343 create(t, "/tmp/a/ignoreme.txt", filesystem)
1344 link(t, "/tmp/hi.txt", "a/hi.txt", filesystem)
1345 link(t, "/tmp/b/hi.txt", "../a/hi.txt", filesystem)
1346 link(t, "/tmp/c/hi.txt", "/tmp/hi.txt", filesystem)
1347 link(t, "/tmp/d/hi.txt", "../a/bye.txt", filesystem)
1348 link(t, "/tmp/d/bye.txt", "../a/hi.txt", filesystem)
1349 link(t, "/tmp/e/bye.txt", "../a/bye.txt", filesystem)
1350 link(t, "/tmp/f/hi.txt", "somethingThatDoesntExist", filesystem)
1351
1352 // set up the finder and run it once
1353 finder := newFinder(
1354 t,
1355 filesystem,
1356 CacheParams{
1357 RootDirs: []string{"/tmp"},
1358 IncludeFiles: []string{"hi.txt"},
1359 },
1360 )
1361 foundPaths := finder.FindNamedAt("/tmp", "hi.txt")
1362 // should search based on the name of the link rather than the destination or validity of the link
1363 correctResponse := []string{
1364 "/tmp/a/hi.txt",
1365 "/tmp/hi.txt",
1366 "/tmp/b/hi.txt",
1367 "/tmp/c/hi.txt",
1368 "/tmp/d/hi.txt",
1369 "/tmp/f/hi.txt",
1370 }
1371 assertSameResponse(t, foundPaths, correctResponse)
1372
1373}
1374
1375func TestSymlinkPointingToDirectory(t *testing.T) {
1376 // setup filesystem
1377 filesystem := newFs()
1378 create(t, "/tmp/dir/hi.txt", filesystem)
1379 create(t, "/tmp/dir/ignoreme.txt", filesystem)
1380
1381 link(t, "/tmp/links/dir", "../dir", filesystem)
1382 link(t, "/tmp/links/link", "../dir", filesystem)
1383 link(t, "/tmp/links/broken", "nothingHere", filesystem)
1384 link(t, "/tmp/links/recursive", "recursive", filesystem)
1385
1386 // set up the finder and run it once
1387 finder := newFinder(
1388 t,
1389 filesystem,
1390 CacheParams{
1391 RootDirs: []string{"/tmp"},
1392 IncludeFiles: []string{"hi.txt"},
1393 },
1394 )
1395
1396 foundPaths := finder.FindNamedAt("/tmp", "hi.txt")
1397
1398 // should completely ignore symlinks that point to directories
1399 correctResponse := []string{
1400 "/tmp/dir/hi.txt",
1401 }
1402 assertSameResponse(t, foundPaths, correctResponse)
1403
1404}
1405
1406// TestAddPruneFile confirms that adding a prune-file (into a directory for which we
1407// already had a cache) causes the directory to be ignored
1408func TestAddPruneFile(t *testing.T) {
1409 // setup filesystem
1410 filesystem := newFs()
1411 create(t, "/tmp/out/hi.txt", filesystem)
1412 create(t, "/tmp/out/a/hi.txt", filesystem)
1413 create(t, "/tmp/hi.txt", filesystem)
1414
1415 // do find
1416 finder := newFinder(
1417 t,
1418 filesystem,
1419 CacheParams{
1420 RootDirs: []string{"/tmp"},
1421 PruneFiles: []string{".ignore-out-dir"},
1422 IncludeFiles: []string{"hi.txt"},
1423 },
1424 )
1425
1426 foundPaths := finder.FindNamedAt("/tmp", "hi.txt")
1427
1428 // check result
1429 assertSameResponse(t, foundPaths,
1430 []string{"/tmp/hi.txt",
1431 "/tmp/out/hi.txt",
1432 "/tmp/out/a/hi.txt"},
1433 )
1434 finder.Shutdown()
1435
1436 // modify filesystem
1437 filesystem.Clock.Tick()
1438 create(t, "/tmp/out/.ignore-out-dir", filesystem)
1439 // run another find and check its result
1440 finder2 := finderWithSameParams(t, finder)
1441 foundPaths = finder2.FindNamedAt("/tmp", "hi.txt")
1442 assertSameResponse(t, foundPaths, []string{"/tmp/hi.txt"})
1443 finder2.Shutdown()
1444}
1445
1446func TestUpdatingDbIffChanged(t *testing.T) {
1447 // setup filesystem
1448 filesystem := newFs()
1449 create(t, "/tmp/a/hi.txt", filesystem)
1450 create(t, "/tmp/b/bye.txt", filesystem)
1451
1452 // run the first finder
1453 finder := newFinder(
1454 t,
1455 filesystem,
1456 CacheParams{
1457 RootDirs: []string{"/tmp"},
1458 IncludeFiles: []string{"hi.txt"},
1459 },
1460 )
1461 foundPaths := finder.FindAll()
1462 filesystem.Clock.Tick()
1463 finder.Shutdown()
1464 // check results
1465 assertSameResponse(t, foundPaths, []string{"/tmp/a/hi.txt"})
1466
1467 // modify the filesystem
1468 filesystem.Clock.Tick()
1469 create(t, "/tmp/b/hi.txt", filesystem)
1470 filesystem.Clock.Tick()
1471 filesystem.ClearMetrics()
1472
1473 // run the second finder
1474 finder2 := finderWithSameParams(t, finder)
1475 foundPaths = finder2.FindAll()
1476 finder2.Shutdown()
1477 // check results
1478 assertSameResponse(t, foundPaths, []string{"/tmp/a/hi.txt", "/tmp/b/hi.txt"})
1479 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{"/tmp/b"})
1480 expectedDbWriteTime := filesystem.Clock.Time()
1481 actualDbWriteTime := modTime(t, finder2.DbPath, filesystem)
1482 if actualDbWriteTime != expectedDbWriteTime {
1483 t.Fatalf("Expected to write db at %v, actually wrote db at %v\n",
1484 expectedDbWriteTime, actualDbWriteTime)
1485 }
1486
1487 // reset metrics
1488 filesystem.ClearMetrics()
1489
1490 // run the third finder
1491 finder3 := finderWithSameParams(t, finder2)
1492 foundPaths = finder3.FindAll()
1493
1494 // check results
1495 assertSameResponse(t, foundPaths, []string{"/tmp/a/hi.txt", "/tmp/b/hi.txt"})
1496 assertSameReadDirCalls(t, filesystem.ReadDirCalls, []string{})
1497 finder3.Shutdown()
1498 actualDbWriteTime = modTime(t, finder3.DbPath, filesystem)
1499 if actualDbWriteTime != expectedDbWriteTime {
1500 t.Fatalf("Re-wrote db even when contents did not change")
1501 }
1502
1503}
1504
1505func TestDirectoryNotPermitted(t *testing.T) {
1506 // setup filesystem
1507 filesystem := newFs()
1508 create(t, "/tmp/hi.txt", filesystem)
1509 create(t, "/tmp/a/hi.txt", filesystem)
1510 create(t, "/tmp/a/a/hi.txt", filesystem)
1511 create(t, "/tmp/b/hi.txt", filesystem)
1512
1513 // run the first finder
1514 finder := newFinder(
1515 t,
1516 filesystem,
1517 CacheParams{
1518 RootDirs: []string{"/tmp"},
1519 IncludeFiles: []string{"hi.txt"},
1520 },
1521 )
1522 foundPaths := finder.FindAll()
1523 filesystem.Clock.Tick()
1524 finder.Shutdown()
1525 allPaths := []string{"/tmp/hi.txt", "/tmp/a/hi.txt", "/tmp/a/a/hi.txt", "/tmp/b/hi.txt"}
1526 // check results
1527 assertSameResponse(t, foundPaths, allPaths)
1528
1529 // modify the filesystem
1530 filesystem.Clock.Tick()
1531
1532 setReadable(t, "/tmp/a", false, filesystem)
1533 filesystem.Clock.Tick()
1534
1535 // run the second finder
1536 finder2 := finderWithSameParams(t, finder)
1537 foundPaths = finder2.FindAll()
1538 finder2.Shutdown()
1539 // check results
1540 assertSameResponse(t, foundPaths, []string{"/tmp/hi.txt", "/tmp/b/hi.txt"})
1541
1542 // modify the filesystem back
1543 setReadable(t, "/tmp/a", true, filesystem)
1544
1545 // run the third finder
1546 finder3 := finderWithSameParams(t, finder2)
1547 foundPaths = finder3.FindAll()
1548 finder3.Shutdown()
1549 // check results
1550 assertSameResponse(t, foundPaths, allPaths)
1551}
1552
1553func TestFileNotPermitted(t *testing.T) {
1554 // setup filesystem
1555 filesystem := newFs()
1556 create(t, "/tmp/hi.txt", filesystem)
1557 setReadable(t, "/tmp/hi.txt", false, filesystem)
1558
1559 // run the first finder
1560 finder := newFinder(
1561 t,
1562 filesystem,
1563 CacheParams{
1564 RootDirs: []string{"/tmp"},
1565 IncludeFiles: []string{"hi.txt"},
1566 },
1567 )
1568 foundPaths := finder.FindAll()
1569 filesystem.Clock.Tick()
1570 finder.Shutdown()
1571 // check results
1572 assertSameResponse(t, foundPaths, []string{"/tmp/hi.txt"})
1573}