125
125
tree = tree.bzrdir.open_workingtree()
127
127
self.addCleanup(tree.unlock)
128
# One could add 'tree._validate' here but that would cause 'early' failues
129
# as far as higher level code is concerned. Possibly adding an
130
# expect_fail parameter to this function and if that is False then do a
128
if not invalid_delta:
132
130
return tree.inventory
135
def apply_inventory_WT_basis(self, basis, delta):
133
def _create_repo_revisions(repo, basis, delta, invalid_delta):
134
repo.start_write_group()
136
rev = revision.Revision('basis', timestamp=0, timezone=None,
137
message="", committer="foo@example.com")
138
basis.revision_id = 'basis'
139
create_texts_for_inv(repo, basis)
140
repo.add_revision('basis', rev, basis)
142
# We don't want to apply the delta to the basis, because we expect
143
# the delta is invalid.
145
result_inv.revision_id = 'result'
146
target_entries = None
148
result_inv = basis.create_by_apply_delta(delta, 'result')
149
create_texts_for_inv(repo, result_inv)
150
target_entries = list(result_inv.iter_entries_by_dir())
151
rev = revision.Revision('result', timestamp=0, timezone=None,
152
message="", committer="foo@example.com")
153
repo.add_revision('result', rev, result_inv)
154
repo.commit_write_group()
156
repo.abort_write_group()
158
return target_entries
161
def _get_basis_entries(tree):
162
basis_tree = tree.basis_tree()
163
basis_tree.lock_read()
164
basis_tree_entries = list(basis_tree.inventory.iter_entries_by_dir())
166
return basis_tree_entries
169
def _populate_different_tree(tree, basis, delta):
170
"""Put all entries into tree, but at a unique location."""
173
tree.add(['unique-dir'], ['unique-dir-id'], ['directory'])
174
for path, ie in basis.iter_entries_by_dir():
175
if ie.file_id in added_ids:
177
# We want a unique path for each of these, we use the file-id
178
tree.add(['unique-dir/' + ie.file_id], [ie.file_id], [ie.kind])
179
added_ids.add(ie.file_id)
180
for old_path, new_path, file_id, ie in delta:
181
if file_id in added_ids:
183
tree.add(['unique-dir/' + file_id], [file_id], [ie.kind])
186
def apply_inventory_WT_basis(test, basis, delta, invalid_delta=True):
136
187
"""Apply delta to basis and return the result.
138
189
This sets the parent and then calls update_basis_by_delta.
140
191
allow safety checks made by the WT to succeed, and finally ensures that all
141
192
items in the delta with a new path are present in the WT before calling
142
193
update_basis_by_delta.
144
195
:param basis: An inventory to be used as the basis.
145
196
:param delta: The inventory delta to apply:
146
197
:return: An inventory resulting from the application.
148
control = self.make_bzrdir('tree', format=self.format._matchingbzrdir)
199
control = test.make_bzrdir('tree', format=test.format._matchingbzrdir)
149
200
control.create_repository()
150
201
control.create_branch()
151
tree = self.format.initialize(control)
202
tree = test.format.initialize(control)
152
203
tree.lock_write()
154
repo = tree.branch.repository
155
repo.start_write_group()
157
rev = revision.Revision('basis', timestamp=0, timezone=None,
158
message="", committer="foo@example.com")
159
basis.revision_id = 'basis'
160
create_texts_for_inv(tree.branch.repository, basis)
161
repo.add_revision('basis', rev, basis)
162
# Add a revision for the result, with the basis content -
163
# update_basis_by_delta doesn't check that the delta results in
164
# result, and we want inconsistent deltas to get called on the
165
# tree, or else the code isn't actually checked.
166
rev = revision.Revision('result', timestamp=0, timezone=None,
167
message="", committer="foo@example.com")
168
basis.revision_id = 'result'
169
repo.add_revision('result', rev, basis)
170
repo.commit_write_group()
172
repo.abort_write_group()
205
target_entries = _create_repo_revisions(tree.branch.repository, basis,
206
delta, invalid_delta)
174
207
# Set the basis state as the trees current state
175
208
tree._write_inventory(basis)
176
209
# This reads basis from the repo and puts it into the tree's local
182
215
tree.lock_write()
184
217
tree.update_basis_by_delta('result', delta)
218
if not invalid_delta:
187
222
# reload tree - ensure we get what was written.
188
223
tree = tree.bzrdir.open_workingtree()
189
224
basis_tree = tree.basis_tree()
190
225
basis_tree.lock_read()
191
self.addCleanup(basis_tree.unlock)
192
# Note, that if the tree does not have a local cache, the trick above of
193
# setting the result as the basis, will come back to bite us. That said,
194
# all the implementations in bzr do have a local cache.
195
return basis_tree.inventory
198
def apply_inventory_Repository_add_inventory_by_delta(self, basis, delta):
226
test.addCleanup(basis_tree.unlock)
227
basis_inv = basis_tree.inventory
229
basis_entries = list(basis_inv.iter_entries_by_dir())
230
test.assertEqual(target_entries, basis_entries)
234
def apply_inventory_Repository_add_inventory_by_delta(self, basis, delta,
199
236
"""Apply delta to basis and return the result.
201
238
This inserts basis as a whole inventory and then uses
578
618
file1.text_sha1 = ''
580
620
delta = [(u'path', None, 'file-id', None)]
581
res_inv = self.apply_delta(self, inv, delta)
621
res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
582
622
self.assertEqual(None, res_inv.path2id('path'))
583
623
self.assertRaises(errors.NoSuchId, res_inv.id2path, 'file-id')
585
625
def test_rename_file(self):
586
626
inv = self.get_empty_inventory()
587
file1 = inventory.InventoryFile('file-id', 'path', inv.root.file_id)
588
file1.revision = 'result'
627
file1 = self.make_file_ie(name='path', parent_id=inv.root.file_id)
629
file2 = self.make_file_ie(name='path2', parent_id=inv.root.file_id)
594
630
delta = [(u'path', 'path2', 'file-id', file2)]
595
res_inv = self.apply_delta(self, inv, delta)
631
res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
596
632
self.assertEqual(None, res_inv.path2id('path'))
597
633
self.assertEqual('file-id', res_inv.path2id('path2'))
635
def test_replaced_at_new_path(self):
636
inv = self.get_empty_inventory()
637
file1 = self.make_file_ie(file_id='id1', parent_id=inv.root.file_id)
639
file2 = self.make_file_ie(file_id='id2', parent_id=inv.root.file_id)
640
delta = [(u'name', None, 'id1', None),
641
(None, u'name', 'id2', file2)]
642
res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
643
self.assertEqual('id2', res_inv.path2id('name'))
645
def test_rename_dir(self):
646
inv = self.get_empty_inventory()
647
dir1 = inventory.InventoryDirectory('dir-id', 'dir1', inv.root.file_id)
648
dir1.revision = 'basis'
649
file1 = self.make_file_ie(parent_id='dir-id')
652
dir2 = inventory.InventoryDirectory('dir-id', 'dir2', inv.root.file_id)
653
dir2.revision = 'result'
654
delta = [('dir1', 'dir2', 'dir-id', dir2)]
655
res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
656
# The file should be accessible under the new path
657
self.assertEqual('file-id', res_inv.path2id('dir2/name'))
659
def test_renamed_dir_with_renamed_child(self):
660
inv = self.get_empty_inventory()
661
dir1 = inventory.InventoryDirectory('dir-id', 'dir1', inv.root.file_id)
662
dir1.revision = 'basis'
663
file1 = self.make_file_ie('file-id-1', 'name1', parent_id='dir-id')
664
file2 = self.make_file_ie('file-id-2', 'name2', parent_id='dir-id')
668
dir2 = inventory.InventoryDirectory('dir-id', 'dir2', inv.root.file_id)
669
dir2.revision = 'result'
670
file2b = self.make_file_ie('file-id-2', 'name2', inv.root.file_id)
671
delta = [('dir1', 'dir2', 'dir-id', dir2),
672
('dir1/name2', 'name2', 'file-id-2', file2b)]
673
res_inv = self.apply_delta(self, inv, delta, invalid_delta=False)
674
# The file should be accessible under the new path
675
self.assertEqual('file-id-1', res_inv.path2id('dir2/name1'))
676
self.assertEqual(None, res_inv.path2id('dir2/name2'))
677
self.assertEqual('file-id-2', res_inv.path2id('name2'))
599
679
def test_is_root(self):
600
680
"""Ensure our root-checking code is accurate."""
601
681
inv = inventory.Inventory('TREE_ROOT')