-
Notifications
You must be signed in to change notification settings - Fork 1
/
walk_test.go
329 lines (301 loc) · 7.29 KB
/
walk_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package nogo
import (
"errors"
"fmt"
"io/fs"
"path/filepath"
"regexp"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
)
func TestNoGo_WalkFunc(t *testing.T) {
type fields struct {
groups []group
}
type args struct {
fsys fs.FS
ignoreFileName string
path string
isDir bool
err error
}
tests := []struct {
name string
fields fields
args args
want bool
wantErr assert.ErrorAssertionFunc
}{
{
name: "not ignored file",
fields: fields{
groups: TestFSGroups,
},
args: args{
fsys: NewTestFS(),
ignoreFileName: ".gitignore",
path: "aFile",
},
want: true,
wantErr: assert.NoError,
},
{
name: "error is set",
args: args{
fsys: NewTestFS(),
ignoreFileName: ".gitignore",
path: "aFile",
err: errors.New("an error"),
},
want: false,
wantErr: assert.Error,
},
{
name: "ignored folder",
fields: fields{
groups: TestFSGroups,
},
args: args{
fsys: NewTestFS(),
ignoreFileName: ".gitignore",
path: "ignoredFolder",
isDir: true,
},
want: false,
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.ErrorIs(t, err, fs.SkipDir, i...)
},
},
{
name: "ignore file should be read if folder gets loaded",
fields: fields{
groups: nil,
},
args: args{
fsys: NewTestFS(),
ignoreFileName: ".gitignore",
path: "",
isDir: true,
},
want: true,
wantErr: assert.NoError,
},
{
name: "ignore file should be ignored if it is already ignored by a previous ignore file",
fields: fields{
groups: []group{
{
prefix: "",
rules: []Rule{
{
Regexp: []*regexp.Regexp{regexp.MustCompile(`\.gitignore`)},
},
},
},
},
},
args: args{
fsys: NewTestFS(),
ignoreFileName: ".gitignore",
path: "",
isDir: true,
},
// But still return ok as the folder itself is not ignored.
want: true,
wantErr: assert.NoError,
},
{
name: "ignore file should be ignored if it is already ignored by a previous ignore file",
fields: fields{
groups: []group{
{
prefix: "",
rules: []Rule{
{
Regexp: []*regexp.Regexp{regexp.MustCompile(`\.gitignore`)},
},
},
},
},
},
args: args{
fsys: NewTestFS(),
ignoreFileName: ".gitignore",
path: "",
isDir: true,
},
// But still return ok as the folder itself is not ignored.
want: true,
wantErr: assert.NoError,
},
{
name: "ignore file which doesn't exist should be ignored",
fields: fields{
groups: []group{
{
prefix: "",
rules: []Rule{
{
Regexp: []*regexp.Regexp{regexp.MustCompile(`\.gitignore`)},
},
},
},
},
},
args: args{
fsys: NewTestFS(),
ignoreFileName: "noIgnoreFile",
path: "",
isDir: true,
},
// But still return ok as the folder itself is not ignored.
want: true,
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := &NoGo{
groups: tt.fields.groups,
}
assert.NoError(t, n.AddFromFS(tt.args.fsys, tt.args.ignoreFileName))
got, err := n.WalkFunc(tt.args.fsys, tt.args.path, tt.args.isDir, tt.args.err)
if !tt.wantErr(t, err, fmt.Sprintf("WalkFunc(%v, %v, %v, %v, %v)", tt.args.fsys, tt.args.ignoreFileName, tt.args.path, tt.args.isDir, tt.args.err)) {
return
}
assert.Equalf(t, tt.want, got, "WalkFunc(%v, %v, %v, %v, %v)", tt.args.fsys, tt.args.ignoreFileName, tt.args.path, tt.args.isDir, tt.args.err)
})
}
}
var ErrShouldNotBeReached = errors.New("file should not be reached")
// ForbiddenFS is a fstest.MapFS but allows to define
// files which should not be loaded.
// Note: Define folders explicitely in the map if you want to forbid them.
type ForbiddenFS struct {
fstest.MapFS
NotExpected map[string]struct{}
}
func (ofs ForbiddenFS) Open(name string) (fs.File, error) {
file, err := ofs.MapFS.Open(name)
if err != nil {
return nil, err
}
if _, found := ofs.NotExpected[name]; found {
return nil, ErrShouldNotBeReached
}
return ForbiddenFile{
File: file,
notExpected: ofs.NotExpected,
folder: name,
}, nil
}
func (ofs ForbiddenFS) ReadDir(name string) ([]fs.DirEntry, error) {
entries, err := ofs.MapFS.ReadDir(name)
if err != nil {
return nil, err
}
if _, found := ofs.NotExpected[name]; found {
return nil, ErrShouldNotBeReached
}
for i := range entries {
entries[i] = ForbiddenDirEntry{
DirEntry: entries[i],
notExpected: ofs.NotExpected,
folder: name,
}
}
return entries, nil
}
func (ofs ForbiddenFS) Stat(name string) (fs.FileInfo, error) {
fileInfo, err := ofs.MapFS.Stat(name)
if err != nil {
return nil, err
}
if _, found := ofs.NotExpected[name]; found {
return nil, ErrShouldNotBeReached
}
return fileInfo, nil
}
type ForbiddenFile struct {
fs.File
notExpected map[string]struct{}
folder string
}
func (ofs ForbiddenFile) Stat() (fs.FileInfo, error) {
fileInfo, err := ofs.File.Stat()
if err != nil {
return nil, err
}
if _, found := ofs.notExpected[filepath.Join(ofs.folder, fileInfo.Name())]; found {
return nil, ErrShouldNotBeReached
}
return fileInfo, nil
}
type ForbiddenDirEntry struct {
fs.DirEntry
notExpected map[string]struct{}
folder string
}
func (ofs ForbiddenDirEntry) Info() (fs.FileInfo, error) {
fileInfo, err := ofs.DirEntry.Info()
if err != nil {
return nil, err
}
if _, found := ofs.notExpected[filepath.Join(ofs.folder, fileInfo.Name())]; found {
return nil, ErrShouldNotBeReached
}
return fileInfo, nil
}
func TestNoGo_AddFromFS_ignored_nested_files(t *testing.T) {
// This tests a bug where AddFromFS did walk the whole tree because
// the nogo-instance was not mutated with found .gitingore files.
// (due to missing *-receivers in the walk methods)
type args struct {
fsys fs.FS
}
tests := []struct {
name string
args args
wantErr assert.ErrorAssertionFunc
}{
{
name: "nested ignored files should not be loaded to speed up ignore-file loading",
args: args{
fsys: ForbiddenFS{
NotExpected: map[string]struct{}{
"ignoredFolder/.gitignore": {},
"ignoredFolder/sub": {},
"notIgnored/subignored/lol": {},
},
MapFS: fstest.MapFS{
"ignoredFolder": &fstest.MapFile{
Mode: fs.ModeDir,
},
"ignoredFolder/sub": &fstest.MapFile{
Mode: fs.ModeDir,
},
"ignoredFolder/sub/something": &fstest.MapFile{},
"ignoredFolder/.gitignore": &fstest.MapFile{},
"aFile": &fstest.MapFile{},
".gitignore": &fstest.MapFile{
Data: []byte("ignoredFolder"),
},
"notIgnored/sub": &fstest.MapFile{},
"notIgnored/.gitignore": &fstest.MapFile{
Data: []byte("subignored"),
},
"notIgnored/subignored/lol": &fstest.MapFile{},
},
},
},
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := &NoGo{}
assert.NoError(t, n.AddFromFS(tt.args.fsys, ".gitignore"))
})
}
}