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