1
# Copyright (C) 2006 Canonical Ltd
2
# -*- coding: utf-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from bzrlib.globbing import (
23
from bzrlib.tests import (
29
class TestGlobster(TestCase):
31
def assertMatch(self, matchset, glob_prefix=None):
32
for glob, positive, negative in matchset:
34
glob = glob_prefix + glob
35
globster = Globster([glob])
37
self.failUnless(globster.match(name), repr(
38
u'name "%s" does not match glob "%s" (re=%s)' %
39
(name, glob, globster._regex_patterns[0][0].pattern)))
41
self.failIf(globster.match(name), repr(
42
u'name "%s" does match glob "%s" (re=%s)' %
43
(name, glob, globster._regex_patterns[0][0].pattern)))
45
def assertMatchBasenameAndFullpath(self, matchset):
46
# test basename matcher
47
self.assertMatch(matchset)
48
# test fullpath matcher
49
self.assertMatch(matchset, glob_prefix='./')
51
def test_char_group_digit(self):
52
self.assertMatchBasenameAndFullpath([
53
# The definition of digit this uses includes arabic digits from
54
# non-latin scripts (arabic, indic, etc.) and subscript/superscript
55
# digits, but neither roman numerals nor vulgar fractions.
57
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9'],
58
[u'T', u'q', u' ', u'\u8336', u'.']),
60
[u'T', u'q', u' ', u'\u8336', u'.'],
61
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9']),
64
def test_char_group_space(self):
65
self.assertMatchBasenameAndFullpath([
67
[u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002'],
68
[u'a', u'-', u'\u8336', u'.']),
70
[u'a', u'-', u'\u8336', u'.'],
71
[u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002']),
74
def test_char_group_alnum(self):
75
self.assertMatchBasenameAndFullpath([
77
[u'a', u'Z', u'\u017e', u'\u8336'],
78
[u':', u'-', u'\u25cf', u'.']),
80
[u':', u'-', u'\u25cf', u'.'],
84
def test_char_group_ascii(self):
85
self.assertMatchBasenameAndFullpath([
87
[u'a', u'Q', u'^', u'.'],
88
[u'\xcc', u'\u8336']),
91
[u'a', u'Q', u'^', u'.']),
94
def test_char_group_blank(self):
95
self.assertMatchBasenameAndFullpath([
98
[u'x', u'y', u'z', u'.']),
100
[u'x', u'y', u'z', u'.'],
104
def test_char_group_cntrl(self):
105
self.assertMatchBasenameAndFullpath([
107
[u'\b', u'\t', '\x7f'],
108
[u'a', u'Q', u'\u8336', u'.']),
110
[u'a', u'Q', u'\u8336', u'.'],
111
[u'\b', u'\t', '\x7f']),
114
def test_char_group_range(self):
115
self.assertMatchBasenameAndFullpath([
124
[u'afoo', u'ABfoo']),
126
[u'fooAbar', u'foo.bar'],
128
(u'[\x20-\x30\u8336]',
129
[u'\040', u'\044', u'\u8336'],
131
(u'[^\x20-\x30\u8336]',
133
[u'\040', u'\044', u'\u8336']),
136
def test_regex(self):
139
[u'a', u'b', u'ccc'],
140
[u'd', u'aa', u'c+', u'-a']),
142
[u'a', u'b', u'ccc'],
143
[u'd', u'aa', u'c+', u'-a']),
144
(u'RE:(?P<a>.)(?P=a)',
146
[u'ab', u'aa', u'aaa']),
147
# test we can handle odd numbers of trailing backslashes
150
[u'a', u'ab', u'aa', u'aaa']),
153
def test_question_mark(self):
156
[u'xfoo', u'bar/xfoo', u'bar/\u8336foo', u'.foo', u'bar/.foo'],
157
[u'bar/foo', u'foo']),
159
[u'fooxbar', u'foo.bar', u'foo\u8336bar', u'qyzzy/foo.bar'],
162
[u'foo/xbar', u'foo/\u8336bar', u'foo/.bar'],
163
[u'foo/bar', u'bar/foo/xbar']),
166
def test_asterisk(self):
169
[u'xx', u'x.x', u'x\u8336..x', u'\u8336/x.x', u'x.y.x'],
170
[u'x/x', u'bar/x/bar/x', u'bax/abaxab']),
172
[u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
175
[u'\u8336/x', u'foo/x', u'foo/bax', u'x/a.x', u'.foo/x',
176
u'\u8336/.x', u'foo/.q.x'],
179
[u'foo', u'foo.bar'],
180
[u'.foo', u'foo/bar', u'foo/.bar']),
182
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
183
u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
187
def test_double_asterisk(self):
189
# expected uses of double asterisk
191
[u'foo/x', u'foo/bar/x'],
192
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
194
[u'bar', u'foo/bar'],
195
[u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
196
u'.bar', u'foo/.bar']),
197
# check that we ignore extra *s, so *** is treated like ** not *.
199
[u'foo/x', u'foo/bar/x'],
200
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
202
[u'bar', u'foo/bar'],
203
[u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
204
u'.bar', u'foo/.bar']),
205
# the remaining tests check that ** is interpreted as *
206
# unless it is a whole path component
208
[u'x\u8336/x', u'x/x'],
209
[u'xx', u'x.x', u'bar/x/bar/x', u'x.y.x', u'x/y/x']),
211
[u'xx', u'x.x', u'x\u8336..x', u'foo/x.x', u'x.y.x'],
212
[u'bar/x/bar/x', u'xfoo/bar/x', u'x/x', u'bax/abaxab']),
214
[u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
217
[u'foo', u'foo.bar'],
218
[u'.foo', u'foo/bar', u'foo/.bar']),
220
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
221
u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
225
def test_leading_dot_slash(self):
229
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
232
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
235
def test_backslash(self):
239
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
242
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
244
[u'foo/x', u'foo/bar/x'],
245
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
248
def test_trailing_slash(self):
252
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
255
[u'foo/', u'\u8336/foo', u'barfoo', u'x/y/foo']),
258
def test_leading_asterisk_dot(self):
261
[u'foo/bar/baz.x', u'\u8336/Q.x', u'foo.y.x', u'.foo.x',
262
u'bar/.foo.x', u'.x',],
265
[u'foo/b.bar', u'foo/a.b.bar', u'foo/.bar'],
268
[u'foo.py.~1~', u'.foo.py.~1~'],
272
def test_end_anchor(self):
282
def test_mixed_globs(self):
283
"""tests handling of combinations of path type matches.
285
The types being extension, basename and full path.
287
patterns = [ u'*.foo', u'.*.swp', u'./*.png']
288
globster = Globster(patterns)
289
self.assertEqual(u'*.foo', globster.match('bar.foo'))
290
self.assertEqual(u'./*.png', globster.match('foo.png'))
291
self.assertEqual(None, globster.match('foo/bar.png'))
292
self.assertEqual(u'.*.swp', globster.match('foo/.bar.py.swp'))
294
def test_large_globset(self):
295
"""tests that the globster can handle a large set of patterns.
297
Large is defined as more than supported by python regex groups,
299
This test assumes the globs are broken into regexs containing 99
302
patterns = [ u'*.%03d' % i for i in xrange(0,300) ]
303
globster = Globster(patterns)
304
# test the fence posts
305
for x in (0,98,99,197,198,296,297,299):
306
filename = u'foo.%03d' % x
307
self.assertEqual(patterns[x],globster.match(filename))
308
self.assertEqual(None,globster.match('foobar.300'))
311
class TestOrderedGlobster(TestCase):
313
def test_ordered_globs(self):
314
"""test that the first match in a list is the one found"""
315
patterns = [ u'*.foo', u'bar.*']
316
globster = _OrderedGlobster(patterns)
317
self.assertEqual(u'*.foo', globster.match('bar.foo'))
318
self.assertEqual(None, globster.match('foo.bar'))
319
globster = _OrderedGlobster(reversed(patterns))
320
self.assertEqual(u'bar.*', globster.match('bar.foo'))
321
self.assertEqual(None, globster.match('foo.bar'))
324
class TestNormalizePattern(TestCase):
326
def test_backslashes(self):
327
"""tests that backslashes are converted to forward slashes, multiple
328
backslashes are collapsed to single forward slashes and trailing
329
backslashes are removed"""
330
self.assertEqual(u'/', normalize_pattern(u'\\'))
331
self.assertEqual(u'/', normalize_pattern(u'\\\\'))
332
self.assertEqual(u'/foo/bar', normalize_pattern(u'\\foo\\bar'))
333
self.assertEqual(u'foo/bar', normalize_pattern(u'foo\\bar\\'))
334
self.assertEqual(u'/foo/bar', normalize_pattern(u'\\\\foo\\\\bar\\\\'))
336
def test_forward_slashes(self):
337
"""tests that multiple foward slashes are collapsed to single forward
338
slashes and trailing forward slashes are removed"""
339
self.assertEqual(u'/', normalize_pattern(u'/'))
340
self.assertEqual(u'/', normalize_pattern(u'//'))
341
self.assertEqual(u'/foo/bar', normalize_pattern(u'/foo/bar'))
342
self.assertEqual(u'foo/bar', normalize_pattern(u'foo/bar/'))
343
self.assertEqual(u'/foo/bar', normalize_pattern(u'//foo//bar//'))
345
def test_mixed_slashes(self):
346
"""tests that multiple mixed slashes are collapsed to single forward
347
slashes and trailing mixed slashes are removed"""
348
self.assertEqual(u'/foo/bar', normalize_pattern(u'\\/\\foo//\\///bar/\\\\/'))