/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2006, 2007, 2009, 2010 Canonical Ltd
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
2
# Authors:  Robert Collins <robert.collins@canonical.com>
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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
17
5819.2.1 by Jelmer Vernooij
Fix imports.
18
from bzrlib import config, ignores
4523.1.4 by Martin Pool
Rename remaining *_implementations tests
19
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
20
21
22
class TestIsIgnored(TestCaseWithWorkingTree):
23
5339.3.7 by Parth Malwankar
fixed test_is_ignored test case
24
    def _set_user_ignore_content(self, ignores):
25
        """Create user ignore file and set its content to ignores."""
26
        config.ensure_config_dir_exists()
27
        user_ignore_file = config.user_ignore_config_filename()
28
        f = open(user_ignore_file, 'wb')
29
        try:
30
            f.write(ignores)
31
        finally:
32
            f.close()
33
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
34
    def test_is_ignored(self):
35
        tree = self.make_branch_and_tree('.')
36
        # this will break if a tree changes the ignored format. That is fine
37
        # because at the moment tree format is orthogonal to user data, and
38
        # .bzrignore is user data so must not be changed by a tree format.
39
        self.build_tree_contents([
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
40
            ('.bzrignore', './rootdir\n'
41
                           'randomfile*\n'
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
42
                           '*bar\n'
4948.5.1 by John Whitley
Implementation of ignore exclusions and basic tests for same.
43
                           '!bazbar\n'
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
44
                           '?foo\n'
45
                           '*.~*\n'
46
                           'dir1/*f1\n'
47
                           'dir1/?f2\n'
4948.5.1 by John Whitley
Implementation of ignore exclusions and basic tests for same.
48
                           'RE:dir2/.*\.wombat\n'
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
49
                           'path/from/ro?t\n'
4948.5.1 by John Whitley
Implementation of ignore exclusions and basic tests for same.
50
                           '**/piffle.py\n'
51
                           '!b/piffle.py\n'
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
52
                           'unicode\xc2\xb5\n' # u'\xb5'.encode('utf8')
53
                           'dos\r\n'
54
                           '\n' # empty line
55
                           '#comment\n'
56
                           ' xx \n' # whitespace
57
            )])
5339.3.8 by Parth Malwankar
updated NEWS; comment cleanup.
58
        # We set user ignore file to contain '' to avoid patterns from
59
        # user ignore being used instead of bzrignore. For .e.g. If we
60
        # don't do this 'foo.~1~' will match '*~' default user ignore
61
        # pattern instead of '*.~*' from bzr ignore as we expect below.
5339.3.7 by Parth Malwankar
fixed test_is_ignored test case
62
        self._set_user_ignore_content('')
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
63
        # is_ignored returns the matching ignore regex when a path is ignored.
64
        # we check some expected matches for each rule, and one or more
65
        # relevant not-matches that look plausible as cases for bugs.
66
        self.assertEqual('./rootdir', tree.is_ignored('rootdir'))
67
        self.assertEqual(None, tree.is_ignored('foo/rootdir'))
1713.2.3 by Robert Collins
Combine ignore rules into a single regex preventing pathological behaviour during add.
68
        self.assertEqual(None, tree.is_ignored('rootdirtrailer'))
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
69
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
70
        self.assertEqual('randomfile*', tree.is_ignored('randomfile'))
71
        self.assertEqual('randomfile*', tree.is_ignored('randomfiles'))
72
        self.assertEqual('randomfile*', tree.is_ignored('foo/randomfiles'))
73
        self.assertEqual(None, tree.is_ignored('randomfil'))
74
        self.assertEqual(None, tree.is_ignored('foo/randomfil'))
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
75
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
76
        self.assertEqual("path/from/ro?t", tree.is_ignored('path/from/root'))
77
        self.assertEqual("path/from/ro?t", tree.is_ignored('path/from/roat'))
78
        self.assertEqual(None, tree.is_ignored('roat'))
4948.5.1 by John Whitley
Implementation of ignore exclusions and basic tests for same.
79
        
80
        self.assertEqual('**/piffle.py', tree.is_ignored('piffle.py'))
81
        self.assertEqual('**/piffle.py', tree.is_ignored('a/piffle.py'))
82
        self.assertEqual(None, tree.is_ignored('b/piffle.py')) # exclusion
83
        self.assertEqual('**/piffle.py', tree.is_ignored('foo/bar/piffle.py'))
84
        self.assertEqual(None, tree.is_ignored('p/iffle.py'))
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
85
86
        self.assertEqual(u'unicode\xb5', tree.is_ignored(u'unicode\xb5'))
87
        self.assertEqual(u'unicode\xb5', tree.is_ignored(u'subdir/unicode\xb5'))
88
        self.assertEqual(None, tree.is_ignored(u'unicode\xe5'))
89
        self.assertEqual(None, tree.is_ignored(u'unicode'))
90
        self.assertEqual(None, tree.is_ignored(u'\xb5'))
91
92
        self.assertEqual('dos', tree.is_ignored('dos'))
93
        self.assertEqual(None, tree.is_ignored('dosfoo'))
94
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
95
        self.assertEqual('*bar', tree.is_ignored('foobar'))
96
        self.assertEqual('*bar', tree.is_ignored(r'foo\nbar'))
97
        self.assertEqual('*bar', tree.is_ignored('bar'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
98
        self.assertEqual('*bar', tree.is_ignored('.bar'))
4948.5.1 by John Whitley
Implementation of ignore exclusions and basic tests for same.
99
        
100
        self.assertEqual(None, tree.is_ignored('bazbar')) # exclusion
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
101
102
        self.assertEqual('?foo', tree.is_ignored('afoo'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
103
        self.assertEqual('?foo', tree.is_ignored('.foo'))
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
104
105
        self.assertEqual('*.~*', tree.is_ignored('blah.py.~1~'))
106
107
        self.assertEqual('dir1/*f1', tree.is_ignored('dir1/foof1'))
108
        self.assertEqual('dir1/*f1', tree.is_ignored('dir1/f1'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
109
        self.assertEqual('dir1/*f1', tree.is_ignored('dir1/.f1'))
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
110
111
        self.assertEqual('dir1/?f2', tree.is_ignored('dir1/ff2'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
112
        self.assertEqual('dir1/?f2', tree.is_ignored('dir1/.f2'))
4948.5.1 by John Whitley
Implementation of ignore exclusions and basic tests for same.
113
        
114
        self.assertEqual('RE:dir2/.*\.wombat', tree.is_ignored('dir2/foo.wombat'))
115
        self.assertEqual(None, tree.is_ignored('dir2/foo'))
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
116
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
117
        # Blank lines and comments should be ignored
118
        self.assertEqual(None, tree.is_ignored(''))
119
        self.assertEqual(None, tree.is_ignored('test/'))
120
121
        self.assertEqual(None, tree.is_ignored('#comment'))
122
123
        # Whitespace should not be stripped
124
        self.assertEqual(' xx ', tree.is_ignored(' xx '))
125
        self.assertEqual(' xx ', tree.is_ignored('subdir/ xx '))
126
        self.assertEqual(None, tree.is_ignored('xx'))
127
        self.assertEqual(None, tree.is_ignored('xx '))
128
        self.assertEqual(None, tree.is_ignored(' xx'))
129
        self.assertEqual(None, tree.is_ignored('subdir/xx '))
130
131
    def test_global_ignored(self):
132
        tree = self.make_branch_and_tree('.')
133
134
        config.ensure_config_dir_exists()
1836.1.6 by John Arbash Meinel
Creating a helper function for getting the user ignore filename
135
        user_ignore_file = config.user_ignore_config_filename()
5339.3.7 by Parth Malwankar
fixed test_is_ignored test case
136
        self._set_user_ignore_content(
137
            '*.py[co]\n'
138
            './.shelf\n'
139
            '# comment line\n'
140
            '\n' #Blank line
141
            '\r\n' #Blank dos line
142
            ' * \n' #Trailing and suffix spaces
143
            'crlf\r\n' # dos style line
144
            '*\xc3\xa5*\n' # u'\xe5'.encode('utf8')
145
            )
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
146
147
        # Rooted
148
        self.assertEqual('./.shelf', tree.is_ignored('.shelf'))
149
        self.assertEqual(None, tree.is_ignored('foo/.shelf'))
150
151
        # Glob style
152
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
153
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyo'))
154
        self.assertEqual(None, tree.is_ignored('foo.py'))
155
156
        # Glob in subdir
157
        self.assertEqual('*.py[co]', tree.is_ignored('bar/foo.pyc'))
158
        self.assertEqual('*.py[co]', tree.is_ignored('bar/foo.pyo'))
159
        self.assertEqual(None, tree.is_ignored('bar/foo.py'))
160
161
        # Unicode
162
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b\xe5gfors'))
163
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'\xe5gfors'))
164
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'\xe5'))
165
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b\xe5'))
166
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b/\xe5'))
167
168
        # Whitespace
169
        self.assertEqual(' * ', tree.is_ignored(' bbb '))
170
        self.assertEqual(' * ', tree.is_ignored('subdir/ bbb '))
171
        self.assertEqual(None, tree.is_ignored('bbb '))
172
        self.assertEqual(None, tree.is_ignored(' bbb'))
173
174
        # Dos lines
175
        self.assertEqual('crlf', tree.is_ignored('crlf'))
176
        self.assertEqual('crlf', tree.is_ignored('subdir/crlf'))
177
178
        # Comment line should be ignored
179
        self.assertEqual(None, tree.is_ignored('# comment line'))
180
181
        # Blank line should also be ignored
182
        self.assertEqual(None, tree.is_ignored(''))
183
        self.assertEqual(None, tree.is_ignored('baz/'))
1836.1.20 by John Arbash Meinel
Make sure that mixing global and local ignores work
184
185
    def test_mixed_is_ignored(self):
186
        tree = self.make_branch_and_tree('.')
1836.1.31 by John Arbash Meinel
Make set_user_ignores a private function, and update the doc string to recommend it isn't used.
187
        ignores._set_user_ignores(['*.py[co]', './.shelf'])
2135.2.3 by Kent Gibson
Revert unnecessary change to test_mixed_is_ignored.
188
        self.build_tree_contents([('.bzrignore', './rootdir\n*.swp\n')])
1836.1.20 by John Arbash Meinel
Make sure that mixing global and local ignores work
189
190
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
191
        self.assertEqual('./.shelf', tree.is_ignored('.shelf'))
192
        self.assertEqual('./rootdir', tree.is_ignored('rootdir'))
2135.2.3 by Kent Gibson
Revert unnecessary change to test_mixed_is_ignored.
193
        self.assertEqual('*.swp', tree.is_ignored('foo.py.swp'))
194
        self.assertEqual('*.swp', tree.is_ignored('.foo.py.swp'))
1836.1.20 by John Arbash Meinel
Make sure that mixing global and local ignores work
195
        self.assertEqual(None, tree.is_ignored('.foo.py.swo'))
1836.1.21 by John Arbash Meinel
Restore the ability to ignore items by modifying DEFAULT_IGNORE
196
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
197
    def test_runtime_ignores(self):
198
        tree = self.make_branch_and_tree('.')
199
        self.build_tree_contents([('.bzrignore', '')])
1836.1.31 by John Arbash Meinel
Make set_user_ignores a private function, and update the doc string to recommend it isn't used.
200
        ignores._set_user_ignores([])
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
201
202
        orig_runtime = ignores._runtime_ignores
203
        try:
204
            ignores._runtime_ignores = set()
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
205
            self.assertEqual(None, tree.is_ignored('foobar.py'))
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
206
2135.2.7 by Kent Gibson
Implement JAM's review suggestions.
207
            tree._flush_ignore_list_cache()
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
208
            ignores.add_runtime_ignores(['./foobar.py'])
209
            self.assertEqual(set(['./foobar.py']), ignores.get_runtime_ignores())
210
            self.assertEqual('./foobar.py', tree.is_ignored('foobar.py'))
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
211
        finally:
212
            ignores._runtime_ignores = orig_runtime
2665.3.1 by Daniel Watkins
Added test to ensure that WorkingTree.unknowns() will return an accurate list of unknown files (by way of ensuring that the cached list of ignore definitions in WorkingTree is kept up to date).
213
214
    def test_ignore_caching(self):
215
        tree = self.make_branch_and_tree('.')
216
        self.build_tree(['ignoreme'])
217
218
        self.assertEqual(None, tree.is_ignored('ignoreme'))
219
220
        # Bug #129694 specifically references WorkingTree.unknowns()
221
        tree.unknowns()
222
223
        self.build_tree_contents([('.bzrignore', 'ignoreme')])
224
        self.assertEqual('ignoreme', tree.is_ignored('ignoreme'))