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"""
40
class TestInventoryBasics(TestCase):
41
# Most of these were moved the rather old bzrlib.tests.test_inv module
40
class TestInventory(TestCase):
43
42
def make_inventory(self, root_id):
44
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):
46
62
def test_creation_from_root_id(self):
47
63
# iff a root id is passed to the constructor, a root directory is made
48
64
inv = self.make_inventory(root_id='tree-root')
88
104
self.assertEquals('someroot', inv2.root.file_id)
89
105
self.assertEquals('therev', inv2.root.revision)
91
def test_is_root(self):
92
"""Ensure our root-checking code is accurate."""
93
inv = self.make_inventory('TREE_ROOT')
94
self.assertTrue(inv.is_root('TREE_ROOT'))
95
self.assertFalse(inv.is_root('booga'))
96
inv.root.file_id = 'booga'
97
self.assertFalse(inv.is_root('TREE_ROOT'))
98
self.assertTrue(inv.is_root('booga'))
99
# works properly even if no root is set
101
self.assertFalse(inv.is_root('TREE_ROOT'))
102
self.assertFalse(inv.is_root('booga'))
104
107
def test_create_tree_reference(self):
105
108
inv = self.make_inventory('tree-root-123')
106
109
inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
117
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'))
119
185
def test_ids(self):
120
186
"""Test detection of files within selected directories."""
121
187
inv = self.make_inventory(ROOT_ID)
145
211
('doc', 'directory', 'doc-id'),
146
212
('src/hello.c', 'file', 'hello-id'),
147
213
('src/bye.c', 'file', 'bye-id'),
214
('src/sub', 'directory', 'sub-id'),
215
('src/sub/a', 'file', 'a-id'),
148
216
('Makefile', 'file', 'makefile-id')]:
149
217
inv.add_path(*args)
150
220
self.assertEqual([
151
221
('', 'tree-root'),
152
222
('Makefile', 'makefile-id'),
154
224
('src', 'src-id'),
155
225
('src/bye.c', 'bye-id'),
156
226
('src/hello.c', 'hello-id'),
227
('src/sub', 'sub-id'),
228
('src/sub/a', 'a-id'),
157
229
], [(path, ie.file_id) for path, ie in inv.iter_entries()])
231
# Test a subdirectory
234
('hello.c', 'hello-id'),
237
], [(path, ie.file_id) for path, ie in inv.iter_entries(
240
# Test not recursing at the root level
243
('Makefile', 'makefile-id'),
246
], [(path, ie.file_id) for path, ie in inv.iter_entries(
249
# Test not recursing at a subdirectory level
252
('hello.c', 'hello-id'),
254
], [(path, ie.file_id) for path, ie in inv.iter_entries(
255
from_dir='src-id', recursive=False)])
257
def test_iter_just_entries(self):
258
inv = self.make_inventory('tree-root')
259
for args in [('src', 'directory', 'src-id'),
260
('doc', 'directory', 'doc-id'),
261
('src/hello.c', 'file', 'hello-id'),
262
('src/bye.c', 'file', 'bye-id'),
263
('Makefile', 'file', 'makefile-id')]:
272
], sorted([ie.file_id for ie in inv.iter_just_entries()]))
159
274
def test_iter_entries_by_dir(self):
160
inv = self.make_inventory('tree-root')
161
for args in [('src', 'directory', 'src-id'),
162
('doc', 'directory', 'doc-id'),
163
('src/hello.c', 'file', 'hello-id'),
164
('src/bye.c', 'file', 'bye-id'),
165
('zz', 'file', 'zz-id'),
166
('src/sub/', 'directory', 'sub-id'),
167
('src/zz.c', 'file', 'zzc-id'),
168
('src/sub/a', 'file', 'a-id'),
169
('Makefile', 'file', 'makefile-id')]:
275
inv = self. prepare_inv_with_nested_dirs()
171
276
self.assertEqual([
172
277
('', 'tree-root'),
173
278
('Makefile', 'makefile-id'),
231
336
('src/bye.c', 'bye-id'),
232
337
], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
233
338
specific_file_ids=('bye-id',), yield_parents=True)])
235
def test_add_recursive(self):
236
parent = InventoryDirectory('src-id', 'src', 'tree-root')
237
child = InventoryFile('hello-id', 'hello.c', 'src-id')
238
parent.children[child.file_id] = child
239
inv = self.make_inventory('tree-root')
241
self.assertEqual('src/hello.c', inv.id2path('hello-id'))
341
class TestInventoryFiltering(TestInventory):
343
def test_inv_filter_empty(self):
344
inv = self.prepare_inv_with_nested_dirs()
345
new_inv = inv.filter([])
348
], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
350
def test_inv_filter_files(self):
351
inv = self.prepare_inv_with_nested_dirs()
352
new_inv = inv.filter(['zz-id', 'hello-id', 'a-id'])
356
('src/hello.c', 'hello-id'),
357
('src/sub', 'sub-id'),
358
('src/sub/a', 'a-id'),
360
], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
362
def test_inv_filter_dirs(self):
363
inv = self.prepare_inv_with_nested_dirs()
364
new_inv = inv.filter(['doc-id', 'sub-id'])
369
('src/sub', 'sub-id'),
370
('src/sub/a', 'a-id'),
371
], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
373
def test_inv_filter_files_and_dirs(self):
374
inv = self.prepare_inv_with_nested_dirs()
375
new_inv = inv.filter(['makefile-id', 'src-id'])
378
('Makefile', 'makefile-id'),
380
('src/bye.c', 'bye-id'),
381
('src/hello.c', 'hello-id'),
382
('src/sub', 'sub-id'),
383
('src/sub/a', 'a-id'),
384
('src/zz.c', 'zzc-id'),
385
], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])