/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([
6855.4.1 by Jelmer Vernooij
Yet more bees.
43
            ('.bzrignore', b'./rootdir\n'
44
                           b'randomfile*\n'
45
                           b'*bar\n'
46
                           b'!bazbar\n'
47
                           b'?foo\n'
48
                           b'*.~*\n'
49
                           b'dir1/*f1\n'
50
                           b'dir1/?f2\n'
51
                           b'RE:dir2/.*\\.wombat\n'
52
                           b'path/from/ro?t\n'
53
                           b'**/piffle.py\n'
54
                           b'!b/piffle.py\n'
7143.15.2 by Jelmer Vernooij
Run autopep8.
55
                           b'unicode\xc2\xb5\n'  # u'\xb5'.encode('utf8')
6855.4.1 by Jelmer Vernooij
Yet more bees.
56
                           b'dos\r\n'
7143.15.2 by Jelmer Vernooij
Run autopep8.
57
                           b'\n'  # empty line
6855.4.1 by Jelmer Vernooij
Yet more bees.
58
                           b'#comment\n'
7143.15.2 by Jelmer Vernooij
Run autopep8.
59
                           b' 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.
6973.10.1 by Jelmer Vernooij
Fix some tests.
65
        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.
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'))
7143.15.2 by Jelmer Vernooij
Run autopep8.
85
        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.
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'))
7143.15.2 by Jelmer Vernooij
Run autopep8.
90
        self.assertEqual(
91
            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
92
        self.assertEqual(None, tree.is_ignored(u'unicode\xe5'))
93
        self.assertEqual(None, tree.is_ignored(u'unicode'))
94
        self.assertEqual(None, tree.is_ignored(u'\xb5'))
95
96
        self.assertEqual('dos', tree.is_ignored('dos'))
97
        self.assertEqual(None, tree.is_ignored('dosfoo'))
98
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
99
        self.assertEqual('*bar', tree.is_ignored('foobar'))
100
        self.assertEqual('*bar', tree.is_ignored(r'foo\nbar'))
101
        self.assertEqual('*bar', tree.is_ignored('bar'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
102
        self.assertEqual('*bar', tree.is_ignored('.bar'))
6876.4.1 by Jelmer Vernooij
Add format setting with ignore filename.
103
7143.15.2 by Jelmer Vernooij
Run autopep8.
104
        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)
105
106
        self.assertEqual('?foo', tree.is_ignored('afoo'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
107
        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)
108
109
        self.assertEqual('*.~*', tree.is_ignored('blah.py.~1~'))
110
111
        self.assertEqual('dir1/*f1', tree.is_ignored('dir1/foof1'))
112
        self.assertEqual('dir1/*f1', tree.is_ignored('dir1/f1'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
113
        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)
114
115
        self.assertEqual('dir1/?f2', tree.is_ignored('dir1/ff2'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
116
        self.assertEqual('dir1/?f2', tree.is_ignored('dir1/.f2'))
6876.4.1 by Jelmer Vernooij
Add format setting with ignore filename.
117
7143.15.2 by Jelmer Vernooij
Run autopep8.
118
        self.assertEqual('RE:dir2/.*\\.wombat',
119
                         tree.is_ignored('dir2/foo.wombat'))
4948.5.1 by John Whitley
Implementation of ignore exclusions and basic tests for same.
120
        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)
121
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
122
        # Blank lines and comments should be ignored
123
        self.assertEqual(None, tree.is_ignored(''))
124
        self.assertEqual(None, tree.is_ignored('test/'))
125
126
        self.assertEqual(None, tree.is_ignored('#comment'))
127
128
        # Whitespace should not be stripped
129
        self.assertEqual(' xx ', tree.is_ignored(' xx '))
130
        self.assertEqual(' xx ', tree.is_ignored('subdir/ xx '))
131
        self.assertEqual(None, tree.is_ignored('xx'))
132
        self.assertEqual(None, tree.is_ignored('xx '))
133
        self.assertEqual(None, tree.is_ignored(' xx'))
134
        self.assertEqual(None, tree.is_ignored('subdir/xx '))
135
136
    def test_global_ignored(self):
137
        tree = self.make_branch_and_tree('.')
138
139
        config.ensure_config_dir_exists()
1836.1.6 by John Arbash Meinel
Creating a helper function for getting the user ignore filename
140
        user_ignore_file = config.user_ignore_config_filename()
5339.3.7 by Parth Malwankar
fixed test_is_ignored test case
141
        self._set_user_ignore_content(
6973.10.1 by Jelmer Vernooij
Fix some tests.
142
            b'*.py[co]\n'
143
            b'./.shelf\n'
144
            b'# comment line\n'
7143.15.2 by Jelmer Vernooij
Run autopep8.
145
            b'\n'  # Blank line
146
            b'\r\n'  # Blank dos line
147
            b' * \n'  # Trailing and suffix spaces
148
            b'crlf\r\n'  # dos style line
149
            b'*\xc3\xa5*\n'  # u'\xe5'.encode('utf8')
5339.3.7 by Parth Malwankar
fixed test_is_ignored test case
150
            )
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
151
152
        # Rooted
153
        self.assertEqual('./.shelf', tree.is_ignored('.shelf'))
154
        self.assertEqual(None, tree.is_ignored('foo/.shelf'))
155
156
        # Glob style
157
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
158
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyo'))
159
        self.assertEqual(None, tree.is_ignored('foo.py'))
160
161
        # Glob in subdir
162
        self.assertEqual('*.py[co]', tree.is_ignored('bar/foo.pyc'))
163
        self.assertEqual('*.py[co]', tree.is_ignored('bar/foo.pyo'))
164
        self.assertEqual(None, tree.is_ignored('bar/foo.py'))
165
166
        # Unicode
167
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b\xe5gfors'))
168
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'\xe5gfors'))
169
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'\xe5'))
170
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b\xe5'))
171
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b/\xe5'))
172
173
        # Whitespace
174
        self.assertEqual(' * ', tree.is_ignored(' bbb '))
175
        self.assertEqual(' * ', tree.is_ignored('subdir/ bbb '))
176
        self.assertEqual(None, tree.is_ignored('bbb '))
177
        self.assertEqual(None, tree.is_ignored(' bbb'))
178
179
        # Dos lines
180
        self.assertEqual('crlf', tree.is_ignored('crlf'))
181
        self.assertEqual('crlf', tree.is_ignored('subdir/crlf'))
182
183
        # Comment line should be ignored
184
        self.assertEqual(None, tree.is_ignored('# comment line'))
185
186
        # Blank line should also be ignored
187
        self.assertEqual(None, tree.is_ignored(''))
188
        self.assertEqual(None, tree.is_ignored('baz/'))
1836.1.20 by John Arbash Meinel
Make sure that mixing global and local ignores work
189
190
    def test_mixed_is_ignored(self):
191
        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.
192
        ignores._set_user_ignores(['*.py[co]', './.shelf'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
193
        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
194
195
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
196
        self.assertEqual('./.shelf', tree.is_ignored('.shelf'))
197
        self.assertEqual('./rootdir', tree.is_ignored('rootdir'))
2135.2.3 by Kent Gibson
Revert unnecessary change to test_mixed_is_ignored.
198
        self.assertEqual('*.swp', tree.is_ignored('foo.py.swp'))
199
        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
200
        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
201
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
202
    def test_runtime_ignores(self):
203
        tree = self.make_branch_and_tree('.')
6855.4.1 by Jelmer Vernooij
Yet more bees.
204
        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.
205
        ignores._set_user_ignores([])
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
206
207
        orig_runtime = ignores._runtime_ignores
208
        try:
209
            ignores._runtime_ignores = set()
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
210
            self.assertEqual(None, tree.is_ignored('foobar.py'))
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
211
2135.2.7 by Kent Gibson
Implement JAM's review suggestions.
212
            tree._flush_ignore_list_cache()
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
213
            ignores.add_runtime_ignores(['./foobar.py'])
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
214
            self.assertEqual({'./foobar.py'}, ignores.get_runtime_ignores())
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
215
            self.assertEqual('./foobar.py', tree.is_ignored('foobar.py'))
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
216
        finally:
217
            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).
218
219
    def test_ignore_caching(self):
220
        tree = self.make_branch_and_tree('.')
221
        self.build_tree(['ignoreme'])
222
223
        self.assertEqual(None, tree.is_ignored('ignoreme'))
224
225
        # Bug #129694 specifically references WorkingTree.unknowns()
226
        tree.unknowns()
227
6855.4.1 by Jelmer Vernooij
Yet more bees.
228
        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).
229
        self.assertEqual('ignoreme', tree.is_ignored('ignoreme'))