1
# Copyright (C) 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
17
"""Tests for interface conformance of inventories of trees."""
20
from cStringIO import StringIO
23
from bzrlib.diff import internal_diff
24
from bzrlib.osutils import (
27
from bzrlib.tests.tree_implementations import TestCaseWithTree
28
from bzrlib.uncommit import uncommit
31
class TestEntryDiffing(TestCaseWithTree):
34
super(TestEntryDiffing, self).setUp()
35
self.wt = self.make_branch_and_tree('.')
36
self.branch = self.wt.branch
37
print >> open('file', 'wb'), 'foo'
38
print >> open('binfile', 'wb'), 'foo'
39
self.wt.add(['file'], ['fileid'])
40
self.wt.add(['binfile'], ['binfileid'])
42
os.symlink('target1', 'symlink')
43
self.wt.add(['symlink'], ['linkid'])
44
self.wt.commit('message_1', rev_id = '1')
45
print >> open('file', 'wb'), 'bar'
46
print >> open('binfile', 'wb'), 'x' * 1023 + '\x00'
49
os.symlink('target2', 'symlink')
50
self.tree_1 = self.branch.repository.revision_tree('1')
51
self.inv_1 = self.branch.repository.get_inventory('1')
52
self.file_1 = self.inv_1['fileid']
53
self.file_1b = self.inv_1['binfileid']
54
self.tree_2 = self.workingtree_to_test_tree(self.wt)
55
self.tree_2.lock_read()
56
self.addCleanup(self.tree_2.unlock)
57
self.inv_2 = self.tree_2.inventory
58
self.file_2 = self.inv_2['fileid']
59
self.file_2b = self.inv_2['binfileid']
61
self.link_1 = self.inv_1['linkid']
62
self.link_2 = self.inv_2['linkid']
64
def test_file_diff_deleted(self):
66
self.file_1.diff(internal_diff,
67
"old_label", self.tree_1,
68
"/dev/null", None, None,
70
self.assertEqual(output.getvalue(), "--- old_label\n"
76
def test_file_diff_added(self):
78
self.file_1.diff(internal_diff,
79
"new_label", self.tree_1,
80
"/dev/null", None, None,
82
self.assertEqual(output.getvalue(), "--- /dev/null\n"
88
def test_file_diff_changed(self):
90
self.file_1.diff(internal_diff,
91
"/dev/null", self.tree_1,
92
"new_label", self.file_2, self.tree_2,
94
self.assertEqual(output.getvalue(), "--- /dev/null\n"
101
def test_file_diff_binary(self):
103
self.file_1.diff(internal_diff,
104
"/dev/null", self.tree_1,
105
"new_label", self.file_2b, self.tree_2,
107
self.assertEqual(output.getvalue(),
108
"Binary files /dev/null and new_label differ\n")
109
def test_link_diff_deleted(self):
110
if not has_symlinks():
113
self.link_1.diff(internal_diff,
114
"old_label", self.tree_1,
115
"/dev/null", None, None,
117
self.assertEqual(output.getvalue(),
118
"=== target was 'target1'\n")
120
def test_link_diff_added(self):
121
if not has_symlinks():
124
self.link_1.diff(internal_diff,
125
"new_label", self.tree_1,
126
"/dev/null", None, None,
127
output, reverse=True)
128
self.assertEqual(output.getvalue(),
129
"=== target is 'target1'\n")
131
def test_link_diff_changed(self):
132
if not has_symlinks():
135
self.link_1.diff(internal_diff,
136
"/dev/null", self.tree_1,
137
"new_label", self.link_2, self.tree_2,
139
self.assertEqual(output.getvalue(),
140
"=== target changed 'target1' => 'target2'\n")
143
class TestPreviousHeads(TestCaseWithTree):
146
# we want several inventories, that respectively
147
# give use the following scenarios:
148
# A) fileid not in any inventory (A),
149
# B) fileid present in one inventory (B) and (A,B)
150
# C) fileid present in two inventories, and they
151
# are not mutual descendents (B, C)
152
# D) fileid present in two inventories and one is
153
# a descendent of the other. (B, D)
154
super(TestPreviousHeads, self).setUp()
155
self.wt = self.make_branch_and_tree('.')
156
self.branch = self.wt.branch
157
self.build_tree(['file'])
158
self.wt.commit('new branch', allow_pointless=True, rev_id='A')
159
self.inv_A = self.branch.repository.get_inventory('A')
160
self.wt.add(['file'], ['fileid'])
161
self.wt.commit('add file', rev_id='B')
162
self.inv_B = self.branch.repository.get_inventory('B')
163
uncommit(self.branch, tree=self.wt)
164
self.assertEqual(self.branch.revision_history(), ['A'])
165
self.wt.commit('another add of file', rev_id='C')
166
self.inv_C = self.branch.repository.get_inventory('C')
167
self.wt.add_parent_tree_id('B')
168
self.wt.commit('merge in B', rev_id='D')
169
self.inv_D = self.branch.repository.get_inventory('D')
170
self.tree = self.workingtree_to_test_tree(self.wt)
171
self.tree.lock_read()
172
self.addCleanup(self.tree.unlock)
173
self.file_active = self.tree.inventory['fileid']
174
self.weave = self.branch.repository.weave_store.get_weave('fileid',
175
self.branch.repository.get_transaction())
177
def get_previous_heads(self, inventories):
178
return self.file_active.find_previous_heads(
180
self.branch.repository.weave_store,
181
self.branch.repository.get_transaction())
183
def test_fileid_in_no_inventory(self):
184
self.assertEqual({}, self.get_previous_heads([self.inv_A]))
186
def test_fileid_in_one_inventory(self):
187
self.assertEqual({'B':self.inv_B['fileid']},
188
self.get_previous_heads([self.inv_B]))
189
self.assertEqual({'B':self.inv_B['fileid']},
190
self.get_previous_heads([self.inv_A, self.inv_B]))
191
self.assertEqual({'B':self.inv_B['fileid']},
192
self.get_previous_heads([self.inv_B, self.inv_A]))
194
def test_fileid_in_two_inventories_gives_both_entries(self):
195
self.assertEqual({'B':self.inv_B['fileid'],
196
'C':self.inv_C['fileid']},
197
self.get_previous_heads([self.inv_B, self.inv_C]))
198
self.assertEqual({'B':self.inv_B['fileid'],
199
'C':self.inv_C['fileid']},
200
self.get_previous_heads([self.inv_C, self.inv_B]))
202
def test_fileid_in_two_inventories_already_merged_gives_head(self):
203
self.assertEqual({'D':self.inv_D['fileid']},
204
self.get_previous_heads([self.inv_B, self.inv_D]))
205
self.assertEqual({'D':self.inv_D['fileid']},
206
self.get_previous_heads([self.inv_D, self.inv_B]))
208
# TODO: test two inventories with the same file revision