/brz/remove-bazaar

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