37
32
glob = glob_prefix + glob
38
33
globster = Globster([glob])
39
34
for name in positive:
40
self.assertTrue(globster.match(name), repr(
35
self.failUnless(globster.match(name), repr(
41
36
u'name "%s" does not match glob "%s" (re=%s)' %
42
37
(name, glob, globster._regex_patterns[0][0].pattern)))
43
38
for name in negative:
44
self.assertFalse(globster.match(name), repr(
39
self.failIf(globster.match(name), repr(
45
40
u'name "%s" does match glob "%s" (re=%s)' %
46
41
(name, glob, globster._regex_patterns[0][0].pattern)))
54
49
def test_char_group_digit(self):
55
50
self.assertMatchBasenameAndFullpath([
56
51
# The definition of digit this uses includes arabic digits from
57
# non-latin scripts (arabic, indic, etc.) but neither roman
58
# numerals nor vulgar fractions. Some characters such as
59
# subscript/superscript digits may or may not match depending on
60
# the Python version used, see: <http://bugs.python.org/issue6561>
52
# non-latin scripts (arabic, indic, etc.) and subscript/superscript
53
# digits, but neither roman numerals nor vulgar fractions.
62
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21'],
55
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9'],
63
56
[u'T', u'q', u' ', u'\u8336', u'.']),
65
58
[u'T', u'q', u' ', u'\u8336', u'.'],
66
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21']),
59
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9']),
69
62
def test_char_group_space(self):
122
115
[u'a', u'q', u'f'],
123
116
[u'A', u'Q', u'F']),
125
118
[u'A', u'Q', u'F'],
126
119
[u'a', u'q', u'f']),
128
121
[u'Afoo', u'.foo'],
129
122
[u'afoo', u'ABfoo']),
131
124
[u'fooAbar', u'foo.bar'],
133
(u'[\x20-\x30\u8336]',
126
(ur'[\x20-\x30\u8336]',
134
127
[u'\040', u'\044', u'\u8336'],
136
(u'[^\x20-\x30\u8336]',
129
(ur'[^\x20-\x30\u8336]',
138
131
[u'\040', u'\044', u'\u8336']),
141
134
def test_regex(self):
142
135
self.assertMatch([
144
[u'a', u'b', u'ccc'],
145
[u'd', u'aa', u'c+', u'-a']),
147
[u'a', u'b', u'ccc'],
148
[u'd', u'aa', u'c+', u'-a']),
149
(u'RE:(?P<a>.)(?P=a)',
137
[u'a', u'b', u'ccc'],
138
[u'd', u'aa', u'c+', u'-a']),
140
[u'a', u'b', u'ccc'],
141
[u'd', u'aa', u'c+', u'-a']),
142
(ur'RE:(?P<a>.)(?P=a)',
151
144
[u'ab', u'aa', u'aaa']),
152
# test we can handle odd numbers of trailing backslashes
155
[u'a', u'ab', u'aa', u'aaa']),
158
147
def test_question_mark(self):
177
166
[u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
178
167
[u'foo/bar/bax']),
180
[u'\u8336/x', u'foo/x', u'foo/bax', u'x/a.x', u'.foo/x',
169
[u'\u8336/x', u'foo/x', u'foo/bax', u'x/a.x', u'.foo/x',
181
170
u'\u8336/.x', u'foo/.q.x'],
182
171
[u'foo/bar/bax']),
184
173
[u'foo', u'foo.bar'],
185
174
[u'.foo', u'foo/bar', u'foo/.bar']),
187
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
176
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
188
177
u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
237
226
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
240
def test_backslash(self):
244
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
247
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
249
[u'foo/x', u'foo/bar/x'],
250
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
253
def test_trailing_slash(self):
257
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
260
[u'foo/', u'\u8336/foo', u'barfoo', u'x/y/foo']),
263
229
def test_leading_asterisk_dot(self):
264
230
self.assertMatch([
266
[u'foo/bar/baz.x', u'\u8336/Q.x', u'foo.y.x', u'.foo.x',
232
[u'foo/bar/baz.x', u'\u8336/Q.x', u'foo.y.x', u'.foo.x',
267
233
u'bar/.foo.x', u'.x',],
312
278
self.assertEqual(patterns[x],globster.match(filename))
313
279
self.assertEqual(None,globster.match('foobar.300'))
315
def test_bad_pattern(self):
316
"""Ensure that globster handles bad patterns cleanly."""
317
patterns = [u'RE:[', u'/home/foo', u'RE:*.cpp']
318
g = Globster(patterns)
319
e = self.assertRaises(errors.InvalidPattern, g.match, 'filename')
320
self.assertContainsRe(e.msg,
321
"File.*ignore.*contains error.*RE:\[.*RE:\*\.cpp", flags=re.DOTALL)
324
class TestExceptionGlobster(TestCase):
326
def test_exclusion_patterns(self):
327
"""test that exception patterns are not matched"""
328
patterns = [ u'*', u'!./local', u'!./local/**/*', u'!RE:\.z.*',u'!!./.zcompdump' ]
329
globster = ExceptionGlobster(patterns)
330
self.assertEqual(u'*', globster.match('tmp/foo.txt'))
331
self.assertEqual(None, globster.match('local'))
332
self.assertEqual(None, globster.match('local/bin/wombat'))
333
self.assertEqual(None, globster.match('.zshrc'))
334
self.assertEqual(None, globster.match('.zfunctions/fiddle/flam'))
335
self.assertEqual(u'!!./.zcompdump', globster.match('.zcompdump'))
337
def test_exclusion_order(self):
338
"""test that ordering of exclusion patterns does not matter"""
339
patterns = [ u'static/**/*.html', u'!static/**/versionable.html']
340
globster = ExceptionGlobster(patterns)
341
self.assertEqual(u'static/**/*.html', globster.match('static/foo.html'))
342
self.assertEqual(None, globster.match('static/versionable.html'))
343
self.assertEqual(None, globster.match('static/bar/versionable.html'))
344
globster = ExceptionGlobster(reversed(patterns))
345
self.assertEqual(u'static/**/*.html', globster.match('static/foo.html'))
346
self.assertEqual(None, globster.match('static/versionable.html'))
347
self.assertEqual(None, globster.match('static/bar/versionable.html'))
349
class TestOrderedGlobster(TestCase):
351
def test_ordered_globs(self):
352
"""test that the first match in a list is the one found"""
353
patterns = [ u'*.foo', u'bar.*']
354
globster = _OrderedGlobster(patterns)
355
self.assertEqual(u'*.foo', globster.match('bar.foo'))
356
self.assertEqual(None, globster.match('foo.bar'))
357
globster = _OrderedGlobster(reversed(patterns))
358
self.assertEqual(u'bar.*', globster.match('bar.foo'))
359
self.assertEqual(None, globster.match('foo.bar'))
362
class TestNormalizePattern(TestCase):
364
def test_backslashes(self):
365
"""tests that backslashes are converted to forward slashes, multiple
366
backslashes are collapsed to single forward slashes and trailing
367
backslashes are removed"""
368
self.assertEqual(u'/', normalize_pattern(u'\\'))
369
self.assertEqual(u'/', normalize_pattern(u'\\\\'))
370
self.assertEqual(u'/foo/bar', normalize_pattern(u'\\foo\\bar'))
371
self.assertEqual(u'foo/bar', normalize_pattern(u'foo\\bar\\'))
372
self.assertEqual(u'/foo/bar', normalize_pattern(u'\\\\foo\\\\bar\\\\'))
374
def test_forward_slashes(self):
375
"""tests that multiple foward slashes are collapsed to single forward
376
slashes and trailing forward slashes are removed"""
377
self.assertEqual(u'/', normalize_pattern(u'/'))
378
self.assertEqual(u'/', normalize_pattern(u'//'))
379
self.assertEqual(u'/foo/bar', normalize_pattern(u'/foo/bar'))
380
self.assertEqual(u'foo/bar', normalize_pattern(u'foo/bar/'))
381
self.assertEqual(u'/foo/bar', normalize_pattern(u'//foo//bar//'))
383
def test_mixed_slashes(self):
384
"""tests that multiple mixed slashes are collapsed to single forward
385
slashes and trailing mixed slashes are removed"""
386
self.assertEqual(u'/foo/bar', normalize_pattern(u'\\/\\foo//\\///bar/\\\\/'))