/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006-2009, 2011, 2012, 2016 Canonical Ltd
1551.9.21 by Aaron Bentley
Fix copyright statements
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1551.9.21 by Aaron Bentley
Fix copyright statements
16
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
17
from breezy import (
2255.11.5 by Martin Pool
Tree.id2path should raise NoSuchId, not return None.
18
    errors,
3363.13.1 by Aaron Bentley
Implement PreviewTree.extras
19
    conflicts,
3363.15.4 by Aaron Bentley
Implement PreviewTree.get_file_sha1 properly
20
    osutils,
3363.13.1 by Aaron Bentley
Implement PreviewTree.extras
21
    revisiontree,
2255.2.180 by Martin Pool
merge dirstate
22
    tests,
6670.4.3 by Jelmer Vernooij
Fix more imports.
23
    )
24
from breezy.bzr import (
3363.13.1 by Aaron Bentley
Implement PreviewTree.extras
25
    workingtree_4,
2255.11.5 by Martin Pool
Tree.id2path should raise NoSuchId, not return None.
26
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
27
from breezy.tests import TestSkipped
28
from breezy.tests.per_tree import TestCaseWithTree
5967.7.1 by Martin Pool
Deprecate __contains__ on Tree and Inventory
29
1551.9.16 by Aaron Bentley
Implement Tree.annotate_iter for RevisionTree and WorkingTree
30
31
class TestAnnotate(TestCaseWithTree):
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
32
1551.9.16 by Aaron Bentley
Implement Tree.annotate_iter for RevisionTree and WorkingTree
33
    def test_annotate(self):
34
        work_tree = self.make_branch_and_tree('wt')
35
        tree = self.get_tree_no_parents_abc_content(work_tree)
36
        tree_revision = getattr(tree, 'get_revision_id', lambda: 'current:')()
2255.2.69 by John Arbash Meinel
Implement annotate_iter, get_revision_id, and walkdirs so that all tree_implementations now pass
37
        tree.lock_read()
1551.15.48 by Aaron Bentley
Add tests for annotate and plan_merge
38
        self.addCleanup(tree.unlock)
39
        for revision, line in tree.annotate_iter('a-id'):
40
            self.assertEqual('contents of a\n', line)
41
            self.assertEqual(tree_revision, revision)
42
        tree_revision = getattr(tree, 'get_revision_id', lambda: 'random:')()
43
        for revision, line in tree.annotate_iter('a-id', 'random:'):
44
            self.assertEqual('contents of a\n', line)
45
            self.assertEqual(tree_revision, revision)
46
47
1551.15.52 by Aaron Bentley
Tweak from review comments
48
class TestPlanFileMerge(TestCaseWithTree):
1551.15.48 by Aaron Bentley
Add tests for annotate and plan_merge
49
1551.15.52 by Aaron Bentley
Tweak from review comments
50
    def test_plan_file_merge(self):
1551.15.48 by Aaron Bentley
Add tests for annotate and plan_merge
51
        work_a = self.make_branch_and_tree('wta')
52
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
53
        work_a.add('file', 'file-id')
54
        work_a.commit('base version')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
55
        work_b = work_a.controldir.sprout('wtb').open_workingtree()
1551.15.48 by Aaron Bentley
Add tests for annotate and plan_merge
56
        self.build_tree_contents([('wta/file', 'b\nc\nd\ne\n')])
57
        tree_a = self.workingtree_to_test_tree(work_a)
58
        tree_a.lock_read()
59
        self.addCleanup(tree_a.unlock)
60
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
61
        tree_b = self.workingtree_to_test_tree(work_b)
62
        tree_b.lock_read()
63
        self.addCleanup(tree_b.unlock)
64
        self.assertEqual([
3514.2.3 by John Arbash Meinel
Fix a failing test in tree_implementations
65
            ('killed-a', 'a\n'),
1551.15.48 by Aaron Bentley
Add tests for annotate and plan_merge
66
            ('killed-b', 'b\n'),
67
            ('unchanged', 'c\n'),
68
            ('unchanged', 'd\n'),
69
            ('new-a', 'e\n'),
70
            ('new-b', 'f\n'),
1551.15.52 by Aaron Bentley
Tweak from review comments
71
        ], list(tree_a.plan_file_merge('file-id', tree_b)))
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
72
73
74
class TestReference(TestCaseWithTree):
75
76
    def skip_if_no_reference(self, tree):
77
        if not getattr(tree, 'supports_tree_reference', lambda: False)():
3504.2.1 by John Arbash Meinel
Shortcut iter_references when we know references aren't supported.
78
            raise tests.TestNotApplicable('Tree references not supported')
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
79
2100.3.27 by Aaron Bentley
Enable nested commits
80
    def create_nested(self):
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
81
        work_tree = self.make_branch_and_tree('wt')
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
82
        work_tree.lock_write()
83
        try:
84
            self.skip_if_no_reference(work_tree)
85
            subtree = self.make_branch_and_tree('wt/subtree')
86
            subtree.set_root_id('sub-root')
87
            subtree.commit('foo', rev_id='sub-1')
88
            work_tree.add_reference(subtree)
89
        finally:
90
            work_tree.unlock()
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
91
        tree = self._convert_tree(work_tree)
92
        self.skip_if_no_reference(tree)
2100.3.27 by Aaron Bentley
Enable nested commits
93
        return tree
94
95
    def test_get_reference_revision(self):
96
        tree = self.create_nested()
3504.2.1 by John Arbash Meinel
Shortcut iter_references when we know references aren't supported.
97
        tree.lock_read()
98
        self.addCleanup(tree.unlock)
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
99
        path = tree.id2path('sub-root')
5967.7.1 by Martin Pool
Deprecate __contains__ on Tree and Inventory
100
        self.assertEqual('sub-1',
101
            tree.get_reference_revision('sub-root', path))
2100.3.27 by Aaron Bentley
Enable nested commits
102
2255.2.226 by Robert Collins
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.
103
    def test_iter_references(self):
2100.3.27 by Aaron Bentley
Enable nested commits
104
        tree = self.create_nested()
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
105
        tree.lock_read()
2255.2.226 by Robert Collins
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.
106
        self.addCleanup(tree.unlock)
6405.2.9 by Jelmer Vernooij
More test fixes.
107
        entry = tree.root_inventory['sub-root']
3504.2.1 by John Arbash Meinel
Shortcut iter_references when we know references aren't supported.
108
        self.assertEqual([(u'subtree', 'sub-root')],
2255.2.226 by Robert Collins
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.
109
            list(tree.iter_references()))
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
110
111
    def test_get_root_id(self):
112
        # trees should return some kind of root id; it can be none
113
        tree = self.make_branch_and_tree('tree')
114
        root_id = tree.get_root_id()
115
        if root_id is not None:
116
            self.assertIsInstance(root_id, str)
2255.2.180 by Martin Pool
merge dirstate
117
118
2255.11.5 by Martin Pool
Tree.id2path should raise NoSuchId, not return None.
119
class TestFileIds(TestCaseWithTree):
120
121
    def test_id2path(self):
122
        # translate from file-id back to path
123
        work_tree = self.make_branch_and_tree('wt')
124
        tree = self.get_tree_no_parents_abc_content(work_tree)
125
        tree.lock_read()
126
        try:
127
            self.assertEqual(u'a', tree.id2path('a-id'))
128
            # other ids give an error- don't return None for this case
129
            self.assertRaises(errors.NoSuchId, tree.id2path, 'a')
130
        finally:
131
            tree.unlock()
2708.1.1 by Aaron Bentley
Implement Tree.extract_files
132
3146.8.16 by Aaron Bentley
Updates from review
133
    def test_all_file_ids(self):
3146.8.2 by Aaron Bentley
Introduce iter_all_file_ids, to avoid hitting Inventory for this case
134
        work_tree = self.make_branch_and_tree('wt')
135
        tree = self.get_tree_no_parents_abc_content(work_tree)
136
        tree.lock_read()
137
        self.addCleanup(tree.unlock)
3146.8.16 by Aaron Bentley
Updates from review
138
        self.assertEqual(tree.all_file_ids(),
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
139
                         {'b-id', 'root-id', 'c-id', 'a-id'})
3146.8.2 by Aaron Bentley
Introduce iter_all_file_ids, to avoid hitting Inventory for this case
140
2708.1.1 by Aaron Bentley
Implement Tree.extract_files
141
3146.8.4 by Aaron Bentley
Eliminate direct use of inventory from transform application
142
class TestStoredKind(TestCaseWithTree):
143
144
    def test_stored_kind(self):
145
        tree = self.make_branch_and_tree('tree')
146
        work_tree = self.make_branch_and_tree('wt')
147
        tree = self.get_tree_no_parents_abc_content(work_tree)
148
        tree.lock_read()
149
        self.addCleanup(tree.unlock)
150
        self.assertEqual('file', tree.stored_kind('a-id'))
151
        self.assertEqual('directory', tree.stored_kind('b-id'))
152
153
2743.3.5 by Ian Clatworthy
Incorporate feedback from abentley
154
class TestFileContent(TestCaseWithTree):
155
156
    def test_get_file(self):
157
        work_tree = self.make_branch_and_tree('wt')
158
        tree = self.get_tree_no_parents_abc_content_2(work_tree)
159
        tree.lock_read()
6437.20.6 by Martin Packman
Poke test_get_file in bt.per_tree.test_tree to be more careful about cleanup
160
        self.addCleanup(tree.unlock)
161
        # Test lookup without path works
162
        file_without_path = tree.get_file('a-id')
2743.3.5 by Ian Clatworthy
Incorporate feedback from abentley
163
        try:
6437.20.2 by Wouter van Heyst
close files returned by transport.get
164
            lines = file_without_path.readlines()
2743.3.5 by Ian Clatworthy
Incorporate feedback from abentley
165
            self.assertEqual(['foobar\n'], lines)
6437.20.6 by Martin Packman
Poke test_get_file in bt.per_tree.test_tree to be more careful about cleanup
166
        finally:
167
            file_without_path.close()
168
        # Test lookup with path works
169
        file_with_path = tree.get_file('a-id', path='a')
170
        try:
6437.20.2 by Wouter van Heyst
close files returned by transport.get
171
            lines = file_with_path.readlines()
2743.3.5 by Ian Clatworthy
Incorporate feedback from abentley
172
            self.assertEqual(['foobar\n'], lines)
173
        finally:
6437.20.2 by Wouter van Heyst
close files returned by transport.get
174
            file_with_path.close()
2743.3.5 by Ian Clatworthy
Incorporate feedback from abentley
175
3774.1.1 by Aaron Bentley
Test Tree.get_file_text() and supply default implementation.
176
    def test_get_file_text(self):
177
        work_tree = self.make_branch_and_tree('wt')
178
        tree = self.get_tree_no_parents_abc_content_2(work_tree)
179
        tree.lock_read()
180
        self.addCleanup(tree.unlock)
181
        # test read by file-id
182
        self.assertEqual('foobar\n', tree.get_file_text('a-id'))
183
        # test read by path
184
        self.assertEqual('foobar\n', tree.get_file_text('a-id', path='a'))
185
3774.1.2 by Aaron Bentley
Test Tree.get_file_lines, provide a default implementation
186
    def test_get_file_lines(self):
187
        work_tree = self.make_branch_and_tree('wt')
188
        tree = self.get_tree_no_parents_abc_content_2(work_tree)
189
        tree.lock_read()
190
        self.addCleanup(tree.unlock)
191
        # test read by file-id
192
        self.assertEqual(['foobar\n'], tree.get_file_lines('a-id'))
193
        # test read by path
194
        self.assertEqual(['foobar\n'], tree.get_file_lines('a-id', path='a'))
2743.3.5 by Ian Clatworthy
Incorporate feedback from abentley
195
3774.1.4 by Aaron Bentley
Use file.readlines on working trees.
196
    def test_get_file_lines_multi_line_breaks(self):
197
        work_tree = self.make_branch_and_tree('wt')
198
        self.build_tree_contents([('wt/foobar', 'a\rb\nc\r\nd')])
199
        work_tree.add('foobar', 'foobar-id')
200
        tree = self._convert_tree(work_tree)
201
        tree.lock_read()
202
        self.addCleanup(tree.unlock)
203
        self.assertEqual(['a\rb\n', 'c\r\n', 'd'],
204
                         tree.get_file_lines('foobar-id'))
205
206
2708.1.1 by Aaron Bentley
Implement Tree.extract_files
207
class TestExtractFilesBytes(TestCaseWithTree):
208
2708.1.7 by Aaron Bentley
Rename extract_files_bytes to iter_files_bytes
209
    def test_iter_files_bytes(self):
2708.1.1 by Aaron Bentley
Implement Tree.extract_files
210
        work_tree = self.make_branch_and_tree('wt')
211
        self.build_tree_contents([('wt/foo', 'foo'),
212
                                  ('wt/bar', 'bar'),
213
                                  ('wt/baz', 'baz')])
214
        work_tree.add(['foo', 'bar', 'baz'], ['foo-id', 'bar-id', 'baz-id'])
215
        tree = self._convert_tree(work_tree)
216
        tree.lock_read()
217
        self.addCleanup(tree.unlock)
2708.1.6 by Aaron Bentley
Turn extract_files_bytes into an iterator
218
        extracted = dict((i, ''.join(b)) for i, b in
2708.1.7 by Aaron Bentley
Rename extract_files_bytes to iter_files_bytes
219
                         tree.iter_files_bytes([('foo-id', 'id1'),
220
                                                ('bar-id', 'id2'),
221
                                                ('baz-id', 'id3')]))
2708.1.6 by Aaron Bentley
Turn extract_files_bytes into an iterator
222
        self.assertEqual('foo', extracted['id1'])
223
        self.assertEqual('bar', extracted['id2'])
224
        self.assertEqual('baz', extracted['id3'])
2708.1.11 by Aaron Bentley
Test and tweak error handling
225
        self.assertRaises(errors.NoSuchId, lambda: list(
226
                          tree.iter_files_bytes(
227
                          [('qux-id', 'file1-notpresent')])))
2748.2.2 by Lukáš Lalinsky
Add TestConflicts to tests.tree_implementations.test_tree. Update NEWS.
228
229
230
class TestConflicts(TestCaseWithTree):
231
232
    def test_conflicts(self):
233
        """Tree.conflicts() should return a ConflictList instance."""
234
        work_tree = self.make_branch_and_tree('wt')
235
        tree = self._convert_tree(work_tree)
2761.1.3 by Aaron Bentley
Update test to use assertIsInstance
236
        self.assertIsInstance(tree.conflicts(), conflicts.ConflictList)
3363.5.4 by Aaron Bentley
Fix iteration order of iter_entries_by_dir
237
238
239
class TestIterEntriesByDir(TestCaseWithTree):
240
241
    def test_iteration_order(self):
242
        work_tree = self.make_branch_and_tree('.')
243
        self.build_tree(['a/', 'a/b/', 'a/b/c', 'a/d/', 'a/d/e', 'f/', 'f/g'])
244
        work_tree.add(['a', 'a/b', 'a/b/c', 'a/d', 'a/d/e', 'f', 'f/g'])
245
        tree = self._convert_tree(work_tree)
246
        output_order = [p for p, e in tree.iter_entries_by_dir()]
247
        self.assertEqual(['', 'a', 'f', 'a/b', 'a/d', 'a/b/c', 'a/d/e', 'f/g'],
248
                         output_order)
3363.13.1 by Aaron Bentley
Implement PreviewTree.extras
249
250
6471.1.3 by Jelmer Vernooij
Add Tree.iter_child_entries.
251
class TestIterChildEntries(TestCaseWithTree):
252
253
    def test_iteration_order(self):
254
        work_tree = self.make_branch_and_tree('.')
255
        self.build_tree(['a/', 'a/b/', 'a/b/c', 'a/d/', 'a/d/e', 'f/', 'f/g'])
256
        work_tree.add(['a', 'a/b', 'a/b/c', 'a/d', 'a/d/e', 'f', 'f/g'])
257
        tree = self._convert_tree(work_tree)
258
        output = [e.name for e in
259
            tree.iter_child_entries(tree.get_root_id())]
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
260
        self.assertEqual({'a', 'f'}, set(output))
6471.1.3 by Jelmer Vernooij
Add Tree.iter_child_entries.
261
        output = [e.name for e in
262
            tree.iter_child_entries(tree.path2id('a'))]
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
263
        self.assertEqual({'b', 'd'}, set(output))
6471.1.3 by Jelmer Vernooij
Add Tree.iter_child_entries.
264
265
    def test_does_not_exist(self):
266
        work_tree = self.make_branch_and_tree('.')
267
        self.build_tree(['a/'])
268
        work_tree.add(['a'])
269
        tree = self._convert_tree(work_tree)
270
        self.assertRaises(errors.NoSuchId, lambda:
271
            list(tree.iter_child_entries('unknown')))
272
273
3363.12.7 by Aaron Bentley
Add HasId test
274
class TestHasId(TestCaseWithTree):
275
276
    def test_has_id(self):
277
        work_tree = self.make_branch_and_tree('tree')
278
        self.build_tree(['tree/file'])
279
        work_tree.add('file', 'file-id')
280
        tree = self._convert_tree(work_tree)
281
        tree.lock_read()
282
        self.addCleanup(tree.unlock)
283
        self.assertTrue(tree.has_id('file-id'))
284
        self.assertFalse(tree.has_id('dir-id'))
3363.13.5 by Aaron Bentley
Merge with paths2ids
285
286
3363.13.1 by Aaron Bentley
Implement PreviewTree.extras
287
class TestExtras(TestCaseWithTree):
288
289
    def test_extras(self):
290
        work_tree = self.make_branch_and_tree('tree')
3363.13.4 by Aaron Bentley
Ensure versioned files are not listed by extras
291
        self.build_tree(['tree/file', 'tree/versioned-file'])
292
        work_tree.add(['file', 'versioned-file'])
293
        work_tree.commit('add files')
3363.13.1 by Aaron Bentley
Implement PreviewTree.extras
294
        work_tree.remove('file')
295
        tree = self._convert_tree(work_tree)
296
        if isinstance(tree,
297
                      (revisiontree.RevisionTree,
298
                       workingtree_4.DirStateRevisionTree)):
299
            expected = []
300
        else:
301
            expected = ['file']
302
        tree.lock_read()
303
        self.addCleanup(tree.unlock)
304
        self.assertEqual(expected, list(tree.extras()))
3363.15.1 by Aaron Bentley
Add test for has_id
305
3363.17.3 by Aaron Bentley
Merge with has_id
306
3363.15.4 by Aaron Bentley
Implement PreviewTree.get_file_sha1 properly
307
class TestGetFileSha1(TestCaseWithTree):
308
309
    def test_get_file_sha1(self):
310
        work_tree = self.make_branch_and_tree('tree')
311
        self.build_tree_contents([('tree/file', 'file content')])
312
        work_tree.add('file', 'file-id')
313
        tree = self._convert_tree(work_tree)
314
        tree.lock_read()
315
        self.addCleanup(tree.unlock)
316
        expected = osutils.sha_strings('file content')
317
        self.assertEqual(expected, tree.get_file_sha1('file-id'))
5906.1.8 by Jelmer Vernooij
Tests.
318
319
320
class TestGetFileVerifier(TestCaseWithTree):
321
322
    def test_get_file_verifier(self):
323
        work_tree = self.make_branch_and_tree('tree')
324
        self.build_tree_contents([
325
            ('tree/file1', 'file content'),
326
            ('tree/file2', 'file content')])
327
        work_tree.add(['file1', 'file2'], ['file-id-1', 'file-id-2'])
328
        tree = self._convert_tree(work_tree)
329
        tree.lock_read()
330
        self.addCleanup(tree.unlock)
331
        (kind, data) = tree.get_file_verifier('file-id-1')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
332
        self.assertEqual(
5906.1.8 by Jelmer Vernooij
Tests.
333
            tree.get_file_verifier('file-id-1'),
334
            tree.get_file_verifier('file-id-2'))
5906.1.10 by Jelmer Vernooij
sha1 -> SHA1
335
        if kind == "SHA1":
5906.1.8 by Jelmer Vernooij
Tests.
336
            expected = osutils.sha_strings('file content')
337
            self.assertEqual(expected, data)
6110.6.1 by Jelmer Vernooij
Add Tree.has_versioned_directories.
338
339
340
class TestHasVersionedDirectories(TestCaseWithTree):
341
342
    def test_has_versioned_directories(self):
343
        work_tree = self.make_branch_and_tree('tree')
344
        tree = self._convert_tree(work_tree)
345
        self.assertSubset([tree.has_versioned_directories()], (True, False))