20
20
from cStringIO import StringIO
23
from bzrlib.diff import internal_diff
26
from bzrlib.tests import tree_implementations
24
27
from bzrlib.mutabletree import MutableTree
25
from bzrlib.osutils import (
28
from bzrlib.tests import TestSkipped
29
from bzrlib.tests.tree_implementations import TestCaseWithTree
28
from bzrlib.tests import SymlinkFeature, TestSkipped
29
from bzrlib.transform import _PreviewTree
30
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):
33
def get_entry(tree, file_id):
34
return tree.iter_entries_by_dir([file_id]).next()[1]
37
class TestPreviousHeads(tree_implementations.TestCaseWithTree):
148
40
# we want several inventories, that respectively
172
64
self.tree = self.workingtree_to_test_tree(self.wt)
173
65
self.tree.lock_read()
174
66
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):
67
self.file_active = get_entry(self.tree, 'fileid')
69
# TODO: test two inventories with the same file revision
72
class TestInventoryWithSymlinks(tree_implementations.TestCaseWithTree):
74
_test_needs_features = [tests.SymlinkFeature]
77
tree_implementations.TestCaseWithTree.setUp(self)
216
78
self.tree = self.get_tree_with_subdirs_and_all_content_types()
217
79
self.tree.lock_read()
218
80
self.addCleanup(self.tree.unlock)
219
# Commenting out the following line still fails.
220
self.inv = self.tree.inventory
222
82
def test_symlink_target(self):
223
if not has_symlinks():
224
raise TestSkipped('No symlink support')
226
if isinstance(self.tree, MutableTree):
83
if isinstance(self.tree, (MutableTree, _PreviewTree)):
227
84
raise TestSkipped(
228
'symlinks not accurately represented in working trees')
229
entry = self.inv[self.inv.path2id('symlink')]
85
'symlinks not accurately represented in working trees and'
87
entry = get_entry(self.tree, self.tree.path2id('symlink'))
230
88
self.assertEqual(entry.symlink_target, 'link-target')
90
def test_symlink_target_tree(self):
91
self.assertEqual('link-target',
92
self.tree.get_symlink_target('symlink'))
94
def test_kind_symlink(self):
95
self.assertEqual('symlink', self.tree.kind('symlink'))
96
self.assertIs(None, self.tree.get_file_size('symlink'))
232
98
def test_symlink(self):
233
if not has_symlinks():
234
raise TestSkipped('No symlink support')
236
entry = self.inv[self.inv.path2id('symlink')]
99
entry = get_entry(self.tree, self.tree.path2id('symlink'))
237
100
self.assertEqual(entry.kind, 'symlink')
238
101
self.assertEqual(None, entry.text_size)
104
class TestInventory(tree_implementations.TestCaseWithTree):
106
def test_paths2ids_recursive(self):
107
work_tree = self.make_branch_and_tree('tree')
108
self.build_tree(['tree/dir/', 'tree/dir/file'])
109
work_tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
110
tree = self._convert_tree(work_tree)
112
self.addCleanup(tree.unlock)
113
self.assertEqual(set(['dir-id', 'file-id']), tree.paths2ids(['dir']))
115
def test_paths2ids_forget_old(self):
116
work_tree = self.make_branch_and_tree('tree')
117
self.build_tree(['tree/file'])
118
work_tree.add('file', 'first-id')
119
work_tree.commit('commit old state')
120
work_tree.remove('file')
121
tree = self._convert_tree(work_tree)
123
self.addCleanup(tree.unlock)
124
self.assertEqual(set([]), tree.paths2ids(['file'],
125
require_versioned=False))
127
def _make_canonical_test_tree(self, commit=True):
128
# make a tree used by all the 'canonical' tests below.
129
work_tree = self.make_branch_and_tree('tree')
130
self.build_tree(['tree/dir/', 'tree/dir/file'])
131
work_tree.add(['dir', 'dir/file'])
133
work_tree.commit('commit 1')
136
def test_canonical_path(self):
137
work_tree = self._make_canonical_test_tree()
138
self.assertEqual('dir/file',
139
work_tree.get_canonical_inventory_path('Dir/File'))
141
def test_canonical_path_before_commit(self):
142
work_tree = self._make_canonical_test_tree(False)
143
# note: not committed.
144
self.assertEqual('dir/file',
145
work_tree.get_canonical_inventory_path('Dir/File'))
147
def test_canonical_path_dir(self):
148
# check it works when asked for just the directory portion.
149
work_tree = self._make_canonical_test_tree()
150
self.assertEqual('dir', work_tree.get_canonical_inventory_path('Dir'))
152
def test_canonical_path_root(self):
153
work_tree = self._make_canonical_test_tree()
154
self.assertEqual('', work_tree.get_canonical_inventory_path(''))
155
self.assertEqual('/', work_tree.get_canonical_inventory_path('/'))
157
def test_canonical_path_invalid_all(self):
158
work_tree = self._make_canonical_test_tree()
159
self.assertEqual('foo/bar',
160
work_tree.get_canonical_inventory_path('foo/bar'))
162
def test_canonical_invalid_child(self):
163
work_tree = self._make_canonical_test_tree()
164
self.assertEqual('dir/None',
165
work_tree.get_canonical_inventory_path('Dir/None'))