/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

  • Committer: John Arbash Meinel
  • Date: 2009-07-31 17:42:29 UTC
  • mto: This revision was merged to the branch mainline in revision 4611.
  • Revision ID: john@arbash-meinel.com-20090731174229-w2zdsdlfpeddk8gl
Now we got to the per-workingtree tests, etc.

The main causes seem to break down into:
  bzrdir.clone() is known to be broken wrt locking, this effects
  everything that tries to 'push'

  shelf code is not compatible with strict locking

  merge code seems to have an issue. This might actually be the
  root cause of the clone() problems.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
 
18
18
from bzrlib import (
19
19
    chk_map,
20
 
    groupcompress,
21
20
    bzrdir,
22
21
    errors,
23
22
    inventory,
24
23
    osutils,
25
24
    repository,
26
25
    revision,
27
 
    tests,
28
26
    )
29
27
from bzrlib.inventory import (CHKInventory, Inventory, ROOT_ID, InventoryFile,
30
28
    InventoryDirectory, InventoryEntry, TreeReference)
67
65
    return multiply_tests(to_adapt, scenarios, result)
68
66
 
69
67
 
70
 
def create_texts_for_inv(repo, inv):
71
 
    for path, ie in inv.iter_entries():
72
 
        if ie.text_size:
73
 
            lines = ['a' * ie.text_size]
74
 
        else:
75
 
            lines = []
76
 
        repo.texts.add_lines((ie.file_id, ie.revision), [], lines)
77
 
    
78
68
def apply_inventory_Inventory(self, basis, delta):
79
69
    """Apply delta to basis and return the result.
80
70
    
147
137
            rev = revision.Revision('basis', timestamp=0, timezone=None,
148
138
                message="", committer="foo@example.com")
149
139
            basis.revision_id = 'basis'
150
 
            create_texts_for_inv(tree.branch.repository, basis)
151
140
            repo.add_revision('basis', rev, basis)
152
141
            # Add a revision for the result, with the basis content - 
153
142
            # update_basis_by_delta doesn't check that the delta results in
157
146
                message="", committer="foo@example.com")
158
147
            basis.revision_id = 'result'
159
148
            repo.add_revision('result', rev, basis)
160
 
            repo.commit_write_group()
161
149
        except:
162
150
            repo.abort_write_group()
163
151
            raise
 
152
        else:
 
153
            repo.commit_write_group()
164
154
        # Set the basis state as the trees current state
165
155
        tree._write_inventory(basis)
166
156
        # This reads basis from the repo and puts it into the tree's local
231
221
            rev = revision.Revision('basis', timestamp=0, timezone=None,
232
222
                message="", committer="foo@example.com")
233
223
            basis.revision_id = 'basis'
234
 
            create_texts_for_inv(repo, basis)
235
224
            repo.add_revision('basis', rev, basis)
236
 
            repo.commit_write_group()
237
225
        except:
238
226
            repo.abort_write_group()
239
227
            raise
 
228
        else:
 
229
            repo.commit_write_group()
240
230
    finally:
241
231
        repo.unlock()
242
232
    repo.lock_write()
259
249
    return repo.get_inventory('result')
260
250
 
261
251
 
262
 
class TestInventoryUpdates(TestCase):
263
 
 
264
 
    def test_creation_from_root_id(self):
265
 
        # iff a root id is passed to the constructor, a root directory is made
266
 
        inv = inventory.Inventory(root_id='tree-root')
267
 
        self.assertNotEqual(None, inv.root)
268
 
        self.assertEqual('tree-root', inv.root.file_id)
269
 
 
270
 
    def test_add_path_of_root(self):
271
 
        # if no root id is given at creation time, there is no root directory
272
 
        inv = inventory.Inventory(root_id=None)
273
 
        self.assertIs(None, inv.root)
274
 
        # add a root entry by adding its path
275
 
        ie = inv.add_path("", "directory", "my-root")
276
 
        ie.revision = 'test-rev'
277
 
        self.assertEqual("my-root", ie.file_id)
278
 
        self.assertIs(ie, inv.root)
279
 
 
280
 
    def test_add_path(self):
281
 
        inv = inventory.Inventory(root_id='tree_root')
282
 
        ie = inv.add_path('hello', 'file', 'hello-id')
283
 
        self.assertEqual('hello-id', ie.file_id)
284
 
        self.assertEqual('file', ie.kind)
285
 
 
286
 
    def test_copy(self):
287
 
        """Make sure copy() works and creates a deep copy."""
288
 
        inv = inventory.Inventory(root_id='some-tree-root')
289
 
        ie = inv.add_path('hello', 'file', 'hello-id')
290
 
        inv2 = inv.copy()
291
 
        inv.root.file_id = 'some-new-root'
292
 
        ie.name = 'file2'
293
 
        self.assertEqual('some-tree-root', inv2.root.file_id)
294
 
        self.assertEqual('hello', inv2['hello-id'].name)
295
 
 
296
 
    def test_copy_empty(self):
297
 
        """Make sure an empty inventory can be copied."""
298
 
        inv = inventory.Inventory(root_id=None)
299
 
        inv2 = inv.copy()
300
 
        self.assertIs(None, inv2.root)
301
 
 
302
 
    def test_copy_copies_root_revision(self):
303
 
        """Make sure the revision of the root gets copied."""
304
 
        inv = inventory.Inventory(root_id='someroot')
305
 
        inv.root.revision = 'therev'
306
 
        inv2 = inv.copy()
307
 
        self.assertEquals('someroot', inv2.root.file_id)
308
 
        self.assertEquals('therev', inv2.root.revision)
309
 
 
310
 
    def test_create_tree_reference(self):
311
 
        inv = inventory.Inventory('tree-root-123')
312
 
        inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
313
 
                              revision='rev', reference_revision='rev2'))
314
 
 
315
 
    def test_error_encoding(self):
316
 
        inv = inventory.Inventory('tree-root')
317
 
        inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
318
 
        e = self.assertRaises(errors.InconsistentDelta, inv.add,
319
 
            InventoryFile('b-id', u'\u1234', 'tree-root'))
320
 
        self.assertContainsRe(str(e), r'\\u1234')
321
 
 
322
 
    def test_add_recursive(self):
323
 
        parent = InventoryDirectory('src-id', 'src', 'tree-root')
324
 
        child = InventoryFile('hello-id', 'hello.c', 'src-id')
325
 
        parent.children[child.file_id] = child
326
 
        inv = inventory.Inventory('tree-root')
327
 
        inv.add(parent)
328
 
        self.assertEqual('src/hello.c', inv.id2path('hello-id'))
329
 
 
330
 
 
331
 
 
332
252
class TestDeltaApplication(TestCaseWithTransport):
333
253
 
334
254
    def get_empty_inventory(self, reference_inv=None):
575
495
            inv, delta)
576
496
 
577
497
 
578
 
class TestInventory(TestCase):
579
 
 
580
 
    def test_is_root(self):
581
 
        """Ensure our root-checking code is accurate."""
582
 
        inv = inventory.Inventory('TREE_ROOT')
583
 
        self.assertTrue(inv.is_root('TREE_ROOT'))
584
 
        self.assertFalse(inv.is_root('booga'))
585
 
        inv.root.file_id = 'booga'
586
 
        self.assertFalse(inv.is_root('TREE_ROOT'))
587
 
        self.assertTrue(inv.is_root('booga'))
588
 
        # works properly even if no root is set
589
 
        inv.root = None
590
 
        self.assertFalse(inv.is_root('TREE_ROOT'))
591
 
        self.assertFalse(inv.is_root('booga'))
592
 
 
593
 
 
594
498
class TestInventoryEntry(TestCase):
595
499
 
596
500
    def test_file_kind_character(self):
738
642
        self.assertEqual(expected_change, change)
739
643
 
740
644
 
741
 
class TestCHKInventory(tests.TestCaseWithMemoryTransport):
 
645
class TestCHKInventory(TestCaseWithTransport):
742
646
 
743
647
    def get_chk_bytes(self):
744
 
        factory = groupcompress.make_pack_factory(True, True, 1)
745
 
        trans = self.get_transport('')
746
 
        return factory(trans)
 
648
        # The easiest way to get a CHK store is a development6 repository and
 
649
        # then work with the chk_bytes attribute directly.
 
650
        repo = self.make_repository(".", format="development6-rich-root")
 
651
        repo.lock_write()
 
652
        self.addCleanup(repo.unlock)
 
653
        repo.start_write_group()
 
654
        self.addCleanup(repo.abort_write_group)
 
655
        return repo.chk_bytes
747
656
 
748
657
    def read_bytes(self, chk_bytes, key):
749
658
        stream = chk_bytes.get_record_stream([key], 'unordered', True)
1214
1123
        self.assertIsInstance(ie2.name, unicode)
1215
1124
        self.assertEqual(('tree\xce\xa9name', 'tree-root-id', 'tree-rev-id'),
1216
1125
                         inv._bytes_to_utf8name_key(bytes))
1217
 
 
1218
 
 
1219
 
class TestCHKInventoryExpand(tests.TestCaseWithMemoryTransport):
1220
 
 
1221
 
    def get_chk_bytes(self):
1222
 
        factory = groupcompress.make_pack_factory(True, True, 1)
1223
 
        trans = self.get_transport('')
1224
 
        return factory(trans)
1225
 
 
1226
 
    def make_dir(self, inv, name, parent_id):
1227
 
        inv.add(inv.make_entry('directory', name, parent_id, name + '-id'))
1228
 
 
1229
 
    def make_file(self, inv, name, parent_id, content='content\n'):
1230
 
        ie = inv.make_entry('file', name, parent_id, name + '-id')
1231
 
        ie.text_sha1 = osutils.sha_string(content)
1232
 
        ie.text_size = len(content)
1233
 
        inv.add(ie)
1234
 
 
1235
 
    def make_simple_inventory(self):
1236
 
        inv = Inventory('TREE_ROOT')
1237
 
        inv.revision_id = "revid"
1238
 
        inv.root.revision = "rootrev"
1239
 
        # /                 TREE_ROOT
1240
 
        # dir1/             dir1-id
1241
 
        #   sub-file1       sub-file1-id
1242
 
        #   sub-file2       sub-file2-id
1243
 
        #   sub-dir1/       sub-dir1-id
1244
 
        #     subsub-file1  subsub-file1-id
1245
 
        # dir2/             dir2-id
1246
 
        #   sub2-file1      sub2-file1-id
1247
 
        # top               top-id
1248
 
        self.make_dir(inv, 'dir1', 'TREE_ROOT')
1249
 
        self.make_dir(inv, 'dir2', 'TREE_ROOT')
1250
 
        self.make_dir(inv, 'sub-dir1', 'dir1-id')
1251
 
        self.make_file(inv, 'top', 'TREE_ROOT')
1252
 
        self.make_file(inv, 'sub-file1', 'dir1-id')
1253
 
        self.make_file(inv, 'sub-file2', 'dir1-id')
1254
 
        self.make_file(inv, 'subsub-file1', 'sub-dir1-id')
1255
 
        self.make_file(inv, 'sub2-file1', 'dir2-id')
1256
 
        chk_bytes = self.get_chk_bytes()
1257
 
        #  use a small maximum_size to force internal paging structures
1258
 
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv,
1259
 
                        maximum_size=100,
1260
 
                        search_key_name='hash-255-way')
1261
 
        bytes = ''.join(chk_inv.to_lines())
1262
 
        return CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
1263
 
 
1264
 
    def assert_Getitems(self, expected_fileids, inv, file_ids):
1265
 
        self.assertEqual(sorted(expected_fileids),
1266
 
                         sorted([ie.file_id for ie in inv._getitems(file_ids)]))
1267
 
 
1268
 
    def assertExpand(self, all_ids, inv, file_ids):
1269
 
        (val_all_ids,
1270
 
         val_children) = inv._expand_fileids_to_parents_and_children(file_ids)
1271
 
        self.assertEqual(set(all_ids), val_all_ids)
1272
 
        entries = inv._getitems(val_all_ids)
1273
 
        expected_children = {}
1274
 
        for entry in entries:
1275
 
            s = expected_children.setdefault(entry.parent_id, [])
1276
 
            s.append(entry.file_id)
1277
 
        val_children = dict((k, sorted(v)) for k, v
1278
 
                            in val_children.iteritems())
1279
 
        expected_children = dict((k, sorted(v)) for k, v
1280
 
                            in expected_children.iteritems())
1281
 
        self.assertEqual(expected_children, val_children)
1282
 
 
1283
 
    def test_make_simple_inventory(self):
1284
 
        inv = self.make_simple_inventory()
1285
 
        layout = []
1286
 
        for path, entry in inv.iter_entries_by_dir():
1287
 
            layout.append((path, entry.file_id))
1288
 
        self.assertEqual([
1289
 
            ('', 'TREE_ROOT'),
1290
 
            ('dir1', 'dir1-id'),
1291
 
            ('dir2', 'dir2-id'),
1292
 
            ('top', 'top-id'),
1293
 
            ('dir1/sub-dir1', 'sub-dir1-id'),
1294
 
            ('dir1/sub-file1', 'sub-file1-id'),
1295
 
            ('dir1/sub-file2', 'sub-file2-id'),
1296
 
            ('dir1/sub-dir1/subsub-file1', 'subsub-file1-id'),
1297
 
            ('dir2/sub2-file1', 'sub2-file1-id'),
1298
 
            ], layout)
1299
 
 
1300
 
    def test__getitems(self):
1301
 
        inv = self.make_simple_inventory()
1302
 
        # Reading from disk
1303
 
        self.assert_Getitems(['dir1-id'], inv, ['dir1-id'])
1304
 
        self.assertTrue('dir1-id' in inv._fileid_to_entry_cache)
1305
 
        self.assertFalse('sub-file2-id' in inv._fileid_to_entry_cache)
1306
 
        # From cache
1307
 
        self.assert_Getitems(['dir1-id'], inv, ['dir1-id'])
1308
 
        # Mixed
1309
 
        self.assert_Getitems(['dir1-id', 'sub-file2-id'], inv,
1310
 
                             ['dir1-id', 'sub-file2-id'])
1311
 
        self.assertTrue('dir1-id' in inv._fileid_to_entry_cache)
1312
 
        self.assertTrue('sub-file2-id' in inv._fileid_to_entry_cache)
1313
 
 
1314
 
    def test_single_file(self):
1315
 
        inv = self.make_simple_inventory()
1316
 
        self.assertExpand(['TREE_ROOT', 'top-id'], inv, ['top-id'])
1317
 
 
1318
 
    def test_get_all_parents(self):
1319
 
        inv = self.make_simple_inventory()
1320
 
        self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id',
1321
 
                           'subsub-file1-id',
1322
 
                          ], inv, ['subsub-file1-id'])
1323
 
 
1324
 
    def test_get_children(self):
1325
 
        inv = self.make_simple_inventory()
1326
 
        self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id',
1327
 
                           'sub-file1-id', 'sub-file2-id', 'subsub-file1-id',
1328
 
                          ], inv, ['dir1-id'])
1329
 
 
1330
 
    def test_from_root(self):
1331
 
        inv = self.make_simple_inventory()
1332
 
        self.assertExpand(['TREE_ROOT', 'dir1-id', 'dir2-id', 'sub-dir1-id',
1333
 
                           'sub-file1-id', 'sub-file2-id', 'sub2-file1-id',
1334
 
                           'subsub-file1-id', 'top-id'], inv, ['TREE_ROOT'])
1335
 
 
1336
 
    def test_top_level_file(self):
1337
 
        inv = self.make_simple_inventory()
1338
 
        self.assertExpand(['TREE_ROOT', 'top-id'], inv, ['top-id'])
1339
 
 
1340
 
    def test_subsub_file(self):
1341
 
        inv = self.make_simple_inventory()
1342
 
        self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id',
1343
 
                           'subsub-file1-id'], inv, ['subsub-file1-id'])
1344
 
 
1345
 
    def test_sub_and_root(self):
1346
 
        inv = self.make_simple_inventory()
1347
 
        self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id', 'top-id',
1348
 
                           'subsub-file1-id'], inv, ['top-id', 'subsub-file1-id'])