/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_glob.py

  • Committer: Kent Gibson
  • Date: 2006-11-13 14:13:56 UTC
  • mto: (2178.1.1 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 2179.
  • Revision ID: warthog618@gmail.com-20061113141356-kdfo22erpow15g4k
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Canonical Ltd
 
2
# -*- coding: utf-8 -*-
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
from bzrlib.tests import TestCase, TestCaseInTempDir
 
19
 
 
20
from bzrlib.glob import (
 
21
        Globster
 
22
        )
 
23
 
 
24
 
 
25
class TestGlobster(TestCase):
 
26
 
 
27
    def assertMatch(self, matchset, glob_prefix=None):
 
28
        for glob, positive, negative in matchset:
 
29
            if glob_prefix:
 
30
                glob = glob_prefix + glob
 
31
            globster = Globster([glob])
 
32
            for name in positive:
 
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)))
 
36
            for name in negative:
 
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)))
 
40
 
 
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.
 
45
        matchset = [
 
46
            (u'[[:digit:]]',
 
47
             [u'0', u'5', u'\u0663', u'\u06f9', u'\u0f21', u'\xb9'],
 
48
             [u'T', u'q', u' ', u'\u8336', u'.']),
 
49
            (u'[[:space:]]',
 
50
             [u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002'],
 
51
             [u'a', u'-', u'\u8336', u'.']),
 
52
            (u'[^[:space:]]',
 
53
             [u'a', u'-', u'\u8336'],
 
54
             [u' ', u'\t', u'\n', u'\xa0', u'\u2000', u'\u2002', u'.']),
 
55
            (u'[[:alnum:]]',
 
56
             [u'a', u'Z', u'\u017e', u'\u8336'],
 
57
             [u':', u'-', u'\u25cf', u'.']),
 
58
            (u'[^[:alnum:]]',
 
59
             [u':', u'-', u'\u25cf'],
 
60
             [u'a', u'.']),
 
61
            (u'[[:ascii:]]',
 
62
             [u'a', u'Q', u'^'],
 
63
             [u'\xcc', u'\u8336', u'.']),
 
64
            (u'[^[:ascii:]]',
 
65
             [u'\xcc', u'\u8336'],
 
66
             [u'a', u'Q', u'^', u'.']),
 
67
            (u'[[:blank:]]',
 
68
             [u'\t'],
 
69
             [u'x', u'y', u'z', u'.']),
 
70
            (u'[^[:blank:]]',
 
71
             [u'x', u'y', u'z'],
 
72
             [u'\t', u'.']),
 
73
            (u'[[:cntrl:]]',
 
74
             [u'\b', u'\t', '\x7f'],
 
75
             [u'a', u'Q', u'\u8336', u'.']),
 
76
            (u'[a-z]',
 
77
             [u'a', u'q', u'f'],
 
78
             [u'A', u'Q', u'F']),
 
79
            (ur'[^a-z]',
 
80
             [u'A', u'Q', u'F'],
 
81
             [u'a', u'q', u'f']),
 
82
            (u'[!a-z]foo',
 
83
             [u'Afoo'],
 
84
             [u'.foo']),
 
85
            (ur'foo[!a-z]bar',
 
86
             [u'fooAbar', u'foo.bar'],
 
87
             [u'foojbar']),
 
88
            (ur'[\x20-\x30\u8336]',
 
89
             [u'\040', u'\044', u'\u8336'],
 
90
             []),
 
91
            (ur'[^\x20-\x30\u8336]',
 
92
             [],
 
93
             [u'\040', u'\044', u'\u8336']),
 
94
            ]
 
95
        self.assertMatch(matchset)
 
96
        self.assertMatch(matchset,glob_prefix='./')
 
97
 
 
98
    def test_regex(self):
 
99
        self.assertMatch([
 
100
            (ur'RE:(a|b|c+)',
 
101
             [u'a', u'b', u'ccc'],
 
102
             [u'd', u'aa', u'c+', u'-a']),
 
103
            (ur'RE:(?:a|b|c+)',
 
104
             [u'a', u'b', u'ccc'],
 
105
             [u'd', u'aa', u'c+', u'-a']),
 
106
            (ur'RE:(?P<a>.)(?P=a)',
 
107
             [u'a'],
 
108
             [u'ab', u'aa', u'aaa']),
 
109
            ])
 
110
 
 
111
    def test_question_mark(self):
 
112
        self.assertMatch([
 
113
            (u'?foo',
 
114
             [u'xfoo', u'bar/xfoo', u'bar/\u8336foo'],
 
115
             [u'.foo', u'bar/.foo', u'bar/foo', u'foo']),
 
116
            (u'foo?bar',
 
117
             [u'fooxbar', u'foo.bar', u'foo\u8336bar', u'qyzzy/foo.bar'],
 
118
             [u'foo/bar']),
 
119
            (u'foo/?bar',
 
120
             [u'foo/xbar', u'foo/\u8336bar'],
 
121
             [u'foo/.bar', u'foo/bar', u'bar/foo/xbar']),
 
122
            ])
 
123
 
 
124
    def test_asterisk(self):
 
125
        self.assertMatch([
 
126
            (u'x*x',
 
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']),
 
129
            (u'foo/*x',
 
130
             [u'foo/x', u'foo/bax', u'foo/a.x'],
 
131
             [u'foo/.x', u'foo/.q.x', u'foo/bar/bax']),
 
132
            (u'*/*x',
 
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']),
 
135
            (u'f*',
 
136
             [u'foo', u'foo.bar'],
 
137
             [u'.foo', u'foo/bar', u'foo/.bar']),
 
138
            (u'*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']),
 
142
            ])
 
143
 
 
144
    def test_leading_dotslash(self):
 
145
        self.assertMatch([
 
146
            (u'./foo',
 
147
             [u'foo'],
 
148
             [u'\u8336/foo', u'barfoo', u'x/y/foo']),
 
149
            (u'./f*',
 
150
             [u'foo'],
 
151
             [u'foo/bar', u'foo/.bar', u'x/foo/y']),
 
152
            ])
 
153
 
 
154
    def test_leading_stardot(self):
 
155
        self.assertMatch([
 
156
            (u'*.x',
 
157
             [u'foo/bar/baz.x', u'\u8336/Q.x', u'foo.y.x'],
 
158
             [ u'.foo.x', u'bar/.foo.x', u'.x']),
 
159
            (u'foo/*.bar',
 
160
             [u'foo/b.bar', u'foo/a.b.bar'],
 
161
             [u'foo/.bar', u'foo/bar']),
 
162
            (u'*.~*',
 
163
             [u'foo.py.~1~'],
 
164
             [u'.foo.py.~1~']),
 
165
            ])
 
166
 
 
167
    def test_end_anchor(self):
 
168
        self.assertMatch([
 
169
            (u'*.333',
 
170
             [u'foo.333'],
 
171
             [u'foo.3']),
 
172
            (u'*.3',
 
173
             [u'foo.3'],
 
174
             [u'foo.333']),
 
175
            ])
 
176
 
 
177
    def test_mixed_globs(self):
 
178
        """tests handling of combinations of path type matches.
 
179
 
 
180
        The types being extension, basename and full path.
 
181
        """
 
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'))
 
188
 
 
189
    def test_large_globset(self):
 
190
        """tests that the globster can handle a large set of patterns.
 
191
 
 
192
        Large is defined as more than supported by python regex groups, 
 
193
        i.e. 99.
 
194
        This test assumes the globs are broken into regexs containing 99
 
195
        groups.
 
196
        """
 
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'))
 
204