17
17
"""Tests for handling of ignore files"""
19
from cStringIO import StringIO
19
from io import BytesIO
21
from bzrlib import config, errors, ignores
22
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
29
TestCaseWithTransport,
25
33
class TestParseIgnoreFile(TestCase):
27
35
def test_parse_fancy(self):
28
ignored = ignores.parse_ignore_file(StringIO(
32
'unicode\xc2\xb5\n' # u'\xb5'.encode('utf8')
40
self.assertEqual(set(['./rootdir',
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',
50
58
def test_parse_empty(self):
51
ignored = ignores.parse_ignore_file(StringIO(''))
59
ignored = ignores.parse_ignore_file(BytesIO(b''))
52
60
self.assertEqual(set([]), ignored)
54
62
def test_parse_non_utf8(self):
55
63
"""Lines with non utf 8 characters should be discarded."""
56
ignored = ignores.parse_ignore_file(StringIO(
61
self.assertEqual(set([
64
ignored = ignores.parse_ignore_file(BytesIO(
67
75
class TestUserIgnores(TestCaseInTempDir):
69
77
def test_create_if_missing(self):
70
78
# $HOME should be set to '.'
71
ignore_path = config.user_ignore_config_filename()
72
self.failIfExists(ignore_path)
79
ignore_path = bedding.user_ignore_config_path()
80
self.assertPathDoesNotExist(ignore_path)
73
81
user_ignores = ignores.get_user_ignores()
74
82
self.assertEqual(set(ignores.USER_DEFAULTS), user_ignores)
76
self.failUnlessExists(ignore_path)
77
f = open(ignore_path, 'rb')
84
self.assertPathExists(ignore_path)
85
with open(ignore_path, 'rb') as f:
79
86
entries = ignores.parse_ignore_file(f)
82
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(
96
self.test_dir, 'empty-home', 'foo', '.config')
97
self.overrideEnv('BRZ_HOME', config_path)
98
self.assertPathDoesNotExist(config_path)
100
user_ignores = ignores.get_user_ignores()
101
self.assertEqual(set(ignores.USER_DEFAULTS), user_ignores)
103
ignore_path = bedding.user_ignore_config_path()
104
self.assertPathDoesNotExist(ignore_path)
84
106
def test_use_existing(self):
85
patterns = ['*.o', '*.py[co]', u'\xe5*']
107
patterns = [u'*.o', u'*.py[co]', u'\xe5*']
86
108
ignores._set_user_ignores(patterns)
88
110
user_ignores = ignores.get_user_ignores()
134
156
added = ignores.add_unique_user_ignores(
135
157
['xxx', './bar', 'xxx', 'dir1/', 'dir2/', 'dir3\\'])
136
158
self.assertEqual(['xxx', 'dir2'], added)
137
self.assertEqual(set(['foo', './bar', u'b\xe5z',
138
'xxx', 'dir1', 'dir2', 'dir3']),
159
self.assertEqual({'foo', './bar', u'b\xe5z',
160
'xxx', 'dir1', 'dir2', 'dir3'},
139
161
ignores.get_user_ignores())
142
164
class TestRuntimeIgnores(TestCase):
167
super(TestRuntimeIgnores, self).setUp()
147
169
# For the purposes of these tests, we must have no
148
170
# runtime ignores
153
175
self.assertEqual(set(), ignores.get_runtime_ignores())
155
177
ignores.add_runtime_ignores(['foo'])
156
self.assertEqual(set(['foo']), ignores.get_runtime_ignores())
178
self.assertEqual({'foo'}, ignores.get_runtime_ignores())
158
180
def test_add_duplicate(self):
159
181
"""Adding the same ignore twice shouldn't add a new entry."""
160
182
ignores.add_runtime_ignores(['foo', 'bar'])
161
self.assertEqual(set(['foo', 'bar']), ignores.get_runtime_ignores())
183
self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
163
185
ignores.add_runtime_ignores(['bar'])
164
self.assertEqual(set(['foo', 'bar']), ignores.get_runtime_ignores())
186
self.assertEqual({'foo', 'bar'}, ignores.get_runtime_ignores())
167
189
class TestTreeIgnores(TestCaseWithTransport):
191
def assertPatternsEquals(self, patterns):
192
with open(".bzrignore", "rb") as f:
193
contents = f.read().decode("utf-8").splitlines()
194
self.assertEqual(sorted(patterns), sorted(contents))
169
196
def test_new_file(self):
170
197
tree = self.make_branch_and_tree(".")
171
ignores.tree_ignores_add_patterns(tree, ["myentry"])
198
ignores.tree_ignores_add_patterns(tree, [u"myentry"])
172
199
self.assertTrue(tree.has_filename(".bzrignore"))
173
self.assertEquals("myentry\n",
174
open(".bzrignore", 'r').read())
200
self.assertPatternsEquals(["myentry"])
176
202
def test_add_to_existing(self):
177
203
tree = self.make_branch_and_tree(".")
178
self.build_tree_contents([('.bzrignore', "myentry1\n")])
204
self.build_tree_contents([('.bzrignore', b"myentry1\n")])
205
tree.add([".bzrignore"])
206
ignores.tree_ignores_add_patterns(tree, [u"myentry2", u"foo"])
207
self.assertPatternsEquals(["myentry1", "myentry2", "foo"])
209
def test_adds_ending_newline(self):
210
tree = self.make_branch_and_tree(".")
211
self.build_tree_contents([('.bzrignore', b"myentry1")])
212
tree.add([".bzrignore"])
213
ignores.tree_ignores_add_patterns(tree, [u"myentry2"])
214
self.assertPatternsEquals(["myentry1", "myentry2"])
215
with open(".bzrignore") as f:
217
self.assertTrue(text.endswith(('\r\n', '\n', '\r')))
219
def test_does_not_add_dupe(self):
220
tree = self.make_branch_and_tree(".")
221
self.build_tree_contents([('.bzrignore', b"myentry\n")])
222
tree.add([".bzrignore"])
223
ignores.tree_ignores_add_patterns(tree, [u"myentry"])
224
self.assertPatternsEquals(["myentry"])
226
def test_non_ascii(self):
227
tree = self.make_branch_and_tree(".")
228
self.build_tree_contents([('.bzrignore',
229
u"myentry\u1234\n".encode('utf-8'))])
230
tree.add([".bzrignore"])
231
ignores.tree_ignores_add_patterns(tree, [u"myentry\u5678"])
232
self.assertPatternsEquals([u"myentry\u1234", u"myentry\u5678"])
235
tree = self.make_branch_and_tree(".")
236
self.build_tree_contents([('.bzrignore', b"myentry1\r\n")])
179
237
tree.add([".bzrignore"])
180
238
ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
181
self.assertEquals("myentry1\nmyentry2\nfoo\n",
182
open(".bzrignore", 'r').read())
184
def test_adds_ending_newline(self):
185
tree = self.make_branch_and_tree(".")
186
self.build_tree_contents([('.bzrignore', "myentry1")])
187
tree.add([".bzrignore"])
188
ignores.tree_ignores_add_patterns(tree, ["myentry2"])
189
self.assertEquals("myentry1\nmyentry2\n",
190
open(".bzrignore", 'r').read())
239
with open('.bzrignore', 'rb') as f:
240
self.assertEqual(f.read(), b'myentry1\r\nmyentry2\r\nfoo\r\n')
241
self.assertPatternsEquals(["myentry1", "myentry2", "foo"])