~bzr-pqm/bzr/bzr.dev

2255.7.83 by John Arbash Meinel
Update some obvious copyright headers to include 2007.
1
# Copyright (C) 2006, 2007 Canonical Ltd
1852.6.1 by Robert Collins
Start tree implementation tests.
2
#
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.
7
#
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.
12
#
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
16
17
"""Tests for the test trees used by the tree_implementations tests."""
18
2408.1.3 by Alexander Belchenko
tree_implementations: make usage of symlinks optional
19
from bzrlib.osutils import has_symlinks
20
from bzrlib.tests import TestSkipped
1852.6.1 by Robert Collins
Start tree implementation tests.
21
from bzrlib.tests.tree_implementations import TestCaseWithTree
22
23
24
class TestTreeShapes(TestCaseWithTree):
25
26
    def test_empty_tree_no_parents(self):
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
27
        tree = self.make_branch_and_tree('.')
28
        tree = self.get_tree_no_parents_no_content(tree)
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
29
        tree.lock_read()
30
        self.addCleanup(tree.unlock)
1852.6.1 by Robert Collins
Start tree implementation tests.
31
        self.assertEqual([], tree.get_parent_ids())
32
        self.assertEqual([], tree.conflicts())
33
        self.assertEqual([], list(tree.unknowns()))
1731.1.33 by Aaron Bentley
Revert no-special-root changes
34
        self.assertEqual(['empty-root-id'], list(iter(tree)))
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
35
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
36
            [('', 'empty-root-id')],
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
37
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
38
39
    def test_abc_tree_no_parents(self):
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
40
        tree = self.make_branch_and_tree('.')
41
        tree = self.get_tree_no_parents_abc_content(tree)
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
42
        tree.lock_read()
43
        self.addCleanup(tree.unlock)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
44
        self.assertEqual([], tree.get_parent_ids())
45
        self.assertEqual([], tree.conflicts())
46
        self.assertEqual([], list(tree.unknowns()))
47
        # __iter__ has no strongly defined order
48
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
49
            set(['root-id', 'a-id', 'b-id', 'c-id']),
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
50
            set(iter(tree)))
51
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
52
            [('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
53
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
54
        self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
55
        self.assertFalse(tree.is_executable('c-id'))
56
57
    def test_abc_tree_content_2_no_parents(self):
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
58
        tree = self.make_branch_and_tree('.')
59
        tree = self.get_tree_no_parents_abc_content_2(tree)
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
60
        tree.lock_read()
61
        self.addCleanup(tree.unlock)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
62
        self.assertEqual([], tree.get_parent_ids())
63
        self.assertEqual([], tree.conflicts())
64
        self.assertEqual([], list(tree.unknowns()))
65
        # __iter__ has no strongly defined order
66
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
67
            set(['root-id', 'a-id', 'b-id', 'c-id']),
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
68
            set(iter(tree)))
69
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
70
            [('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
71
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
72
        self.assertEqualDiff('foobar\n', tree.get_file_text('a-id'))
73
        self.assertFalse(tree.is_executable('c-id'))
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
74
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
75
    def test_abc_tree_content_3_no_parents(self):
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
76
        tree = self.make_branch_and_tree('.')
77
        tree = self.get_tree_no_parents_abc_content_3(tree)
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
78
        tree.lock_read()
79
        self.addCleanup(tree.unlock)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
80
        self.assertEqual([], tree.get_parent_ids())
81
        self.assertEqual([], tree.conflicts())
82
        self.assertEqual([], list(tree.unknowns()))
83
        # __iter__ has no strongly defined order
84
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
85
            set(['root-id', 'a-id', 'b-id', 'c-id']),
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
86
            set(iter(tree)))
87
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
88
            [('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('b/c', 'c-id')],
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
89
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
90
        self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
91
        self.assertTrue(tree.is_executable('c-id'))
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
92
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
93
    def test_abc_tree_content_4_no_parents(self):
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
94
        tree = self.make_branch_and_tree('.')
95
        tree = self.get_tree_no_parents_abc_content_4(tree)
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
96
        tree.lock_read()
97
        self.addCleanup(tree.unlock)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
98
        self.assertEqual([], tree.get_parent_ids())
99
        self.assertEqual([], tree.conflicts())
100
        self.assertEqual([], list(tree.unknowns()))
101
        # __iter__ has no strongly defined order
102
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
103
            set(['root-id', 'a-id', 'b-id', 'c-id']),
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
104
            set(iter(tree)))
105
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
106
            [('', 'root-id'), ('b', 'b-id'), ('d', 'a-id'), ('b/c', 'c-id')],
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
107
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
108
        self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
109
        self.assertFalse(tree.is_executable('c-id'))
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
110
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
111
    def test_abc_tree_content_5_no_parents(self):
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
112
        tree = self.make_branch_and_tree('.')
113
        tree = self.get_tree_no_parents_abc_content_5(tree)
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
114
        tree.lock_read()
115
        self.addCleanup(tree.unlock)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
116
        self.assertEqual([], tree.get_parent_ids())
117
        self.assertEqual([], tree.conflicts())
118
        self.assertEqual([], list(tree.unknowns()))
119
        # __iter__ has no strongly defined order
120
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
121
            set(['root-id', 'a-id', 'b-id', 'c-id']),
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
122
            set(iter(tree)))
123
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
124
            [('', 'root-id'), ('b', 'b-id'), ('d', 'a-id'), ('b/c', 'c-id')],
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
125
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
126
        self.assertEqualDiff('bar\n', tree.get_file_text('a-id'))
127
        self.assertFalse(tree.is_executable('c-id'))
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
128
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
129
    def test_abc_tree_content_6_no_parents(self):
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
130
        tree = self.make_branch_and_tree('.')
131
        tree = self.get_tree_no_parents_abc_content_6(tree)
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
132
        tree.lock_read()
133
        self.addCleanup(tree.unlock)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
134
        self.assertEqual([], tree.get_parent_ids())
135
        self.assertEqual([], tree.conflicts())
136
        self.assertEqual([], list(tree.unknowns()))
137
        # __iter__ has no strongly defined order
138
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
139
            set(['root-id', 'a-id', 'b-id', 'c-id']),
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
140
            set(iter(tree)))
141
        self.assertEqual(
1731.1.33 by Aaron Bentley
Revert no-special-root changes
142
            [('', 'root-id'), ('a', 'a-id'), ('b', 'b-id'), ('e', 'c-id')],
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
143
            [(path, node.file_id) for path, node in tree.iter_entries_by_dir()])
144
        self.assertEqualDiff('contents of a\n', tree.get_file_text('a-id'))
145
        self.assertTrue(tree.is_executable('c-id'))
2294.1.1 by John Arbash Meinel
Track down some non-ascii deficiencies in commit logic.
146
1852.15.1 by Robert Collins
Add a complex test tree for use with Tree.walkdirs.
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.
2408.1.3 by Alexander Belchenko
tree_implementations: make usage of symlinks optional
151
        if not has_symlinks():
152
            raise TestSkipped('No symlink support')
1852.15.1 by Robert Collins
Add a complex test tree for use with Tree.walkdirs.
153
        tree = self.get_tree_with_subdirs_and_all_content_types()
2255.2.64 by John Arbash Meinel
Add the DirStateRevisionTree to the list of 'tree_implementations'
154
        tree.lock_read()
155
        self.addCleanup(tree.unlock)
1852.15.1 by Robert Collins
Add a complex test tree for use with Tree.walkdirs.
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
2255.2.17 by Robert Collins
tweaks - finishes off all the test_test_trees tests for dirstate.
160
        tree_root = tree.path2id('')
1852.15.1 by Robert Collins
Add a complex test tree for use with Tree.walkdirs.
161
        self.assertEqual(
2255.2.17 by Robert Collins
tweaks - finishes off all the test_test_trees tests for dirstate.
162
            set([tree_root,
1852.15.1 by Robert Collins
Add a complex test tree for use with Tree.walkdirs.
163
                '2file',
164
                '1top-dir',
165
                '1file-in-1topdir',
166
                '0dir-in-1topdir',
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
167
                 u'0utf\u1234file'.encode('utf8'),
1852.15.1 by Robert Collins
Add a complex test tree for use with Tree.walkdirs.
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(
2255.2.17 by Robert Collins
tweaks - finishes off all the test_test_trees tests for dirstate.
174
            [('', tree_root, 'directory'),
1852.15.1 by Robert Collins
Add a complex test tree for use with Tree.walkdirs.
175
             ('0file', '2file', 'file'),
176
             ('1top-dir', '1top-dir', 'directory'),
2255.2.107 by John Arbash Meinel
(working), fix dirstate to use utf8 file ids.
177
             (u'2utf\u1234file', u'0utf\u1234file'.encode('utf8'), 'file'),
1852.15.1 by Robert Collins
Add a complex test tree for use with Tree.walkdirs.
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()])
2255.2.106 by John Arbash Meinel
[merge] bzr.dev 2298 (broken)
182
2408.1.3 by Alexander Belchenko
tree_implementations: make usage of symlinks optional
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
2294.1.1 by John Arbash Meinel
Track down some non-ascii deficiencies in commit logic.
215
    def test_tree_with_utf8(self):
216
        tree = self.make_branch_and_tree('.')
217
        tree = self.get_tree_with_utf8(tree)
2294.1.2 by John Arbash Meinel
Track down and add tests that all tree.commit() can handle
218
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
219
        revision_id = u'r\xe9v-1'.encode('utf8')
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
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),
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
228
                       ]
229
        tree.lock_read()
230
        try:
231
            path_entries = list(tree.iter_entries_by_dir())
232
        finally:
233
            tree.unlock()
234
2294.1.7 by John Arbash Meinel
Add tests for parent ids in test_test_trees
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)
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
239
            self.assertIsInstance(ie.file_id, str)
2294.1.7 by John Arbash Meinel
Add tests for parent ids in test_test_trees
240
            self.assertEqual(expected[2], ie.parent_id)
241
            if expected[2] is not None:
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
242
                self.assertIsInstance(ie.parent_id, str)
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
243
            # WorkingTree's return None for the last modified revision
2294.1.7 by John Arbash Meinel
Add tests for parent ids in test_test_trees
244
            if ie.revision is not None:
245
                self.assertIsInstance(ie.revision, str)
246
                if expected[0] != '':
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
247
                    # Some trees will preserve the revision id of the tree root,
248
                    # but not all will
2294.1.7 by John Arbash Meinel
Add tests for parent ids in test_test_trees
249
                    self.assertEqual(revision_id, ie.revision)
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
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
2294.1.2 by John Arbash Meinel
Track down and add tests that all tree.commit() can handle
258
    def test_tree_with_merged_utf8(self):
259
        tree = self.make_branch_and_tree('.')
260
        tree = self.get_tree_with_merged_utf8(tree)
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
261
262
        revision_id_1 = u'r\xe9v-1'.encode('utf8')
263
        revision_id_2 = u'r\xe9v-2'.encode('utf8')
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
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),
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
274
                       ]
275
        tree.lock_read()
276
        try:
277
            path_entries = list(tree.iter_entries_by_dir())
278
        finally:
279
            tree.unlock()
280
2294.1.7 by John Arbash Meinel
Add tests for parent ids in test_test_trees
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)
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
285
            self.assertIsInstance(ie.file_id, str)
2294.1.7 by John Arbash Meinel
Add tests for parent ids in test_test_trees
286
            self.assertEqual(expected[2], ie.parent_id)
287
            if expected[2] is not None:
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
288
                self.assertIsInstance(ie.parent_id, str)
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
289
            # WorkingTree's return None for the last modified revision
2294.1.7 by John Arbash Meinel
Add tests for parent ids in test_test_trees
290
            if ie.revision is not None:
291
                self.assertIsInstance(ie.revision, str)
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
292
                if expected[0] == '':
293
                    # Some trees will preserve the revision id of the tree root,
294
                    # but not all will
295
                    continue
2294.1.7 by John Arbash Meinel
Add tests for parent ids in test_test_trees
296
                self.assertEqual(expected[3], ie.revision)
2294.1.3 by John Arbash Meinel
Make sure the inventory enrtries returned are properly formed.
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)