~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 04:25:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5472.
  • Revision ID: andrew.bennetts@canonical.com-20101008042510-sg9vdhmnggilzxsk
Fix stray TAB in source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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
24
24
    repository,
25
25
    revision,
26
26
    tests,
27
 
    workingtree,
28
 
    )
29
 
from bzrlib.inventory import (
30
 
    CHKInventory,
31
 
    Inventory,
32
 
    ROOT_ID,
33
 
    InventoryFile,
34
 
    InventoryDirectory,
35
 
    InventoryEntry,
36
 
    TreeReference,
37
 
    mutable_inventory_from_tree,
38
 
    )
 
27
    )
 
28
from bzrlib.inventory import (CHKInventory, Inventory, ROOT_ID, InventoryFile,
 
29
    InventoryDirectory, InventoryEntry, TreeReference)
39
30
from bzrlib.tests import (
40
31
    TestCase,
41
32
    TestCaseWithTransport,
 
33
    condition_isinstance,
 
34
    multiply_tests,
 
35
    split_suite_by_condition,
42
36
    )
43
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
44
 
 
45
 
 
46
 
load_tests = load_tests_apply_scenarios
47
 
 
48
 
 
49
 
def delta_application_scenarios():
 
37
from bzrlib.tests.per_workingtree import workingtree_formats
 
38
 
 
39
 
 
40
def load_tests(standard_tests, module, loader):
 
41
    """Parameterise some inventory tests."""
 
42
    to_adapt, result = split_suite_by_condition(standard_tests,
 
43
        condition_isinstance(TestDeltaApplication))
50
44
    scenarios = [
51
45
        ('Inventory', {'apply_delta':apply_inventory_Inventory}),
52
46
        ]
57
51
    # just creating trees.
58
52
    formats = set()
59
53
    for _, format in repository.format_registry.iteritems():
60
 
        if format.supports_full_versioned_files:
61
 
            scenarios.append((str(format.__name__), {
62
 
                'apply_delta':apply_inventory_Repository_add_inventory_by_delta,
63
 
                'format':format}))
64
 
    for format in workingtree.format_registry._get_all():
65
 
        repo_fmt = format._matchingbzrdir.repository_format
66
 
        if not repo_fmt.supports_full_versioned_files:
67
 
            continue
 
54
        scenarios.append((str(format.__name__), {
 
55
            'apply_delta':apply_inventory_Repository_add_inventory_by_delta,
 
56
            'format':format}))
 
57
    for format in workingtree_formats():
68
58
        scenarios.append(
69
59
            (str(format.__class__.__name__) + ".update_basis_by_delta", {
70
60
            'apply_delta':apply_inventory_WT_basis,
73
63
            (str(format.__class__.__name__) + ".apply_inventory_delta", {
74
64
            'apply_delta':apply_inventory_WT,
75
65
            'format':format}))
76
 
    return scenarios
 
66
    return multiply_tests(to_adapt, scenarios, result)
77
67
 
78
68
 
79
69
def create_texts_for_inv(repo, inv):
83
73
        else:
84
74
            lines = []
85
75
        repo.texts.add_lines((ie.file_id, ie.revision), [], lines)
86
 
 
87
 
 
 
76
    
88
77
def apply_inventory_Inventory(self, basis, delta):
89
78
    """Apply delta to basis and return the result.
90
79
    
340
329
 
341
330
 
342
331
class TestDeltaApplication(TestCaseWithTransport):
343
 
 
344
 
    scenarios = delta_application_scenarios()
345
332
 
346
333
    def get_empty_inventory(self, reference_inv=None):
347
334
        """Get an empty inventory.
655
642
 
656
643
    def test_file_has_text(self):
657
644
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
658
 
        self.assertTrue(file.has_text())
 
645
        self.failUnless(file.has_text())
659
646
 
660
647
    def test_directory_has_text(self):
661
648
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
662
 
        self.assertFalse(dir.has_text())
 
649
        self.failIf(dir.has_text())
663
650
 
664
651
    def test_link_has_text(self):
665
652
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
666
 
        self.assertFalse(link.has_text())
 
653
        self.failIf(link.has_text())
667
654
 
668
655
    def test_make_entry(self):
669
656
        self.assertIsInstance(inventory.make_entry("file", "name", ROOT_ID),
1224
1211
        self.assertEqual(('tree\xce\xa9name', 'tree-root-id', 'tree-rev-id'),
1225
1212
                         inv._bytes_to_utf8name_key(bytes))
1226
1213
 
1227
 
    def make_basic_utf8_inventory(self):
1228
 
        inv = Inventory()
1229
 
        inv.revision_id = "revid"
1230
 
        inv.root.revision = "rootrev"
1231
 
        root_id = inv.root.file_id
1232
 
        inv.add(InventoryFile("fileid", u'f\xefle', root_id))
1233
 
        inv["fileid"].revision = "filerev"
1234
 
        inv["fileid"].text_sha1 = "ffff"
1235
 
        inv["fileid"].text_size = 0
1236
 
        inv.add(InventoryDirectory("dirid", u'dir-\N{EURO SIGN}', root_id))
1237
 
        inv.add(InventoryFile("childid", u'ch\xefld', "dirid"))
1238
 
        inv["childid"].revision = "filerev"
1239
 
        inv["childid"].text_sha1 = "ffff"
1240
 
        inv["childid"].text_size = 0
1241
 
        chk_bytes = self.get_chk_bytes()
1242
 
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
1243
 
        bytes = ''.join(chk_inv.to_lines())
1244
 
        return CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
1245
 
 
1246
 
    def test__preload_handles_utf8(self):
1247
 
        new_inv = self.make_basic_utf8_inventory()
1248
 
        self.assertEqual({}, new_inv._fileid_to_entry_cache)
1249
 
        self.assertFalse(new_inv._fully_cached)
1250
 
        new_inv._preload_cache()
1251
 
        self.assertEqual(
1252
 
            sorted([new_inv.root_id, "fileid", "dirid", "childid"]),
1253
 
            sorted(new_inv._fileid_to_entry_cache.keys()))
1254
 
        ie_root = new_inv._fileid_to_entry_cache[new_inv.root_id]
1255
 
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
1256
 
                         sorted(ie_root._children.keys()))
1257
 
        ie_dir = new_inv._fileid_to_entry_cache['dirid']
1258
 
        self.assertEqual([u'ch\xefld'], sorted(ie_dir._children.keys()))
1259
 
 
1260
 
    def test__preload_populates_cache(self):
1261
 
        inv = Inventory()
1262
 
        inv.revision_id = "revid"
1263
 
        inv.root.revision = "rootrev"
1264
 
        root_id = inv.root.file_id
1265
 
        inv.add(InventoryFile("fileid", "file", root_id))
1266
 
        inv["fileid"].revision = "filerev"
1267
 
        inv["fileid"].executable = True
1268
 
        inv["fileid"].text_sha1 = "ffff"
1269
 
        inv["fileid"].text_size = 1
1270
 
        inv.add(InventoryDirectory("dirid", "dir", root_id))
1271
 
        inv.add(InventoryFile("childid", "child", "dirid"))
1272
 
        inv["childid"].revision = "filerev"
1273
 
        inv["childid"].executable = False
1274
 
        inv["childid"].text_sha1 = "dddd"
1275
 
        inv["childid"].text_size = 1
1276
 
        chk_bytes = self.get_chk_bytes()
1277
 
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
1278
 
        bytes = ''.join(chk_inv.to_lines())
1279
 
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
1280
 
        self.assertEqual({}, new_inv._fileid_to_entry_cache)
1281
 
        self.assertFalse(new_inv._fully_cached)
1282
 
        new_inv._preload_cache()
1283
 
        self.assertEqual(
1284
 
            sorted([root_id, "fileid", "dirid", "childid"]),
1285
 
            sorted(new_inv._fileid_to_entry_cache.keys()))
1286
 
        self.assertTrue(new_inv._fully_cached)
1287
 
        ie_root = new_inv._fileid_to_entry_cache[root_id]
1288
 
        self.assertEqual(['dir', 'file'], sorted(ie_root._children.keys()))
1289
 
        ie_dir = new_inv._fileid_to_entry_cache['dirid']
1290
 
        self.assertEqual(['child'], sorted(ie_dir._children.keys()))
1291
 
 
1292
 
    def test__preload_handles_partially_evaluated_inventory(self):
1293
 
        new_inv = self.make_basic_utf8_inventory()
1294
 
        ie = new_inv[new_inv.root_id]
1295
 
        self.assertIs(None, ie._children)
1296
 
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
1297
 
                         sorted(ie.children.keys()))
1298
 
        # Accessing .children loads _children
1299
 
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
1300
 
                         sorted(ie._children.keys()))
1301
 
        new_inv._preload_cache()
1302
 
        # No change
1303
 
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
1304
 
                         sorted(ie._children.keys()))
1305
 
        ie_dir = new_inv["dirid"]
1306
 
        self.assertEqual([u'ch\xefld'],
1307
 
                         sorted(ie_dir._children.keys()))
1308
 
 
1309
1214
 
1310
1215
class TestCHKInventoryExpand(tests.TestCaseWithMemoryTransport):
1311
1216
 
1437
1342
        inv = self.make_simple_inventory()
1438
1343
        self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id', 'top-id',
1439
1344
                           'subsub-file1-id'], inv, ['top-id', 'subsub-file1-id'])
1440
 
 
1441
 
 
1442
 
class TestMutableInventoryFromTree(TestCaseWithTransport):
1443
 
 
1444
 
    def test_empty(self):
1445
 
        repository = self.make_repository('.')
1446
 
        tree = repository.revision_tree(revision.NULL_REVISION)
1447
 
        inv = mutable_inventory_from_tree(tree)
1448
 
        self.assertEquals(revision.NULL_REVISION, inv.revision_id)
1449
 
        self.assertEquals(0, len(inv))
1450
 
 
1451
 
    def test_some_files(self):
1452
 
        wt = self.make_branch_and_tree('.')
1453
 
        self.build_tree(['a'])
1454
 
        wt.add(['a'], ['thefileid'])
1455
 
        revid = wt.commit("commit")
1456
 
        tree = wt.branch.repository.revision_tree(revid)
1457
 
        inv = mutable_inventory_from_tree(tree)
1458
 
        self.assertEquals(revid, inv.revision_id)
1459
 
        self.assertEquals(2, len(inv))
1460
 
        self.assertEquals("a", inv['thefileid'].name)
1461
 
        # The inventory should be mutable and independent of
1462
 
        # the original tree
1463
 
        self.assertFalse(tree.inventory['thefileid'].executable)
1464
 
        inv['thefileid'].executable = True
1465
 
        self.assertFalse(tree.inventory['thefileid'].executable)