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