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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for interface conformance of inventories of working trees."""
27
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
30
class TestRevert(TestCaseWithWorkingTree):
32
def test_dangling_id(self):
33
wt = self.make_branch_and_tree('b1')
35
self.addCleanup(wt.unlock)
36
self.assertEqual(len(wt.inventory), 1)
37
open('b1/a', 'wb').write('a test\n')
39
self.assertEqual(len(wt.inventory), 2)
40
wt.flush() # workaround revert doing wt._write_inventory for now.
43
self.assertEqual(len(wt.inventory), 1)
46
class TestApplyInventoryDelta(TestCaseWithWorkingTree):
49
wt = self.make_branch_and_tree('.')
51
self.addCleanup(wt.unlock)
52
root_id = wt.get_root_id()
53
wt.apply_inventory_delta([(None, 'bar/foo', 'foo-id',
54
inventory.InventoryFile('foo-id', 'foo', parent_id='bar-id')),
55
(None, 'bar', 'bar-id', inventory.InventoryDirectory('bar-id',
56
'bar', parent_id=root_id))])
57
self.assertEqual('bar/foo', wt.inventory.id2path('foo-id'))
58
self.assertEqual('bar', wt.inventory.id2path('bar-id'))
60
def test_remove(self):
61
wt = self.make_branch_and_tree('.')
63
self.addCleanup(wt.unlock)
64
self.build_tree(['foo/', 'foo/bar'])
65
wt.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
66
wt.apply_inventory_delta([('foo', None, 'foo-id', None),
67
('foo/bar', None, 'bar-id', None)])
68
self.assertIs(None, wt.path2id('foo'))
70
def test_rename_dir_with_children(self):
71
wt = self.make_branch_and_tree('.')
73
root_id = wt.get_root_id()
74
self.addCleanup(wt.unlock)
75
self.build_tree(['foo/', 'foo/bar'])
76
wt.add(['foo', 'foo/bar'],
78
wt.apply_inventory_delta([('foo', 'baz', 'foo-id',
79
inventory.InventoryDirectory('foo-id', 'baz', root_id))])
80
# foo/bar should have been followed the rename of its parent to baz/bar
81
self.assertEqual('baz', wt.id2path('foo-id'))
82
self.assertEqual('baz/bar', wt.id2path('bar-id'))
84
def test_rename_dir_with_children_with_children(self):
85
wt = self.make_branch_and_tree('.')
87
root_id = wt.get_root_id()
88
self.addCleanup(wt.unlock)
89
self.build_tree(['foo/', 'foo/bar/', 'foo/bar/baz'])
90
wt.add(['foo', 'foo/bar', 'foo/bar/baz'],
91
['foo-id', 'bar-id', 'baz-id'])
92
wt.apply_inventory_delta([('foo', 'quux', 'foo-id',
93
inventory.InventoryDirectory('foo-id', 'quux', root_id))])
94
# foo/bar/baz should have been followed the rename of its parent's
95
# parent to quux/bar/baz
96
self.assertEqual('quux/bar/baz', wt.id2path('baz-id'))
98
def test_rename_file(self):
99
wt = self.make_branch_and_tree('.')
101
self.addCleanup(wt.unlock)
102
self.build_tree(['foo/', 'foo/bar', 'baz/'])
103
wt.add(['foo', 'foo/bar', 'baz'],
104
['foo-id', 'bar-id', 'baz-id'])
105
wt.apply_inventory_delta([('foo/bar', 'baz/bar', 'bar-id',
106
inventory.InventoryFile('bar-id', 'bar', 'baz-id'))])
107
self.assertEqual('baz/bar', wt.id2path('bar-id'))
109
def test_rename_swap(self):
110
"""Test the swap-names edge case.
112
foo and bar should swap names, but retain their children. If this
113
works, any simpler rename ought to work.
115
wt = self.make_branch_and_tree('.')
117
root_id = wt.get_root_id()
118
self.addCleanup(wt.unlock)
119
self.build_tree(['foo/', 'foo/bar', 'baz/', 'baz/qux'])
120
wt.add(['foo', 'foo/bar', 'baz', 'baz/qux'],
121
['foo-id', 'bar-id', 'baz-id', 'qux-id'])
122
wt.apply_inventory_delta([('foo', 'baz', 'foo-id',
123
inventory.InventoryDirectory('foo-id', 'baz', root_id)),
124
('baz', 'foo', 'baz-id',
125
inventory.InventoryDirectory('baz-id', 'foo', root_id))])
126
self.assertEqual('baz/bar', wt.id2path('bar-id'))
127
self.assertEqual('foo/qux', wt.id2path('qux-id'))
129
def test_child_rename_ordering(self):
130
"""Test the rename-parent, move child edge case.
132
(A naive implementation may move the parent first, and then be
133
unable to find the child.)
135
wt = self.make_branch_and_tree('.')
136
root_id = wt.get_root_id()
137
self.build_tree(['dir/', 'dir/child', 'other/'])
138
wt.add(['dir', 'dir/child', 'other'],
139
['dir-id', 'child-id', 'other-id'])
140
# this delta moves dir-id to dir2 and reparents
141
# child-id to a parent of other-id
142
wt.apply_inventory_delta([('dir', 'dir2', 'dir-id',
143
inventory.InventoryDirectory('dir-id', 'dir2', root_id)),
144
('dir/child', 'other/child', 'child-id',
145
inventory.InventoryFile('child-id', 'child', 'other-id'))])
146
self.assertEqual('dir2', wt.id2path('dir-id'))
147
self.assertEqual('other/child', wt.id2path('child-id'))
149
def test_replace_root(self):
150
wt = self.make_branch_and_tree('.')
152
self.addCleanup(wt.unlock)
154
root_id = wt.get_root_id()
155
wt.apply_inventory_delta([('', None, root_id, None),
156
(None, '', 'root-id',
157
inventory.InventoryDirectory('root-id', '', None))])