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
(ur'[\x20-\x30\u8336]',
127
[u'\040', u'\044', u'\u8336'],
129
(ur'[^\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
(ur'RE:(?P<a>.)(?P=a)',
144
[u'ab', u'aa', u'aaa']),
147
def test_question_mark(self):
150
[u'xfoo', u'bar/xfoo', u'bar/\u8336foo', u'.foo', u'bar/.foo'],
151
[u'bar/foo', u'foo']),
153
[u'fooxbar', u'foo.bar', u'foo\u8336bar', u'qyzzy/foo.bar'],
156
[u'foo/xbar', u'foo/\u8336bar', u'foo/.bar'],
157
[u'foo/bar', u'bar/foo/xbar']),
160
def test_asterisk(self):
163
[u'xx', u'x.x', u'x\u8336..x', u'\u8336/x.x', u'x.y.x'],
164
[u'x/x', u'bar/x/bar/x', u'bax/abaxab']),
166
[u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
169
[u'\u8336/x', u'foo/x', u'foo/bax', u'x/a.x', u'.foo/x',
170
u'\u8336/.x', u'foo/.q.x'],
173
[u'foo', u'foo.bar'],
174
[u'.foo', u'foo/bar', u'foo/.bar']),
176
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
177
u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
181
def test_double_asterisk(self):
183
# expected uses of double asterisk
185
[u'foo/x', u'foo/bar/x'],
186
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
188
[u'bar', u'foo/bar'],
189
[u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
190
u'.bar', u'foo/.bar']),
191
# check that we ignore extra *s, so *** is treated like ** not *.
193
[u'foo/x', u'foo/bar/x'],
194
[u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
196
[u'bar', u'foo/bar'],
197
[u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
198
u'.bar', u'foo/.bar']),
199
# the remaining tests check that ** is interpreted as *
200
# unless it is a whole path component
202
[u'x\u8336/x', u'x/x'],
203
[u'xx', u'x.x', u'bar/x/bar/x', u'x.y.x', u'x/y/x']),
205
[u'xx', u'x.x', u'x\u8336..x', u'foo/x.x', u'x.y.x'],
206
[u'bar/x/bar/x', u'xfoo/bar/x', u'x/x', u'bax/abaxab']),
208
[u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
211
[u'foo', u'foo.bar'],
212
[u'.foo', u'foo/bar', u'foo/.bar']),
214
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
215
u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
219
def test_leading_dot_slash(self):
223
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
226
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
229
def test_leading_asterisk_dot(self):
232
[u'foo/bar/baz.x', u'\u8336/Q.x', u'foo.y.x', u'.foo.x',
233
u'bar/.foo.x', u'.x',],
236
[u'foo/b.bar', u'foo/a.b.bar', u'foo/.bar'],
239
[u'foo.py.~1~', u'.foo.py.~1~'],
243
def test_end_anchor(self):
253
def test_mixed_globs(self):
254
"""tests handling of combinations of path type matches.
256
The types being extension, basename and full path.
258
patterns = [ u'*.foo', u'.*.swp', u'./*.png']
259
globster = Globster(patterns)
260
self.assertEqual(u'*.foo', globster.match('bar.foo'))
261
self.assertEqual(u'./*.png', globster.match('foo.png'))
262
self.assertEqual(None, globster.match('foo/bar.png'))
263
self.assertEqual(u'.*.swp', globster.match('foo/.bar.py.swp'))
265
def test_large_globset(self):
266
"""tests that the globster can handle a large set of patterns.
268
Large is defined as more than supported by python regex groups,
270
This test assumes the globs are broken into regexs containing 99
273
patterns = [ u'*.%03d' % i for i in xrange(0,300) ]
274
globster = Globster(patterns)
275
# test the fence posts
276
for x in (0,98,99,197,198,296,297,299):
277
filename = u'foo.%03d' % x
278
self.assertEqual(patterns[x],globster.match(filename))
279
self.assertEqual(None,globster.match('foobar.300'))