~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_repository.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 19:18:39 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216191839-eg681lxqibi1qxu1
Fix remaining tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-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
23
23
"""
24
24
 
25
25
from stat import S_ISDIR
26
 
import sys
27
26
 
28
27
import bzrlib
29
 
from bzrlib.errors import (NoSuchFile,
30
 
                           UnknownFormatError,
31
 
                           UnsupportedFormatError,
32
 
                           )
 
28
from bzrlib.errors import (
 
29
    UnknownFormatError,
 
30
    UnsupportedFormatError,
 
31
    )
33
32
from bzrlib import (
34
33
    btree_index,
35
 
    graph,
 
34
    symbol_versioning,
36
35
    tests,
 
36
    transport,
 
37
    vf_search,
37
38
    )
38
39
from bzrlib.btree_index import BTreeBuilder, BTreeGraphIndex
39
40
from bzrlib.index import GraphIndex
42
43
    TestCase,
43
44
    TestCaseWithTransport,
44
45
    )
45
 
from bzrlib.transport import (
46
 
    get_transport,
47
 
    )
48
46
from bzrlib import (
49
47
    bzrdir,
50
48
    errors,
54
52
    revision as _mod_revision,
55
53
    upgrade,
56
54
    versionedfile,
 
55
    vf_repository,
57
56
    workingtree,
58
57
    )
59
58
from bzrlib.repofmt import (
60
59
    groupcompress_repo,
61
60
    knitrepo,
 
61
    knitpack_repo,
62
62
    pack_repo,
63
 
    weaverepo,
64
63
    )
65
64
 
66
65
 
69
68
    def test_get_set_default_format(self):
70
69
        old_default = bzrdir.format_registry.get('default')
71
70
        private_default = old_default().repository_format.__class__
72
 
        old_format = repository.RepositoryFormat.get_default_format()
 
71
        old_format = repository.format_registry.get_default()
73
72
        self.assertTrue(isinstance(old_format, private_default))
74
73
        def make_sample_bzrdir():
75
74
            my_bzrdir = bzrdir.BzrDirMetaFormat1()
89
88
            bzrdir.format_registry.remove('default')
90
89
            bzrdir.format_registry.remove('sample')
91
90
            bzrdir.format_registry.register('default', old_default, '')
92
 
        self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
 
91
        self.assertIsInstance(repository.format_registry.get_default(),
93
92
                              old_format.__class__)
94
93
 
95
94
 
96
 
class SampleRepositoryFormat(repository.RepositoryFormat):
 
95
class SampleRepositoryFormat(repository.RepositoryFormatMetaDir):
97
96
    """A sample format
98
97
 
99
98
    this format is initializable, unsupported to aid in testing the
100
99
    open and open(unsupported=True) routines.
101
100
    """
102
101
 
103
 
    def get_format_string(self):
 
102
    @classmethod
 
103
    def get_format_string(cls):
104
104
        """See RepositoryFormat.get_format_string()."""
105
105
        return "Sample .bzr repository format."
106
106
 
117
117
        return "opened repository."
118
118
 
119
119
 
 
120
class SampleExtraRepositoryFormat(repository.RepositoryFormat):
 
121
    """A sample format that can not be used in a metadir
 
122
 
 
123
    """
 
124
 
 
125
    def get_format_string(self):
 
126
        raise NotImplementedError
 
127
 
 
128
 
120
129
class TestRepositoryFormat(TestCaseWithTransport):
121
130
    """Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
122
131
 
128
137
        def check_format(format, url):
129
138
            dir = format._matchingbzrdir.initialize(url)
130
139
            format.initialize(dir)
131
 
            t = get_transport(url)
132
 
            found_format = repository.RepositoryFormat.find_format(dir)
133
 
            self.failUnless(isinstance(found_format, format.__class__))
134
 
        check_format(weaverepo.RepositoryFormat7(), "bar")
 
140
            t = transport.get_transport_from_path(url)
 
141
            found_format = repository.RepositoryFormatMetaDir.find_format(dir)
 
142
            self.assertIsInstance(found_format, format.__class__)
 
143
        check_format(repository.format_registry.get_default(), "bar")
135
144
 
136
145
    def test_find_format_no_repository(self):
137
146
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
138
147
        self.assertRaises(errors.NoRepositoryPresent,
139
 
                          repository.RepositoryFormat.find_format,
 
148
                          repository.RepositoryFormatMetaDir.find_format,
140
149
                          dir)
141
150
 
 
151
    def test_from_string(self):
 
152
        self.assertIsInstance(
 
153
            SampleRepositoryFormat.from_string(
 
154
                "Sample .bzr repository format."),
 
155
            SampleRepositoryFormat)
 
156
        self.assertRaises(ValueError,
 
157
            SampleRepositoryFormat.from_string,
 
158
                "Different .bzr repository format.")
 
159
 
142
160
    def test_find_format_unknown_format(self):
143
161
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
144
162
        SampleRepositoryFormat().initialize(dir)
145
163
        self.assertRaises(UnknownFormatError,
146
 
                          repository.RepositoryFormat.find_format,
 
164
                          repository.RepositoryFormatMetaDir.find_format,
147
165
                          dir)
148
166
 
149
167
    def test_register_unregister_format(self):
 
168
        # Test deprecated format registration functions
150
169
        format = SampleRepositoryFormat()
151
170
        # make a control dir
152
171
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
153
172
        # make a repo
154
173
        format.initialize(dir)
155
174
        # register a format for it.
156
 
        repository.RepositoryFormat.register_format(format)
 
175
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
176
            repository.RepositoryFormat.register_format, format)
157
177
        # which repository.Open will refuse (not supported)
158
 
        self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
 
178
        self.assertRaises(UnsupportedFormatError, repository.Repository.open,
 
179
            self.get_url())
159
180
        # but open(unsupported) will work
160
181
        self.assertEqual(format.open(dir), "opened repository.")
161
182
        # unregister the format
162
 
        repository.RepositoryFormat.unregister_format(format)
163
 
 
164
 
 
165
 
class TestFormat6(TestCaseWithTransport):
166
 
 
167
 
    def test_attribute__fetch_order(self):
168
 
        """Weaves need topological data insertion."""
169
 
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
170
 
        repo = weaverepo.RepositoryFormat6().initialize(control)
171
 
        self.assertEqual('topological', repo._format._fetch_order)
172
 
 
173
 
    def test_attribute__fetch_uses_deltas(self):
174
 
        """Weaves do not reuse deltas."""
175
 
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
176
 
        repo = weaverepo.RepositoryFormat6().initialize(control)
177
 
        self.assertEqual(False, repo._format._fetch_uses_deltas)
178
 
 
179
 
    def test_attribute__fetch_reconcile(self):
180
 
        """Weave repositories need a reconcile after fetch."""
181
 
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
182
 
        repo = weaverepo.RepositoryFormat6().initialize(control)
183
 
        self.assertEqual(True, repo._format._fetch_reconcile)
184
 
 
185
 
    def test_no_ancestry_weave(self):
186
 
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
187
 
        repo = weaverepo.RepositoryFormat6().initialize(control)
188
 
        # We no longer need to create the ancestry.weave file
189
 
        # since it is *never* used.
190
 
        self.assertRaises(NoSuchFile,
191
 
                          control.transport.get,
192
 
                          'ancestry.weave')
193
 
 
194
 
    def test_supports_external_lookups(self):
195
 
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
196
 
        repo = weaverepo.RepositoryFormat6().initialize(control)
197
 
        self.assertFalse(repo._format.supports_external_lookups)
198
 
 
199
 
 
200
 
class TestFormat7(TestCaseWithTransport):
201
 
 
202
 
    def test_attribute__fetch_order(self):
203
 
        """Weaves need topological data insertion."""
204
 
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
205
 
        repo = weaverepo.RepositoryFormat7().initialize(control)
206
 
        self.assertEqual('topological', repo._format._fetch_order)
207
 
 
208
 
    def test_attribute__fetch_uses_deltas(self):
209
 
        """Weaves do not reuse deltas."""
210
 
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
211
 
        repo = weaverepo.RepositoryFormat7().initialize(control)
212
 
        self.assertEqual(False, repo._format._fetch_uses_deltas)
213
 
 
214
 
    def test_attribute__fetch_reconcile(self):
215
 
        """Weave repositories need a reconcile after fetch."""
216
 
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
217
 
        repo = weaverepo.RepositoryFormat7().initialize(control)
218
 
        self.assertEqual(True, repo._format._fetch_reconcile)
219
 
 
220
 
    def test_disk_layout(self):
221
 
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
222
 
        repo = weaverepo.RepositoryFormat7().initialize(control)
223
 
        # in case of side effects of locking.
224
 
        repo.lock_write()
225
 
        repo.unlock()
226
 
        # we want:
227
 
        # format 'Bazaar-NG Repository format 7'
228
 
        # lock ''
229
 
        # inventory.weave == empty_weave
230
 
        # empty revision-store directory
231
 
        # empty weaves directory
232
 
        t = control.get_repository_transport(None)
233
 
        self.assertEqualDiff('Bazaar-NG Repository format 7',
234
 
                             t.get('format').read())
235
 
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
236
 
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
237
 
        self.assertEqualDiff('# bzr weave file v5\n'
238
 
                             'w\n'
239
 
                             'W\n',
240
 
                             t.get('inventory.weave').read())
241
 
        # Creating a file with id Foo:Bar results in a non-escaped file name on
242
 
        # disk.
243
 
        control.create_branch()
244
 
        tree = control.create_workingtree()
245
 
        tree.add(['foo'], ['Foo:Bar'], ['file'])
246
 
        tree.put_file_bytes_non_atomic('Foo:Bar', 'content\n')
247
 
        try:
248
 
            tree.commit('first post', rev_id='first')
249
 
        except errors.IllegalPath:
250
 
            if sys.platform != 'win32':
251
 
                raise
252
 
            self.knownFailure('Foo:Bar cannot be used as a file-id on windows'
253
 
                              ' in repo format 7')
254
 
            return
255
 
        self.assertEqualDiff(
256
 
            '# bzr weave file v5\n'
257
 
            'i\n'
258
 
            '1 7fe70820e08a1aac0ef224d9c66ab66831cc4ab1\n'
259
 
            'n first\n'
260
 
            '\n'
261
 
            'w\n'
262
 
            '{ 0\n'
263
 
            '. content\n'
264
 
            '}\n'
265
 
            'W\n',
266
 
            t.get('weaves/74/Foo%3ABar.weave').read())
267
 
 
268
 
    def test_shared_disk_layout(self):
269
 
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
270
 
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
271
 
        # we want:
272
 
        # format 'Bazaar-NG Repository format 7'
273
 
        # inventory.weave == empty_weave
274
 
        # empty revision-store directory
275
 
        # empty weaves directory
276
 
        # a 'shared-storage' marker file.
277
 
        # lock is not present when unlocked
278
 
        t = control.get_repository_transport(None)
279
 
        self.assertEqualDiff('Bazaar-NG Repository format 7',
280
 
                             t.get('format').read())
281
 
        self.assertEqualDiff('', t.get('shared-storage').read())
282
 
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
283
 
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
284
 
        self.assertEqualDiff('# bzr weave file v5\n'
285
 
                             'w\n'
286
 
                             'W\n',
287
 
                             t.get('inventory.weave').read())
288
 
        self.assertFalse(t.has('branch-lock'))
289
 
 
290
 
    def test_creates_lockdir(self):
291
 
        """Make sure it appears to be controlled by a LockDir existence"""
292
 
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
293
 
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
294
 
        t = control.get_repository_transport(None)
295
 
        # TODO: Should check there is a 'lock' toplevel directory,
296
 
        # regardless of contents
297
 
        self.assertFalse(t.has('lock/held/info'))
298
 
        repo.lock_write()
299
 
        try:
300
 
            self.assertTrue(t.has('lock/held/info'))
301
 
        finally:
302
 
            # unlock so we don't get a warning about failing to do so
303
 
            repo.unlock()
304
 
 
305
 
    def test_uses_lockdir(self):
306
 
        """repo format 7 actually locks on lockdir"""
307
 
        base_url = self.get_url()
308
 
        control = bzrdir.BzrDirMetaFormat1().initialize(base_url)
309
 
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
310
 
        t = control.get_repository_transport(None)
311
 
        repo.lock_write()
312
 
        repo.unlock()
313
 
        del repo
314
 
        # make sure the same lock is created by opening it
315
 
        repo = repository.Repository.open(base_url)
316
 
        repo.lock_write()
317
 
        self.assertTrue(t.has('lock/held/info'))
318
 
        repo.unlock()
319
 
        self.assertFalse(t.has('lock/held/info'))
320
 
 
321
 
    def test_shared_no_tree_disk_layout(self):
322
 
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
323
 
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
324
 
        repo.set_make_working_trees(False)
325
 
        # we want:
326
 
        # format 'Bazaar-NG Repository format 7'
327
 
        # lock ''
328
 
        # inventory.weave == empty_weave
329
 
        # empty revision-store directory
330
 
        # empty weaves directory
331
 
        # a 'shared-storage' marker file.
332
 
        t = control.get_repository_transport(None)
333
 
        self.assertEqualDiff('Bazaar-NG Repository format 7',
334
 
                             t.get('format').read())
335
 
        ## self.assertEqualDiff('', t.get('lock').read())
336
 
        self.assertEqualDiff('', t.get('shared-storage').read())
337
 
        self.assertEqualDiff('', t.get('no-working-trees').read())
338
 
        repo.set_make_working_trees(True)
339
 
        self.assertFalse(t.has('no-working-trees'))
340
 
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
341
 
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
342
 
        self.assertEqualDiff('# bzr weave file v5\n'
343
 
                             'w\n'
344
 
                             'W\n',
345
 
                             t.get('inventory.weave').read())
346
 
 
347
 
    def test_supports_external_lookups(self):
348
 
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
349
 
        repo = weaverepo.RepositoryFormat7().initialize(control)
350
 
        self.assertFalse(repo._format.supports_external_lookups)
 
183
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
184
            repository.RepositoryFormat.unregister_format, format)
 
185
 
 
186
 
 
187
class TestRepositoryFormatRegistry(TestCase):
 
188
 
 
189
    def setUp(self):
 
190
        super(TestRepositoryFormatRegistry, self).setUp()
 
191
        self.registry = repository.RepositoryFormatRegistry()
 
192
 
 
193
    def test_register_unregister_format(self):
 
194
        format = SampleRepositoryFormat()
 
195
        self.registry.register(format)
 
196
        self.assertEquals(format, self.registry.get("Sample .bzr repository format."))
 
197
        self.registry.remove(format)
 
198
        self.assertRaises(KeyError, self.registry.get, "Sample .bzr repository format.")
 
199
 
 
200
    def test_get_all(self):
 
201
        format = SampleRepositoryFormat()
 
202
        self.assertEquals([], self.registry._get_all())
 
203
        self.registry.register(format)
 
204
        self.assertEquals([format], self.registry._get_all())
 
205
 
 
206
    def test_register_extra(self):
 
207
        format = SampleExtraRepositoryFormat()
 
208
        self.assertEquals([], self.registry._get_all())
 
209
        self.registry.register_extra(format)
 
210
        self.assertEquals([format], self.registry._get_all())
 
211
 
 
212
    def test_register_extra_lazy(self):
 
213
        self.assertEquals([], self.registry._get_all())
 
214
        self.registry.register_extra_lazy("bzrlib.tests.test_repository",
 
215
            "SampleExtraRepositoryFormat")
 
216
        formats = self.registry._get_all()
 
217
        self.assertEquals(1, len(formats))
 
218
        self.assertIsInstance(formats[0], SampleExtraRepositoryFormat)
351
219
 
352
220
 
353
221
class TestFormatKnit1(TestCaseWithTransport):
521
389
        # classes do not barf inappropriately when a surprising repository type
522
390
        # is handed to them.
523
391
        dummy_a = DummyRepository()
 
392
        dummy_a._format = RepositoryFormat()
 
393
        dummy_a._format.supports_full_versioned_files = True
524
394
        dummy_b = DummyRepository()
 
395
        dummy_b._format = RepositoryFormat()
 
396
        dummy_b._format.supports_full_versioned_files = True
525
397
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
526
398
 
527
399
    def assertGetsDefaultInterRepository(self, repo_a, repo_b):
531
403
        no actual sane default in the presence of incompatible data models.
532
404
        """
533
405
        inter_repo = repository.InterRepository.get(repo_a, repo_b)
534
 
        self.assertEqual(repository.InterSameDataRepository,
 
406
        self.assertEqual(vf_repository.InterSameDataRepository,
535
407
                         inter_repo.__class__)
536
408
        self.assertEqual(repo_a, inter_repo.source)
537
409
        self.assertEqual(repo_b, inter_repo.target)
551
423
        dummy_a._serializer = repo._serializer
552
424
        dummy_a._format.supports_tree_reference = repo._format.supports_tree_reference
553
425
        dummy_a._format.rich_root_data = repo._format.rich_root_data
 
426
        dummy_a._format.supports_full_versioned_files = repo._format.supports_full_versioned_files
554
427
        dummy_b._serializer = repo._serializer
555
428
        dummy_b._format.supports_tree_reference = repo._format.supports_tree_reference
556
429
        dummy_b._format.rich_root_data = repo._format.rich_root_data
 
430
        dummy_b._format.supports_full_versioned_files = repo._format.supports_full_versioned_files
557
431
        repository.InterRepository.register_optimiser(InterDummy)
558
432
        try:
559
433
            # we should get the default for something InterDummy returns False
572
446
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
573
447
 
574
448
 
575
 
class TestInterWeaveRepo(TestCaseWithTransport):
576
 
 
577
 
    def test_is_compatible_and_registered(self):
578
 
        # InterWeaveRepo is compatible when either side
579
 
        # is a format 5/6/7 branch
580
 
        from bzrlib.repofmt import knitrepo, weaverepo
581
 
        formats = [weaverepo.RepositoryFormat5(),
582
 
                   weaverepo.RepositoryFormat6(),
583
 
                   weaverepo.RepositoryFormat7()]
584
 
        incompatible_formats = [weaverepo.RepositoryFormat4(),
585
 
                                knitrepo.RepositoryFormatKnit1(),
586
 
                                ]
587
 
        repo_a = self.make_repository('a')
588
 
        repo_b = self.make_repository('b')
589
 
        is_compatible = repository.InterWeaveRepo.is_compatible
590
 
        for source in incompatible_formats:
591
 
            # force incompatible left then right
592
 
            repo_a._format = source
593
 
            repo_b._format = formats[0]
594
 
            self.assertFalse(is_compatible(repo_a, repo_b))
595
 
            self.assertFalse(is_compatible(repo_b, repo_a))
596
 
        for source in formats:
597
 
            repo_a._format = source
598
 
            for target in formats:
599
 
                repo_b._format = target
600
 
                self.assertTrue(is_compatible(repo_a, repo_b))
601
 
        self.assertEqual(repository.InterWeaveRepo,
602
 
                         repository.InterRepository.get(repo_a,
603
 
                                                        repo_b).__class__)
 
449
class TestRepositoryFormat1(knitrepo.RepositoryFormatKnit1):
 
450
 
 
451
    @classmethod
 
452
    def get_format_string(cls):
 
453
        return "Test Format 1"
 
454
 
 
455
 
 
456
class TestRepositoryFormat2(knitrepo.RepositoryFormatKnit1):
 
457
 
 
458
    @classmethod
 
459
    def get_format_string(cls):
 
460
        return "Test Format 2"
604
461
 
605
462
 
606
463
class TestRepositoryConverter(TestCaseWithTransport):
607
464
 
608
465
    def test_convert_empty(self):
609
 
        t = get_transport(self.get_url('.'))
 
466
        source_format = TestRepositoryFormat1()
 
467
        target_format = TestRepositoryFormat2()
 
468
        repository.format_registry.register(source_format)
 
469
        self.addCleanup(repository.format_registry.remove,
 
470
            source_format)
 
471
        repository.format_registry.register(target_format)
 
472
        self.addCleanup(repository.format_registry.remove,
 
473
            target_format)
 
474
        t = self.get_transport()
610
475
        t.mkdir('repository')
611
476
        repo_dir = bzrdir.BzrDirMetaFormat1().initialize('repository')
612
 
        repo = weaverepo.RepositoryFormat7().initialize(repo_dir)
613
 
        target_format = knitrepo.RepositoryFormatKnit1()
 
477
        repo = TestRepositoryFormat1().initialize(repo_dir)
614
478
        converter = repository.CopyConverter(target_format)
615
479
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
616
480
        try:
621
485
        self.assertTrue(isinstance(target_format, repo._format.__class__))
622
486
 
623
487
 
624
 
class TestMisc(TestCase):
625
 
 
626
 
    def test_unescape_xml(self):
627
 
        """We get some kind of error when malformed entities are passed"""
628
 
        self.assertRaises(KeyError, repository._unescape_xml, 'foo&bar;')
629
 
 
630
 
 
631
488
class TestRepositoryFormatKnit3(TestCaseWithTransport):
632
489
 
633
490
    def test_attribute__fetch_order(self):
828
685
        target = self.make_repository('target', format='rich-root-pack')
829
686
        stream = source._get_source(target._format)
830
687
        # We don't want the child GroupCHKStreamSource
831
 
        self.assertIs(type(stream), repository.StreamSource)
 
688
        self.assertIs(type(stream), vf_repository.StreamSource)
832
689
 
833
690
    def test_get_stream_for_missing_keys_includes_all_chk_refs(self):
834
691
        source_builder = self.make_branch_builder('source',
861
718
 
862
719
        # On a regular pass, getting the inventories and chk pages for rev-2
863
720
        # would only get the newly created chk pages
864
 
        search = graph.SearchResult(set(['rev-2']), set(['rev-1']), 1,
 
721
        search = vf_search.SearchResult(set(['rev-2']), set(['rev-1']), 1,
865
722
                                    set(['rev-2']))
866
723
        simple_chk_records = []
867
724
        for vf_name, substream in source.get_stream(search):
910
767
        source = self.make_repository('source', format='pack-0.92')
911
768
        target = self.make_repository('target', format='pack-0.92')
912
769
        stream_source = source._get_source(target._format)
913
 
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
770
        self.assertIsInstance(stream_source, knitpack_repo.KnitPackStreamSource)
914
771
 
915
772
    def test_source_to_exact_pack_rich_root_pack(self):
916
773
        source = self.make_repository('source', format='rich-root-pack')
917
774
        target = self.make_repository('target', format='rich-root-pack')
918
775
        stream_source = source._get_source(target._format)
919
 
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
776
        self.assertIsInstance(stream_source, knitpack_repo.KnitPackStreamSource)
920
777
 
921
778
    def test_source_to_exact_pack_19(self):
922
779
        source = self.make_repository('source', format='1.9')
923
780
        target = self.make_repository('target', format='1.9')
924
781
        stream_source = source._get_source(target._format)
925
 
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
782
        self.assertIsInstance(stream_source, knitpack_repo.KnitPackStreamSource)
926
783
 
927
784
    def test_source_to_exact_pack_19_rich_root(self):
928
785
        source = self.make_repository('source', format='1.9-rich-root')
929
786
        target = self.make_repository('target', format='1.9-rich-root')
930
787
        stream_source = source._get_source(target._format)
931
 
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
788
        self.assertIsInstance(stream_source, knitpack_repo.KnitPackStreamSource)
932
789
 
933
790
    def test_source_to_remote_exact_pack_19(self):
934
791
        trans = self.make_smart_server('target')
937
794
        target = self.make_repository('target', format='1.9')
938
795
        target = repository.Repository.open(trans.base)
939
796
        stream_source = source._get_source(target._format)
940
 
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
797
        self.assertIsInstance(stream_source, knitpack_repo.KnitPackStreamSource)
941
798
 
942
799
    def test_stream_source_to_non_exact(self):
943
800
        source = self.make_repository('source', format='pack-0.92')
944
801
        target = self.make_repository('target', format='1.9')
945
802
        stream = source._get_source(target._format)
946
 
        self.assertIs(type(stream), repository.StreamSource)
 
803
        self.assertIs(type(stream), vf_repository.StreamSource)
947
804
 
948
805
    def test_stream_source_to_non_exact_rich_root(self):
949
806
        source = self.make_repository('source', format='1.9')
950
807
        target = self.make_repository('target', format='1.9-rich-root')
951
808
        stream = source._get_source(target._format)
952
 
        self.assertIs(type(stream), repository.StreamSource)
 
809
        self.assertIs(type(stream), vf_repository.StreamSource)
953
810
 
954
811
    def test_source_to_remote_non_exact_pack_19(self):
955
812
        trans = self.make_smart_server('target')
958
815
        target = self.make_repository('target', format='1.6')
959
816
        target = repository.Repository.open(trans.base)
960
817
        stream_source = source._get_source(target._format)
961
 
        self.assertIs(type(stream_source), repository.StreamSource)
 
818
        self.assertIs(type(stream_source), vf_repository.StreamSource)
962
819
 
963
820
    def test_stream_source_to_knit(self):
964
821
        source = self.make_repository('source', format='pack-0.92')
965
822
        target = self.make_repository('target', format='dirstate')
966
823
        stream = source._get_source(target._format)
967
 
        self.assertIs(type(stream), repository.StreamSource)
 
824
        self.assertIs(type(stream), vf_repository.StreamSource)
968
825
 
969
826
 
970
827
class TestDevelopment6FindParentIdsOfRevisions(TestCaseWithTransport):
972
829
 
973
830
    def setUp(self):
974
831
        super(TestDevelopment6FindParentIdsOfRevisions, self).setUp()
975
 
        self.builder = self.make_branch_builder('source',
976
 
            format='development6-rich-root')
 
832
        self.builder = self.make_branch_builder('source')
977
833
        self.builder.start_series()
978
834
        self.builder.build_snapshot('initial', None,
979
835
            [('add', ('', 'tree-root', 'directory', None))])
1049
905
            revision = _mod_revision.Revision('rev1a',
1050
906
                committer='jrandom@example.com', timestamp=0,
1051
907
                inventory_sha1='', timezone=0, message='foo', parent_ids=[])
1052
 
            repo.add_revision('rev1a',revision, inv)
 
908
            repo.add_revision('rev1a', revision, inv)
1053
909
 
1054
910
            # make rev1b, which has no Revision, but has an Inventory, and
1055
911
            # file1
1090
946
        revision = _mod_revision.Revision(revision_id,
1091
947
            committer='jrandom@example.com', timestamp=0, inventory_sha1='',
1092
948
            timezone=0, message='foo', parent_ids=parent_ids)
1093
 
        repo.add_revision(revision_id,revision, inv)
 
949
        repo.add_revision(revision_id, revision, inv)
1094
950
 
1095
951
    def add_file(self, repo, inv, filename, revision, parents):
1096
952
        file_id = filename + '-id'
1226
1082
            sorted(set([osutils.splitext(n)[0] for n in
1227
1083
                        packs._index_transport.list_dir('.')])))
1228
1084
 
 
1085
    def test__obsolete_packs_missing_directory(self):
 
1086
        tree, r, packs, revs = self.make_packs_and_alt_repo(write_lock=True)
 
1087
        r.control_transport.rmdir('obsolete_packs')
 
1088
        names = packs.names()
 
1089
        pack = packs.get_pack_by_name(names[0])
 
1090
        # Schedule this one for removal
 
1091
        packs._remove_pack_from_memory(pack)
 
1092
        # Now trigger the obsoletion, and ensure that all the remaining files
 
1093
        # are still renamed
 
1094
        packs._obsolete_packs([pack])
 
1095
        self.assertEqual([n + '.pack' for n in names[1:]],
 
1096
                         sorted(packs._pack_transport.list_dir('.')))
 
1097
        # names[0] should not be present in the index anymore
 
1098
        self.assertEqual(names[1:],
 
1099
            sorted(set([osutils.splitext(n)[0] for n in
 
1100
                        packs._index_transport.list_dir('.')])))
 
1101
 
1229
1102
    def test_pack_distribution_zero(self):
1230
1103
        packs = self.get_packs()
1231
1104
        self.assertEqual([0], packs.pack_distribution(0))
1501
1374
        obsolete_names = set([osutils.splitext(n)[0] for n in obsolete_packs])
1502
1375
        self.assertEqual([pack.name], sorted(obsolete_names))
1503
1376
 
 
1377
    def test_pack_no_obsolete_packs_directory(self):
 
1378
        """Bug #314314, don't fail if obsolete_packs directory does
 
1379
        not exist."""
 
1380
        tree, r, packs, revs = self.make_packs_and_alt_repo(write_lock=True)
 
1381
        r.control_transport.rmdir('obsolete_packs')
 
1382
        packs._clear_obsolete_packs()
1504
1383
 
1505
1384
 
1506
1385
class TestPack(TestCaseWithTransport):
1607
1486
        # Because of how they were built, they correspond to
1608
1487
        # ['D', 'C', 'B', 'A']
1609
1488
        packs = b.repository._pack_collection.packs
1610
 
        packer = pack_repo.Packer(b.repository._pack_collection,
 
1489
        packer = knitpack_repo.KnitPacker(b.repository._pack_collection,
1611
1490
                                  packs, 'testing',
1612
1491
                                  revision_ids=['B', 'C'])
1613
1492
        # Now, when we are copying the B & C revisions, their pack files should
1627
1506
        return repo._pack_collection
1628
1507
 
1629
1508
    def test_open_pack_will_optimise(self):
1630
 
        packer = pack_repo.OptimisingPacker(self.get_pack_collection(),
 
1509
        packer = knitpack_repo.OptimisingKnitPacker(self.get_pack_collection(),
1631
1510
                                            [], '.test')
1632
1511
        new_pack = packer.open_pack()
1633
1512
        self.addCleanup(new_pack.abort) # ensure cleanup
1638
1517
        self.assertTrue(new_pack.signature_index._optimize_for_size)
1639
1518
 
1640
1519
 
 
1520
class TestGCCHKPacker(TestCaseWithTransport):
 
1521
 
 
1522
    def make_abc_branch(self):
 
1523
        builder = self.make_branch_builder('source')
 
1524
        builder.start_series()
 
1525
        builder.build_snapshot('A', None, [
 
1526
            ('add', ('', 'root-id', 'directory', None)),
 
1527
            ('add', ('file', 'file-id', 'file', 'content\n')),
 
1528
            ])
 
1529
        builder.build_snapshot('B', ['A'], [
 
1530
            ('add', ('dir', 'dir-id', 'directory', None))])
 
1531
        builder.build_snapshot('C', ['B'], [
 
1532
            ('modify', ('file-id', 'new content\n'))])
 
1533
        builder.finish_series()
 
1534
        return builder.get_branch()
 
1535
 
 
1536
    def make_branch_with_disjoint_inventory_and_revision(self):
 
1537
        """a repo with separate packs for a revisions Revision and Inventory.
 
1538
 
 
1539
        There will be one pack file that holds the Revision content, and one
 
1540
        for the Inventory content.
 
1541
 
 
1542
        :return: (repository,
 
1543
                  pack_name_with_rev_A_Revision,
 
1544
                  pack_name_with_rev_A_Inventory,
 
1545
                  pack_name_with_rev_C_content)
 
1546
        """
 
1547
        b_source = self.make_abc_branch()
 
1548
        b_base = b_source.bzrdir.sprout('base', revision_id='A').open_branch()
 
1549
        b_stacked = b_base.bzrdir.sprout('stacked', stacked=True).open_branch()
 
1550
        b_stacked.lock_write()
 
1551
        self.addCleanup(b_stacked.unlock)
 
1552
        b_stacked.fetch(b_source, 'B')
 
1553
        # Now re-open the stacked repo directly (no fallbacks) so that we can
 
1554
        # fill in the A rev.
 
1555
        repo_not_stacked = b_stacked.bzrdir.open_repository()
 
1556
        repo_not_stacked.lock_write()
 
1557
        self.addCleanup(repo_not_stacked.unlock)
 
1558
        # Now we should have a pack file with A's inventory, but not its
 
1559
        # Revision
 
1560
        self.assertEqual([('A',), ('B',)],
 
1561
                         sorted(repo_not_stacked.inventories.keys()))
 
1562
        self.assertEqual([('B',)],
 
1563
                         sorted(repo_not_stacked.revisions.keys()))
 
1564
        stacked_pack_names = repo_not_stacked._pack_collection.names()
 
1565
        # We have a couple names here, figure out which has A's inventory
 
1566
        for name in stacked_pack_names:
 
1567
            pack = repo_not_stacked._pack_collection.get_pack_by_name(name)
 
1568
            keys = [n[1] for n in pack.inventory_index.iter_all_entries()]
 
1569
            if ('A',) in keys:
 
1570
                inv_a_pack_name = name
 
1571
                break
 
1572
        else:
 
1573
            self.fail('Could not find pack containing A\'s inventory')
 
1574
        repo_not_stacked.fetch(b_source.repository, 'A')
 
1575
        self.assertEqual([('A',), ('B',)],
 
1576
                         sorted(repo_not_stacked.revisions.keys()))
 
1577
        new_pack_names = set(repo_not_stacked._pack_collection.names())
 
1578
        rev_a_pack_names = new_pack_names.difference(stacked_pack_names)
 
1579
        self.assertEqual(1, len(rev_a_pack_names))
 
1580
        rev_a_pack_name = list(rev_a_pack_names)[0]
 
1581
        # Now fetch 'C', so we have a couple pack files to join
 
1582
        repo_not_stacked.fetch(b_source.repository, 'C')
 
1583
        rev_c_pack_names = set(repo_not_stacked._pack_collection.names())
 
1584
        rev_c_pack_names = rev_c_pack_names.difference(new_pack_names)
 
1585
        self.assertEqual(1, len(rev_c_pack_names))
 
1586
        rev_c_pack_name = list(rev_c_pack_names)[0]
 
1587
        return (repo_not_stacked, rev_a_pack_name, inv_a_pack_name,
 
1588
                rev_c_pack_name)
 
1589
 
 
1590
    def test_pack_with_distant_inventories(self):
 
1591
        # See https://bugs.launchpad.net/bzr/+bug/437003
 
1592
        # When repacking, it is possible to have an inventory in a different
 
1593
        # pack file than the associated revision. An autopack can then come
 
1594
        # along, and miss that inventory, and complain.
 
1595
        (repo, rev_a_pack_name, inv_a_pack_name, rev_c_pack_name
 
1596
         ) = self.make_branch_with_disjoint_inventory_and_revision()
 
1597
        a_pack = repo._pack_collection.get_pack_by_name(rev_a_pack_name)
 
1598
        c_pack = repo._pack_collection.get_pack_by_name(rev_c_pack_name)
 
1599
        packer = groupcompress_repo.GCCHKPacker(repo._pack_collection,
 
1600
                    [a_pack, c_pack], '.test-pack')
 
1601
        # This would raise ValueError in bug #437003, but should not raise an
 
1602
        # error once fixed.
 
1603
        packer.pack()
 
1604
 
 
1605
    def test_pack_with_missing_inventory(self):
 
1606
        # Similar to test_pack_with_missing_inventory, but this time, we force
 
1607
        # the A inventory to actually be gone from the repository.
 
1608
        (repo, rev_a_pack_name, inv_a_pack_name, rev_c_pack_name
 
1609
         ) = self.make_branch_with_disjoint_inventory_and_revision()
 
1610
        inv_a_pack = repo._pack_collection.get_pack_by_name(inv_a_pack_name)
 
1611
        repo._pack_collection._remove_pack_from_memory(inv_a_pack)
 
1612
        packer = groupcompress_repo.GCCHKPacker(repo._pack_collection,
 
1613
            repo._pack_collection.all_packs(), '.test-pack')
 
1614
        e = self.assertRaises(ValueError, packer.pack)
 
1615
        packer.new_pack.abort()
 
1616
        self.assertContainsRe(str(e),
 
1617
            r"We are missing inventories for revisions: .*'A'")
 
1618
 
 
1619
 
1641
1620
class TestCrossFormatPacks(TestCaseWithTransport):
1642
1621
 
1643
1622
    def log_pack(self, hint=None):
1658
1637
        self.addCleanup(target.unlock)
1659
1638
        source = source_tree.branch.repository._get_source(target._format)
1660
1639
        self.orig_pack = target.pack
1661
 
        target.pack = self.log_pack
 
1640
        self.overrideAttr(target, "pack", self.log_pack)
1662
1641
        search = target.search_missing_revision_ids(
1663
 
            source_tree.branch.repository, tip)
 
1642
            source_tree.branch.repository, revision_ids=[tip])
1664
1643
        stream = source.get_stream(search)
1665
1644
        from_format = source_tree.branch.repository._format
1666
1645
        sink = target._get_sink()
1682
1661
        self.addCleanup(target.unlock)
1683
1662
        source = source_tree.branch.repository
1684
1663
        self.orig_pack = target.pack
1685
 
        target.pack = self.log_pack
 
1664
        self.overrideAttr(target, "pack", self.log_pack)
1686
1665
        target.fetch(source)
1687
1666
        if expect_pack_called:
1688
1667
            self.assertLength(1, self.calls)
1716
1695
    def test_IDS_format_same_no(self):
1717
1696
        # When the formats are the same, pack is not called.
1718
1697
        self.run_fetch('2a', '2a', False)
 
1698
 
 
1699
 
 
1700
class Test_LazyListJoin(tests.TestCase):
 
1701
 
 
1702
    def test__repr__(self):
 
1703
        lazy = repository._LazyListJoin(['a'], ['b'])
 
1704
        self.assertEqual("bzrlib.repository._LazyListJoin((['a'], ['b']))",
 
1705
                         repr(lazy))