~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
 
    )
28
 
from bzrlib.inventory import (CHKInventory, Inventory, ROOT_ID, InventoryFile,
29
 
    InventoryDirectory, InventoryEntry, TreeReference)
 
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
    )
30
39
from bzrlib.tests import (
31
40
    TestCase,
32
41
    TestCaseWithTransport,
33
 
    condition_isinstance,
34
 
    multiply_tests,
35
 
    split_suite_by_condition,
36
42
    )
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))
 
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():
44
50
    scenarios = [
45
51
        ('Inventory', {'apply_delta':apply_inventory_Inventory}),
46
52
        ]
51
57
    # just creating trees.
52
58
    formats = set()
53
59
    for _, format in repository.format_registry.iteritems():
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():
 
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
58
68
        scenarios.append(
59
69
            (str(format.__class__.__name__) + ".update_basis_by_delta", {
60
70
            'apply_delta':apply_inventory_WT_basis,
63
73
            (str(format.__class__.__name__) + ".apply_inventory_delta", {
64
74
            'apply_delta':apply_inventory_WT,
65
75
            'format':format}))
66
 
    return multiply_tests(to_adapt, scenarios, result)
 
76
    return scenarios
67
77
 
68
78
 
69
79
def create_texts_for_inv(repo, inv):
73
83
        else:
74
84
            lines = []
75
85
        repo.texts.add_lines((ie.file_id, ie.revision), [], lines)
76
 
    
 
86
 
 
87
 
77
88
def apply_inventory_Inventory(self, basis, delta):
78
89
    """Apply delta to basis and return the result.
79
90
    
329
340
 
330
341
 
331
342
class TestDeltaApplication(TestCaseWithTransport):
 
343
 
 
344
    scenarios = delta_application_scenarios()
332
345
 
333
346
    def get_empty_inventory(self, reference_inv=None):
334
347
        """Get an empty inventory.
589
602
        self.assertFalse(inv.is_root('TREE_ROOT'))
590
603
        self.assertFalse(inv.is_root('booga'))
591
604
 
 
605
    def test_entries_for_empty_inventory(self):
 
606
        """Test that entries() will not fail for an empty inventory"""
 
607
        inv = Inventory(root_id=None)
 
608
        self.assertEqual([], inv.entries())
 
609
 
592
610
 
593
611
class TestInventoryEntry(TestCase):
594
612
 
637
655
 
638
656
    def test_file_has_text(self):
639
657
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
640
 
        self.failUnless(file.has_text())
 
658
        self.assertTrue(file.has_text())
641
659
 
642
660
    def test_directory_has_text(self):
643
661
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
644
 
        self.failIf(dir.has_text())
 
662
        self.assertFalse(dir.has_text())
645
663
 
646
664
    def test_link_has_text(self):
647
665
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
648
 
        self.failIf(link.has_text())
 
666
        self.assertFalse(link.has_text())
649
667
 
650
668
    def test_make_entry(self):
651
669
        self.assertIsInstance(inventory.make_entry("file", "name", ROOT_ID),
1206
1224
        self.assertEqual(('tree\xce\xa9name', 'tree-root-id', 'tree-rev-id'),
1207
1225
                         inv._bytes_to_utf8name_key(bytes))
1208
1226
 
 
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
 
1209
1309
 
1210
1310
class TestCHKInventoryExpand(tests.TestCaseWithMemoryTransport):
1211
1311
 
1337
1437
        inv = self.make_simple_inventory()
1338
1438
        self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id', 'top-id',
1339
1439
                           '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)