/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
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
20
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
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'
34
                           '?foo\n'
35
                           '*.~*\n'
36
                           'dir1/*f1\n'
37
                           'dir1/?f2\n'
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
38
                           'path/from/ro?t\n'
39
                           'unicode\xc2\xb5\n' # u'\xb5'.encode('utf8')
40
                           'dos\r\n'
41
                           '\n' # empty line
42
                           '#comment\n'
43
                           ' xx \n' # whitespace
44
            )])
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
45
        # is_ignored returns the matching ignore regex when a path is ignored.
46
        # we check some expected matches for each rule, and one or more
47
        # relevant not-matches that look plausible as cases for bugs.
48
        self.assertEqual('./rootdir', tree.is_ignored('rootdir'))
49
        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.
50
        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
51
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
52
        self.assertEqual('randomfile*', tree.is_ignored('randomfile'))
53
        self.assertEqual('randomfile*', tree.is_ignored('randomfiles'))
54
        self.assertEqual('randomfile*', tree.is_ignored('foo/randomfiles'))
55
        self.assertEqual(None, tree.is_ignored('randomfil'))
56
        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
57
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
58
        self.assertEqual("path/from/ro?t", tree.is_ignored('path/from/root'))
59
        self.assertEqual("path/from/ro?t", tree.is_ignored('path/from/roat'))
60
        self.assertEqual(None, tree.is_ignored('roat'))
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
61
62
        self.assertEqual(u'unicode\xb5', tree.is_ignored(u'unicode\xb5'))
63
        self.assertEqual(u'unicode\xb5', tree.is_ignored(u'subdir/unicode\xb5'))
64
        self.assertEqual(None, tree.is_ignored(u'unicode\xe5'))
65
        self.assertEqual(None, tree.is_ignored(u'unicode'))
66
        self.assertEqual(None, tree.is_ignored(u'\xb5'))
67
68
        self.assertEqual('dos', tree.is_ignored('dos'))
69
        self.assertEqual(None, tree.is_ignored('dosfoo'))
70
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
71
        self.assertEqual('*bar', tree.is_ignored('foobar'))
72
        self.assertEqual('*bar', tree.is_ignored(r'foo\nbar'))
73
        self.assertEqual('*bar', tree.is_ignored('bar'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
74
        self.assertEqual('*bar', tree.is_ignored('.bar'))
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
75
76
        self.assertEqual('?foo', tree.is_ignored('afoo'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
77
        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)
78
79
        self.assertEqual('*.~*', tree.is_ignored('blah.py.~1~'))
80
81
        self.assertEqual('dir1/*f1', tree.is_ignored('dir1/foof1'))
82
        self.assertEqual('dir1/*f1', tree.is_ignored('dir1/f1'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
83
        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)
84
85
        self.assertEqual('dir1/?f2', tree.is_ignored('dir1/ff2'))
2135.2.2 by Kent Gibson
Ignore pattern matcher (glob.py) patches:
86
        self.assertEqual('dir1/?f2', tree.is_ignored('dir1/.f2'))
2135.2.1 by Kent Gibson
Added glob module to replace broken fnmatch based ignore pattern matching (#57637)
87
1836.1.4 by John Arbash Meinel
Cleanup is_ignored to handle comment lines, and a global ignore pattern
88
        # Blank lines and comments should be ignored
89
        self.assertEqual(None, tree.is_ignored(''))
90
        self.assertEqual(None, tree.is_ignored('test/'))
91
92
        self.assertEqual(None, tree.is_ignored('#comment'))
93
94
        # Whitespace should not be stripped
95
        self.assertEqual(' xx ', tree.is_ignored(' xx '))
96
        self.assertEqual(' xx ', tree.is_ignored('subdir/ xx '))
97
        self.assertEqual(None, tree.is_ignored('xx'))
98
        self.assertEqual(None, tree.is_ignored('xx '))
99
        self.assertEqual(None, tree.is_ignored(' xx'))
100
        self.assertEqual(None, tree.is_ignored('subdir/xx '))
101
102
    def test_global_ignored(self):
103
        tree = self.make_branch_and_tree('.')
104
105
        config.ensure_config_dir_exists()
1836.1.6 by John Arbash Meinel
Creating a helper function for getting the user ignore filename
106
        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
107
        f = open(user_ignore_file, 'wb')
108
        try:
109
            f.write('*.py[co]\n'
110
                    './.shelf\n'
111
                    '# comment line\n'
112
                    '\n' #Blank line
113
                    '\r\n' #Blank dos line
114
                    ' * \n' #Trailing and suffix spaces
115
                    'crlf\r\n' # dos style line
116
                    '*\xc3\xa5*\n' # u'\xe5'.encode('utf8')
117
                    )
118
        finally:
119
            f.close()
120
121
        # Rooted
122
        self.assertEqual('./.shelf', tree.is_ignored('.shelf'))
123
        self.assertEqual(None, tree.is_ignored('foo/.shelf'))
124
125
        # Glob style
126
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
127
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyo'))
128
        self.assertEqual(None, tree.is_ignored('foo.py'))
129
130
        # Glob in subdir
131
        self.assertEqual('*.py[co]', tree.is_ignored('bar/foo.pyc'))
132
        self.assertEqual('*.py[co]', tree.is_ignored('bar/foo.pyo'))
133
        self.assertEqual(None, tree.is_ignored('bar/foo.py'))
134
135
        # Unicode
136
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b\xe5gfors'))
137
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'\xe5gfors'))
138
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'\xe5'))
139
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b\xe5'))
140
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b/\xe5'))
141
142
        # Whitespace
143
        self.assertEqual(' * ', tree.is_ignored(' bbb '))
144
        self.assertEqual(' * ', tree.is_ignored('subdir/ bbb '))
145
        self.assertEqual(None, tree.is_ignored('bbb '))
146
        self.assertEqual(None, tree.is_ignored(' bbb'))
147
148
        # Dos lines
149
        self.assertEqual('crlf', tree.is_ignored('crlf'))
150
        self.assertEqual('crlf', tree.is_ignored('subdir/crlf'))
151
152
        # Comment line should be ignored
153
        self.assertEqual(None, tree.is_ignored('# comment line'))
154
155
        # Blank line should also be ignored
156
        self.assertEqual(None, tree.is_ignored(''))
157
        self.assertEqual(None, tree.is_ignored('baz/'))
1836.1.20 by John Arbash Meinel
Make sure that mixing global and local ignores work
158
159
    def test_mixed_is_ignored(self):
160
        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.
161
        ignores._set_user_ignores(['*.py[co]', './.shelf'])
2135.2.3 by Kent Gibson
Revert unnecessary change to test_mixed_is_ignored.
162
        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
163
164
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
165
        self.assertEqual('./.shelf', tree.is_ignored('.shelf'))
166
        self.assertEqual('./rootdir', tree.is_ignored('rootdir'))
2135.2.3 by Kent Gibson
Revert unnecessary change to test_mixed_is_ignored.
167
        self.assertEqual('*.swp', tree.is_ignored('foo.py.swp'))
168
        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
169
        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
170
171
    def test_DEFAULT_IGNORE(self):
172
        tree = self.make_branch_and_tree('.')
173
        # It used to be possible for plugins to modify DEFAULT_IGNORE
174
        # directly, and get their working files to be ignored.
175
        # It is still possible to do so, but this is deprecated.
176
177
        # No configured ignores
178
        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.
179
        ignores._set_user_ignores([])
1836.1.21 by John Arbash Meinel
Restore the ability to ignore items by modifying DEFAULT_IGNORE
180
181
        self.assertEqual(None, tree.is_ignored('foo.pyc'))
182
183
        # Must reset the list so that it reads a new one
2135.2.7 by Kent Gibson
Implement JAM's review suggestions.
184
        tree._flush_ignore_list_cache()
1836.1.21 by John Arbash Meinel
Restore the ability to ignore items by modifying DEFAULT_IGNORE
185
186
        # use list.append() to get around the deprecation warnings
187
        list.append(bzrlib.DEFAULT_IGNORE, '*.py[co]')
188
        try:
189
            self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
190
        finally:
191
            list.remove(bzrlib.DEFAULT_IGNORE, '*.py[co]')
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
192
193
    def test_runtime_ignores(self):
194
        tree = self.make_branch_and_tree('.')
195
        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.
196
        ignores._set_user_ignores([])
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
197
198
        orig_runtime = ignores._runtime_ignores
199
        try:
200
            ignores._runtime_ignores = set()
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
201
            self.assertEqual(None, tree.is_ignored('foobar.py'))
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
202
2135.2.7 by Kent Gibson
Implement JAM's review suggestions.
203
            tree._flush_ignore_list_cache()
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
204
            ignores.add_runtime_ignores(['./foobar.py'])
205
            self.assertEqual(set(['./foobar.py']), ignores.get_runtime_ignores())
206
            self.assertEqual('./foobar.py', tree.is_ignored('foobar.py'))
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
207
        finally:
208
            ignores._runtime_ignores = orig_runtime