/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
1
# (C) 2006 Canonical Ltd
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'
33
                           'path/from/ro?t\n'
34
                           'unicode\xc2\xb5\n' # u'\xb5'.encode('utf8')
35
                           'dos\r\n'
36
                           '\n' # empty line
37
                           '#comment\n'
38
                           ' xx \n' # whitespace
39
            )])
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
40
        # is_ignored returns the matching ignore regex when a path is ignored.
41
        # we check some expected matches for each rule, and one or more
42
        # relevant not-matches that look plausible as cases for bugs.
43
        self.assertEqual('./rootdir', tree.is_ignored('rootdir'))
44
        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.
45
        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
46
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
47
        self.assertEqual('randomfile*', tree.is_ignored('randomfile'))
48
        self.assertEqual('randomfile*', tree.is_ignored('randomfiles'))
49
        self.assertEqual('randomfile*', tree.is_ignored('foo/randomfiles'))
50
        self.assertEqual(None, tree.is_ignored('randomfil'))
51
        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
52
1713.2.1 by Robert Collins
Some tests for WorkingTree.is_ignored so it can be refactored with confidence.
53
        self.assertEqual("path/from/ro?t", tree.is_ignored('path/from/root'))
54
        self.assertEqual("path/from/ro?t", tree.is_ignored('path/from/roat'))
55
        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
56
57
        self.assertEqual(u'unicode\xb5', tree.is_ignored(u'unicode\xb5'))
58
        self.assertEqual(u'unicode\xb5', tree.is_ignored(u'subdir/unicode\xb5'))
59
        self.assertEqual(None, tree.is_ignored(u'unicode\xe5'))
60
        self.assertEqual(None, tree.is_ignored(u'unicode'))
61
        self.assertEqual(None, tree.is_ignored(u'\xb5'))
62
63
        self.assertEqual('dos', tree.is_ignored('dos'))
64
        self.assertEqual(None, tree.is_ignored('dosfoo'))
65
66
        # Blank lines and comments should be ignored
67
        self.assertEqual(None, tree.is_ignored(''))
68
        self.assertEqual(None, tree.is_ignored('test/'))
69
70
        self.assertEqual(None, tree.is_ignored('#comment'))
71
72
        # Whitespace should not be stripped
73
        self.assertEqual(' xx ', tree.is_ignored(' xx '))
74
        self.assertEqual(' xx ', tree.is_ignored('subdir/ xx '))
75
        self.assertEqual(None, tree.is_ignored('xx'))
76
        self.assertEqual(None, tree.is_ignored('xx '))
77
        self.assertEqual(None, tree.is_ignored(' xx'))
78
        self.assertEqual(None, tree.is_ignored('subdir/xx '))
79
80
    def test_global_ignored(self):
81
        tree = self.make_branch_and_tree('.')
82
83
        config.ensure_config_dir_exists()
1836.1.6 by John Arbash Meinel
Creating a helper function for getting the user ignore filename
84
        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
85
        f = open(user_ignore_file, 'wb')
86
        try:
87
            f.write('*.py[co]\n'
88
                    './.shelf\n'
89
                    '# comment line\n'
90
                    '\n' #Blank line
91
                    '\r\n' #Blank dos line
92
                    ' * \n' #Trailing and suffix spaces
93
                    'crlf\r\n' # dos style line
94
                    '*\xc3\xa5*\n' # u'\xe5'.encode('utf8')
95
                    )
96
        finally:
97
            f.close()
98
99
        # Rooted
100
        self.assertEqual('./.shelf', tree.is_ignored('.shelf'))
101
        self.assertEqual(None, tree.is_ignored('foo/.shelf'))
102
103
        # Glob style
104
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
105
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyo'))
106
        self.assertEqual(None, tree.is_ignored('foo.py'))
107
108
        # Glob in subdir
109
        self.assertEqual('*.py[co]', tree.is_ignored('bar/foo.pyc'))
110
        self.assertEqual('*.py[co]', tree.is_ignored('bar/foo.pyo'))
111
        self.assertEqual(None, tree.is_ignored('bar/foo.py'))
112
113
        # Unicode
114
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b\xe5gfors'))
115
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'\xe5gfors'))
116
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'\xe5'))
117
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b\xe5'))
118
        self.assertEqual(u'*\xe5*', tree.is_ignored(u'b/\xe5'))
119
120
        # Whitespace
121
        self.assertEqual(' * ', tree.is_ignored(' bbb '))
122
        self.assertEqual(' * ', tree.is_ignored('subdir/ bbb '))
123
        self.assertEqual(None, tree.is_ignored('bbb '))
124
        self.assertEqual(None, tree.is_ignored(' bbb'))
125
126
        # Dos lines
127
        self.assertEqual('crlf', tree.is_ignored('crlf'))
128
        self.assertEqual('crlf', tree.is_ignored('subdir/crlf'))
129
130
        # Comment line should be ignored
131
        self.assertEqual(None, tree.is_ignored('# comment line'))
132
133
        # Blank line should also be ignored
134
        self.assertEqual(None, tree.is_ignored(''))
135
        self.assertEqual(None, tree.is_ignored('baz/'))
1836.1.20 by John Arbash Meinel
Make sure that mixing global and local ignores work
136
137
    def test_mixed_is_ignored(self):
138
        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.
139
        ignores._set_user_ignores(['*.py[co]', './.shelf'])
1836.1.20 by John Arbash Meinel
Make sure that mixing global and local ignores work
140
        self.build_tree_contents([('.bzrignore', './rootdir\n*.swp\n')])
141
142
        self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
143
        self.assertEqual('./.shelf', tree.is_ignored('.shelf'))
144
        self.assertEqual('./rootdir', tree.is_ignored('rootdir'))
145
        self.assertEqual('*.swp', tree.is_ignored('.foo.py.swp'))
146
        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
147
148
    def test_DEFAULT_IGNORE(self):
149
        tree = self.make_branch_and_tree('.')
150
        # It used to be possible for plugins to modify DEFAULT_IGNORE
151
        # directly, and get their working files to be ignored.
152
        # It is still possible to do so, but this is deprecated.
153
154
        # No configured ignores
155
        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.
156
        ignores._set_user_ignores([])
1836.1.21 by John Arbash Meinel
Restore the ability to ignore items by modifying DEFAULT_IGNORE
157
158
        self.assertEqual(None, tree.is_ignored('foo.pyc'))
159
160
        # Must reset the list so that it reads a new one
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
161
        tree._ignoreset = None
1836.1.21 by John Arbash Meinel
Restore the ability to ignore items by modifying DEFAULT_IGNORE
162
163
        # use list.append() to get around the deprecation warnings
164
        list.append(bzrlib.DEFAULT_IGNORE, '*.py[co]')
165
        try:
166
            self.assertEqual('*.py[co]', tree.is_ignored('foo.pyc'))
167
        finally:
168
            list.remove(bzrlib.DEFAULT_IGNORE, '*.py[co]')
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
169
170
    def test_runtime_ignores(self):
171
        tree = self.make_branch_and_tree('.')
172
        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.
173
        ignores._set_user_ignores([])
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
174
175
        orig_runtime = ignores._runtime_ignores
176
        try:
177
            ignores._runtime_ignores = set()
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
178
            self.assertEqual(None, tree.is_ignored('foobar.py'))
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
179
1836.1.30 by John Arbash Meinel
Change ignore functions to use sets instead of lists.
180
            tree._ignoreset = None
181
            ignores.add_runtime_ignores(['./foobar.py'])
182
            self.assertEqual(set(['./foobar.py']), ignores.get_runtime_ignores())
183
            self.assertEqual('./foobar.py', tree.is_ignored('foobar.py'))
1836.1.28 by John Arbash Meinel
Add a function for adding runtime ignores.
184
        finally:
185
            ignores._runtime_ignores = orig_runtime