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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
from bzrlib.tests import TestSkipped
26
from bzrlib.tests.per_tree import TestCaseWithTree
28
class TestAnnotate(TestCaseWithTree):
30
def test_annotate(self):
31
work_tree = self.make_branch_and_tree('wt')
32
tree = self.get_tree_no_parents_abc_content(work_tree)
33
tree_revision = getattr(tree, 'get_revision_id', lambda: 'current:')()
35
self.addCleanup(tree.unlock)
36
for revision, line in tree.annotate_iter('a-id'):
37
self.assertEqual('contents of a\n', line)
38
self.assertEqual(tree_revision, revision)
39
tree_revision = getattr(tree, 'get_revision_id', lambda: 'random:')()
40
for revision, line in tree.annotate_iter('a-id', 'random:'):
41
self.assertEqual('contents of a\n', line)
42
self.assertEqual(tree_revision, revision)
45
class TestPlanFileMerge(TestCaseWithTree):
47
def test_plan_file_merge(self):
48
work_a = self.make_branch_and_tree('wta')
49
self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
50
work_a.add('file', 'file-id')
51
work_a.commit('base version')
52
work_b = work_a.bzrdir.sprout('wtb').open_workingtree()
53
self.build_tree_contents([('wta/file', 'b\nc\nd\ne\n')])
54
tree_a = self.workingtree_to_test_tree(work_a)
56
self.addCleanup(tree_a.unlock)
57
self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
58
tree_b = self.workingtree_to_test_tree(work_b)
60
self.addCleanup(tree_b.unlock)
68
], list(tree_a.plan_file_merge('file-id', tree_b)))
71
class TestReference(TestCaseWithTree):
73
def skip_if_no_reference(self, tree):
74
if not getattr(tree, 'supports_tree_reference', lambda: False)():
75
raise tests.TestNotApplicable('Tree references not supported')
77
def create_nested(self):
78
work_tree = self.make_branch_and_tree('wt')
79
work_tree.lock_write()
81
self.skip_if_no_reference(work_tree)
82
subtree = self.make_branch_and_tree('wt/subtree')
83
subtree.set_root_id('sub-root')
84
subtree.commit('foo', rev_id='sub-1')
85
work_tree.add_reference(subtree)
88
tree = self._convert_tree(work_tree)
89
self.skip_if_no_reference(tree)
92
def test_get_reference_revision(self):
93
tree = self.create_nested()
95
self.addCleanup(tree.unlock)
96
path = tree.id2path('sub-root')
97
self.assertEqual('sub-1', tree.get_reference_revision('sub-root', path))
99
def test_iter_references(self):
100
tree = self.create_nested()
102
self.addCleanup(tree.unlock)
103
entry = tree.inventory['sub-root']
104
self.assertEqual([(u'subtree', 'sub-root')],
105
list(tree.iter_references()))
107
def test_get_root_id(self):
108
# trees should return some kind of root id; it can be none
109
tree = self.make_branch_and_tree('tree')
110
root_id = tree.get_root_id()
111
if root_id is not None:
112
self.assertIsInstance(root_id, str)
115
class TestFileIds(TestCaseWithTree):
117
def test_id2path(self):
118
# translate from file-id back to path
119
work_tree = self.make_branch_and_tree('wt')
120
tree = self.get_tree_no_parents_abc_content(work_tree)
123
self.assertEqual(u'a', tree.id2path('a-id'))
124
# other ids give an error- don't return None for this case
125
self.assertRaises(errors.NoSuchId, tree.id2path, 'a')
129
def test_all_file_ids(self):
130
work_tree = self.make_branch_and_tree('wt')
131
tree = self.get_tree_no_parents_abc_content(work_tree)
133
self.addCleanup(tree.unlock)
134
self.assertEqual(tree.all_file_ids(),
135
set(['b-id', 'root-id', 'c-id', 'a-id']))
138
class TestStoredKind(TestCaseWithTree):
140
def test_stored_kind(self):
141
tree = self.make_branch_and_tree('tree')
142
work_tree = self.make_branch_and_tree('wt')
143
tree = self.get_tree_no_parents_abc_content(work_tree)
145
self.addCleanup(tree.unlock)
146
self.assertEqual('file', tree.stored_kind('a-id'))
147
self.assertEqual('directory', tree.stored_kind('b-id'))
150
class TestFileContent(TestCaseWithTree):
152
def test_get_file(self):
153
work_tree = self.make_branch_and_tree('wt')
154
tree = self.get_tree_no_parents_abc_content_2(work_tree)
157
# Test lookup without path works
158
lines = tree.get_file('a-id').readlines()
159
self.assertEqual(['foobar\n'], lines)
160
# Test lookup with path works
161
lines = tree.get_file('a-id', path='a').readlines()
162
self.assertEqual(['foobar\n'], lines)
166
def test_get_file_text(self):
167
work_tree = self.make_branch_and_tree('wt')
168
tree = self.get_tree_no_parents_abc_content_2(work_tree)
170
self.addCleanup(tree.unlock)
171
# test read by file-id
172
self.assertEqual('foobar\n', tree.get_file_text('a-id'))
174
self.assertEqual('foobar\n', tree.get_file_text('a-id', path='a'))
176
def test_get_file_lines(self):
177
work_tree = self.make_branch_and_tree('wt')
178
tree = self.get_tree_no_parents_abc_content_2(work_tree)
180
self.addCleanup(tree.unlock)
181
# test read by file-id
182
self.assertEqual(['foobar\n'], tree.get_file_lines('a-id'))
184
self.assertEqual(['foobar\n'], tree.get_file_lines('a-id', path='a'))
186
def test_get_file_lines_multi_line_breaks(self):
187
work_tree = self.make_branch_and_tree('wt')
188
self.build_tree_contents([('wt/foobar', 'a\rb\nc\r\nd')])
189
work_tree.add('foobar', 'foobar-id')
190
tree = self._convert_tree(work_tree)
192
self.addCleanup(tree.unlock)
193
self.assertEqual(['a\rb\n', 'c\r\n', 'd'],
194
tree.get_file_lines('foobar-id'))
197
class TestExtractFilesBytes(TestCaseWithTree):
199
def test_iter_files_bytes(self):
200
work_tree = self.make_branch_and_tree('wt')
201
self.build_tree_contents([('wt/foo', 'foo'),
204
work_tree.add(['foo', 'bar', 'baz'], ['foo-id', 'bar-id', 'baz-id'])
205
tree = self._convert_tree(work_tree)
207
self.addCleanup(tree.unlock)
208
extracted = dict((i, ''.join(b)) for i, b in
209
tree.iter_files_bytes([('foo-id', 'id1'),
212
self.assertEqual('foo', extracted['id1'])
213
self.assertEqual('bar', extracted['id2'])
214
self.assertEqual('baz', extracted['id3'])
215
self.assertRaises(errors.NoSuchId, lambda: list(
216
tree.iter_files_bytes(
217
[('qux-id', 'file1-notpresent')])))
220
class TestConflicts(TestCaseWithTree):
222
def test_conflicts(self):
223
"""Tree.conflicts() should return a ConflictList instance."""
224
work_tree = self.make_branch_and_tree('wt')
225
tree = self._convert_tree(work_tree)
226
self.assertIsInstance(tree.conflicts(), conflicts.ConflictList)
229
class TestIterEntriesByDir(TestCaseWithTree):
231
def test_iteration_order(self):
232
work_tree = self.make_branch_and_tree('.')
233
self.build_tree(['a/', 'a/b/', 'a/b/c', 'a/d/', 'a/d/e', 'f/', 'f/g'])
234
work_tree.add(['a', 'a/b', 'a/b/c', 'a/d', 'a/d/e', 'f', 'f/g'])
235
tree = self._convert_tree(work_tree)
236
output_order = [p for p, e in tree.iter_entries_by_dir()]
237
self.assertEqual(['', 'a', 'f', 'a/b', 'a/d', 'a/b/c', 'a/d/e', 'f/g'],
241
class TestHasId(TestCaseWithTree):
243
def test_has_id(self):
244
work_tree = self.make_branch_and_tree('tree')
245
self.build_tree(['tree/file'])
246
work_tree.add('file', 'file-id')
247
tree = self._convert_tree(work_tree)
249
self.addCleanup(tree.unlock)
250
self.assertTrue(tree.has_id('file-id'))
251
self.assertFalse(tree.has_id('dir-id'))
253
def test___contains__(self):
254
work_tree = self.make_branch_and_tree('tree')
255
self.build_tree(['tree/file'])
256
work_tree.add('file', 'file-id')
257
tree = self._convert_tree(work_tree)
259
self.addCleanup(tree.unlock)
260
self.assertTrue('file-id' in tree)
261
self.assertFalse('dir-id' in tree)
264
class TestExtras(TestCaseWithTree):
266
def test_extras(self):
267
work_tree = self.make_branch_and_tree('tree')
268
self.build_tree(['tree/file', 'tree/versioned-file'])
269
work_tree.add(['file', 'versioned-file'])
270
work_tree.commit('add files')
271
work_tree.remove('file')
272
tree = self._convert_tree(work_tree)
274
(revisiontree.RevisionTree,
275
workingtree_4.DirStateRevisionTree)):
280
self.addCleanup(tree.unlock)
281
self.assertEqual(expected, list(tree.extras()))
284
class TestGetFileSha1(TestCaseWithTree):
286
def test_get_file_sha1(self):
287
work_tree = self.make_branch_and_tree('tree')
288
self.build_tree_contents([('tree/file', 'file content')])
289
work_tree.add('file', 'file-id')
290
tree = self._convert_tree(work_tree)
292
self.addCleanup(tree.unlock)
293
expected = osutils.sha_strings('file content')
294
self.assertEqual(expected, tree.get_file_sha1('file-id'))