forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilepathfilter_test.go
142 lines (119 loc) · 3.95 KB
/
filepathfilter_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
package filepathfilter
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPatternMatch(t *testing.T) {
for _, wildcard := range []string{`*`, `.`, `./`, `.\`} {
assertPatternMatch(t, wildcard,
"a",
"a/",
"a.a",
"a/b",
"a/b/",
"a/b.b",
"a/b/c",
"a/b/c/",
"a/b/c.c",
)
}
assertPatternMatch(t, "filename.txt", "filename.txt")
assertPatternMatch(t, "*.txt", "filename.txt")
refutePatternMatch(t, "*.tx", "filename.txt")
assertPatternMatch(t, "f*.txt", "filename.txt")
refutePatternMatch(t, "g*.txt", "filename.txt")
assertPatternMatch(t, "file*", "filename.txt")
refutePatternMatch(t, "file", "filename.txt")
// With no path separators, should match in subfolders
assertPatternMatch(t, "*.txt", "sub/filename.txt")
refutePatternMatch(t, "*.tx", "sub/filename.txt")
assertPatternMatch(t, "f*.txt", "sub/filename.txt")
refutePatternMatch(t, "g*.txt", "sub/filename.txt")
assertPatternMatch(t, "file*", "sub/filename.txt")
refutePatternMatch(t, "file", "sub/filename.txt")
// matches only in subdir
assertPatternMatch(t, "sub/*.txt", "sub/filename.txt")
refutePatternMatch(t, "sub/*.txt",
"top/sub/filename.txt",
"sub/filename.dat",
"other/filename.txt",
)
// Needs wildcard for exact filename
assertPatternMatch(t, "**/filename.txt", "sub/sub/sub/filename.txt")
// Should not match dots to subparts
refutePatternMatch(t, "*.ign", "sub/shouldignoreme.txt")
// Path specific
assertPatternMatch(t, "sub",
"sub/",
"sub",
"sub/filename.txt",
"top/sub/",
"top/sub",
"top/sub/filename.txt",
)
assertPatternMatch(t, "sub/", "sub/filename.txt", "top/sub/filename.txt")
assertPatternMatch(t, "/sub", "sub/", "sub", "sub/filename.txt")
assertPatternMatch(t, "/sub/", "sub/filename.txt")
refutePatternMatch(t, "/sub", "subfilename.txt", "top/sub/", "top/sub", "top/sub/filename.txt")
refutePatternMatch(t, "sub", "subfilename.txt")
refutePatternMatch(t, "sub/", "subfilename.txt")
refutePatternMatch(t, "/sub/", "subfilename.txt", "top/sub/filename.txt")
// nested path
assertPatternMatch(t, "top/sub",
"top/sub/filename.txt",
"top/sub/",
"top/sub",
"root/top/sub/filename.txt",
"root/top/sub/",
"root/top/sub",
)
assertPatternMatch(t, "top/sub/", "top/sub/filename.txt")
assertPatternMatch(t, "top/sub/", "root/top/sub/filename.txt")
assertPatternMatch(t, "/top/sub", "top/sub/", "top/sub", "top/sub/filename.txt")
assertPatternMatch(t, "/top/sub/", "top/sub/filename.txt")
refutePatternMatch(t, "top/sub", "top/subfilename.txt")
refutePatternMatch(t, "top/sub/", "top/subfilename.txt")
refutePatternMatch(t, "/top/sub",
"top/subfilename.txt",
"root/top/sub/filename.txt",
"root/top/sub/",
"root/top/sub",
)
refutePatternMatch(t, "/top/sub/",
"root/top/sub/filename.txt",
"top/subfilename.txt",
)
// Absolute
assertPatternMatch(t, "*.dat", "/path/to/sub/.git/test.dat")
assertPatternMatch(t, "**/.git", "/path/to/sub/.git")
// Match anything
assertPatternMatch(t, ".", "path.txt")
assertPatternMatch(t, "./", "path.txt")
assertPatternMatch(t, ".\\", "path.txt")
}
func assertPatternMatch(t *testing.T, pattern string, filenames ...string) {
p := NewPattern(pattern)
for _, filename := range filenames {
assert.True(t, p.Match(filename), "%q should match pattern %q", filename, pattern)
}
}
func refutePatternMatch(t *testing.T, pattern string, filenames ...string) {
p := NewPattern(pattern)
for _, filename := range filenames {
assert.False(t, p.Match(filename), "%q should not match pattern %q", filename, pattern)
}
}
type filterTest struct {
expectedResult bool
expectedPattern string
includes []string
excludes []string
}
func TestFilterReportsIncludePatterns(t *testing.T) {
filter := New([]string{"*.foo", "*.bar"}, nil)
assert.Equal(t, []string{"*.foo", "*.bar"}, filter.Include())
}
func TestFilterReportsExcludePatterns(t *testing.T) {
filter := New(nil, []string{"*.baz", "*.quux"})
assert.Equal(t, []string{"*.baz", "*.quux"}, filter.Exclude())
}