13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for different inventory implementations"""
35
35
from bzrlib.tests import (
37
multiply_tests_from_modules,
41
class TestInventoryBasics(TestCase):
42
# Most of these were moved the rather old bzrlib.tests.test_inv module
40
class TestInventory(TestCase):
44
42
def make_inventory(self, root_id):
45
43
return self.inventory_class(root_id=root_id)
45
def prepare_inv_with_nested_dirs(self):
46
inv = self.make_inventory('tree-root')
47
for args in [('src', 'directory', 'src-id'),
48
('doc', 'directory', 'doc-id'),
49
('src/hello.c', 'file', 'hello-id'),
50
('src/bye.c', 'file', 'bye-id'),
51
('zz', 'file', 'zz-id'),
52
('src/sub/', 'directory', 'sub-id'),
53
('src/zz.c', 'file', 'zzc-id'),
54
('src/sub/a', 'file', 'a-id'),
55
('Makefile', 'file', 'makefile-id')]:
60
class TestInventoryUpdates(TestInventory):
47
62
def test_creation_from_root_id(self):
48
63
# iff a root id is passed to the constructor, a root directory is made
49
64
inv = self.make_inventory(root_id='tree-root')
89
104
self.assertEquals('someroot', inv2.root.file_id)
90
105
self.assertEquals('therev', inv2.root.revision)
92
def test_is_root(self):
93
"""Ensure our root-checking code is accurate."""
94
inv = self.make_inventory('TREE_ROOT')
95
self.assertTrue(inv.is_root('TREE_ROOT'))
96
self.assertFalse(inv.is_root('booga'))
97
inv.root.file_id = 'booga'
98
self.assertFalse(inv.is_root('TREE_ROOT'))
99
self.assertTrue(inv.is_root('booga'))
100
# works properly even if no root is set
102
self.assertFalse(inv.is_root('TREE_ROOT'))
103
self.assertFalse(inv.is_root('booga'))
105
107
def test_create_tree_reference(self):
106
108
inv = self.make_inventory('tree-root-123')
107
109
inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
118
120
self.fail('BzrError not raised')
122
def test_add_recursive(self):
123
parent = InventoryDirectory('src-id', 'src', 'tree-root')
124
child = InventoryFile('hello-id', 'hello.c', 'src-id')
125
parent.children[child.file_id] = child
126
inv = self.make_inventory('tree-root')
128
self.assertEqual('src/hello.c', inv.id2path('hello-id'))
131
class TestInventoryApplyDelta(TestInventory):
133
def test_apply_delta_add(self):
134
inv = self.make_inventory('tree-root')
136
(None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
138
self.assertEqual('a', inv.id2path('a-id'))
140
def test_apply_delta_delete(self):
141
inv = self.make_inventory('tree-root')
143
(None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
145
self.assertEqual('a', inv.id2path('a-id'))
147
inv.apply_delta([("a", None, "a-id", a_ie)])
148
self.assertRaises(errors.NoSuchId, inv.id2path, 'a-id')
150
def test_apply_delta_rename(self):
151
inv = self.make_inventory('tree-root')
153
(None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
155
self.assertEqual('a', inv.id2path('a-id'))
157
b_ie = InventoryFile(a_ie.file_id, "b", a_ie.parent_id)
158
inv.apply_delta([("a", "b", "a-id", b_ie)])
159
self.assertEqual("b", inv.id2path('a-id'))
161
def test_apply_delta_illegal(self):
162
# A file-id cannot appear in a delta more than once
163
inv = self.make_inventory('tree-root')
164
self.assertRaises(AssertionError, inv.apply_delta, [
165
("a", "a", "id-1", InventoryFile('id-1', 'a', 'tree-root')),
166
("a", "b", "id-1", InventoryFile('id-1', 'b', 'tree-root')),
170
class TestInventoryReads(TestInventory):
172
def test_is_root(self):
173
"""Ensure our root-checking code is accurate."""
174
inv = self.make_inventory('TREE_ROOT')
175
self.assertTrue(inv.is_root('TREE_ROOT'))
176
self.assertFalse(inv.is_root('booga'))
177
inv.root.file_id = 'booga'
178
self.assertFalse(inv.is_root('TREE_ROOT'))
179
self.assertTrue(inv.is_root('booga'))
180
# works properly even if no root is set
182
self.assertFalse(inv.is_root('TREE_ROOT'))
183
self.assertFalse(inv.is_root('booga'))
120
185
def test_ids(self):
121
186
"""Test detection of files within selected directories."""
122
187
inv = self.make_inventory(ROOT_ID)
158
223
], [(path, ie.file_id) for path, ie in inv.iter_entries()])
160
225
def test_iter_entries_by_dir(self):
161
inv = self.make_inventory('tree-root')
162
for args in [('src', 'directory', 'src-id'),
163
('doc', 'directory', 'doc-id'),
164
('src/hello.c', 'file', 'hello-id'),
165
('src/bye.c', 'file', 'bye-id'),
166
('zz', 'file', 'zz-id'),
167
('src/sub/', 'directory', 'sub-id'),
168
('src/zz.c', 'file', 'zzc-id'),
169
('src/sub/a', 'file', 'a-id'),
170
('Makefile', 'file', 'makefile-id')]:
226
inv = self. prepare_inv_with_nested_dirs()
172
227
self.assertEqual([
173
228
('', 'tree-root'),
174
229
('Makefile', 'makefile-id'),
232
287
('src/bye.c', 'bye-id'),
233
288
], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
234
289
specific_file_ids=('bye-id',), yield_parents=True)])
236
def test_add_recursive(self):
237
parent = InventoryDirectory('src-id', 'src', 'tree-root')
238
child = InventoryFile('hello-id', 'hello.c', 'src-id')
239
parent.children[child.file_id] = child
240
inv = self.make_inventory('tree-root')
242
self.assertEqual('src/hello.c', inv.id2path('hello-id'))
292
class TestInventoryFiltering(TestInventory):
294
def test_inv_filter_empty(self):
295
inv = self.prepare_inv_with_nested_dirs()
296
new_inv = inv.filter([])
299
], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
301
def test_inv_filter_files(self):
302
inv = self.prepare_inv_with_nested_dirs()
303
new_inv = inv.filter(['zz-id', 'hello-id', 'a-id'])
307
('src/hello.c', 'hello-id'),
308
('src/sub', 'sub-id'),
309
('src/sub/a', 'a-id'),
311
], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
313
def test_inv_filter_dirs(self):
314
inv = self.prepare_inv_with_nested_dirs()
315
new_inv = inv.filter(['doc-id', 'sub-id'])
320
('src/sub', 'sub-id'),
321
('src/sub/a', 'a-id'),
322
], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
324
def test_inv_filter_files_and_dirs(self):
325
inv = self.prepare_inv_with_nested_dirs()
326
new_inv = inv.filter(['makefile-id', 'src-id'])
329
('Makefile', 'makefile-id'),
331
('src/bye.c', 'bye-id'),
332
('src/hello.c', 'hello-id'),
333
('src/sub', 'sub-id'),
334
('src/sub/a', 'a-id'),
335
('src/zz.c', 'zzc-id'),
336
], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])