1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for handling of ignore files"""
19
from io import BytesIO
29
TestCaseWithTransport,
33
class TestParseIgnoreFile(TestCase):
35
def test_parse_fancy(self):
36
ignored = ignores.parse_ignore_file(BytesIO(
40
b'unicode\xc2\xb5\n' # u'\xb5'.encode('utf8')
44
b' xx \n' # whitespace
48
self.assertEqual({u'./rootdir',
58
def test_parse_empty(self):
59
ignored = ignores.parse_ignore_file(BytesIO(b''))
60
self.assertEqual(set([]), ignored)
62
def test_parse_non_utf8(self):
63
"""Lines with non utf 8 characters should be discarded."""
64
ignored = ignores.parse_ignore_file(BytesIO(
75
class TestUserIgnores(TestCaseInTempDir):
77
def test_create_if_missing(self):
78
# $HOME should be set to '.'
79
ignore_path = bedding.user_ignore_config_path()
80
self.assertPathDoesNotExist(ignore_path)
81
user_ignores = ignores.get_user_ignores()
82
self.assertEqual(set(ignores.USER_DEFAULTS), user_ignores)
84
self.assertPathExists(ignore_path)
85
with open(ignore_path, 'rb') as f:
86
entries = ignores.parse_ignore_file(f)
87
self.assertEqual(set(ignores.USER_DEFAULTS), entries)
89
def test_create_with_intermediate_missing(self):
90
# $HOME should be set to '.'
91
ignore_path = bedding.user_ignore_config_path()
92
self.assertPathDoesNotExist(ignore_path)
93
os.mkdir('empty-home')
95
config_path = os.path.join(self.test_dir, 'empty-home', '.config')
96
self.overrideEnv('BRZ_HOME', config_path)
97
self.assertPathDoesNotExist(config_path)
99
user_ignores = ignores.get_user_ignores()
100
self.assertEqual(set(ignores.USER_DEFAULTS), user_ignores)
102
ignore_path = bedding.user_ignore_config_path()
103
self.assertPathDoesNotExist(ignore_path)
105
def test_use_existing(self):
106
patterns = [u'*.o', u'*.py[co]', u'\xe5*']
107
ignores._set_user_ignores(patterns)
109
user_ignores = ignores.get_user_ignores()
110
self.assertEqual(set(patterns), user_ignores)
112
def test_use_empty(self):
113
ignores._set_user_ignores([])
114
ignore_path = bedding.user_ignore_config_path()
115
self.check_file_contents(ignore_path, b'')
117
self.assertEqual(set([]), ignores.get_user_ignores())
120
patterns = ['*.py[co]', '*.py[oc]']
121
ignores._set_user_ignores(patterns)
123
self.assertEqual(set(patterns), ignores.get_user_ignores())
125
patterns = ['vim', '*.swp']
126
ignores._set_user_ignores(patterns)
127
self.assertEqual(set(patterns), ignores.get_user_ignores())
130
"""Test that adding will not duplicate ignores"""
131
# Create an empty file
132
ignores._set_user_ignores([])
134
patterns = ['foo', './bar', u'b\xe5z']
135
added = ignores.add_unique_user_ignores(patterns)
136
self.assertEqual(patterns, added)
137
self.assertEqual(set(patterns), ignores.get_user_ignores())
139
def test_add_directory(self):
140
"""Test that adding a directory will strip any trailing slash"""
141
# Create an empty file
142
ignores._set_user_ignores([])
144
in_patterns = ['foo/', 'bar/', 'baz\\']
145
added = ignores.add_unique_user_ignores(in_patterns)
146
out_patterns = [x.rstrip('/\\') for x in in_patterns]
147
self.assertEqual(out_patterns, added)
148
self.assertEqual(set(out_patterns), ignores.get_user_ignores())
150
def test_add_unique(self):
151
"""Test that adding will not duplicate ignores"""
152
ignores._set_user_ignores(
153
['foo', './bar', u'b\xe5z', 'dir1/', 'dir3\\'])
155
added = ignores.add_unique_user_ignores(
156
['xxx', './bar', 'xxx', 'dir1/', 'dir2/', 'dir3\\'])
157
self.assertEqual(['xxx', 'dir2'], added)
158
self.assertEqual({'foo', './bar', u'b\xe5z',
159
'xxx', 'dir1', 'dir2', 'dir3'},
160
ignores.get_user_ignores())
163
class TestRuntimeIgnores(TestCase):
166
super(TestRuntimeIgnores, self).setUp()
168
# For the purposes of these tests, we must have no
170
self.overrideAttr(ignores, '_runtime_ignores', set())
173
"""Test that we can add an entry to the list."""
174
self.assertEqual(set(), ignores.get_runtime_ignores())
176
ignores.add_runtime_ignores(['foo'])
177
self.assertEqual({'foo'}, ignores.get_runtime_ignores())
179
def test_add_duplicate(self):
180
"""Adding the same ignore twice shouldn't add a new entry."""
181
ignores.add_runtime_ignores(['foo', 'bar'])
182
self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
184
ignores.add_runtime_ignores(['bar'])
185
self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
188
class TestTreeIgnores(TestCaseWithTransport):
190
def assertPatternsEquals(self, patterns):
191
with open(".bzrignore", "rb") as f:
192
contents = f.read().decode("utf-8").splitlines()
193
self.assertEqual(sorted(patterns), sorted(contents))
195
def test_new_file(self):
196
tree = self.make_branch_and_tree(".")
197
ignores.tree_ignores_add_patterns(tree, [u"myentry"])
198
self.assertTrue(tree.has_filename(".bzrignore"))
199
self.assertPatternsEquals(["myentry"])
201
def test_add_to_existing(self):
202
tree = self.make_branch_and_tree(".")
203
self.build_tree_contents([('.bzrignore', b"myentry1\n")])
204
tree.add([".bzrignore"])
205
ignores.tree_ignores_add_patterns(tree, [u"myentry2", u"foo"])
206
self.assertPatternsEquals(["myentry1", "myentry2", "foo"])
208
def test_adds_ending_newline(self):
209
tree = self.make_branch_and_tree(".")
210
self.build_tree_contents([('.bzrignore', b"myentry1")])
211
tree.add([".bzrignore"])
212
ignores.tree_ignores_add_patterns(tree, [u"myentry2"])
213
self.assertPatternsEquals(["myentry1", "myentry2"])
214
with open(".bzrignore") as f:
216
self.assertTrue(text.endswith(('\r\n', '\n', '\r')))
218
def test_does_not_add_dupe(self):
219
tree = self.make_branch_and_tree(".")
220
self.build_tree_contents([('.bzrignore', b"myentry\n")])
221
tree.add([".bzrignore"])
222
ignores.tree_ignores_add_patterns(tree, [u"myentry"])
223
self.assertPatternsEquals(["myentry"])
225
def test_non_ascii(self):
226
tree = self.make_branch_and_tree(".")
227
self.build_tree_contents([('.bzrignore',
228
u"myentry\u1234\n".encode('utf-8'))])
229
tree.add([".bzrignore"])
230
ignores.tree_ignores_add_patterns(tree, [u"myentry\u5678"])
231
self.assertPatternsEquals([u"myentry\u1234", u"myentry\u5678"])
234
tree = self.make_branch_and_tree(".")
235
self.build_tree_contents([('.bzrignore', b"myentry1\r\n")])
236
tree.add([".bzrignore"])
237
ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
238
with open('.bzrignore', 'rb') as f:
239
self.assertEqual(f.read(), b'myentry1\r\nmyentry2\r\nfoo\r\n')
240
self.assertPatternsEquals(["myentry1", "myentry2", "foo"])