1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
# Copyright (C) 2006, 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for the test trees used by the per_tree tests."""
from breezy import errors
from breezy.tests import per_tree
from breezy.tests import (
TestNotApplicable,
TestSkipped,
features,
)
class TestTreeShapes(per_tree.TestCaseWithTree):
def test_empty_tree_no_parents(self):
tree = self.make_branch_and_tree('.')
tree = self.get_tree_no_parents_no_content(tree)
tree.lock_read()
self.addCleanup(tree.unlock)
self.assertEqual([], tree.get_parent_ids())
self.assertEqual([], tree.conflicts())
self.assertEqual([], list(tree.unknowns()))
self.assertEqual([''], list(tree.all_versioned_paths()))
self.assertEqual(
[('', tree.path2id(''))],
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
def test_abc_tree_no_parents(self):
tree = self.make_branch_and_tree('.')
tree = self.get_tree_no_parents_abc_content(tree)
tree.lock_read()
self.addCleanup(tree.unlock)
self.assertEqual([], tree.get_parent_ids())
self.assertEqual([], tree.conflicts())
self.assertEqual([], list(tree.unknowns()))
# __iter__ has no strongly defined order
self.assertEqual(
{'', 'a', 'b', 'b/c'},
set(tree.all_versioned_paths()))
self.assertTrue(tree.is_versioned('a'))
self.assertTrue(tree.is_versioned('b'))
self.assertTrue(tree.is_versioned('b/c'))
self.assertEqual(
[(p, tree.path2id(p)) for p in ['', 'a', 'b', 'b/c']],
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
self.assertEqualDiff(b'contents of a\n', tree.get_file_text('a'))
self.assertFalse(tree.is_executable('b/c'))
def test_abc_tree_content_2_no_parents(self):
tree = self.make_branch_and_tree('.')
tree = self.get_tree_no_parents_abc_content_2(tree)
tree.lock_read()
self.addCleanup(tree.unlock)
self.assertEqual([], tree.get_parent_ids())
self.assertEqual([], tree.conflicts())
self.assertEqual([], list(tree.unknowns()))
# __iter__ has no strongly defined order
self.assertEqual(
{'', 'a', 'b', 'b/c'},
set(tree.all_versioned_paths()))
self.assertEqual(
[(p, tree.path2id(p)) for p in ['', 'a', 'b', 'b/c']],
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
self.assertEqual(
[(p, tree.path2id(p)) for p in ['b']],
[(path, node.file_id) for path, node in tree.iter_entries_by_dir(
specific_files=['b'])])
self.assertEqualDiff(b'foobar\n', tree.get_file_text('a'))
self.assertFalse(tree.is_executable('b//c'))
self.assertFalse(tree.is_executable('b/c'))
def test_abc_tree_content_3_no_parents(self):
tree = self.make_branch_and_tree('.')
tree = self.get_tree_no_parents_abc_content_3(tree)
tree.lock_read()
self.addCleanup(tree.unlock)
self.assertEqual([], tree.get_parent_ids())
self.assertEqual([], tree.conflicts())
self.assertEqual([], list(tree.unknowns()))
# __iter__ has no strongly defined order
self.assertEqual(
{'', 'a', 'b', 'b/c'},
set(tree.all_versioned_paths()))
self.assertEqual(
[(p, tree.path2id(p)) for p in ['', 'a', 'b', 'b/c']],
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
self.assertEqualDiff(b'contents of a\n', tree.get_file_text('a'))
self.assertTrue(tree.is_executable('b/c'))
def test_abc_tree_content_4_no_parents(self):
tree = self.make_branch_and_tree('.')
tree = self.get_tree_no_parents_abc_content_4(tree)
tree.lock_read()
self.addCleanup(tree.unlock)
self.assertEqual([], tree.get_parent_ids())
self.assertEqual([], tree.conflicts())
self.assertEqual([], list(tree.unknowns()))
# __iter__ has no strongly defined order
self.assertEqual(
{'', 'b', 'd', 'b/c'},
set(tree.all_versioned_paths()))
self.assertEqual(
[(p, tree.path2id(p)) for p in ['', 'b', 'd', 'b/c']],
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
self.assertEqualDiff(b'contents of a\n', tree.get_file_text('d'))
self.assertFalse(tree.is_executable('b/c'))
def test_abc_tree_content_5_no_parents(self):
tree = self.make_branch_and_tree('.')
tree = self.get_tree_no_parents_abc_content_5(tree)
tree.lock_read()
self.addCleanup(tree.unlock)
self.assertEqual([], tree.get_parent_ids())
self.assertEqual([], tree.conflicts())
self.assertEqual([], list(tree.unknowns()))
# __iter__ has no strongly defined order
self.assertEqual(
{'', 'd', 'b', 'b/c'},
set(tree.all_versioned_paths()))
self.assertEqual(
[(p, tree.path2id(p)) for p in ['', 'b', 'd', 'b/c']],
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
self.assertEqualDiff(b'bar\n', tree.get_file_text('d'))
self.assertFalse(tree.is_executable('b/c'))
def test_abc_tree_content_6_no_parents(self):
tree = self.make_branch_and_tree('.')
tree = self.get_tree_no_parents_abc_content_6(tree)
tree.lock_read()
self.addCleanup(tree.unlock)
self.assertEqual([], tree.get_parent_ids())
self.assertEqual([], tree.conflicts())
self.assertEqual([], list(tree.unknowns()))
# __iter__ has no strongly defined order
expected_paths = (
['', 'a']
+ (['b'] if tree.has_versioned_directories() else [])
+ ['e'])
self.assertEqual(
set(expected_paths),
set(tree.all_versioned_paths()))
self.assertEqual(
[(p, tree.path2id(p)) for p in expected_paths],
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
self.assertEqualDiff(b'contents of a\n', tree.get_file_text('a'))
self.assertTrue(tree.is_executable('e'))
def test_tree_with_subdirs_and_all_content_types(self):
# currently this test tree requires unicode. It might be good
# to have it simply stop having the single unicode file in it
# when dealing with a non-unicode filesystem.
self.requireFeature(features.SymlinkFeature)
tree = self.get_tree_with_subdirs_and_all_content_types()
tree.lock_read()
self.addCleanup(tree.unlock)
self.assertEqual([], tree.get_parent_ids())
self.assertEqual([], tree.conflicts())
self.assertEqual([], list(tree.unknowns()))
# __iter__ has no strongly defined order
tree_root = tree.path2id('')
try:
all_file_ids = set(tree.all_file_ids())
except errors.UnsupportedOperation:
all_file_ids = None
if tree.has_versioned_directories():
if all_file_ids is not None:
self.assertEqual(
{tree.path2id(p) for p in [
'', '0file', '1top-dir', '1top-dir/1dir-in-1topdir',
'1top-dir/0file-in-1topdir', 'symlink', u'2utf\u1234file']},
set(tree.all_file_ids()))
# note that the order of the paths and fileids is deliberately
# mismatched to ensure that the result order is path based.
self.assertEqual(
[('', 'directory'),
('0file', 'file'),
('1top-dir', 'directory'),
(u'2utf\u1234file', 'file'),
('symlink', 'symlink'),
('1top-dir/0file-in-1topdir', 'file'),
('1top-dir/1dir-in-1topdir', 'directory')],
[(path, node.kind) for path, node in tree.iter_entries_by_dir()])
else:
if all_file_ids is not None:
self.assertEqual(
{tree.path2id(p) for p in [
'', '0file', '1top-dir',
'1top-dir/0file-in-1topdir', 'symlink',
u'2utf\u1234file']},
set(tree.all_file_ids()))
# note that the order of the paths and fileids is deliberately
# mismatched to ensure that the result order is path based.
self.assertEqual(
[('', 'directory'),
('0file', 'file'),
('1top-dir', 'directory'),
(u'2utf\u1234file', 'file'),
('symlink', 'symlink'),
('1top-dir/0file-in-1topdir', 'file')],
[(path, node.kind) for path, node in tree.iter_entries_by_dir()])
def test_tree_with_subdirs_and_all_content_types_wo_symlinks(self):
# currently this test tree requires unicode. It might be good
# to have it simply stop having the single unicode file in it
# when dealing with a non-unicode filesystem.
tree = self.get_tree_with_subdirs_and_all_supported_content_types(
False)
tree.lock_read()
self.addCleanup(tree.unlock)
self.assertEqual([], tree.get_parent_ids())
self.assertEqual([], tree.conflicts())
self.assertEqual([], list(tree.unknowns()))
# __iter__ has no strongly defined order
tree_root = tree.path2id('')
if tree.has_versioned_directories():
self.assertEqual(
{'', '0file', '1top-dir', '1top-dir/0file-in-1topdir',
'1top-dir/1dir-in-1topdir', u'2utf\u1234file'},
set(tree.all_versioned_paths()))
# note that the order of the paths and fileids is deliberately
# mismatched to ensure that the result order is path based.
self.assertEqual(
[('', 'directory'),
('0file', 'file'),
('1top-dir', 'directory'),
(u'2utf\u1234file', 'file'),
('1top-dir/0file-in-1topdir', 'file'),
('1top-dir/1dir-in-1topdir', 'directory')],
[(path, node.kind) for path, node in tree.iter_entries_by_dir()])
else:
self.assertEqual(
{'', '0file', '1top-dir', '1top-dir/0file-in-1topdir',
u'2utf\u1234file'},
set(tree.all_versioned_paths()))
# note that the order of the paths and fileids is deliberately
# mismatched to ensure that the result order is path based.
self.assertEqual(
[('', 'directory'),
('0file', 'file'),
('1top-dir', 'directory'),
(u'2utf\u1234file', 'file'),
('1top-dir/0file-in-1topdir', 'file')],
[(path, node.kind) for path, node in tree.iter_entries_by_dir()])
def _create_tree_with_utf8(self, tree):
self.requireFeature(features.UnicodeFilenameFeature)
# We avoid combining characters in file names here, normalization
# checks (as performed by some file systems (OSX) are outside the scope
# of these tests). We use the euro sign \N{Euro Sign} or \u20ac in
# unicode strings or '\xe2\x82\ac' (its utf-8 encoding) in raw strings.
paths = [u'',
u'fo\N{Euro Sign}o',
u'ba\N{Euro Sign}r/',
u'ba\N{Euro Sign}r/ba\N{Euro Sign}z',
]
# bzr itself does not create unicode file ids, but we want them for
# testing.
file_ids = [b'TREE_ROOT',
b'fo\xe2\x82\xaco-id',
b'ba\xe2\x82\xacr-id',
b'ba\xe2\x82\xacz-id',
]
self.build_tree(paths[1:])
if tree.path2id('') is None:
# Some trees do not have a root yet.
tree.add(paths, file_ids)
else:
# Some trees will already have a root
if tree.supports_setting_file_ids():
tree.set_root_id(file_ids[0])
tree.add(paths[1:], file_ids[1:])
else:
tree.add(paths[1:])
if tree.branch.repository._format.supports_setting_revision_ids:
try:
tree.commit(u'in\xedtial', rev_id=u'r\xe9v-1'.encode('utf8'))
except errors.NonAsciiRevisionId:
raise TestSkipped('non-ascii revision ids not supported')
else:
tree.commit(u'in\xedtial')
return tree
def test_tree_with_utf8(self):
tree = self.make_branch_and_tree('.')
if not tree.supports_setting_file_ids():
raise TestNotApplicable(
'format does not support custom file ids')
self._create_tree_with_utf8(tree)
tree = self.workingtree_to_test_tree(tree)
revision_id = u'r\xe9v-1'.encode('utf8')
root_id = b'TREE_ROOT'
bar_id = u'ba\N{Euro Sign}r-id'.encode('utf8')
foo_id = u'fo\N{Euro Sign}o-id'.encode('utf8')
baz_id = u'ba\N{Euro Sign}z-id'.encode('utf8')
path_and_ids = [(u'', root_id, None, None),
(u'ba\N{Euro Sign}r', bar_id, root_id, revision_id),
(u'fo\N{Euro Sign}o', foo_id, root_id, revision_id),
(u'ba\N{Euro Sign}r/ba\N{Euro Sign}z',
baz_id, bar_id, revision_id),
]
with tree.lock_read():
path_entries = list(tree.iter_entries_by_dir())
for expected, (path, ie) in zip(path_and_ids, path_entries):
self.assertEqual(expected[0], path) # Paths should match
self.assertIsInstance(path, str)
self.assertEqual(expected[1], ie.file_id)
self.assertIsInstance(ie.file_id, bytes)
self.assertEqual(expected[2], ie.parent_id)
if expected[2] is not None:
self.assertIsInstance(ie.parent_id, bytes)
# WorkingTree's return None for the last modified revision
if ie.revision is not None:
self.assertIsInstance(ie.revision, bytes)
if expected[0] != '':
# Some trees will preserve the revision id of the tree root,
# but not all will
self.assertEqual(revision_id, ie.revision)
self.assertEqual(len(path_and_ids), len(path_entries))
get_revision_id = getattr(tree, 'get_revision_id', None)
if get_revision_id is not None:
self.assertIsInstance(get_revision_id(), bytes)
last_revision = getattr(tree, 'last_revision', None)
if last_revision is not None:
self.assertIsInstance(last_revision(), bytes)
def test_tree_with_merged_utf8(self):
wt = self.make_branch_and_tree('.')
self._create_tree_with_utf8(wt)
tree2 = wt.controldir.sprout('tree2').open_workingtree()
self.build_tree([u'tree2/ba\N{Euro Sign}r/qu\N{Euro Sign}x'])
if wt.supports_setting_file_ids():
tree2.add([u'ba\N{Euro Sign}r/qu\N{Euro Sign}x'],
[u'qu\N{Euro Sign}x-id'.encode('utf-8')])
else:
tree2.add([u'ba\N{Euro Sign}r/qu\N{Euro Sign}x'])
if wt.branch.repository._format.supports_setting_revision_ids:
tree2.commit(u'to m\xe9rge', rev_id=u'r\xe9v-2'.encode('utf8'))
else:
tree2.commit(u'to m\xe9rge')
self.assertTrue(tree2.is_versioned(
u'ba\N{Euro Sign}r/qu\N{Euro Sign}x'))
wt.merge_from_branch(tree2.branch)
self.assertTrue(wt.is_versioned(u'ba\N{Euro Sign}r/qu\N{Euro Sign}x'))
if wt.branch.repository._format.supports_setting_revision_ids:
wt.commit(u'm\xe9rge', rev_id=u'r\xe9v-3'.encode('utf8'))
else:
wt.commit(u'm\xe9rge')
tree = self.workingtree_to_test_tree(wt)
revision_id_1 = u'r\xe9v-1'.encode('utf8')
revision_id_2 = u'r\xe9v-2'.encode('utf8')
root_id = b'TREE_ROOT'
bar_id = u'ba\N{Euro Sign}r-id'.encode('utf8')
foo_id = u'fo\N{Euro Sign}o-id'.encode('utf8')
baz_id = u'ba\N{Euro Sign}z-id'.encode('utf8')
qux_id = u'qu\N{Euro Sign}x-id'.encode('utf8')
path_and_ids = [(u'', root_id, None, None),
(u'ba\N{Euro Sign}r', bar_id, root_id, revision_id_1),
(u'fo\N{Euro Sign}o', foo_id, root_id, revision_id_1),
(u'ba\N{Euro Sign}r/ba\N{Euro Sign}z',
baz_id, bar_id, revision_id_1),
(u'ba\N{Euro Sign}r/qu\N{Euro Sign}x',
qux_id, bar_id, revision_id_2),
]
with tree.lock_read():
path_entries = list(tree.iter_entries_by_dir())
for (epath, efid, eparent, erev), (path, ie) in zip(path_and_ids,
path_entries):
self.assertEqual(epath, path) # Paths should match
self.assertIsInstance(path, str)
self.assertIsInstance(ie.file_id, bytes)
if wt.supports_setting_file_ids():
self.assertEqual(efid, ie.file_id)
self.assertEqual(eparent, ie.parent_id)
if eparent is not None:
self.assertIsInstance(ie.parent_id, bytes)
self.assertEqual(len(path_and_ids), len(path_entries),
"%r vs %r" % (
[p for (p, f, pf, r) in path_and_ids],
[p for (p, e) in path_entries]))
get_revision_id = getattr(tree, 'get_revision_id', None)
if get_revision_id is not None:
self.assertIsInstance(get_revision_id(), bytes)
last_revision = getattr(tree, 'last_revision', None)
if last_revision is not None:
self.assertIsInstance(last_revision(), bytes)
def skip_if_no_reference(self, tree):
if not getattr(tree, 'supports_tree_reference', lambda: False)():
raise TestNotApplicable('Tree references not supported')
def create_nested(self):
work_tree = self.make_branch_and_tree('wt')
with work_tree.lock_write():
self.skip_if_no_reference(work_tree)
subtree = self.make_branch_and_tree('wt/subtree')
self.build_tree(['wt/subtree/a'])
subtree.add(['a'])
subtree.commit('foo')
work_tree.add_reference(subtree)
tree = self._convert_tree(work_tree)
self.skip_if_no_reference(tree)
return tree, subtree
def test_iter_entries_with_unfollowed_reference(self):
tree, subtree = self.create_nested()
expected = [
('', 'directory'),
('subtree', 'tree-reference')]
with tree.lock_read():
path_entries = list(tree.iter_entries_by_dir(recurse_nested=False))
actual = [(path, ie.kind)
for path, ie in path_entries]
self.assertEqual(expected, actual)
def test_iter_entries_with_followed_reference(self):
tree, subtree = self.create_nested()
expected = [
('', 'directory'),
('subtree', 'directory'),
('subtree/a', 'file')]
with tree.lock_read():
path_entries = list(tree.iter_entries_by_dir(recurse_nested=True))
actual = [(path, ie.kind)
for path, ie in path_entries]
self.assertEqual(expected, actual)
|