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