~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# Copyright (C) 2006-2010 Canonical Ltd
# -*- coding: utf-8 -*-
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import re

from bzrlib import errors
from bzrlib.globbing import (
    Globster,
    ExceptionGlobster,
    _OrderedGlobster,
    normalize_pattern
    )
from bzrlib.tests import (
    TestCase,
    TestCaseInTempDir,
    )


class TestGlobster(TestCase):

    def assertMatch(self, matchset, glob_prefix=None):
        for glob, positive, negative in matchset:
            if glob_prefix:
                glob = glob_prefix + glob
            globster = Globster([glob])
            for name in positive:
                self.failUnless(globster.match(name), repr(
                    u'name "%s" does not match glob "%s" (re=%s)' %
                    (name, glob, globster._regex_patterns[0][0].pattern)))
            for name in negative:
                self.failIf(globster.match(name), repr(
                    u'name "%s" does match glob "%s" (re=%s)' %
                    (name, glob, globster._regex_patterns[0][0].pattern)))

    def assertMatchBasenameAndFullpath(self, matchset):
        # test basename matcher
        self.assertMatch(matchset)
        # test fullpath matcher
        self.assertMatch(matchset, glob_prefix='./')

    def test_char_group_digit(self):
        self.assertMatchBasenameAndFullpath([
            # The definition of digit this uses includes arabic digits from
            # non-latin scripts (arabic, indic, etc.) but neither roman
            # numerals nor vulgar fractions. Some characters such as
            # subscript/superscript digits may or may not match depending on
            # the Python version used, see: <http://bugs.python.org/issue6561>
            (u'[[:digit:]]',
             [u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21'],
             [u'T', u'q', u' ', u'\u8336', u'.']),
            (u'[^[:digit:]]',
             [u'T', u'q', u' ', u'\u8336', u'.'],
             [u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21']),
            ])

    def test_char_group_space(self):
        self.assertMatchBasenameAndFullpath([
            (u'[[:space:]]',
             [u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002'],
             [u'a', u'-', u'\u8336', u'.']),
            (u'[^[:space:]]',
             [u'a', u'-', u'\u8336', u'.'],
             [u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002']),
            ])

    def test_char_group_alnum(self):
        self.assertMatchBasenameAndFullpath([
            (u'[[:alnum:]]',
             [u'a', u'Z', u'\u017e', u'\u8336'],
             [u':', u'-', u'\u25cf', u'.']),
            (u'[^[:alnum:]]',
             [u':', u'-', u'\u25cf', u'.'],
             [u'a']),
            ])

    def test_char_group_ascii(self):
        self.assertMatchBasenameAndFullpath([
            (u'[[:ascii:]]',
             [u'a', u'Q', u'^', u'.'],
             [u'\xcc', u'\u8336']),
            (u'[^[:ascii:]]',
             [u'\xcc', u'\u8336'],
             [u'a', u'Q', u'^', u'.']),
            ])

    def test_char_group_blank(self):
        self.assertMatchBasenameAndFullpath([
            (u'[[:blank:]]',
             [u'\t'],
             [u'x', u'y', u'z', u'.']),
            (u'[^[:blank:]]',
             [u'x', u'y', u'z', u'.'],
             [u'\t']),
            ])

    def test_char_group_cntrl(self):
        self.assertMatchBasenameAndFullpath([
            (u'[[:cntrl:]]',
             [u'\b', u'\t', '\x7f'],
             [u'a', u'Q', u'\u8336', u'.']),
            (u'[^[:cntrl:]]',
             [u'a', u'Q', u'\u8336', u'.'],
             [u'\b', u'\t', '\x7f']),
            ])

    def test_char_group_range(self):
        self.assertMatchBasenameAndFullpath([
            (u'[a-z]',
             [u'a', u'q', u'f'],
             [u'A', u'Q', u'F']),
            (u'[^a-z]',
             [u'A', u'Q', u'F'],
             [u'a', u'q', u'f']),
            (u'[!a-z]foo',
             [u'Afoo', u'.foo'],
             [u'afoo', u'ABfoo']),
            (u'foo[!a-z]bar',
             [u'fooAbar', u'foo.bar'],
             [u'foojbar']),
            (u'[\x20-\x30\u8336]',
             [u'\040', u'\044', u'\u8336'],
             [u'\x1f']),
            (u'[^\x20-\x30\u8336]',
             [u'\x1f'],
             [u'\040', u'\044', u'\u8336']),
            ])

    def test_regex(self):
        self.assertMatch([
            (u'RE:(a|b|c+)',
             [u'a', u'b', u'ccc'],
             [u'd', u'aa', u'c+', u'-a']),
            (u'RE:(?:a|b|c+)',
             [u'a', u'b', u'ccc'],
             [u'd', u'aa', u'c+', u'-a']),
            (u'RE:(?P<a>.)(?P=a)',
             [u'a'],
             [u'ab', u'aa', u'aaa']),
            # test we can handle odd numbers of trailing backslashes
            (u'RE:a\\\\\\',
             [u'a\\'],
             [u'a', u'ab', u'aa', u'aaa']),
            ])

    def test_question_mark(self):
        self.assertMatch([
            (u'?foo',
             [u'xfoo', u'bar/xfoo', u'bar/\u8336foo', u'.foo', u'bar/.foo'],
             [u'bar/foo', u'foo']),
            (u'foo?bar',
             [u'fooxbar', u'foo.bar', u'foo\u8336bar', u'qyzzy/foo.bar'],
             [u'foo/bar']),
            (u'foo/?bar',
             [u'foo/xbar', u'foo/\u8336bar', u'foo/.bar'],
             [u'foo/bar', u'bar/foo/xbar']),
            ])

    def test_asterisk(self):
        self.assertMatch([
            (u'x*x',
             [u'xx', u'x.x', u'x\u8336..x', u'\u8336/x.x', u'x.y.x'],
             [u'x/x', u'bar/x/bar/x', u'bax/abaxab']),
            (u'foo/*x',
             [u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
             [u'foo/bar/bax']),
            (u'*/*x',
             [u'\u8336/x', u'foo/x', u'foo/bax', u'x/a.x', u'.foo/x',
              u'\u8336/.x', u'foo/.q.x'],
             [u'foo/bar/bax']),
            (u'f*',
             [u'foo', u'foo.bar'],
             [u'.foo', u'foo/bar', u'foo/.bar']),
            (u'*bar',
             [u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
              u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
             []),
            ])

    def test_double_asterisk(self):
        self.assertMatch([
            # expected uses of double asterisk
            (u'foo/**/x',
             [u'foo/x', u'foo/bar/x'],
             [u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
            (u'**/bar',
             [u'bar', u'foo/bar'],
             [u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
              u'.bar', u'foo/.bar']),
            # check that we ignore extra *s, so *** is treated like ** not *.
            (u'foo/***/x',
             [u'foo/x', u'foo/bar/x'],
             [u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
            (u'***/bar',
             [u'bar', u'foo/bar'],
             [u'foobar', u'foo.bar', u'foo/foobar', u'foo/f.bar',
              u'.bar', u'foo/.bar']),
            # the remaining tests check that ** is interpreted as *
            # unless it is a whole path component
            (u'x**/x',
             [u'x\u8336/x', u'x/x'],
             [u'xx', u'x.x', u'bar/x/bar/x', u'x.y.x', u'x/y/x']),
            (u'x**x',
             [u'xx', u'x.x', u'x\u8336..x', u'foo/x.x', u'x.y.x'],
             [u'bar/x/bar/x', u'xfoo/bar/x', u'x/x', u'bax/abaxab']),
            (u'foo/**x',
             [u'foo/x', u'foo/bax', u'foo/a.x', u'foo/.x', u'foo/.q.x'],
             [u'foo/bar/bax']),
            (u'f**',
             [u'foo', u'foo.bar'],
             [u'.foo', u'foo/bar', u'foo/.bar']),
            (u'**bar',
             [u'bar', u'foobar', ur'foo\nbar', u'foo.bar', u'foo/bar',
              u'foo/foobar', u'foo/f.bar', u'.bar', u'foo/.bar'],
             []),
            ])

    def test_leading_dot_slash(self):
        self.assertMatch([
            (u'./foo',
             [u'foo'],
             [u'\u8336/foo', u'barfoo', u'x/y/foo']),
            (u'./f*',
             [u'foo'],
             [u'foo/bar', u'foo/.bar', u'x/foo/y']),
            ])

    def test_backslash(self):
        self.assertMatch([
            (u'.\\foo',
             [u'foo'],
             [u'\u8336/foo', u'barfoo', u'x/y/foo']),
            (u'.\\f*',
             [u'foo'],
             [u'foo/bar', u'foo/.bar', u'x/foo/y']),
            (u'foo\\**\\x',
             [u'foo/x', u'foo/bar/x'],
             [u'foox', u'foo/bax', u'foo/.x', u'foo/bar/bax']),
            ])

    def test_trailing_slash(self):
        self.assertMatch([
            (u'./foo/',
             [u'foo'],
             [u'\u8336/foo', u'barfoo', u'x/y/foo']),
            (u'.\\foo\\',
             [u'foo'],
             [u'foo/', u'\u8336/foo', u'barfoo', u'x/y/foo']),
            ])

    def test_leading_asterisk_dot(self):
        self.assertMatch([
            (u'*.x',
             [u'foo/bar/baz.x', u'\u8336/Q.x', u'foo.y.x', u'.foo.x',
              u'bar/.foo.x', u'.x',],
             [u'foo.x.y']),
            (u'foo/*.bar',
             [u'foo/b.bar', u'foo/a.b.bar', u'foo/.bar'],
             [u'foo/bar']),
            (u'*.~*',
             [u'foo.py.~1~', u'.foo.py.~1~'],
             []),
            ])

    def test_end_anchor(self):
        self.assertMatch([
            (u'*.333',
             [u'foo.333'],
             [u'foo.3']),
            (u'*.3',
             [u'foo.3'],
             [u'foo.333']),
            ])

    def test_mixed_globs(self):
        """tests handling of combinations of path type matches.

        The types being extension, basename and full path.
        """
        patterns = [ u'*.foo', u'.*.swp', u'./*.png']
        globster = Globster(patterns)
        self.assertEqual(u'*.foo', globster.match('bar.foo'))
        self.assertEqual(u'./*.png', globster.match('foo.png'))
        self.assertEqual(None, globster.match('foo/bar.png'))
        self.assertEqual(u'.*.swp', globster.match('foo/.bar.py.swp'))

    def test_large_globset(self):
        """tests that the globster can handle a large set of patterns.

        Large is defined as more than supported by python regex groups,
        i.e. 99.
        This test assumes the globs are broken into regexs containing 99
        groups.
        """
        patterns = [ u'*.%03d' % i for i in xrange(0,300) ]
        globster = Globster(patterns)
        # test the fence posts
        for x in (0,98,99,197,198,296,297,299):
            filename = u'foo.%03d' % x
            self.assertEqual(patterns[x],globster.match(filename))
        self.assertEqual(None,globster.match('foobar.300'))

    def test_bad_pattern(self):
        """Ensure that globster handles bad patterns cleanly."""
        patterns = [u'RE:[', u'/home/foo', u'RE:*.cpp']
        g = Globster(patterns)
        e = self.assertRaises(errors.InvalidPattern, g.match, 'filename')
        self.assertContainsRe(e.msg,
            "File.*ignore.*contains error.*RE:\[.*RE:\*\.cpp", flags=re.DOTALL)


class TestExceptionGlobster(TestCase):

    def test_exclusion_patterns(self):
        """test that exception patterns are not matched"""
        patterns = [ u'*', u'!./local', u'!./local/**/*', u'!RE:\.z.*',u'!!./.zcompdump' ]
        globster = ExceptionGlobster(patterns)
        self.assertEqual(u'*', globster.match('tmp/foo.txt'))
        self.assertEqual(None, globster.match('local'))
        self.assertEqual(None, globster.match('local/bin/wombat'))
        self.assertEqual(None, globster.match('.zshrc'))
        self.assertEqual(None, globster.match('.zfunctions/fiddle/flam'))
        self.assertEqual(u'!!./.zcompdump', globster.match('.zcompdump'))

    def test_exclusion_order(self):
        """test that ordering of exclusion patterns does not matter"""
        patterns = [ u'static/**/*.html', u'!static/**/versionable.html']
        globster = ExceptionGlobster(patterns)
        self.assertEqual(u'static/**/*.html', globster.match('static/foo.html'))
        self.assertEqual(None, globster.match('static/versionable.html'))
        self.assertEqual(None, globster.match('static/bar/versionable.html'))
        globster = ExceptionGlobster(reversed(patterns))
        self.assertEqual(u'static/**/*.html', globster.match('static/foo.html'))
        self.assertEqual(None, globster.match('static/versionable.html'))
        self.assertEqual(None, globster.match('static/bar/versionable.html'))

class TestOrderedGlobster(TestCase):

    def test_ordered_globs(self):
        """test that the first match in a list is the one found"""
        patterns = [ u'*.foo', u'bar.*']
        globster = _OrderedGlobster(patterns)
        self.assertEqual(u'*.foo', globster.match('bar.foo'))
        self.assertEqual(None, globster.match('foo.bar'))
        globster = _OrderedGlobster(reversed(patterns))
        self.assertEqual(u'bar.*', globster.match('bar.foo'))
        self.assertEqual(None, globster.match('foo.bar'))


class TestNormalizePattern(TestCase):

    def test_backslashes(self):
        """tests that backslashes are converted to forward slashes, multiple
        backslashes are collapsed to single forward slashes and trailing
        backslashes are removed"""
        self.assertEqual(u'/', normalize_pattern(u'\\'))
        self.assertEqual(u'/', normalize_pattern(u'\\\\'))
        self.assertEqual(u'/foo/bar', normalize_pattern(u'\\foo\\bar'))
        self.assertEqual(u'foo/bar', normalize_pattern(u'foo\\bar\\'))
        self.assertEqual(u'/foo/bar', normalize_pattern(u'\\\\foo\\\\bar\\\\'))

    def test_forward_slashes(self):
        """tests that multiple foward slashes are collapsed to single forward
        slashes and trailing forward slashes are removed"""
        self.assertEqual(u'/', normalize_pattern(u'/'))
        self.assertEqual(u'/', normalize_pattern(u'//'))
        self.assertEqual(u'/foo/bar', normalize_pattern(u'/foo/bar'))
        self.assertEqual(u'foo/bar', normalize_pattern(u'foo/bar/'))
        self.assertEqual(u'/foo/bar', normalize_pattern(u'//foo//bar//'))

    def test_mixed_slashes(self):
        """tests that multiple mixed slashes are collapsed to single forward
        slashes and trailing mixed slashes are removed"""
        self.assertEqual(u'/foo/bar', normalize_pattern(u'\\/\\foo//\\///bar/\\\\/'))