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.tests import TestCase, TestCaseInTempDir
20
from bzrlib.glob import (
25
class TestGlobster(TestCase):
27
def assertMatch(self, matchset, glob_prefix=None):
28
for glob, positive, negative in matchset:
30
glob = glob_prefix + glob
31
globster = Globster([glob])
33
self.failUnless(globster.match(name), repr(
34
u'name "%s" does not match glob "%s" (re=%s)' %
35
(name, glob, globster._regex_patterns[0][0].pattern)))
37
self.failIf(globster.match(name), repr(
38
u'name "%s" does match glob "%s" (re=%s)' %
39
(name, glob, globster._regex_patterns[0][0].pattern)))
41
def test_char_groups(self):
42
# The definition of digit this uses includes arabic digits from
43
# non-latin scripts (arabic, indic, etc.) and subscript/superscript
44
# digits, but neither roman numerals nor vulgar fractions.
47
[u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9'],
48
[u'T', u'q', u' ', u'\u8336', u'.']),
50
[u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002'],
51
[u'a', u'-', u'\u8336', u'.']),
53
[u'a', u'-', u'\u8336'],
54
[u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002', u'.']),
56
[u'a', u'Z', u'\u017e', u'\u8336'],
57
[u':', u'-', u'\u25cf', u'.']),
59
[u':', u'-', u'\u25cf'],
63
[u'\xcc', u'\u8336', u'.']),
66
[u'a', u'Q', u'^', u'.']),
69
[u'x', u'y', u'z', u'.']),
74
[u'\b', u'\t', '\x7f'],
75
[u'a', u'Q', u'\u8336', u'.']),
86
[u'fooAbar', u'foo.bar'],
88
(ur'[\x20-\x30\u8336]',
89
[u'\040', u'\044', u'\u8336'],
91
(ur'[^\x20-\x30\u8336]',
93
[u'\040', u'\044', u'\u8336']),
95
self.assertMatch(matchset)
96
self.assertMatch(matchset,glob_prefix='./')
101
[u'a', u'b', u'ccc'],
102
[u'd', u'aa', u'c+', u'-a']),
104
[u'a', u'b', u'ccc'],
105
[u'd', u'aa', u'c+', u'-a']),
106
(ur'RE:(?P<a>.)(?P=a)',
108
[u'ab', u'aa', u'aaa']),
111
def test_question_mark(self):
114
[u'xfoo', u'bar/xfoo', u'bar/\u8336foo'],
115
[u'.foo', u'bar/.foo', u'bar/foo', u'foo']),
117
[u'fooxbar', u'foo.bar', u'foo\u8336bar', u'qyzzy/foo.bar'],
120
[u'foo/xbar', u'foo/\u8336bar'],
121
[u'foo/.bar', u'foo/bar', u'bar/foo/xbar']),
124
def test_asterisk(self):
127
[u'xx', u'x.x', u'x\u8336..x', u'\u8336/x.x', u'x.y.x'],
128
[u'x/x', u'bar/x/bar/x', u'bax/abaxab']),
130
[u'foo/x', u'foo/bax', u'foo/a.x'],
131
[u'foo/.x', u'foo/.q.x', u'foo/bar/bax']),
133
[u'\u8336/x', u'foo/x', u'foo/bax', u'x/a.x'],
134
[u'.foo/x', u'\u8336/.x', u'foo/.q.x', u'foo/bar/bax']),
136
[u'foo', u'foo.bar'],
137
[u'.foo', u'foo/bar', u'foo/.bar']),
139
[u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
140
u'foo/foobar', u'foo/f.bar'],
141
[u'.bar', u'foo/.bar']),
144
def test_leading_dotslash(self):
148
[u'\u8336/foo', u'barfoo', u'x/y/foo']),
151
[u'foo/bar', u'foo/.bar', u'x/foo/y']),
154
def test_leading_stardot(self):
157
[u'foo/bar/baz.x', u'\u8336/Q.x', u'foo.y.x'],
158
[ u'.foo.x', u'bar/.foo.x', u'.x']),
160
[u'foo/b.bar', u'foo/a.b.bar'],
161
[u'foo/.bar', u'foo/bar']),
167
def test_end_anchor(self):
177
def test_mixed_globs(self):
178
"""tests handling of combinations of path type matches.
180
The types being extension, basename and full path.
182
patterns = [ u'*.foo', u'.*.swp', u'./*.png']
183
globster = Globster(patterns)
184
self.assertEqual(u'*.foo', globster.match('bar.foo'))
185
self.assertEqual(u'./*.png', globster.match('foo.png'))
186
self.assertEqual(None, globster.match('foo/bar.png'))
187
self.assertEqual(u'.*.swp', globster.match('foo/.bar.py.swp'))
189
def test_large_globset(self):
190
"""tests that the globster can handle a large set of patterns.
192
Large is defined as more than supported by python regex groups,
194
This test assumes the globs are broken into regexs containing 99
197
patterns = [ u'*.%03d' % i for i in xrange(0,300) ]
198
globster = Globster(patterns)
199
# test the fence posts
200
for x in (0,98,99,197,198,296,297,299):
201
filename = u'foo.%03d' % x
202
self.assertEqual(patterns[x],globster.match(filename))
203
self.assertEqual(None,globster.match('foobar.300'))