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
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
self.assertEqual(expected, tree.get_parent_ids())
31
self.assertEqual(None, tree.last_revision())
33
self.assertEqual(expected[0], tree.last_revision())
34
self.assertEqual(expected[1:], tree.pending_merges())
37
class TestSetParents(TestParents):
39
def test_set_no_parents(self):
40
t = self.make_branch_and_tree('.')
41
t.set_parent_trees([])
42
self.assertEqual([], t.get_parent_ids())
43
# now give it a real parent, and then set it to no parents again.
44
t.commit('first post')
45
t.set_parent_trees([])
46
self.assertConsistentParents([], t)
48
def test_set_one_ghost_parent_rejects(self):
49
t = self.make_branch_and_tree('.')
50
self.assertRaises(errors.GhostRevisionUnusableHere,
51
t.set_parent_trees, [('missing-revision-id', None)])
53
def test_set_one_ghost_parent_force(self):
54
t = self.make_branch_and_tree('.')
55
t.set_parent_trees([('missing-revision-id', None)],
56
allow_leftmost_as_ghost=True)
57
self.assertConsistentParents(['missing-revision-id'], t)
59
def test_set_two_parents_one_ghost(self):
60
t = self.make_branch_and_tree('.')
61
revision_in_repo = t.commit('first post')
62
# remove the tree's history
63
uncommit(t.branch, tree=t)
64
rev_tree = t.branch.repository.revision_tree(revision_in_repo)
65
t.set_parent_trees([(revision_in_repo, rev_tree),
66
('another-missing', None)])
67
self.assertConsistentParents([revision_in_repo, 'another-missing'], t)
69
def test_set_three_parents(self):
70
t = self.make_branch_and_tree('.')
71
first_revision = t.commit('first post')
72
uncommit(t.branch, tree=t)
73
second_revision = t.commit('second post')
74
uncommit(t.branch, tree=t)
75
third_revision = t.commit('third post')
76
uncommit(t.branch, tree=t)
77
rev_tree1 = t.branch.repository.revision_tree(first_revision)
78
rev_tree2 = t.branch.repository.revision_tree(second_revision)
79
rev_tree3 = t.branch.repository.revision_tree(third_revision)
80
t.set_parent_trees([(first_revision, rev_tree1),
81
(second_revision, rev_tree2),
82
(third_revision, rev_tree3)])
83
self.assertConsistentParents(
84
[first_revision, second_revision, third_revision], t)
86
def test_set_no_parents_ids(self):
87
t = self.make_branch_and_tree('.')
89
self.assertEqual([], t.get_parent_ids())
90
# now give it a real parent, and then set it to no parents again.
91
t.commit('first post')
93
self.assertConsistentParents([], t)
95
def test_set_one_ghost_parent_ids_rejects(self):
96
t = self.make_branch_and_tree('.')
97
self.assertRaises(errors.GhostRevisionUnusableHere,
98
t.set_parent_ids, ['missing-revision-id'])
100
def test_set_one_ghost_parent_ids_force(self):
101
t = self.make_branch_and_tree('.')
102
t.set_parent_ids(['missing-revision-id'],
103
allow_leftmost_as_ghost=True)
104
self.assertConsistentParents(['missing-revision-id'], t)
106
def test_set_two_parents_one_ghost_ids(self):
107
t = self.make_branch_and_tree('.')
108
revision_in_repo = t.commit('first post')
109
# remove the tree's history
110
uncommit(t.branch, tree=t)
111
rev_tree = t.branch.repository.revision_tree(revision_in_repo)
112
t.set_parent_ids([revision_in_repo, 'another-missing'])
113
self.assertConsistentParents([revision_in_repo, 'another-missing'], t)
115
def test_set_three_parents_ids(self):
116
t = self.make_branch_and_tree('.')
117
first_revision = t.commit('first post')
118
uncommit(t.branch, tree=t)
119
second_revision = t.commit('second post')
120
uncommit(t.branch, tree=t)
121
third_revision = t.commit('third post')
122
uncommit(t.branch, tree=t)
123
rev_tree1 = t.branch.repository.revision_tree(first_revision)
124
rev_tree2 = t.branch.repository.revision_tree(second_revision)
125
rev_tree3 = t.branch.repository.revision_tree(third_revision)
126
t.set_parent_ids([first_revision, second_revision, third_revision])
127
self.assertConsistentParents(
128
[first_revision, second_revision, third_revision], t)
131
class TestAddParent(TestParents):
133
def test_add_first_parent_id(self):
134
"""Test adding the first parent id"""
135
tree = self.make_branch_and_tree('.')
136
first_revision = tree.commit('first post')
137
uncommit(tree.branch, tree=tree)
138
tree.add_parent_tree_id(first_revision)
139
self.assertConsistentParents([first_revision], tree)
141
def test_add_first_parent_id_ghost_rejects(self):
142
"""Test adding the first parent id - as a ghost"""
143
tree = self.make_branch_and_tree('.')
144
self.assertRaises(errors.GhostRevisionUnusableHere,
145
tree.add_parent_tree_id, 'first-revision')
147
def test_add_first_parent_id_ghost_force(self):
148
"""Test adding the first parent id - as a ghost"""
149
tree = self.make_branch_and_tree('.')
150
tree.add_parent_tree_id('first-revision', allow_leftmost_as_ghost=True)
151
self.assertConsistentParents(['first-revision'], tree)
153
def test_add_second_parent_id_with_ghost_first(self):
154
"""Test adding the second parent when the first is a ghost."""
155
tree = self.make_branch_and_tree('.')
156
tree.add_parent_tree_id('first-revision', allow_leftmost_as_ghost=True)
157
tree.add_parent_tree_id('second')
158
self.assertConsistentParents(['first-revision', 'second'], tree)
160
def test_add_second_parent_id(self):
161
"""Test adding the second parent id"""
162
tree = self.make_branch_and_tree('.')
163
first_revision = tree.commit('first post')
164
uncommit(tree.branch, tree=tree)
165
second_revision = tree.commit('second post')
166
tree.add_parent_tree_id(first_revision)
167
self.assertConsistentParents([second_revision, first_revision], tree)
169
def test_add_second_parent_id_ghost(self):
170
"""Test adding the second parent id - as a ghost"""
171
tree = self.make_branch_and_tree('.')
172
first_revision = tree.commit('first post')
173
tree.add_parent_tree_id('second')
174
self.assertConsistentParents([first_revision, 'second'], tree)
176
def test_add_first_parent_tree(self):
177
"""Test adding the first parent id"""
178
tree = self.make_branch_and_tree('.')
179
first_revision = tree.commit('first post')
180
uncommit(tree.branch, tree=tree)
181
tree.add_parent_tree((first_revision,
182
tree.branch.repository.revision_tree(first_revision)))
183
self.assertConsistentParents([first_revision], tree)
185
def test_add_first_parent_tree_ghost_rejects(self):
186
"""Test adding the first parent id - as a ghost"""
187
tree = self.make_branch_and_tree('.')
188
self.assertRaises(errors.GhostRevisionUnusableHere,
189
tree.add_parent_tree, ('first-revision', None))
191
def test_add_first_parent_tree_ghost_force(self):
192
"""Test adding the first parent id - as a ghost"""
193
tree = self.make_branch_and_tree('.')
194
tree.add_parent_tree(('first-revision', None),
195
allow_leftmost_as_ghost=True)
196
self.assertConsistentParents(['first-revision'], tree)
198
def test_add_second_parent_tree(self):
199
"""Test adding the second parent id"""
200
tree = self.make_branch_and_tree('.')
201
first_revision = tree.commit('first post')
202
uncommit(tree.branch, tree=tree)
203
second_revision = tree.commit('second post')
204
tree.add_parent_tree((first_revision,
205
tree.branch.repository.revision_tree(first_revision)))
206
self.assertConsistentParents([second_revision, first_revision], tree)
208
def test_add_second_parent_tree_ghost(self):
209
"""Test adding the second parent id - as a ghost"""
210
tree = self.make_branch_and_tree('.')
211
first_revision = tree.commit('first post')
212
tree.add_parent_tree(('second', None))
213
self.assertConsistentParents([first_revision, 'second'], tree)