-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathentry_filter_test.go
151 lines (138 loc) · 3.39 KB
/
entry_filter_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
package fastwalk_test
import (
"fmt"
"io/fs"
"math/rand"
"os"
"path/filepath"
"runtime"
"sync"
"testing"
"time"
"github.com/charlievieth/fastwalk"
)
func TestEntryFilter(t *testing.T) {
tempdir := t.TempDir()
files := map[string]string{
"foo/foo.go": "one",
"bar/bar.go": "LINK:../foo/foo.go",
"bar/baz.go": "two",
"bar/loop": "LINK:../bar/", // symlink loop
"file.go": "three",
// Use multiple symdirs to increase the chance that one
// of these and not "foo" is followed first.
"symdir1": "LINK:foo",
"symdir2": "LINK:foo",
"symdir3": "LINK:foo",
"symdir4": "LINK:foo",
}
testCreateFiles(t, tempdir, files)
var mu sync.Mutex
var seen []os.FileInfo
filter := fastwalk.NewEntryFilter()
walkFn := fastwalk.IgnoreDuplicateFiles(func(path string, de fs.DirEntry, err error) error {
requireNoError(t, err)
fi1, err := fastwalk.StatDirEntry(path, de)
if err != nil {
t.Error(err)
return err
}
mu.Lock()
defer mu.Unlock()
if !filter.Entry(path, de) {
for _, fi2 := range seen {
if os.SameFile(fi1, fi2) {
t.Errorf("Visited file twice: %q (%s) and %q (%s)",
path, fi1.Mode(), fi2.Name(), fi2.Mode())
}
}
}
seen = append(seen, fi1)
return nil
})
if err := fastwalk.Walk(nil, tempdir, walkFn); err != nil {
t.Fatal(err)
}
// Test that true is returned for a non-existent directory
// On Windows the Info field of the returned DirEntry
// is already populated so this will succeed.
if runtime.GOOS != "windows" {
path := filepath.Join(tempdir, "src", "foo/foo.go")
fi, err := os.Lstat(path)
if err != nil {
t.Fatal(err)
}
if err := os.Remove(path); err != nil {
t.Fatal(err)
}
if !filter.Entry(path, fs.FileInfoToDirEntry(fi)) {
t.Error("EntryFilter should return true when the file does not exist")
}
}
}
func BenchmarkEntryFilter(b *testing.B) {
tempdir := b.TempDir()
names := make([]string, 0, 2048)
for i := 0; i < 1024; i++ {
name := filepath.Join(tempdir, fmt.Sprintf("dir_%04d", i))
if err := os.Mkdir(name, 0755); err != nil {
b.Fatal(err)
}
names = append(names, name)
}
for i := 0; i < 1024; i++ {
name := filepath.Join(tempdir, fmt.Sprintf("file_%04d", i))
if err := writeFile(name, filepath.Base(name), 0644); err != nil {
b.Fatal(err)
}
names = append(names, name)
}
rr := rand.New(rand.NewSource(time.Now().UnixNano()))
rr.Shuffle(len(names), func(i, j int) {
names[i], names[j] = names[j], names[i]
})
type fileInfo struct {
Name string
Info fs.DirEntry
}
infos := make([]fileInfo, len(names))
for i, name := range names {
fi, err := os.Lstat(name)
if err != nil {
b.Fatal(err)
}
infos[i] = fileInfo{name, fs.FileInfoToDirEntry(fi)}
}
b.ResetTimer()
b.Run("MostlyHits", func(b *testing.B) {
filter := fastwalk.NewEntryFilter()
for i := 0; i < b.N; i++ {
x := infos[i%len(infos)]
filter.Entry(x.Name, x.Info)
}
})
b.Run("MostlyHitsParallel", func(b *testing.B) {
filter := fastwalk.NewEntryFilter()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
x := infos[i%len(infos)]
filter.Entry(x.Name, x.Info)
i++
}
})
})
b.Run("HalfMisses", func(b *testing.B) {
filter := fastwalk.NewEntryFilter()
n := len(infos)
for i := 0; i < b.N; i++ {
x := infos[i%len(infos)]
filter.Entry(x.Name, x.Info)
if i != 0 && i%n == 0 {
b.StopTimer()
filter = fastwalk.NewEntryFilter()
b.StartTimer()
}
}
})
}