~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/tree_implementations/test_test_trees.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Tests for the test trees used by the tree_implementations tests."""
18
18
 
19
 
from bzrlib import inventory 
 
19
from bzrlib.osutils import has_symlinks
 
20
from bzrlib.tests import TestSkipped
20
21
from bzrlib.tests.tree_implementations import TestCaseWithTree
21
22
 
22
23
 
25
26
    def test_empty_tree_no_parents(self):
26
27
        tree = self.make_branch_and_tree('.')
27
28
        tree = self.get_tree_no_parents_no_content(tree)
 
29
        tree.lock_read()
 
30
        self.addCleanup(tree.unlock)
28
31
        self.assertEqual([], tree.get_parent_ids())
29
32
        self.assertEqual([], tree.conflicts())
30
33
        self.assertEqual([], list(tree.unknowns()))
31
 
        self.assertEqual([inventory.ROOT_ID], list(iter(tree)))
 
34
        self.assertEqual(['empty-root-id'], list(iter(tree)))
32
35
        self.assertEqual(
33
 
            [('', inventory.ROOT_ID)],
 
36
            [('', 'empty-root-id')],
34
37
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
35
38
 
36
39
    def test_abc_tree_no_parents(self):
37
40
        tree = self.make_branch_and_tree('.')
38
41
        tree = self.get_tree_no_parents_abc_content(tree)
 
42
        tree.lock_read()
 
43
        self.addCleanup(tree.unlock)
39
44
        self.assertEqual([], tree.get_parent_ids())
40
45
        self.assertEqual([], tree.conflicts())
41
46
        self.assertEqual([], list(tree.unknowns()))
42
47
        # __iter__ has no strongly defined order
43
48
        self.assertEqual(
44
 
            set([inventory.ROOT_ID, 'a-id', 'b-id', 'c-id']),
 
49
            set(['root-id', 'a-id', 'b-id', 'c-id']),
45
50
            set(iter(tree)))
46
51
        self.assertEqual(
47
 
            [('', inventory.ROOT_ID), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
 
52
            [('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
48
53
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
49
54
        self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
50
55
        self.assertFalse(tree.is_executable('c-id'))
52
57
    def test_abc_tree_content_2_no_parents(self):
53
58
        tree = self.make_branch_and_tree('.')
54
59
        tree = self.get_tree_no_parents_abc_content_2(tree)
 
60
        tree.lock_read()
 
61
        self.addCleanup(tree.unlock)
55
62
        self.assertEqual([], tree.get_parent_ids())
56
63
        self.assertEqual([], tree.conflicts())
57
64
        self.assertEqual([], list(tree.unknowns()))
58
65
        # __iter__ has no strongly defined order
59
66
        self.assertEqual(
60
 
            set([inventory.ROOT_ID, 'a-id', 'b-id', 'c-id']),
 
67
            set(['root-id', 'a-id', 'b-id', 'c-id']),
61
68
            set(iter(tree)))
62
69
        self.assertEqual(
63
 
            [('', inventory.ROOT_ID), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
 
70
            [('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
64
71
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
65
72
        self.assertEqualDiff('foobar\n', tree.get_file_text('a-id'))
66
73
        self.assertFalse(tree.is_executable('c-id'))
67
 
        
 
74
 
68
75
    def test_abc_tree_content_3_no_parents(self):
69
76
        tree = self.make_branch_and_tree('.')
70
77
        tree = self.get_tree_no_parents_abc_content_3(tree)
 
78
        tree.lock_read()
 
79
        self.addCleanup(tree.unlock)
71
80
        self.assertEqual([], tree.get_parent_ids())
72
81
        self.assertEqual([], tree.conflicts())
73
82
        self.assertEqual([], list(tree.unknowns()))
74
83
        # __iter__ has no strongly defined order
75
84
        self.assertEqual(
76
 
            set([inventory.ROOT_ID, 'a-id', 'b-id', 'c-id']),
 
85
            set(['root-id', 'a-id', 'b-id', 'c-id']),
77
86
            set(iter(tree)))
78
87
        self.assertEqual(
79
 
            [('', inventory.ROOT_ID), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
 
88
            [('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
80
89
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
81
90
        self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
82
91
        self.assertTrue(tree.is_executable('c-id'))
83
 
        
 
92
 
84
93
    def test_abc_tree_content_4_no_parents(self):
85
94
        tree = self.make_branch_and_tree('.')
86
95
        tree = self.get_tree_no_parents_abc_content_4(tree)
 
96
        tree.lock_read()
 
97
        self.addCleanup(tree.unlock)
87
98
        self.assertEqual([], tree.get_parent_ids())
88
99
        self.assertEqual([], tree.conflicts())
89
100
        self.assertEqual([], list(tree.unknowns()))
90
101
        # __iter__ has no strongly defined order
91
102
        self.assertEqual(
92
 
            set([inventory.ROOT_ID, 'a-id', 'b-id', 'c-id']),
 
103
            set(['root-id', 'a-id', 'b-id', 'c-id']),
93
104
            set(iter(tree)))
94
105
        self.assertEqual(
95
 
            [('', inventory.ROOT_ID), ('b', 'b-id'), ('d', 'a-id'), ('b/c', 'c-id')],
 
106
            [('', 'root-id'), ('b', 'b-id'), ('d', 'a-id'), ('b/c', 'c-id')],
96
107
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
97
108
        self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
98
109
        self.assertFalse(tree.is_executable('c-id'))
99
 
        
 
110
 
100
111
    def test_abc_tree_content_5_no_parents(self):
101
112
        tree = self.make_branch_and_tree('.')
102
113
        tree = self.get_tree_no_parents_abc_content_5(tree)
 
114
        tree.lock_read()
 
115
        self.addCleanup(tree.unlock)
103
116
        self.assertEqual([], tree.get_parent_ids())
104
117
        self.assertEqual([], tree.conflicts())
105
118
        self.assertEqual([], list(tree.unknowns()))
106
119
        # __iter__ has no strongly defined order
107
120
        self.assertEqual(
108
 
            set([inventory.ROOT_ID, 'a-id', 'b-id', 'c-id']),
 
121
            set(['root-id', 'a-id', 'b-id', 'c-id']),
109
122
            set(iter(tree)))
110
123
        self.assertEqual(
111
 
            [('', inventory.ROOT_ID), ('b', 'b-id'), ('d', 'a-id'), ('b/c', 'c-id')],
 
124
            [('', 'root-id'), ('b', 'b-id'), ('d', 'a-id'), ('b/c', 'c-id')],
112
125
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
113
126
        self.assertEqualDiff('bar\n', tree.get_file_text('a-id'))
114
127
        self.assertFalse(tree.is_executable('c-id'))
115
 
        
 
128
 
116
129
    def test_abc_tree_content_6_no_parents(self):
117
130
        tree = self.make_branch_and_tree('.')
118
131
        tree = self.get_tree_no_parents_abc_content_6(tree)
 
132
        tree.lock_read()
 
133
        self.addCleanup(tree.unlock)
119
134
        self.assertEqual([], tree.get_parent_ids())
120
135
        self.assertEqual([], tree.conflicts())
121
136
        self.assertEqual([], list(tree.unknowns()))
122
137
        # __iter__ has no strongly defined order
123
138
        self.assertEqual(
124
 
            set([inventory.ROOT_ID, 'a-id', 'b-id', 'c-id']),
 
139
            set(['root-id', 'a-id', 'b-id', 'c-id']),
125
140
            set(iter(tree)))
126
141
        self.assertEqual(
127
 
            [('', inventory.ROOT_ID), ('a', 'a-id'), ('b', 'b-id'), ('e', 'c-id')],
 
142
            [('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('e', 'c-id')],
128
143
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
129
144
        self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
130
145
        self.assertTrue(tree.is_executable('c-id'))
 
146
 
 
147
    def test_tree_with_subdirs_and_all_content_types(self):
 
148
        # currently this test tree requires unicode. It might be good
 
149
        # to have it simply stop having the single unicode file in it
 
150
        # when dealing with a non-unicode filesystem.
 
151
        if not has_symlinks():
 
152
            raise TestSkipped('No symlink support')
 
153
        tree = self.get_tree_with_subdirs_and_all_content_types()
 
154
        tree.lock_read()
 
155
        self.addCleanup(tree.unlock)
 
156
        self.assertEqual([], tree.get_parent_ids())
 
157
        self.assertEqual([], tree.conflicts())
 
158
        self.assertEqual([], list(tree.unknowns()))
 
159
        # __iter__ has no strongly defined order
 
160
        tree_root = tree.path2id('')
 
161
        self.assertEqual(
 
162
            set([tree_root,
 
163
                '2file',
 
164
                '1top-dir',
 
165
                '1file-in-1topdir',
 
166
                '0dir-in-1topdir',
 
167
                 u'0utf\u1234file'.encode('utf8'),
 
168
                'symlink',
 
169
                 ]),
 
170
            set(iter(tree)))
 
171
        # note that the order of the paths and fileids is deliberately 
 
172
        # mismatched to ensure that the result order is path based.
 
173
        self.assertEqual(
 
174
            [('', tree_root, 'directory'),
 
175
             ('0file', '2file', 'file'),
 
176
             ('1top-dir', '1top-dir', 'directory'),
 
177
             (u'2utf\u1234file', u'0utf\u1234file'.encode('utf8'), 'file'),
 
178
             ('symlink', 'symlink', 'symlink'),
 
179
             ('1top-dir/0file-in-1topdir', '1file-in-1topdir', 'file'),
 
180
             ('1top-dir/1dir-in-1topdir', '0dir-in-1topdir', 'directory')],
 
181
            [(path, node.file_id, node.kind) for path, node in tree.iter_entries_by_dir()])
 
182
 
 
183
    def test_tree_with_subdirs_and_all_content_types_wo_symlinks(self):
 
184
        # currently this test tree requires unicode. It might be good
 
185
        # to have it simply stop having the single unicode file in it
 
186
        # when dealing with a non-unicode filesystem.
 
187
        tree = self.get_tree_with_subdirs_and_all_supported_content_types(False)
 
188
        tree.lock_read()
 
189
        self.addCleanup(tree.unlock)
 
190
        self.assertEqual([], tree.get_parent_ids())
 
191
        self.assertEqual([], tree.conflicts())
 
192
        self.assertEqual([], list(tree.unknowns()))
 
193
        # __iter__ has no strongly defined order
 
194
        tree_root = tree.path2id('')
 
195
        self.assertEqual(
 
196
            set([tree_root,
 
197
                '2file',
 
198
                '1top-dir',
 
199
                '1file-in-1topdir',
 
200
                '0dir-in-1topdir',
 
201
                 u'0utf\u1234file'.encode('utf8'),
 
202
                 ]),
 
203
            set(iter(tree)))
 
204
        # note that the order of the paths and fileids is deliberately 
 
205
        # mismatched to ensure that the result order is path based.
 
206
        self.assertEqual(
 
207
            [('', tree_root, 'directory'),
 
208
             ('0file', '2file', 'file'),
 
209
             ('1top-dir', '1top-dir', 'directory'),
 
210
             (u'2utf\u1234file', u'0utf\u1234file'.encode('utf8'), 'file'),
 
211
             ('1top-dir/0file-in-1topdir', '1file-in-1topdir', 'file'),
 
212
             ('1top-dir/1dir-in-1topdir', '0dir-in-1topdir', 'directory')],
 
213
            [(path, node.file_id, node.kind) for path, node in tree.iter_entries_by_dir()])
 
214
 
 
215
    def test_tree_with_utf8(self):
 
216
        tree = self.make_branch_and_tree('.')
 
217
        tree = self.get_tree_with_utf8(tree)
 
218
 
 
219
        revision_id = u'r\xe9v-1'.encode('utf8')
 
220
        root_id = 'TREE_ROOT'
 
221
        bar_id = u'b\xe5r-id'.encode('utf8')
 
222
        foo_id = u'f\xf6-id'.encode('utf8')
 
223
        baz_id = u'b\xe1z-id'.encode('utf8')
 
224
        path_and_ids = [(u'', root_id, None),
 
225
                        (u'b\xe5r', bar_id, root_id),
 
226
                        (u'f\xf6', foo_id, root_id),
 
227
                        (u'b\xe5r/b\xe1z', baz_id, bar_id),
 
228
                       ]
 
229
        tree.lock_read()
 
230
        try:
 
231
            path_entries = list(tree.iter_entries_by_dir())
 
232
        finally:
 
233
            tree.unlock()
 
234
 
 
235
        for expected, (path, ie) in zip(path_and_ids, path_entries):
 
236
            self.assertEqual(expected[0], path) # Paths should match
 
237
            self.assertIsInstance(path, unicode)
 
238
            self.assertEqual(expected[1], ie.file_id)
 
239
            self.assertIsInstance(ie.file_id, str)
 
240
            self.assertEqual(expected[2], ie.parent_id)
 
241
            if expected[2] is not None:
 
242
                self.assertIsInstance(ie.parent_id, str)
 
243
            # WorkingTree's return None for the last modified revision
 
244
            if ie.revision is not None:
 
245
                self.assertIsInstance(ie.revision, str)
 
246
                if expected[0] != '':
 
247
                    # Some trees will preserve the revision id of the tree root,
 
248
                    # but not all will
 
249
                    self.assertEqual(revision_id, ie.revision)
 
250
        self.assertEqual(len(path_and_ids), len(path_entries))
 
251
        get_revision_id = getattr(tree, 'get_revision_id', None)
 
252
        if get_revision_id is not None:
 
253
            self.assertIsInstance(get_revision_id(), str)
 
254
        last_revision = getattr(tree, 'last_revision', None)
 
255
        if last_revision is not None:
 
256
            self.assertIsInstance(last_revision(), str)
 
257
 
 
258
    def test_tree_with_merged_utf8(self):
 
259
        tree = self.make_branch_and_tree('.')
 
260
        tree = self.get_tree_with_merged_utf8(tree)
 
261
 
 
262
        revision_id_1 = u'r\xe9v-1'.encode('utf8')
 
263
        revision_id_2 = u'r\xe9v-2'.encode('utf8')
 
264
        root_id = 'TREE_ROOT'
 
265
        bar_id = u'b\xe5r-id'.encode('utf8')
 
266
        foo_id = u'f\xf6-id'.encode('utf8')
 
267
        baz_id = u'b\xe1z-id'.encode('utf8')
 
268
        zez_id = u'z\xf7z-id'.encode('utf8')
 
269
        path_and_ids = [(u'', root_id, None, None),
 
270
                        (u'b\xe5r', bar_id, root_id, revision_id_1),
 
271
                        (u'f\xf6', foo_id, root_id, revision_id_1),
 
272
                        (u'b\xe5r/b\xe1z', baz_id, bar_id, revision_id_1),
 
273
                        (u'b\xe5r/z\xf7z', zez_id, bar_id, revision_id_2),
 
274
                       ]
 
275
        tree.lock_read()
 
276
        try:
 
277
            path_entries = list(tree.iter_entries_by_dir())
 
278
        finally:
 
279
            tree.unlock()
 
280
 
 
281
        for expected, (path, ie) in zip(path_and_ids, path_entries):
 
282
            self.assertEqual(expected[0], path) # Paths should match
 
283
            self.assertIsInstance(path, unicode)
 
284
            self.assertEqual(expected[1], ie.file_id)
 
285
            self.assertIsInstance(ie.file_id, str)
 
286
            self.assertEqual(expected[2], ie.parent_id)
 
287
            if expected[2] is not None:
 
288
                self.assertIsInstance(ie.parent_id, str)
 
289
            # WorkingTree's return None for the last modified revision
 
290
            if ie.revision is not None:
 
291
                self.assertIsInstance(ie.revision, str)
 
292
                if expected[0] == '':
 
293
                    # Some trees will preserve the revision id of the tree root,
 
294
                    # but not all will
 
295
                    continue
 
296
                self.assertEqual(expected[3], ie.revision)
 
297
        self.assertEqual(len(path_and_ids), len(path_entries))
 
298
        get_revision_id = getattr(tree, 'get_revision_id', None)
 
299
        if get_revision_id is not None:
 
300
            self.assertIsInstance(get_revision_id(), str)
 
301
        last_revision = getattr(tree, 'last_revision', None)
 
302
        if last_revision is not None:
 
303
            self.assertIsInstance(last_revision(), str)