-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathignore_test.go
More file actions
116 lines (111 loc) · 2.85 KB
/
ignore_test.go
File metadata and controls
116 lines (111 loc) · 2.85 KB
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
package main
import (
"testing"
)
func TestMatchPattern_Simple(t *testing.T) {
tests := []struct {
name string
pattern ignorePattern
relPath string
baseName string
want bool
}{
{
"glob matches basename",
ignorePattern{pattern: "*.log"},
"logs/app.log", "app.log",
true,
},
{
"glob does not match",
ignorePattern{pattern: "*.log"},
"logs/app.txt", "app.txt",
false,
},
{
"exact basename",
ignorePattern{pattern: ".DS_Store"},
"path/to/.DS_Store", ".DS_Store",
true,
},
{
"anchored pattern matches full path",
ignorePattern{pattern: "build/output", anchored: true},
"build/output", "output",
true,
},
{
"anchored pattern does not match nested",
ignorePattern{pattern: "build/output", anchored: true},
"src/build/output", "output",
false,
},
{
"unanchored matches at any depth",
ignorePattern{pattern: "*.o"},
"src/deep/file.o", "file.o",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := matchPattern(tt.pattern, tt.relPath, tt.baseName)
if got != tt.want {
t.Errorf("matchPattern(%+v, %q, %q) = %v, want %v",
tt.pattern, tt.relPath, tt.baseName, got, tt.want)
}
})
}
}
func TestMatchDoublestar(t *testing.T) {
tests := []struct {
name string
pattern string
path string
want bool
}{
{"bare **", "**", "anything/at/all", true},
{"**/file matches top", "**/Makefile", "Makefile", true},
{"**/file matches nested", "**/Makefile", "src/Makefile", true},
{"**/file matches deep", "**/Makefile", "a/b/c/Makefile", true},
{"**/file no match", "**/Makefile", "src/main.go", false},
{"dir/**", "vendor/**", "vendor/pkg/mod", true},
{"dir/** exact", "vendor/**", "vendor", true},
{"dir/** no match", "vendor/**", "src/vendor", false},
{"middle /**/", "src/**/test.go", "src/pkg/test.go", true},
{"middle /**/ deep", "src/**/test.go", "src/a/b/test.go", true},
{"middle /**/ no match", "src/**/test.go", "lib/test.go", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := matchDoublestar(tt.pattern, tt.path)
if got != tt.want {
t.Errorf("matchDoublestar(%q, %q) = %v, want %v",
tt.pattern, tt.path, got, tt.want)
}
})
}
}
func TestIgnorer_IsExcluded(t *testing.T) {
ig := newIgnorer([]string{".git", "node_modules"}, "/nonexistent")
tests := []struct {
name string
relPath string
isDir bool
want bool
}{
{"always excluded dir", ".git", true, true},
{"always excluded dir nested", "node_modules", true, true},
{"normal file", "src/main.go", false, false},
{"normal dir", "src", true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ig.isExcluded(tt.relPath, tt.isDir)
if got != tt.want {
t.Errorf("isExcluded(%q, %v) = %v, want %v",
tt.relPath, tt.isDir, got, tt.want)
}
})
}
}