~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

  • Committer: INADA Naoki
  • Date: 2011-05-05 09:15:34 UTC
  • mto: (5830.3.3 i18n-msgfmt)
  • mto: This revision was merged to the branch mainline in revision 5873.
  • Revision ID: songofacandy@gmail.com-20110505091534-7sv835xpofwrmpt4
Add update-pot command to Makefile and tools/bzrgettext script that
extracts help text from bzr commands.

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-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
18
18
from bzrlib import (
19
19
    chk_map,
20
20
    groupcompress,
21
 
    bzrdir,
22
21
    errors,
23
22
    inventory,
24
23
    osutils,
25
24
    repository,
26
25
    revision,
27
26
    tests,
28
 
    )
29
 
from bzrlib.inventory import (CHKInventory, Inventory, ROOT_ID, InventoryFile,
30
 
    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
    )
31
39
from bzrlib.tests import (
32
40
    TestCase,
33
41
    TestCaseWithTransport,
34
 
    condition_isinstance,
35
 
    multiply_tests,
36
 
    split_suite_by_condition,
37
42
    )
38
 
from bzrlib.tests.per_workingtree import workingtree_formats
39
 
 
40
 
 
41
 
def load_tests(standard_tests, module, loader):
42
 
    """Parameterise some inventory tests."""
43
 
    to_adapt, result = split_suite_by_condition(standard_tests,
44
 
        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():
45
50
    scenarios = [
46
51
        ('Inventory', {'apply_delta':apply_inventory_Inventory}),
47
52
        ]
52
57
    # just creating trees.
53
58
    formats = set()
54
59
    for _, format in repository.format_registry.iteritems():
55
 
        scenarios.append((str(format.__name__), {
56
 
            'apply_delta':apply_inventory_Repository_add_inventory_by_delta,
57
 
            'format':format}))
58
 
    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
59
68
        scenarios.append(
60
69
            (str(format.__class__.__name__) + ".update_basis_by_delta", {
61
70
            'apply_delta':apply_inventory_WT_basis,
64
73
            (str(format.__class__.__name__) + ".apply_inventory_delta", {
65
74
            'apply_delta':apply_inventory_WT,
66
75
            'format':format}))
67
 
    return multiply_tests(to_adapt, scenarios, result)
 
76
    return scenarios
68
77
 
69
78
 
70
79
def create_texts_for_inv(repo, inv):
74
83
        else:
75
84
            lines = []
76
85
        repo.texts.add_lines((ie.file_id, ie.revision), [], lines)
77
 
    
 
86
 
 
87
 
78
88
def apply_inventory_Inventory(self, basis, delta):
79
89
    """Apply delta to basis and return the result.
80
90
    
330
340
 
331
341
 
332
342
class TestDeltaApplication(TestCaseWithTransport):
 
343
 
 
344
    scenarios = delta_application_scenarios()
333
345
 
334
346
    def get_empty_inventory(self, reference_inv=None):
335
347
        """Get an empty inventory.
590
602
        self.assertFalse(inv.is_root('TREE_ROOT'))
591
603
        self.assertFalse(inv.is_root('booga'))
592
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
 
593
610
 
594
611
class TestInventoryEntry(TestCase):
595
612
 
607
624
 
608
625
    def test_dir_detect_changes(self):
609
626
        left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
610
 
        left.text_sha1 = 123
611
 
        left.executable = True
612
 
        left.symlink_target='foo'
613
627
        right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
614
 
        right.text_sha1 = 321
615
 
        right.symlink_target='bar'
616
628
        self.assertEqual((False, False), left.detect_changes(right))
617
629
        self.assertEqual((False, False), right.detect_changes(left))
618
630
 
632
644
 
633
645
    def test_symlink_detect_changes(self):
634
646
        left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
635
 
        left.text_sha1 = 123
636
 
        left.executable = True
637
647
        left.symlink_target='foo'
638
648
        right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
639
 
        right.text_sha1 = 321
640
649
        right.symlink_target='foo'
641
650
        self.assertEqual((False, False), left.detect_changes(right))
642
651
        self.assertEqual((False, False), right.detect_changes(left))
646
655
 
647
656
    def test_file_has_text(self):
648
657
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
649
 
        self.failUnless(file.has_text())
 
658
        self.assertTrue(file.has_text())
650
659
 
651
660
    def test_directory_has_text(self):
652
661
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
653
 
        self.failIf(dir.has_text())
 
662
        self.assertFalse(dir.has_text())
654
663
 
655
664
    def test_link_has_text(self):
656
665
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
657
 
        self.failIf(link.has_text())
 
666
        self.assertFalse(link.has_text())
658
667
 
659
668
    def test_make_entry(self):
660
669
        self.assertIsInstance(inventory.make_entry("file", "name", ROOT_ID),
1215
1224
        self.assertEqual(('tree\xce\xa9name', 'tree-root-id', 'tree-rev-id'),
1216
1225
                         inv._bytes_to_utf8name_key(bytes))
1217
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
 
1218
1309
 
1219
1310
class TestCHKInventoryExpand(tests.TestCaseWithMemoryTransport):
1220
1311
 
1346
1437
        inv = self.make_simple_inventory()
1347
1438
        self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id', 'top-id',
1348
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)