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
22
from bzrlib.tests import TestSkipped
23
from bzrlib.tests.tree_implementations import TestCaseWithTree
25
class TestAnnotate(TestCaseWithTree):
27
def test_annotate(self):
28
work_tree = self.make_branch_and_tree('wt')
29
tree = self.get_tree_no_parents_abc_content(work_tree)
30
tree_revision = getattr(tree, 'get_revision_id', lambda: 'current:')()
32
self.addCleanup(tree.unlock)
33
for revision, line in tree.annotate_iter('a-id'):
34
self.assertEqual('contents of a\n', line)
35
self.assertEqual(tree_revision, revision)
36
tree_revision = getattr(tree, 'get_revision_id', lambda: 'random:')()
37
for revision, line in tree.annotate_iter('a-id', 'random:'):
38
self.assertEqual('contents of a\n', line)
39
self.assertEqual(tree_revision, revision)
42
class TestPlanFileMerge(TestCaseWithTree):
44
def test_plan_file_merge(self):
45
work_a = self.make_branch_and_tree('wta')
46
self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
47
work_a.add('file', 'file-id')
48
work_a.commit('base version')
49
work_b = work_a.bzrdir.sprout('wtb').open_workingtree()
50
self.build_tree_contents([('wta/file', 'b\nc\nd\ne\n')])
51
tree_a = self.workingtree_to_test_tree(work_a)
53
self.addCleanup(tree_a.unlock)
54
self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
55
tree_b = self.workingtree_to_test_tree(work_b)
57
self.addCleanup(tree_b.unlock)
65
], list(tree_a.plan_file_merge('file-id', tree_b)))
68
class TestReference(TestCaseWithTree):
70
def skip_if_no_reference(self, tree):
71
if not getattr(tree, 'supports_tree_reference', lambda: False)():
72
raise tests.TestNotApplicable('Tree references not supported')
74
def create_nested(self):
75
work_tree = self.make_branch_and_tree('wt')
76
work_tree.lock_write()
78
self.skip_if_no_reference(work_tree)
79
subtree = self.make_branch_and_tree('wt/subtree')
80
subtree.set_root_id('sub-root')
81
subtree.commit('foo', rev_id='sub-1')
82
work_tree.add_reference(subtree)
85
tree = self._convert_tree(work_tree)
86
self.skip_if_no_reference(tree)
89
def test_get_reference_revision(self):
90
tree = self.create_nested()
92
self.addCleanup(tree.unlock)
93
path = tree.id2path('sub-root')
94
self.assertEqual('sub-1', tree.get_reference_revision('sub-root', path))
96
def test_iter_references(self):
97
tree = self.create_nested()
99
self.addCleanup(tree.unlock)
100
entry = tree.inventory['sub-root']
101
self.assertEqual([(u'subtree', 'sub-root')],
102
list(tree.iter_references()))
104
def test_get_root_id(self):
105
# trees should return some kind of root id; it can be none
106
tree = self.make_branch_and_tree('tree')
107
root_id = tree.get_root_id()
108
if root_id is not None:
109
self.assertIsInstance(root_id, str)
112
class TestFileIds(TestCaseWithTree):
114
def test_id2path(self):
115
# translate from file-id back to path
116
work_tree = self.make_branch_and_tree('wt')
117
tree = self.get_tree_no_parents_abc_content(work_tree)
120
self.assertEqual(u'a', tree.id2path('a-id'))
121
# other ids give an error- don't return None for this case
122
self.assertRaises(errors.NoSuchId, tree.id2path, 'a')
126
def test_all_file_ids(self):
127
work_tree = self.make_branch_and_tree('wt')
128
tree = self.get_tree_no_parents_abc_content(work_tree)
130
self.addCleanup(tree.unlock)
131
self.assertEqual(tree.all_file_ids(),
132
set(['b-id', 'root-id', 'c-id', 'a-id']))
135
class TestStoredKind(TestCaseWithTree):
137
def test_stored_kind(self):
138
tree = self.make_branch_and_tree('tree')
139
work_tree = self.make_branch_and_tree('wt')
140
tree = self.get_tree_no_parents_abc_content(work_tree)
142
self.addCleanup(tree.unlock)
143
self.assertEqual('file', tree.stored_kind('a-id'))
144
self.assertEqual('directory', tree.stored_kind('b-id'))
147
class TestFileContent(TestCaseWithTree):
149
def test_get_file(self):
150
work_tree = self.make_branch_and_tree('wt')
151
tree = self.get_tree_no_parents_abc_content_2(work_tree)
154
# Test lookup without path works
155
lines = tree.get_file('a-id').readlines()
156
self.assertEqual(['foobar\n'], lines)
157
# Test lookup with path works
158
lines = tree.get_file('a-id', path='a').readlines()
159
self.assertEqual(['foobar\n'], lines)
164
class TestExtractFilesBytes(TestCaseWithTree):
166
def test_iter_files_bytes(self):
167
work_tree = self.make_branch_and_tree('wt')
168
self.build_tree_contents([('wt/foo', 'foo'),
171
work_tree.add(['foo', 'bar', 'baz'], ['foo-id', 'bar-id', 'baz-id'])
172
tree = self._convert_tree(work_tree)
174
self.addCleanup(tree.unlock)
175
extracted = dict((i, ''.join(b)) for i, b in
176
tree.iter_files_bytes([('foo-id', 'id1'),
179
self.assertEqual('foo', extracted['id1'])
180
self.assertEqual('bar', extracted['id2'])
181
self.assertEqual('baz', extracted['id3'])
182
self.assertRaises(errors.NoSuchId, lambda: list(
183
tree.iter_files_bytes(
184
[('qux-id', 'file1-notpresent')])))
187
class TestConflicts(TestCaseWithTree):
189
def test_conflicts(self):
190
"""Tree.conflicts() should return a ConflictList instance."""
191
work_tree = self.make_branch_and_tree('wt')
192
tree = self._convert_tree(work_tree)
193
self.assertIsInstance(tree.conflicts(), conflicts.ConflictList)
196
class TestIterEntriesByDir(TestCaseWithTree):
198
def test_iteration_order(self):
199
work_tree = self.make_branch_and_tree('.')
200
self.build_tree(['a/', 'a/b/', 'a/b/c', 'a/d/', 'a/d/e', 'f/', 'f/g'])
201
work_tree.add(['a', 'a/b', 'a/b/c', 'a/d', 'a/d/e', 'f', 'f/g'])
202
tree = self._convert_tree(work_tree)
203
output_order = [p for p, e in tree.iter_entries_by_dir()]
204
self.assertEqual(['', 'a', 'f', 'a/b', 'a/d', 'a/b/c', 'a/d/e', 'f/g'],