~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_repository.py

  • Committer: Patch Queue Manager
  • Date: 2016-01-31 13:36:59 UTC
  • mfrom: (6613.1.5 1538480-match-hostname)
  • Revision ID: pqm@pqm.ubuntu.com-20160131133659-ouy92ee2wlv9xz8m
(vila) Use ssl.match_hostname instead of our own. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    )
32
32
from bzrlib import (
33
33
    btree_index,
34
 
    graph,
35
34
    symbol_versioning,
36
35
    tests,
37
36
    transport,
 
37
    vf_search,
38
38
    )
39
39
from bzrlib.btree_index import BTreeBuilder, BTreeGraphIndex
40
40
from bzrlib.index import GraphIndex
45
45
    )
46
46
from bzrlib import (
47
47
    bzrdir,
 
48
    controldir,
48
49
    errors,
49
50
    inventory,
50
51
    osutils,
66
67
class TestDefaultFormat(TestCase):
67
68
 
68
69
    def test_get_set_default_format(self):
69
 
        old_default = bzrdir.format_registry.get('default')
 
70
        old_default = controldir.format_registry.get('default')
70
71
        private_default = old_default().repository_format.__class__
71
72
        old_format = repository.format_registry.get_default()
72
73
        self.assertTrue(isinstance(old_format, private_default))
74
75
            my_bzrdir = bzrdir.BzrDirMetaFormat1()
75
76
            my_bzrdir.repository_format = SampleRepositoryFormat()
76
77
            return my_bzrdir
77
 
        bzrdir.format_registry.remove('default')
78
 
        bzrdir.format_registry.register('sample', make_sample_bzrdir, '')
79
 
        bzrdir.format_registry.set_default('sample')
 
78
        controldir.format_registry.remove('default')
 
79
        controldir.format_registry.register('sample', make_sample_bzrdir, '')
 
80
        controldir.format_registry.set_default('sample')
80
81
        # creating a repository should now create an instrumented dir.
81
82
        try:
82
83
            # the default branch format is used by the meta dir format
85
86
            result = dir.create_repository()
86
87
            self.assertEqual(result, 'A bzr repository dir')
87
88
        finally:
88
 
            bzrdir.format_registry.remove('default')
89
 
            bzrdir.format_registry.remove('sample')
90
 
            bzrdir.format_registry.register('default', old_default, '')
 
89
            controldir.format_registry.remove('default')
 
90
            controldir.format_registry.remove('sample')
 
91
            controldir.format_registry.register('default', old_default, '')
91
92
        self.assertIsInstance(repository.format_registry.get_default(),
92
93
                              old_format.__class__)
93
94
 
94
95
 
95
 
class SampleRepositoryFormat(repository.RepositoryFormat):
 
96
class SampleRepositoryFormat(repository.RepositoryFormatMetaDir):
96
97
    """A sample format
97
98
 
98
99
    this format is initializable, unsupported to aid in testing the
99
100
    open and open(unsupported=True) routines.
100
101
    """
101
102
 
102
 
    def get_format_string(self):
 
103
    @classmethod
 
104
    def get_format_string(cls):
103
105
        """See RepositoryFormat.get_format_string()."""
104
106
        return "Sample .bzr repository format."
105
107
 
137
139
            dir = format._matchingbzrdir.initialize(url)
138
140
            format.initialize(dir)
139
141
            t = transport.get_transport_from_path(url)
140
 
            found_format = repository.RepositoryFormat.find_format(dir)
 
142
            found_format = repository.RepositoryFormatMetaDir.find_format(dir)
141
143
            self.assertIsInstance(found_format, format.__class__)
142
144
        check_format(repository.format_registry.get_default(), "bar")
143
145
 
144
146
    def test_find_format_no_repository(self):
145
147
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
146
148
        self.assertRaises(errors.NoRepositoryPresent,
147
 
                          repository.RepositoryFormat.find_format,
 
149
                          repository.RepositoryFormatMetaDir.find_format,
148
150
                          dir)
149
151
 
 
152
    def test_from_string(self):
 
153
        self.assertIsInstance(
 
154
            SampleRepositoryFormat.from_string(
 
155
                "Sample .bzr repository format."),
 
156
            SampleRepositoryFormat)
 
157
        self.assertRaises(AssertionError,
 
158
            SampleRepositoryFormat.from_string,
 
159
                "Different .bzr repository format.")
 
160
 
150
161
    def test_find_format_unknown_format(self):
151
162
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
152
163
        SampleRepositoryFormat().initialize(dir)
153
164
        self.assertRaises(UnknownFormatError,
154
 
                          repository.RepositoryFormat.find_format,
 
165
                          repository.RepositoryFormatMetaDir.find_format,
155
166
                          dir)
156
167
 
157
 
    def test_register_unregister_format(self):
158
 
        # Test deprecated format registration functions
159
 
        format = SampleRepositoryFormat()
160
 
        # make a control dir
161
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
162
 
        # make a repo
163
 
        format.initialize(dir)
164
 
        # register a format for it.
165
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
166
 
            repository.RepositoryFormat.register_format, format)
167
 
        # which repository.Open will refuse (not supported)
168
 
        self.assertRaises(UnsupportedFormatError, repository.Repository.open,
169
 
            self.get_url())
170
 
        # but open(unsupported) will work
171
 
        self.assertEqual(format.open(dir), "opened repository.")
172
 
        # unregister the format
173
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
174
 
            repository.RepositoryFormat.unregister_format, format)
 
168
    def test_find_format_with_features(self):
 
169
        tree = self.make_branch_and_tree('.', format='2a')
 
170
        tree.branch.repository.update_feature_flags({"name": "necessity"})
 
171
        found_format = repository.RepositoryFormatMetaDir.find_format(tree.bzrdir)
 
172
        self.assertIsInstance(found_format, repository.RepositoryFormatMetaDir)
 
173
        self.assertEquals(found_format.features.get("name"), "necessity")
 
174
        self.assertRaises(errors.MissingFeature, found_format.check_support_status,
 
175
            True)
 
176
        self.addCleanup(repository.RepositoryFormatMetaDir.unregister_feature,
 
177
            "name")
 
178
        repository.RepositoryFormatMetaDir.register_feature("name")
 
179
        found_format.check_support_status(True)
175
180
 
176
181
 
177
182
class TestRepositoryFormatRegistry(TestCase):
213
218
    def test_attribute__fetch_order(self):
214
219
        """Knits need topological data insertion."""
215
220
        repo = self.make_repository('.',
216
 
                format=bzrdir.format_registry.get('knit')())
 
221
                format=controldir.format_registry.get('knit')())
217
222
        self.assertEqual('topological', repo._format._fetch_order)
218
223
 
219
224
    def test_attribute__fetch_uses_deltas(self):
220
225
        """Knits reuse deltas."""
221
226
        repo = self.make_repository('.',
222
 
                format=bzrdir.format_registry.get('knit')())
 
227
                format=controldir.format_registry.get('knit')())
223
228
        self.assertEqual(True, repo._format._fetch_uses_deltas)
224
229
 
225
230
    def test_disk_layout(self):
311
316
        is valid when the api is not being abused.
312
317
        """
313
318
        repo = self.make_repository('.',
314
 
                format=bzrdir.format_registry.get('knit')())
 
319
                format=controldir.format_registry.get('knit')())
315
320
        inv_xml = '<inventory format="5">\n</inventory>\n'
316
321
        inv = repo._deserialise_inventory('test-rev-id', inv_xml)
317
322
        self.assertEqual('test-rev-id', inv.root.revision)
319
324
    def test_deserialise_uses_global_revision_id(self):
320
325
        """If it is set, then we re-use the global revision id"""
321
326
        repo = self.make_repository('.',
322
 
                format=bzrdir.format_registry.get('knit')())
 
327
                format=controldir.format_registry.get('knit')())
323
328
        inv_xml = ('<inventory format="5" revision_id="other-rev-id">\n'
324
329
                   '</inventory>\n')
325
330
        # Arguably, the deserialise_inventory should detect a mismatch, and
332
337
 
333
338
    def test_supports_external_lookups(self):
334
339
        repo = self.make_repository('.',
335
 
                format=bzrdir.format_registry.get('knit')())
 
340
                format=controldir.format_registry.get('knit')())
336
341
        self.assertFalse(repo._format.supports_external_lookups)
337
342
 
338
343
 
438
443
 
439
444
class TestRepositoryFormat1(knitrepo.RepositoryFormatKnit1):
440
445
 
441
 
    def get_format_string(self):
 
446
    @classmethod
 
447
    def get_format_string(cls):
442
448
        return "Test Format 1"
443
449
 
444
450
 
445
451
class TestRepositoryFormat2(knitrepo.RepositoryFormatKnit1):
446
452
 
447
 
    def get_format_string(self):
 
453
    @classmethod
 
454
    def get_format_string(cls):
448
455
        return "Test Format 2"
449
456
 
450
457
 
499
506
        revision_tree.lock_read()
500
507
        try:
501
508
            self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
502
 
                revision_tree.inventory.root.file_id)
 
509
                revision_tree.get_root_id())
503
510
        finally:
504
511
            revision_tree.unlock()
505
512
        format = bzrdir.BzrDirMetaFormat1()
509
516
        revision_tree = tree.branch.repository.revision_tree('dull')
510
517
        revision_tree.lock_read()
511
518
        try:
512
 
            revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
 
519
            revision_tree.get_file_lines(revision_tree.get_root_id())
513
520
        finally:
514
521
            revision_tree.unlock()
515
522
        tree.commit("Another dull commit", rev_id='dull2')
516
523
        revision_tree = tree.branch.repository.revision_tree('dull2')
517
524
        revision_tree.lock_read()
518
525
        self.addCleanup(revision_tree.unlock)
519
 
        self.assertEqual('dull', revision_tree.inventory.root.revision)
 
526
        self.assertEqual('dull',
 
527
                revision_tree.get_file_revision(revision_tree.get_root_id()))
520
528
 
521
529
    def test_supports_external_lookups(self):
522
530
        format = bzrdir.BzrDirMetaFormat1()
706
714
 
707
715
        # On a regular pass, getting the inventories and chk pages for rev-2
708
716
        # would only get the newly created chk pages
709
 
        search = graph.SearchResult(set(['rev-2']), set(['rev-1']), 1,
 
717
        search = vf_search.SearchResult(set(['rev-2']), set(['rev-1']), 1,
710
718
                                    set(['rev-2']))
711
719
        simple_chk_records = []
712
720
        for vf_name, substream in source.get_stream(search):
893
901
            revision = _mod_revision.Revision('rev1a',
894
902
                committer='jrandom@example.com', timestamp=0,
895
903
                inventory_sha1='', timezone=0, message='foo', parent_ids=[])
896
 
            repo.add_revision('rev1a',revision, inv)
 
904
            repo.add_revision('rev1a', revision, inv)
897
905
 
898
906
            # make rev1b, which has no Revision, but has an Inventory, and
899
907
            # file1
934
942
        revision = _mod_revision.Revision(revision_id,
935
943
            committer='jrandom@example.com', timestamp=0, inventory_sha1='',
936
944
            timezone=0, message='foo', parent_ids=parent_ids)
937
 
        repo.add_revision(revision_id,revision, inv)
 
945
        repo.add_revision(revision_id, revision, inv)
938
946
 
939
947
    def add_file(self, repo, inv, filename, revision, parents):
940
948
        file_id = filename + '-id'
968
976
class TestRepositoryPackCollection(TestCaseWithTransport):
969
977
 
970
978
    def get_format(self):
971
 
        return bzrdir.format_registry.make_bzrdir('pack-0.92')
 
979
        return controldir.format_registry.make_bzrdir('pack-0.92')
972
980
 
973
981
    def get_packs(self):
974
982
        format = self.get_format()
1070
1078
            sorted(set([osutils.splitext(n)[0] for n in
1071
1079
                        packs._index_transport.list_dir('.')])))
1072
1080
 
 
1081
    def test__obsolete_packs_missing_directory(self):
 
1082
        tree, r, packs, revs = self.make_packs_and_alt_repo(write_lock=True)
 
1083
        r.control_transport.rmdir('obsolete_packs')
 
1084
        names = packs.names()
 
1085
        pack = packs.get_pack_by_name(names[0])
 
1086
        # Schedule this one for removal
 
1087
        packs._remove_pack_from_memory(pack)
 
1088
        # Now trigger the obsoletion, and ensure that all the remaining files
 
1089
        # are still renamed
 
1090
        packs._obsolete_packs([pack])
 
1091
        self.assertEqual([n + '.pack' for n in names[1:]],
 
1092
                         sorted(packs._pack_transport.list_dir('.')))
 
1093
        # names[0] should not be present in the index anymore
 
1094
        self.assertEqual(names[1:],
 
1095
            sorted(set([osutils.splitext(n)[0] for n in
 
1096
                        packs._index_transport.list_dir('.')])))
 
1097
 
1073
1098
    def test_pack_distribution_zero(self):
1074
1099
        packs = self.get_packs()
1075
1100
        self.assertEqual([0], packs.pack_distribution(0))
1345
1370
        obsolete_names = set([osutils.splitext(n)[0] for n in obsolete_packs])
1346
1371
        self.assertEqual([pack.name], sorted(obsolete_names))
1347
1372
 
 
1373
    def test_pack_no_obsolete_packs_directory(self):
 
1374
        """Bug #314314, don't fail if obsolete_packs directory does
 
1375
        not exist."""
 
1376
        tree, r, packs, revs = self.make_packs_and_alt_repo(write_lock=True)
 
1377
        r.control_transport.rmdir('obsolete_packs')
 
1378
        packs._clear_obsolete_packs()
1348
1379
 
1349
1380
 
1350
1381
class TestPack(TestCaseWithTransport):
1668
1699
        lazy = repository._LazyListJoin(['a'], ['b'])
1669
1700
        self.assertEqual("bzrlib.repository._LazyListJoin((['a'], ['b']))",
1670
1701
                         repr(lazy))
 
1702
 
 
1703
 
 
1704
class TestFeatures(tests.TestCaseWithTransport):
 
1705
 
 
1706
    def test_open_with_present_feature(self):
 
1707
        self.addCleanup(
 
1708
            repository.RepositoryFormatMetaDir.unregister_feature,
 
1709
            "makes-cheese-sandwich")
 
1710
        repository.RepositoryFormatMetaDir.register_feature(
 
1711
            "makes-cheese-sandwich")
 
1712
        repo = self.make_repository('.')
 
1713
        repo.lock_write()
 
1714
        repo._format.features["makes-cheese-sandwich"] = "required"
 
1715
        repo._format.check_support_status(False)
 
1716
        repo.unlock()
 
1717
 
 
1718
    def test_open_with_missing_required_feature(self):
 
1719
        repo = self.make_repository('.')
 
1720
        repo.lock_write()
 
1721
        repo._format.features["makes-cheese-sandwich"] = "required"
 
1722
        self.assertRaises(errors.MissingFeature,
 
1723
            repo._format.check_support_status, False)