1
# Copyright (C) 2006 by 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
17
"""Tests of the parent related functions of WorkingTrees."""
21
from bzrlib import errors, symbol_versioning
22
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
23
from bzrlib.uncommit import uncommit
26
class TestParents(TestCaseWithWorkingTree):
28
def assertConsistentParents(self, expected, tree):
29
"""Check that the parents found are as expected.
31
This test helper also checks that they are consistent with
32
the pre-get_parent_ids() api - which is now deprecated.
34
self.assertEqual(expected, tree.get_parent_ids())
36
self.assertEqual(None, tree.last_revision())
38
self.assertEqual(expected[0], tree.last_revision())
39
self.assertEqual(expected[1:],
40
self.applyDeprecated(symbol_versioning.zero_eleven,
44
class TestSetParents(TestParents):
46
def test_set_no_parents(self):
47
t = self.make_branch_and_tree('.')
48
t.set_parent_trees([])
49
self.assertEqual([], t.get_parent_ids())
50
# now give it a real parent, and then set it to no parents again.
51
t.commit('first post')
52
t.set_parent_trees([])
53
self.assertConsistentParents([], t)
55
def test_set_one_ghost_parent_rejects(self):
56
t = self.make_branch_and_tree('.')
57
self.assertRaises(errors.GhostRevisionUnusableHere,
58
t.set_parent_trees, [('missing-revision-id', None)])
60
def test_set_one_ghost_parent_force(self):
61
t = self.make_branch_and_tree('.')
62
t.set_parent_trees([('missing-revision-id', None)],
63
allow_leftmost_as_ghost=True)
64
self.assertConsistentParents(['missing-revision-id'], t)
66
def test_set_two_parents_one_ghost(self):
67
t = self.make_branch_and_tree('.')
68
revision_in_repo = t.commit('first post')
69
# remove the tree's history
70
uncommit(t.branch, tree=t)
71
rev_tree = t.branch.repository.revision_tree(revision_in_repo)
72
t.set_parent_trees([(revision_in_repo, rev_tree),
73
('another-missing', None)])
74
self.assertConsistentParents([revision_in_repo, 'another-missing'], t)
76
def test_set_three_parents(self):
77
t = self.make_branch_and_tree('.')
78
first_revision = t.commit('first post')
79
uncommit(t.branch, tree=t)
80
second_revision = t.commit('second post')
81
uncommit(t.branch, tree=t)
82
third_revision = t.commit('third post')
83
uncommit(t.branch, tree=t)
84
rev_tree1 = t.branch.repository.revision_tree(first_revision)
85
rev_tree2 = t.branch.repository.revision_tree(second_revision)
86
rev_tree3 = t.branch.repository.revision_tree(third_revision)
87
t.set_parent_trees([(first_revision, rev_tree1),
88
(second_revision, rev_tree2),
89
(third_revision, rev_tree3)])
90
self.assertConsistentParents(
91
[first_revision, second_revision, third_revision], t)
93
def test_set_no_parents_ids(self):
94
t = self.make_branch_and_tree('.')
96
self.assertEqual([], t.get_parent_ids())
97
# now give it a real parent, and then set it to no parents again.
98
t.commit('first post')
100
self.assertConsistentParents([], t)
102
def test_set_one_ghost_parent_ids_rejects(self):
103
t = self.make_branch_and_tree('.')
104
self.assertRaises(errors.GhostRevisionUnusableHere,
105
t.set_parent_ids, ['missing-revision-id'])
107
def test_set_one_ghost_parent_ids_force(self):
108
t = self.make_branch_and_tree('.')
109
t.set_parent_ids(['missing-revision-id'],
110
allow_leftmost_as_ghost=True)
111
self.assertConsistentParents(['missing-revision-id'], t)
113
def test_set_two_parents_one_ghost_ids(self):
114
t = self.make_branch_and_tree('.')
115
revision_in_repo = t.commit('first post')
116
# remove the tree's history
117
uncommit(t.branch, tree=t)
118
rev_tree = t.branch.repository.revision_tree(revision_in_repo)
119
t.set_parent_ids([revision_in_repo, 'another-missing'])
120
self.assertConsistentParents([revision_in_repo, 'another-missing'], t)
122
def test_set_three_parents_ids(self):
123
t = self.make_branch_and_tree('.')
124
first_revision = t.commit('first post')
125
uncommit(t.branch, tree=t)
126
second_revision = t.commit('second post')
127
uncommit(t.branch, tree=t)
128
third_revision = t.commit('third post')
129
uncommit(t.branch, tree=t)
130
rev_tree1 = t.branch.repository.revision_tree(first_revision)
131
rev_tree2 = t.branch.repository.revision_tree(second_revision)
132
rev_tree3 = t.branch.repository.revision_tree(third_revision)
133
t.set_parent_ids([first_revision, second_revision, third_revision])
134
self.assertConsistentParents(
135
[first_revision, second_revision, third_revision], t)
138
class TestAddParent(TestParents):
140
def test_add_first_parent_id(self):
141
"""Test adding the first parent id"""
142
tree = self.make_branch_and_tree('.')
143
first_revision = tree.commit('first post')
144
uncommit(tree.branch, tree=tree)
145
tree.add_parent_tree_id(first_revision)
146
self.assertConsistentParents([first_revision], tree)
148
def test_add_first_parent_id_ghost_rejects(self):
149
"""Test adding the first parent id - as a ghost"""
150
tree = self.make_branch_and_tree('.')
151
self.assertRaises(errors.GhostRevisionUnusableHere,
152
tree.add_parent_tree_id, 'first-revision')
154
def test_add_first_parent_id_ghost_force(self):
155
"""Test adding the first parent id - as a ghost"""
156
tree = self.make_branch_and_tree('.')
157
tree.add_parent_tree_id('first-revision', allow_leftmost_as_ghost=True)
158
self.assertConsistentParents(['first-revision'], tree)
160
def test_add_second_parent_id_with_ghost_first(self):
161
"""Test adding the second parent when the first is a ghost."""
162
tree = self.make_branch_and_tree('.')
163
tree.add_parent_tree_id('first-revision', allow_leftmost_as_ghost=True)
164
tree.add_parent_tree_id('second')
165
self.assertConsistentParents(['first-revision', 'second'], tree)
167
def test_add_second_parent_id(self):
168
"""Test adding the second parent id"""
169
tree = self.make_branch_and_tree('.')
170
first_revision = tree.commit('first post')
171
uncommit(tree.branch, tree=tree)
172
second_revision = tree.commit('second post')
173
tree.add_parent_tree_id(first_revision)
174
self.assertConsistentParents([second_revision, first_revision], tree)
176
def test_add_second_parent_id_ghost(self):
177
"""Test adding the second parent id - as a ghost"""
178
tree = self.make_branch_and_tree('.')
179
first_revision = tree.commit('first post')
180
tree.add_parent_tree_id('second')
181
self.assertConsistentParents([first_revision, 'second'], tree)
183
def test_add_first_parent_tree(self):
184
"""Test adding the first parent id"""
185
tree = self.make_branch_and_tree('.')
186
first_revision = tree.commit('first post')
187
uncommit(tree.branch, tree=tree)
188
tree.add_parent_tree((first_revision,
189
tree.branch.repository.revision_tree(first_revision)))
190
self.assertConsistentParents([first_revision], tree)
192
def test_add_first_parent_tree_ghost_rejects(self):
193
"""Test adding the first parent id - as a ghost"""
194
tree = self.make_branch_and_tree('.')
195
self.assertRaises(errors.GhostRevisionUnusableHere,
196
tree.add_parent_tree, ('first-revision', None))
198
def test_add_first_parent_tree_ghost_force(self):
199
"""Test adding the first parent id - as a ghost"""
200
tree = self.make_branch_and_tree('.')
201
tree.add_parent_tree(('first-revision', None),
202
allow_leftmost_as_ghost=True)
203
self.assertConsistentParents(['first-revision'], tree)
205
def test_add_second_parent_tree(self):
206
"""Test adding the second parent id"""
207
tree = self.make_branch_and_tree('.')
208
first_revision = tree.commit('first post')
209
uncommit(tree.branch, tree=tree)
210
second_revision = tree.commit('second post')
211
tree.add_parent_tree((first_revision,
212
tree.branch.repository.revision_tree(first_revision)))
213
self.assertConsistentParents([second_revision, first_revision], tree)
215
def test_add_second_parent_tree_ghost(self):
216
"""Test adding the second parent id - as a ghost"""
217
tree = self.make_branch_and_tree('.')
218
first_revision = tree.commit('first post')
219
tree.add_parent_tree(('second', None))
220
self.assertConsistentParents([first_revision, 'second'], tree)