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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.globbing import (
22
from bzrlib.tests import (
28
class TestGlobster(TestCase):
30
def assertMatch(self, matchset, glob_prefix=None):
31
for glob, positive, negative in matchset:
33
glob = glob_prefix + glob
34
globster = Globster([glob])
36
self.failUnless(globster.match(name), repr(
37
u'name "%s" does not match glob "%s" (re=%s)' %
38
(name, glob, globster._regex_patterns[0][0].pattern)))
40
self.failIf(globster.match(name), repr(
41
u'name "%s" does match glob "%s" (re=%s)' %
42
(name, glob, globster._regex_patterns[0][0].pattern)))
44
def assertMatchBasenameAndFullpath(self, matchset):
45
# test basename matcher
46
self.assertMatch(matchset)
47
# test fullpath matcher
48
self.assertMatch(matchset, glob_prefix='./')
50
def test_char_group_digit(self):
51
self.assertMatchBasenameAndFullpath([
52
# The definition of digit this uses includes arabic digits from
53
# non-latin scripts (arabic, indic, etc.) and subscript/superscript
54
# digits, but neither roman numerals nor vulgar fractions.
56
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9'],
57
[u'T', u'q', u' ', u'\u8336', u'.']),
59
[u'T', u'q', u' ', u'\u8336', u'.'],
60
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9']),
63
def test_char_group_space(self):
64
self.assertMatchBasenameAndFullpath([
66
[u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002'],
67
[u'a', u'-', u'\u8336', u'.']),
69
[u'a', u'-', u'\u8336', u'.'],
70
[u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002']),
73
def test_char_group_alnum(self):
74
self.assertMatchBasenameAndFullpath([
76
[u'a', u'Z', u'\u017e', u'\u8336'],
77
[u':', u'-', u'\u25cf', u'.']),
79
[u':', u'-', u'\u25cf', u'.'],
83
def test_char_group_ascii(self):
84
self.assertMatchBasenameAndFullpath([
86
[u'a', u'Q', u'^', u'.'],
87
[u'\xcc', u'\u8336']),
90
[u'a', u'Q', u'^', u'.']),
93
def test_char_group_blank(self):
94
self.assertMatchBasenameAndFullpath([
97
[u'x', u'y', u'z', u'.']),
99
[u'x', u'y', u'z', u'.'],
103
def test_char_group_cntrl(self):
104
self.assertMatchBasenameAndFullpath([
106
[u'\b', u'\t', '\x7f'],
107
[u'a', u'Q', u'\u8336', u'.']),
109
[u'a', u'Q', u'\u8336', u'.'],
110
[u'\b', u'\t', '\x7f']),
113
def test_char_group_range(self):
114
self.assertMatchBasenameAndFullpath([
123
[u'afoo', u'ABfoo']),
125
[u'fooAbar', u'foo.bar'],
127
(u'[\x20-\x30\u8336]',
128
[u'\040', u'\044', u'\u8336'],
130
(u'[^\x20-\x30\u8336]',
132
[u'\040', u'\044', u'\u8336']),
135
def test_regex(self):
138
[u'a', u'b', u'ccc'],
139
[u'd', u'aa', u'c+', u'-a']),
141
[u'a', u'b', u'ccc'],
142
[u'd', u'aa', u'c+', u'-a']),
143
(u'RE:(?P<a>.)(?P=a)',
145
[u'ab', u'aa', u'aaa']),
146
# test we can handle odd numbers of trailing backslashes
149
[u'a', u'ab', u'aa', u'aaa']),
152
def test_question_mark(self):
155
[u'xfoo', u'bar/xfoo', u'bar/\u8336foo', u'.foo', u'bar/.foo'],
156
[u'bar/foo', u'foo']),
158
[u'fooxbar', u'foo.bar', u'foo\u8336bar', u'qyzzy/foo.bar'],
161
[u'foo/xbar', u'foo/\u8336bar', u'foo/.bar'],
162
[u'foo/bar', u'bar/foo/xbar']),
165
def test_asterisk(self):
168
[u'xx', u'x.x', u'x\u8336..x', u'\u8336/x.x', u'x.y.x'],
169
[u'x/x', u'bar/x/bar/x', u'bax/abaxab']),
171
[u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
174
[u'\u8336/x', u'foo/x', u'foo/bax', u'x/a.x', u'.foo/x',
175
u'\u8336/.x', u'foo/.q.x'],
178
[u'foo', u'foo.bar'],
179
[u'.foo', u'foo/bar', u'foo/.bar']),
181
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
182
u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
186
def test_double_asterisk(self):
188
# expected uses of double asterisk
190
[u'foo/x', u'foo/bar/x'],
191
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
193
[u'bar', u'foo/bar'],
194
[u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
195
u'.bar', u'foo/.bar']),
196
# check that we ignore extra *s, so *** is treated like ** not *.
198
[u'foo/x', u'foo/bar/x'],
199
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
201
[u'bar', u'foo/bar'],
202
[u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
203
u'.bar', u'foo/.bar']),
204
# the remaining tests check that ** is interpreted as *
205
# unless it is a whole path component
207
[u'x\u8336/x', u'x/x'],
208
[u'xx', u'x.x', u'bar/x/bar/x', u'x.y.x', u'x/y/x']),
210
[u'xx', u'x.x', u'x\u8336..x', u'foo/x.x', u'x.y.x'],
211
[u'bar/x/bar/x', u'xfoo/bar/x', u'x/x', u'bax/abaxab']),
213
[u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
216
[u'foo', u'foo.bar'],
217
[u'.foo', u'foo/bar', u'foo/.bar']),
219
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
220
u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
224
def test_leading_dot_slash(self):
228
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
231
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
234
def test_backslash(self):
238
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
241
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
243
[u'foo/x', u'foo/bar/x'],
244
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
247
def test_trailing_slash(self):
251
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
254
[u'foo/', u'\u8336/foo', u'barfoo', u'x/y/foo']),
257
def test_leading_asterisk_dot(self):
260
[u'foo/bar/baz.x', u'\u8336/Q.x', u'foo.y.x', u'.foo.x',
261
u'bar/.foo.x', u'.x',],
264
[u'foo/b.bar', u'foo/a.b.bar', u'foo/.bar'],
267
[u'foo.py.~1~', u'.foo.py.~1~'],
271
def test_end_anchor(self):
281
def test_mixed_globs(self):
282
"""tests handling of combinations of path type matches.
284
The types being extension, basename and full path.
286
patterns = [ u'*.foo', u'.*.swp', u'./*.png']
287
globster = Globster(patterns)
288
self.assertEqual(u'*.foo', globster.match('bar.foo'))
289
self.assertEqual(u'./*.png', globster.match('foo.png'))
290
self.assertEqual(None, globster.match('foo/bar.png'))
291
self.assertEqual(u'.*.swp', globster.match('foo/.bar.py.swp'))
293
def test_large_globset(self):
294
"""tests that the globster can handle a large set of patterns.
296
Large is defined as more than supported by python regex groups,
298
This test assumes the globs are broken into regexs containing 99
301
patterns = [ u'*.%03d' % i for i in xrange(0,300) ]
302
globster = Globster(patterns)
303
# test the fence posts
304
for x in (0,98,99,197,198,296,297,299):
305
filename = u'foo.%03d' % x
306
self.assertEqual(patterns[x],globster.match(filename))
307
self.assertEqual(None,globster.match('foobar.300'))
310
class TestOrderedGlobster(TestCase):
312
def test_ordered_globs(self):
313
"""test that the first match in a list is the one found"""
314
patterns = [ u'*.foo', u'bar.*']
315
globster = _OrderedGlobster(patterns)
316
self.assertEqual(u'*.foo', globster.match('bar.foo'))
317
self.assertEqual(None, globster.match('foo.bar'))
318
globster = _OrderedGlobster(reversed(patterns))
319
self.assertEqual(u'bar.*', globster.match('bar.foo'))
320
self.assertEqual(None, globster.match('foo.bar'))