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'b\xe5r-id'.encode('utf8')
220
foo_id = u'f\xf6-id'.encode('utf8')
221
baz_id = u'b\xe1z-id'.encode('utf8')
222
path_and_ids = [(u'', root_id, None),
223
(u'b\xe5r', bar_id, root_id),
224
(u'f\xf6', foo_id, root_id),
225
(u'b\xe5r/b\xe1z', baz_id, bar_id),
229
path_entries = list(tree.iter_entries_by_dir())
233
for expected, (path, ie) in zip(path_and_ids, path_entries):
234
self.assertEqual(expected[0], path) # Paths should match
235
self.assertIsInstance(path, unicode)
236
self.assertEqual(expected[1], ie.file_id)
237
self.assertIsInstance(ie.file_id, str)
238
self.assertEqual(expected[2], ie.parent_id)
239
if expected[2] is not None:
240
self.assertIsInstance(ie.parent_id, str)
241
# WorkingTree's return None for the last modified revision
242
if ie.revision is not None:
243
self.assertIsInstance(ie.revision, str)
244
if expected[0] != '':
245
# Some trees will preserve the revision id of the tree root,
247
self.assertEqual(revision_id, ie.revision)
248
self.assertEqual(len(path_and_ids), len(path_entries))
249
get_revision_id = getattr(tree, 'get_revision_id', None)
250
if get_revision_id is not None:
251
self.assertIsInstance(get_revision_id(), str)
252
last_revision = getattr(tree, 'last_revision', None)
253
if last_revision is not None:
254
self.assertIsInstance(last_revision(), str)
256
def test_tree_with_merged_utf8(self):
257
tree = self.make_branch_and_tree('.')
258
tree = self.get_tree_with_merged_utf8(tree)
260
revision_id_1 = u'r\xe9v-1'.encode('utf8')
261
revision_id_2 = u'r\xe9v-2'.encode('utf8')
262
root_id = 'TREE_ROOT'
263
bar_id = u'b\xe5r-id'.encode('utf8')
264
foo_id = u'f\xf6-id'.encode('utf8')
265
baz_id = u'b\xe1z-id'.encode('utf8')
266
zez_id = u'z\xf7z-id'.encode('utf8')
267
path_and_ids = [(u'', root_id, None, None),
268
(u'b\xe5r', bar_id, root_id, revision_id_1),
269
(u'f\xf6', foo_id, root_id, revision_id_1),
270
(u'b\xe5r/b\xe1z', baz_id, bar_id, revision_id_1),
271
(u'b\xe5r/z\xf7z', zez_id, bar_id, revision_id_2),
275
path_entries = list(tree.iter_entries_by_dir())
279
for expected, (path, ie) in zip(path_and_ids, path_entries):
280
self.assertEqual(expected[0], path) # Paths should match
281
self.assertIsInstance(path, unicode)
282
self.assertEqual(expected[1], ie.file_id)
283
self.assertIsInstance(ie.file_id, str)
284
self.assertEqual(expected[2], ie.parent_id)
285
if expected[2] is not None:
286
self.assertIsInstance(ie.parent_id, str)
287
# WorkingTree's return None for the last modified revision
288
if ie.revision is not None:
289
self.assertIsInstance(ie.revision, str)
290
if expected[0] == '':
291
# Some trees will preserve the revision id of the tree root,
294
self.assertEqual(expected[3], ie.revision)
295
self.assertEqual(len(path_and_ids), len(path_entries))
296
get_revision_id = getattr(tree, 'get_revision_id', None)
297
if get_revision_id is not None:
298
self.assertIsInstance(get_revision_id(), str)
299
last_revision = getattr(tree, 'last_revision', None)
300
if last_revision is not None:
301
self.assertIsInstance(last_revision(), str)