-
Notifications
You must be signed in to change notification settings - Fork 3
/
glob_test.go
241 lines (209 loc) · 8.59 KB
/
glob_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
package ohmyglob
import (
"os"
"testing"
log "github.com/cihub/seelog"
"github.com/stretchr/testify/assert"
)
func init() {
Logger, _ = log.LoggerFromWriterWithMinLevel(os.Stderr, log.TraceLvl)
}
func TestSimpleGlob(t *testing.T) {
pattern := "foo/*/b?r"
glob, err := Compile(pattern, nil)
assert.NoError(t, err)
match := "foo/baz/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/baz/bbr"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/baz/bdr"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/baz∆˙¨®˙¨¥ƒ®†˙ƒ†¨®†√˙/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/bar"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
// With a * as a partial component
pattern = "foo/*/baz/--foo/--*/--baz"
glob, err = Compile(pattern, nil)
assert.NoError(t, err)
match = "foo/bar/baz/--foo/--bar/--baz"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
}
func TestGlobStar(t *testing.T) {
pattern := "foo/**/bar/**"
glob, err := Compile(pattern, nil)
assert.NoError(t, err)
match := "foo/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/bar/barrrrr"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/baz/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/baz/boop/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/baz/boop/µ∂^~®˙¨˙烮†¨^/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "thisistotal/garbage/ÔÔÈ´^¨∆~∆≈∆∫"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
match = "foobar"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
match = "foobar/bar"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
match = "foo/barbar"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
// Check consecutive globstars work correctly
pattern = "foo/**/**/bar"
glob, err = Compile(pattern, nil)
assert.NoError(t, err)
match = "foo/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/baz/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/baz/boop/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/baz/boop/µ∂^~®˙¨˙烮†¨^/bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "thisistotal/garbage/ÔÔÈ´^¨∆~∆≈∆∫"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
match = "foobar"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
// Globstar at the head
pattern = "**/**/foobar"
glob, err = Compile(pattern, nil)
assert.NoError(t, err)
match = "foobar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/foobar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo/bar/foo/bar/foobar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
// Globstar at the tail
pattern = "foobar/**/**"
glob, err = Compile(pattern, nil)
assert.NoError(t, err)
match = "foobar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foobar/baz/boo"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foobar/baz"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
// Globstar as the only component should match any string
pattern = "**"
glob, err = Compile(pattern, nil)
assert.NoError(t, err)
match = ""
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "a"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "////"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "aaaa/bbb////c///def/////****/*/.?..**//."
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
}
// Check that setting MatchAtStart to false allows any prefix
func TestNoMatchAtStart(t *testing.T) {
pattern := "foo"
glob, err := Compile(pattern, &Options{
Separator: DefaultOptions.Separator,
MatchAtStart: false,
MatchAtEnd: DefaultOptions.MatchAtEnd,
})
assert.NoError(t, err)
// Test without a prefix still works
match := "foo"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
// Test with a prefix still works
match = "bar/baz/foo"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
// Test that a prefix and a suffix does not
match = "bar/baz/foo/boop"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
}
// Check that setting MatchAtStart to false allows any suffix
func TestNoMatchAtEnd(t *testing.T) {
pattern := "foo"
glob, err := Compile(pattern, &Options{
Separator: DefaultOptions.Separator,
MatchAtStart: DefaultOptions.MatchAtStart,
MatchAtEnd: false,
})
assert.NoError(t, err)
// Test without a suffix still works
match := "foo"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
// Test with a suffix still works
match = "foo/bar/baz"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
// Test that a prefix and a suffix does not
match = "bar/baz/foo/boop"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
}
func TestNegation(t *testing.T) {
pattern := "!foo"
glob, err := Compile(pattern, nil)
assert.NoError(t, err)
assert.True(t, glob.IsNegative(), "Glob should be negative")
// Test it negates the exact string (it should report a match, though it is negative)
match := "foo"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
// Should not match other strings
match = "foo.bar.baz"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
match = "h4ughfrfg4598yf5uh"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
}
func TestCustomSeparator(t *testing.T) {
pattern := "foo.*.bar"
glob, err := Compile(pattern, &Options{
Separator: '.',
MatchAtStart: DefaultOptions.MatchAtStart,
MatchAtEnd: DefaultOptions.MatchAtEnd,
})
assert.NoError(t, err)
match := "foo.baz.bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo.baz∆˙¨®˙¨¥ƒ®†˙ƒ†¨®†√˙.bar"
assert.True(t, glob.MatchString(match), "%s should match %s", pattern, match)
match = "foo.bar"
assert.False(t, glob.MatchString(match), "%s should not match %s", pattern, match)
}
// Illegal separators should return an error on construction
func TestIllegalSeparator(t *testing.T) {
_, err := Compile("foo", &Options{
Separator: '?',
})
assert.Error(t, err, "? should not be allowed as a separator")
_, err = Compile("foo", &Options{
Separator: '\\',
})
assert.Error(t, err, "\\ should not be allowed as a separator")
}
// Meaningful can be escaped with a backslash
func TestEscaping(t *testing.T) {
// Maps to a pair of (should, shouldn't) strings
expectations := map[string][2]string{
`\/`: [2]string{`/`, ``},
`foo\bar/\*\/baz`: [2]string{`foobar/*/baz`, `foobar/foo/baz`},
`foo\?bar`: [2]string{`foo?bar`, `foosbar`},
`\/\/\/\*\*.*`: [2]string{`///**.foobar`, `///foobar.baz`},
`\`: [2]string{``, `\`},
`foo\bar/baz`: [2]string{`foobar/baz`, `foo\bar/baz`},
`foo\\bar\/\//barbaz\*\?\\`: [2]string{`foo\bar///barbaz*?\`, `a`},
}
for pattern, s := range expectations {
glob, err := Compile(pattern, DefaultOptions)
assert.NoError(t, err)
should, shouldnt := s[0], s[1]
assert.False(t, glob.MatchString(shouldnt), "Glob `%s` should not match `%s`", pattern, shouldnt)
assert.True(t, glob.MatchString(should), "Glob `%s` should match `%s`", pattern, should)
}
// Custom separator
}
// Benchmark globbing from start to finish; constructing and matching
func BenchmarkGlobbing(b *testing.B) {
pattern := "foo/**/baz/--fo?/*/--baz"
b.ResetTimer()
g, err := Compile(pattern, nil)
assert.NoError(b, err)
assert.True(b, g.MatchString("foo/bar/bar/baz/--foo/--bar/--baz"))
}