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.mutabletree import MutableTree
25
from bzrlib.osutils import (
28
from bzrlib.tests import TestSkipped
29
from bzrlib.tests.tree_implementations import TestCaseWithTree
30
from bzrlib.uncommit import uncommit
33
class TestEntryDiffing(TestCaseWithTree):
36
super(TestEntryDiffing, self).setUp()
37
self.wt = self.make_branch_and_tree('.')
38
self.branch = self.wt.branch
39
print >> open('file', 'wb'), 'foo'
40
print >> open('binfile', 'wb'), 'foo'
41
self.wt.add(['file'], ['fileid'])
42
self.wt.add(['binfile'], ['binfileid'])
44
os.symlink('target1', 'symlink')
45
self.wt.add(['symlink'], ['linkid'])
46
self.wt.commit('message_1', rev_id = '1')
47
print >> open('file', 'wb'), 'bar'
48
print >> open('binfile', 'wb'), 'x' * 1023 + '\x00'
51
os.symlink('target2', 'symlink')
52
self.tree_1 = self.branch.repository.revision_tree('1')
53
self.inv_1 = self.branch.repository.get_inventory('1')
54
self.file_1 = self.inv_1['fileid']
55
self.file_1b = self.inv_1['binfileid']
56
self.tree_2 = self.workingtree_to_test_tree(self.wt)
57
self.tree_2.lock_read()
58
self.addCleanup(self.tree_2.unlock)
59
self.inv_2 = self.tree_2.inventory
60
self.file_2 = self.inv_2['fileid']
61
self.file_2b = self.inv_2['binfileid']
63
self.link_1 = self.inv_1['linkid']
64
self.link_2 = self.inv_2['linkid']
66
def test_file_diff_deleted(self):
68
self.file_1.diff(internal_diff,
69
"old_label", self.tree_1,
70
"/dev/null", None, None,
72
self.assertEqual(output.getvalue(), "--- old_label\n"
78
def test_file_diff_added(self):
80
self.file_1.diff(internal_diff,
81
"new_label", self.tree_1,
82
"/dev/null", None, None,
84
self.assertEqual(output.getvalue(), "--- /dev/null\n"
90
def test_file_diff_changed(self):
92
self.file_1.diff(internal_diff,
93
"/dev/null", self.tree_1,
94
"new_label", self.file_2, self.tree_2,
96
self.assertEqual(output.getvalue(), "--- /dev/null\n"
103
def test_file_diff_binary(self):
105
self.file_1.diff(internal_diff,
106
"/dev/null", self.tree_1,
107
"new_label", self.file_2b, self.tree_2,
109
self.assertEqual(output.getvalue(),
110
"Binary files /dev/null and new_label differ\n")
111
def test_link_diff_deleted(self):
112
if not has_symlinks():
115
self.link_1.diff(internal_diff,
116
"old_label", self.tree_1,
117
"/dev/null", None, None,
119
self.assertEqual(output.getvalue(),
120
"=== target was 'target1'\n")
122
def test_link_diff_added(self):
123
if not has_symlinks():
126
self.link_1.diff(internal_diff,
127
"new_label", self.tree_1,
128
"/dev/null", None, None,
129
output, reverse=True)
130
self.assertEqual(output.getvalue(),
131
"=== target is 'target1'\n")
133
def test_link_diff_changed(self):
134
if not has_symlinks():
137
self.link_1.diff(internal_diff,
138
"/dev/null", self.tree_1,
139
"new_label", self.link_2, self.tree_2,
141
self.assertEqual(output.getvalue(),
142
"=== target changed 'target1' => 'target2'\n")
145
class TestPreviousHeads(TestCaseWithTree):
148
# we want several inventories, that respectively
149
# give use the following scenarios:
150
# A) fileid not in any inventory (A),
151
# B) fileid present in one inventory (B) and (A,B)
152
# C) fileid present in two inventories, and they
153
# are not mutual descendents (B, C)
154
# D) fileid present in two inventories and one is
155
# a descendent of the other. (B, D)
156
super(TestPreviousHeads, self).setUp()
157
self.wt = self.make_branch_and_tree('.')
158
self.branch = self.wt.branch
159
self.build_tree(['file'])
160
self.wt.commit('new branch', allow_pointless=True, rev_id='A')
161
self.inv_A = self.branch.repository.get_inventory('A')
162
self.wt.add(['file'], ['fileid'])
163
self.wt.commit('add file', rev_id='B')
164
self.inv_B = self.branch.repository.get_inventory('B')
165
uncommit(self.branch, tree=self.wt)
166
self.assertEqual(self.branch.revision_history(), ['A'])
167
self.wt.commit('another add of file', rev_id='C')
168
self.inv_C = self.branch.repository.get_inventory('C')
169
self.wt.add_parent_tree_id('B')
170
self.wt.commit('merge in B', rev_id='D')
171
self.inv_D = self.branch.repository.get_inventory('D')
172
self.tree = self.workingtree_to_test_tree(self.wt)
173
self.tree.lock_read()
174
self.addCleanup(self.tree.unlock)
175
self.file_active = self.tree.inventory['fileid']
176
self.weave = self.branch.repository.weave_store.get_weave('fileid',
177
self.branch.repository.get_transaction())
179
def get_previous_heads(self, inventories):
180
return self.file_active.find_previous_heads(
182
self.branch.repository.weave_store,
183
self.branch.repository.get_transaction())
185
def test_fileid_in_no_inventory(self):
186
self.assertEqual({}, self.get_previous_heads([self.inv_A]))
188
def test_fileid_in_one_inventory(self):
189
self.assertEqual({'B':self.inv_B['fileid']},
190
self.get_previous_heads([self.inv_B]))
191
self.assertEqual({'B':self.inv_B['fileid']},
192
self.get_previous_heads([self.inv_A, self.inv_B]))
193
self.assertEqual({'B':self.inv_B['fileid']},
194
self.get_previous_heads([self.inv_B, self.inv_A]))
196
def test_fileid_in_two_inventories_gives_both_entries(self):
197
self.assertEqual({'B':self.inv_B['fileid'],
198
'C':self.inv_C['fileid']},
199
self.get_previous_heads([self.inv_B, self.inv_C]))
200
self.assertEqual({'B':self.inv_B['fileid'],
201
'C':self.inv_C['fileid']},
202
self.get_previous_heads([self.inv_C, self.inv_B]))
204
def test_fileid_in_two_inventories_already_merged_gives_head(self):
205
self.assertEqual({'D':self.inv_D['fileid']},
206
self.get_previous_heads([self.inv_B, self.inv_D]))
207
self.assertEqual({'D':self.inv_D['fileid']},
208
self.get_previous_heads([self.inv_D, self.inv_B]))
210
# TODO: test two inventories with the same file revision
213
class TestInventory(TestCaseWithTree):
216
self.tree = self.get_tree_with_subdirs_and_all_content_types()
217
self.tree.lock_read()
218
self.addCleanup(self.tree.unlock)
219
# Commenting out the following line still fails.
220
self.inv = self.tree.inventory
222
def test_symlink_target(self):
223
if not has_symlinks():
224
raise TestSkipped('No symlink support')
226
if isinstance(self.tree, MutableTree):
228
'symlinks not accurately represented in working trees')
229
entry = self.inv[self.inv.path2id('symlink')]
230
self.assertEqual(entry.symlink_target, 'link-target')
232
def test_symlink(self):
233
if not has_symlinks():
234
raise TestSkipped('No symlink support')
236
entry = self.inv[self.inv.path2id('symlink')]
237
self.assertEqual(entry.kind, 'symlink')
238
self.assertEqual(None, entry.text_size)