1
# Copyright (C) 2006, 2007 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the test trees used by the tree_implementations tests."""
19
from bzrlib.tests import SymlinkFeature
20
from bzrlib.tests.tree_implementations import TestCaseWithTree
23
class TestTreeShapes(TestCaseWithTree):
25
def test_empty_tree_no_parents(self):
26
tree = self.make_branch_and_tree('.')
27
tree = self.get_tree_no_parents_no_content(tree)
29
self.addCleanup(tree.unlock)
30
self.assertEqual([], tree.get_parent_ids())
31
self.assertEqual([], tree.conflicts())
32
self.assertEqual([], list(tree.unknowns()))
33
self.assertEqual(['empty-root-id'], list(iter(tree)))
35
[('', 'empty-root-id')],
36
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
38
def test_abc_tree_no_parents(self):
39
tree = self.make_branch_and_tree('.')
40
tree = self.get_tree_no_parents_abc_content(tree)
42
self.addCleanup(tree.unlock)
43
self.assertEqual([], tree.get_parent_ids())
44
self.assertEqual([], tree.conflicts())
45
self.assertEqual([], list(tree.unknowns()))
46
# __iter__ has no strongly defined order
48
set(['root-id', 'a-id', 'b-id', 'c-id']),
51
[('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
52
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
53
self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
54
self.assertFalse(tree.is_executable('c-id', path='b/c'))
56
def test_abc_tree_content_2_no_parents(self):
57
tree = self.make_branch_and_tree('.')
58
tree = self.get_tree_no_parents_abc_content_2(tree)
60
self.addCleanup(tree.unlock)
61
self.assertEqual([], tree.get_parent_ids())
62
self.assertEqual([], tree.conflicts())
63
self.assertEqual([], list(tree.unknowns()))
64
# __iter__ has no strongly defined order
66
set(['root-id', 'a-id', 'b-id', 'c-id']),
69
[('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
70
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
71
self.assertEqualDiff('foobar\n', tree.get_file_text('a-id'))
72
self.assertFalse(tree.is_executable('c-id'))
74
def test_abc_tree_content_3_no_parents(self):
75
tree = self.make_branch_and_tree('.')
76
tree = self.get_tree_no_parents_abc_content_3(tree)
78
self.addCleanup(tree.unlock)
79
self.assertEqual([], tree.get_parent_ids())
80
self.assertEqual([], tree.conflicts())
81
self.assertEqual([], list(tree.unknowns()))
82
# __iter__ has no strongly defined order
84
set(['root-id', 'a-id', 'b-id', 'c-id']),
87
[('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
88
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
89
self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
90
self.assertTrue(tree.is_executable('c-id'))
92
def test_abc_tree_content_4_no_parents(self):
93
tree = self.make_branch_and_tree('.')
94
tree = self.get_tree_no_parents_abc_content_4(tree)
96
self.addCleanup(tree.unlock)
97
self.assertEqual([], tree.get_parent_ids())
98
self.assertEqual([], tree.conflicts())
99
self.assertEqual([], list(tree.unknowns()))
100
# __iter__ has no strongly defined order
102
set(['root-id', 'a-id', 'b-id', 'c-id']),
105
[('', 'root-id'), ('b', 'b-id'), ('d', 'a-id'), ('b/c', 'c-id')],
106
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
107
self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
108
self.assertFalse(tree.is_executable('c-id'))
110
def test_abc_tree_content_5_no_parents(self):
111
tree = self.make_branch_and_tree('.')
112
tree = self.get_tree_no_parents_abc_content_5(tree)
114
self.addCleanup(tree.unlock)
115
self.assertEqual([], tree.get_parent_ids())
116
self.assertEqual([], tree.conflicts())
117
self.assertEqual([], list(tree.unknowns()))
118
# __iter__ has no strongly defined order
120
set(['root-id', 'a-id', 'b-id', 'c-id']),
123
[('', 'root-id'), ('b', 'b-id'), ('d', 'a-id'), ('b/c', 'c-id')],
124
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
125
self.assertEqualDiff('bar\n', tree.get_file_text('a-id'))
126
self.assertFalse(tree.is_executable('c-id'))
128
def test_abc_tree_content_6_no_parents(self):
129
tree = self.make_branch_and_tree('.')
130
tree = self.get_tree_no_parents_abc_content_6(tree)
132
self.addCleanup(tree.unlock)
133
self.assertEqual([], tree.get_parent_ids())
134
self.assertEqual([], tree.conflicts())
135
self.assertEqual([], list(tree.unknowns()))
136
# __iter__ has no strongly defined order
138
set(['root-id', 'a-id', 'b-id', 'c-id']),
141
[('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('e', 'c-id')],
142
[(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
143
self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
144
self.assertTrue(tree.is_executable('c-id'))
146
def test_tree_with_subdirs_and_all_content_types(self):
147
# currently this test tree requires unicode. It might be good
148
# to have it simply stop having the single unicode file in it
149
# when dealing with a non-unicode filesystem.
150
self.requireFeature(SymlinkFeature)
151
tree = self.get_tree_with_subdirs_and_all_content_types()
153
self.addCleanup(tree.unlock)
154
self.assertEqual([], tree.get_parent_ids())
155
self.assertEqual([], tree.conflicts())
156
self.assertEqual([], list(tree.unknowns()))
157
# __iter__ has no strongly defined order
158
tree_root = tree.path2id('')
165
u'0utf\u1234file'.encode('utf8'),
169
# note that the order of the paths and fileids is deliberately
170
# mismatched to ensure that the result order is path based.
172
[('', tree_root, 'directory'),
173
('0file', '2file', 'file'),
174
('1top-dir', '1top-dir', 'directory'),
175
(u'2utf\u1234file', u'0utf\u1234file'.encode('utf8'), 'file'),
176
('symlink', 'symlink', 'symlink'),
177
('1top-dir/0file-in-1topdir', '1file-in-1topdir', 'file'),
178
('1top-dir/1dir-in-1topdir', '0dir-in-1topdir', 'directory')],
179
[(path, node.file_id, node.kind) for path, node in tree.iter_entries_by_dir()])
181
def test_tree_with_subdirs_and_all_content_types_wo_symlinks(self):
182
# currently this test tree requires unicode. It might be good
183
# to have it simply stop having the single unicode file in it
184
# when dealing with a non-unicode filesystem.
185
tree = self.get_tree_with_subdirs_and_all_supported_content_types(False)
187
self.addCleanup(tree.unlock)
188
self.assertEqual([], tree.get_parent_ids())
189
self.assertEqual([], tree.conflicts())
190
self.assertEqual([], list(tree.unknowns()))
191
# __iter__ has no strongly defined order
192
tree_root = tree.path2id('')
199
u'0utf\u1234file'.encode('utf8'),
202
# note that the order of the paths and fileids is deliberately
203
# mismatched to ensure that the result order is path based.
205
[('', tree_root, 'directory'),
206
('0file', '2file', 'file'),
207
('1top-dir', '1top-dir', 'directory'),
208
(u'2utf\u1234file', u'0utf\u1234file'.encode('utf8'), 'file'),
209
('1top-dir/0file-in-1topdir', '1file-in-1topdir', 'file'),
210
('1top-dir/1dir-in-1topdir', '0dir-in-1topdir', 'directory')],
211
[(path, node.file_id, node.kind) for path, node in tree.iter_entries_by_dir()])
213
def test_tree_with_utf8(self):
214
tree = self.make_branch_and_tree('.')
215
tree = self.get_tree_with_utf8(tree)
217
revision_id = u'r\xe9v-1'.encode('utf8')
218
root_id = 'TREE_ROOT'
219
bar_id = u'ba\N{Euro Sign}r-id'.encode('utf8')
220
foo_id = u'fo\N{Euro Sign}o-id'.encode('utf8')
221
baz_id = u'ba\N{Euro Sign}z-id'.encode('utf8')
222
path_and_ids = [(u'', root_id, None, None),
223
(u'ba\N{Euro Sign}r', bar_id, root_id, revision_id),
224
(u'fo\N{Euro Sign}o', foo_id, root_id, revision_id),
225
(u'ba\N{Euro Sign}r/ba\N{Euro Sign}z',
226
baz_id, bar_id, revision_id),
230
path_entries = list(tree.iter_entries_by_dir())
234
for expected, (path, ie) in zip(path_and_ids, path_entries):
235
self.assertEqual(expected[0], path) # Paths should match
236
self.assertIsInstance(path, unicode)
237
self.assertEqual(expected[1], ie.file_id)
238
self.assertIsInstance(ie.file_id, str)
239
self.assertEqual(expected[2], ie.parent_id)
240
if expected[2] is not None:
241
self.assertIsInstance(ie.parent_id, str)
242
# WorkingTree's return None for the last modified revision
243
if ie.revision is not None:
244
self.assertIsInstance(ie.revision, str)
245
if expected[0] != '':
246
# Some trees will preserve the revision id of the tree root,
248
self.assertEqual(revision_id, ie.revision)
249
self.assertEqual(len(path_and_ids), len(path_entries))
250
get_revision_id = getattr(tree, 'get_revision_id', None)
251
if get_revision_id is not None:
252
self.assertIsInstance(get_revision_id(), str)
253
last_revision = getattr(tree, 'last_revision', None)
254
if last_revision is not None:
255
self.assertIsInstance(last_revision(), str)
257
def test_tree_with_merged_utf8(self):
258
tree = self.make_branch_and_tree('.')
259
tree = self.get_tree_with_merged_utf8(tree)
261
revision_id_1 = u'r\xe9v-1'.encode('utf8')
262
revision_id_2 = u'r\xe9v-2'.encode('utf8')
263
root_id = 'TREE_ROOT'
264
bar_id = u'ba\N{Euro Sign}r-id'.encode('utf8')
265
foo_id = u'fo\N{Euro Sign}o-id'.encode('utf8')
266
baz_id = u'ba\N{Euro Sign}z-id'.encode('utf8')
267
qux_id = u'qu\N{Euro Sign}x-id'.encode('utf8')
268
path_and_ids = [(u'', root_id, None, None),
269
(u'ba\N{Euro Sign}r', bar_id, root_id, revision_id_1),
270
(u'fo\N{Euro Sign}o', foo_id, root_id, revision_id_1),
271
(u'ba\N{Euro Sign}r/ba\N{Euro Sign}z',
272
baz_id, bar_id, revision_id_1),
273
(u'ba\N{Euro Sign}r/qu\N{Euro Sign}x',
274
qux_id, bar_id, revision_id_2),
278
path_entries = list(tree.iter_entries_by_dir())
282
for (epath, efid, eparent, erev), (path, ie) in zip(path_and_ids,
284
self.assertEqual(epath, path) # Paths should match
285
self.assertIsInstance(path, unicode)
286
self.assertEqual(efid, ie.file_id)
287
self.assertIsInstance(ie.file_id, str)
288
self.assertEqual(eparent, ie.parent_id)
289
if eparent is not None:
290
self.assertIsInstance(ie.parent_id, str)
291
# WorkingTree's return None for the last modified revision
292
if ie.revision is not None:
293
self.assertIsInstance(ie.revision, str)
295
# Some trees will preserve the revision id of the tree root,
298
self.assertEqual(erev, ie.revision)
299
self.assertEqual(len(path_and_ids), len(path_entries))
300
get_revision_id = getattr(tree, 'get_revision_id', None)
301
if get_revision_id is not None:
302
self.assertIsInstance(get_revision_id(), str)
303
last_revision = getattr(tree, 'last_revision', None)
304
if last_revision is not None:
305
self.assertIsInstance(last_revision(), str)