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 (
21
from bzrlib.tests import (
27
class TestGlobster(TestCase):
29
def assertMatch(self, matchset, glob_prefix=None):
30
for glob, positive, negative in matchset:
32
glob = glob_prefix + glob
33
globster = Globster([glob])
35
self.failUnless(globster.match(name), repr(
36
u'name "%s" does not match glob "%s" (re=%s)' %
37
(name, glob, globster._regex_patterns[0][0].pattern)))
39
self.failIf(globster.match(name), repr(
40
u'name "%s" does match glob "%s" (re=%s)' %
41
(name, glob, globster._regex_patterns[0][0].pattern)))
43
def assertMatchBasenameAndFullpath(self, matchset):
44
# test basename matcher
45
self.assertMatch(matchset)
46
# test fullpath matcher
47
self.assertMatch(matchset, glob_prefix='./')
49
def test_char_group_digit(self):
50
self.assertMatchBasenameAndFullpath([
51
# The definition of digit this uses includes arabic digits from
52
# non-latin scripts (arabic, indic, etc.) and subscript/superscript
53
# digits, but neither roman numerals nor vulgar fractions.
55
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9'],
56
[u'T', u'q', u' ', u'\u8336', u'.']),
58
[u'T', u'q', u' ', u'\u8336', u'.'],
59
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9']),
62
def test_char_group_space(self):
63
self.assertMatchBasenameAndFullpath([
65
[u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002'],
66
[u'a', u'-', u'\u8336', u'.']),
68
[u'a', u'-', u'\u8336', u'.'],
69
[u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002']),
72
def test_char_group_alnum(self):
73
self.assertMatchBasenameAndFullpath([
75
[u'a', u'Z', u'\u017e', u'\u8336'],
76
[u':', u'-', u'\u25cf', u'.']),
78
[u':', u'-', u'\u25cf', u'.'],
82
def test_char_group_ascii(self):
83
self.assertMatchBasenameAndFullpath([
85
[u'a', u'Q', u'^', u'.'],
86
[u'\xcc', u'\u8336']),
89
[u'a', u'Q', u'^', u'.']),
92
def test_char_group_blank(self):
93
self.assertMatchBasenameAndFullpath([
96
[u'x', u'y', u'z', u'.']),
98
[u'x', u'y', u'z', u'.'],
102
def test_char_group_cntrl(self):
103
self.assertMatchBasenameAndFullpath([
105
[u'\b', u'\t', '\x7f'],
106
[u'a', u'Q', u'\u8336', u'.']),
108
[u'a', u'Q', u'\u8336', u'.'],
109
[u'\b', u'\t', '\x7f']),
112
def test_char_group_range(self):
113
self.assertMatchBasenameAndFullpath([
122
[u'afoo', u'ABfoo']),
124
[u'fooAbar', u'foo.bar'],
126
(u'[\x20-\x30\u8336]',
127
[u'\040', u'\044', u'\u8336'],
129
(u'[^\x20-\x30\u8336]',
131
[u'\040', u'\044', u'\u8336']),
134
def test_regex(self):
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
(u'RE:(?P<a>.)(?P=a)',
144
[u'ab', u'aa', u'aaa']),
145
# test we can handle odd numbers of trailing backslashes
148
[u'a', u'ab', u'aa', u'aaa']),
151
def test_question_mark(self):
154
[u'xfoo', u'bar/xfoo', u'bar/\u8336foo', u'.foo', u'bar/.foo'],
155
[u'bar/foo', u'foo']),
157
[u'fooxbar', u'foo.bar', u'foo\u8336bar', u'qyzzy/foo.bar'],
160
[u'foo/xbar', u'foo/\u8336bar', u'foo/.bar'],
161
[u'foo/bar', u'bar/foo/xbar']),
164
def test_asterisk(self):
167
[u'xx', u'x.x', u'x\u8336..x', u'\u8336/x.x', u'x.y.x'],
168
[u'x/x', u'bar/x/bar/x', u'bax/abaxab']),
170
[u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
173
[u'\u8336/x', u'foo/x', u'foo/bax', u'x/a.x', u'.foo/x',
174
u'\u8336/.x', u'foo/.q.x'],
177
[u'foo', u'foo.bar'],
178
[u'.foo', u'foo/bar', u'foo/.bar']),
180
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
181
u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
185
def test_double_asterisk(self):
187
# expected uses of double asterisk
189
[u'foo/x', u'foo/bar/x'],
190
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
192
[u'bar', u'foo/bar'],
193
[u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
194
u'.bar', u'foo/.bar']),
195
# check that we ignore extra *s, so *** is treated like ** not *.
197
[u'foo/x', u'foo/bar/x'],
198
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
200
[u'bar', u'foo/bar'],
201
[u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
202
u'.bar', u'foo/.bar']),
203
# the remaining tests check that ** is interpreted as *
204
# unless it is a whole path component
206
[u'x\u8336/x', u'x/x'],
207
[u'xx', u'x.x', u'bar/x/bar/x', u'x.y.x', u'x/y/x']),
209
[u'xx', u'x.x', u'x\u8336..x', u'foo/x.x', u'x.y.x'],
210
[u'bar/x/bar/x', u'xfoo/bar/x', u'x/x', u'bax/abaxab']),
212
[u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
215
[u'foo', u'foo.bar'],
216
[u'.foo', u'foo/bar', u'foo/.bar']),
218
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
219
u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
223
def test_leading_dot_slash(self):
227
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
230
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
233
def test_backslash(self):
237
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
240
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
242
[u'foo/x', u'foo/bar/x'],
243
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
246
def test_trailing_slash(self):
250
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
253
[u'foo/', u'\u8336/foo', u'barfoo', u'x/y/foo']),
256
def test_leading_asterisk_dot(self):
259
[u'foo/bar/baz.x', u'\u8336/Q.x', u'foo.y.x', u'.foo.x',
260
u'bar/.foo.x', u'.x',],
263
[u'foo/b.bar', u'foo/a.b.bar', u'foo/.bar'],
266
[u'foo.py.~1~', u'.foo.py.~1~'],
270
def test_end_anchor(self):
280
def test_mixed_globs(self):
281
"""tests handling of combinations of path type matches.
283
The types being extension, basename and full path.
285
patterns = [ u'*.foo', u'.*.swp', u'./*.png']
286
globster = Globster(patterns)
287
self.assertEqual(u'*.foo', globster.match('bar.foo'))
288
self.assertEqual(u'./*.png', globster.match('foo.png'))
289
self.assertEqual(None, globster.match('foo/bar.png'))
290
self.assertEqual(u'.*.swp', globster.match('foo/.bar.py.swp'))
292
def test_large_globset(self):
293
"""tests that the globster can handle a large set of patterns.
295
Large is defined as more than supported by python regex groups,
297
This test assumes the globs are broken into regexs containing 99
300
patterns = [ u'*.%03d' % i for i in xrange(0,300) ]
301
globster = Globster(patterns)
302
# test the fence posts
303
for x in (0,98,99,197,198,296,297,299):
304
filename = u'foo.%03d' % x
305
self.assertEqual(patterns[x],globster.match(filename))
306
self.assertEqual(None,globster.match('foobar.300'))