~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_repository.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 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
25
25
from stat import S_ISDIR
26
26
from StringIO import StringIO
27
27
 
 
28
from bzrlib import symbol_versioning
28
29
import bzrlib
29
30
import bzrlib.bzrdir as bzrdir
30
31
import bzrlib.errors as errors
33
34
                           UnknownFormatError,
34
35
                           UnsupportedFormatError,
35
36
                           )
36
 
import bzrlib.repository as repository
 
37
from bzrlib.repository import RepositoryFormat
37
38
from bzrlib.tests import TestCase, TestCaseWithTransport
38
39
from bzrlib.transport import get_transport
39
 
from bzrlib.transport.http import HttpServer
40
40
from bzrlib.transport.memory import MemoryServer
41
 
from bzrlib import upgrade, workingtree
 
41
from bzrlib import (
 
42
    repository,
 
43
    upgrade,
 
44
    workingtree,
 
45
    )
 
46
from bzrlib.repofmt import knitrepo, weaverepo
42
47
 
43
48
 
44
49
class TestDefaultFormat(TestCase):
45
50
 
46
51
    def test_get_set_default_format(self):
47
 
        private_default = repository._default_format.__class__
 
52
        old_default = bzrdir.format_registry.get('default')
 
53
        private_default = old_default().repository_format.__class__
48
54
        old_format = repository.RepositoryFormat.get_default_format()
49
55
        self.assertTrue(isinstance(old_format, private_default))
50
 
        repository.RepositoryFormat.set_default_format(SampleRepositoryFormat())
 
56
        def make_sample_bzrdir():
 
57
            my_bzrdir = bzrdir.BzrDirMetaFormat1()
 
58
            my_bzrdir.repository_format = SampleRepositoryFormat()
 
59
            return my_bzrdir
 
60
        bzrdir.format_registry.remove('default')
 
61
        bzrdir.format_registry.register('sample', make_sample_bzrdir, '')
 
62
        bzrdir.format_registry.set_default('sample')
51
63
        # creating a repository should now create an instrumented dir.
52
64
        try:
53
65
            # the default branch format is used by the meta dir format
56
68
            result = dir.create_repository()
57
69
            self.assertEqual(result, 'A bzr repository dir')
58
70
        finally:
59
 
            repository.RepositoryFormat.set_default_format(old_format)
60
 
        self.assertEqual(old_format, repository.RepositoryFormat.get_default_format())
 
71
            bzrdir.format_registry.remove('default')
 
72
            bzrdir.format_registry.remove('sample')
 
73
            bzrdir.format_registry.register('default', old_default, '')
 
74
        self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
 
75
                              old_format.__class__)
61
76
 
62
77
 
63
78
class SampleRepositoryFormat(repository.RepositoryFormat):
98
113
            t = get_transport(url)
99
114
            found_format = repository.RepositoryFormat.find_format(dir)
100
115
            self.failUnless(isinstance(found_format, format.__class__))
101
 
        check_format(repository.RepositoryFormat7(), "bar")
 
116
        check_format(weaverepo.RepositoryFormat7(), "bar")
102
117
        
103
118
    def test_find_format_no_repository(self):
104
119
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
133
148
 
134
149
    def test_no_ancestry_weave(self):
135
150
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
136
 
        repo = repository.RepositoryFormat6().initialize(control)
 
151
        repo = weaverepo.RepositoryFormat6().initialize(control)
137
152
        # We no longer need to create the ancestry.weave file
138
153
        # since it is *never* used.
139
154
        self.assertRaises(NoSuchFile,
145
160
    
146
161
    def test_disk_layout(self):
147
162
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
148
 
        repo = repository.RepositoryFormat7().initialize(control)
 
163
        repo = weaverepo.RepositoryFormat7().initialize(control)
149
164
        # in case of side effects of locking.
150
165
        repo.lock_write()
151
166
        repo.unlock()
167
182
 
168
183
    def test_shared_disk_layout(self):
169
184
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
170
 
        repo = repository.RepositoryFormat7().initialize(control, shared=True)
 
185
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
171
186
        # we want:
172
187
        # format 'Bazaar-NG Repository format 7'
173
188
        # inventory.weave == empty_weave
190
205
    def test_creates_lockdir(self):
191
206
        """Make sure it appears to be controlled by a LockDir existence"""
192
207
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
193
 
        repo = repository.RepositoryFormat7().initialize(control, shared=True)
 
208
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
194
209
        t = control.get_repository_transport(None)
195
210
        # TODO: Should check there is a 'lock' toplevel directory, 
196
211
        # regardless of contents
206
221
        """repo format 7 actually locks on lockdir"""
207
222
        base_url = self.get_url()
208
223
        control = bzrdir.BzrDirMetaFormat1().initialize(base_url)
209
 
        repo = repository.RepositoryFormat7().initialize(control, shared=True)
 
224
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
210
225
        t = control.get_repository_transport(None)
211
226
        repo.lock_write()
212
227
        repo.unlock()
220
235
 
221
236
    def test_shared_no_tree_disk_layout(self):
222
237
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
223
 
        repo = repository.RepositoryFormat7().initialize(control, shared=True)
 
238
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
224
239
        repo.set_make_working_trees(False)
225
240
        # we want:
226
241
        # format 'Bazaar-NG Repository format 7'
249
264
    
250
265
    def test_disk_layout(self):
251
266
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
252
 
        repo = repository.RepositoryFormatKnit1().initialize(control)
 
267
        repo = knitrepo.RepositoryFormatKnit1().initialize(control)
253
268
        # in case of side effects of locking.
254
269
        repo.lock_write()
255
270
        repo.unlock()
282
297
 
283
298
    def test_shared_disk_layout(self):
284
299
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
285
 
        repo = repository.RepositoryFormatKnit1().initialize(control, shared=True)
 
300
        repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
286
301
        # we want:
287
302
        # format 'Bazaar-NG Knit Repository Format 1'
288
303
        # lock: is a directory
301
316
 
302
317
    def test_shared_no_tree_disk_layout(self):
303
318
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
304
 
        repo = repository.RepositoryFormatKnit1().initialize(control, shared=True)
 
319
        repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
305
320
        repo.set_make_working_trees(False)
306
321
        # we want:
307
322
        # format 'Bazaar-NG Knit Repository Format 1'
323
338
        self.check_knits(t)
324
339
 
325
340
 
326
 
class InterString(repository.InterRepository):
327
 
    """An inter-repository optimised code path for strings.
328
 
 
329
 
    This is for use during testing where we use strings as repositories
 
341
class DummyRepository(object):
 
342
    """A dummy repository for testing."""
 
343
 
 
344
    _serializer = None
 
345
 
 
346
    def supports_rich_root(self):
 
347
        return False
 
348
 
 
349
 
 
350
class InterDummy(repository.InterRepository):
 
351
    """An inter-repository optimised code path for DummyRepository.
 
352
 
 
353
    This is for use during testing where we use DummyRepository as repositories
330
354
    so that none of the default regsitered inter-repository classes will
331
355
    match.
332
356
    """
333
357
 
334
358
    @staticmethod
335
359
    def is_compatible(repo_source, repo_target):
336
 
        """InterString is compatible with strings-as-repos."""
337
 
        return isinstance(repo_source, str) and isinstance(repo_target, str)
 
360
        """InterDummy is compatible with DummyRepository."""
 
361
        return (isinstance(repo_source, DummyRepository) and 
 
362
            isinstance(repo_target, DummyRepository))
338
363
 
339
364
 
340
365
class TestInterRepository(TestCaseWithTransport):
346
371
        # This also tests that the default registered optimised interrepository
347
372
        # classes do not barf inappropriately when a surprising repository type
348
373
        # is handed to them.
349
 
        dummy_a = "Repository 1."
350
 
        dummy_b = "Repository 2."
 
374
        dummy_a = DummyRepository()
 
375
        dummy_b = DummyRepository()
351
376
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
352
377
 
353
378
    def assertGetsDefaultInterRepository(self, repo_a, repo_b):
354
 
        """Asserts that InterRepository.get(repo_a, repo_b) -> the default."""
 
379
        """Asserts that InterRepository.get(repo_a, repo_b) -> the default.
 
380
        
 
381
        The effective default is now InterSameDataRepository because there is
 
382
        no actual sane default in the presence of incompatible data models.
 
383
        """
355
384
        inter_repo = repository.InterRepository.get(repo_a, repo_b)
356
 
        self.assertEqual(repository.InterRepository,
 
385
        self.assertEqual(repository.InterSameDataRepository,
357
386
                         inter_repo.__class__)
358
387
        self.assertEqual(repo_a, inter_repo.source)
359
388
        self.assertEqual(repo_b, inter_repo.target)
364
393
        # and that it is correctly selected when given a repository
365
394
        # pair that it returns true on for the is_compatible static method
366
395
        # check
367
 
        dummy_a = "Repository 1."
368
 
        dummy_b = "Repository 2."
369
 
        repository.InterRepository.register_optimiser(InterString)
 
396
        dummy_a = DummyRepository()
 
397
        dummy_b = DummyRepository()
 
398
        repo = self.make_repository('.')
 
399
        # hack dummies to look like repo somewhat.
 
400
        dummy_a._serializer = repo._serializer
 
401
        dummy_b._serializer = repo._serializer
 
402
        repository.InterRepository.register_optimiser(InterDummy)
370
403
        try:
371
 
            # we should get the default for something InterString returns False
 
404
            # we should get the default for something InterDummy returns False
372
405
            # to
373
 
            self.assertFalse(InterString.is_compatible(dummy_a, None))
374
 
            self.assertGetsDefaultInterRepository(dummy_a, None)
375
 
            # and we should get an InterString for a pair it 'likes'
376
 
            self.assertTrue(InterString.is_compatible(dummy_a, dummy_b))
 
406
            self.assertFalse(InterDummy.is_compatible(dummy_a, repo))
 
407
            self.assertGetsDefaultInterRepository(dummy_a, repo)
 
408
            # and we should get an InterDummy for a pair it 'likes'
 
409
            self.assertTrue(InterDummy.is_compatible(dummy_a, dummy_b))
377
410
            inter_repo = repository.InterRepository.get(dummy_a, dummy_b)
378
 
            self.assertEqual(InterString, inter_repo.__class__)
 
411
            self.assertEqual(InterDummy, inter_repo.__class__)
379
412
            self.assertEqual(dummy_a, inter_repo.source)
380
413
            self.assertEqual(dummy_b, inter_repo.target)
381
414
        finally:
382
 
            repository.InterRepository.unregister_optimiser(InterString)
 
415
            repository.InterRepository.unregister_optimiser(InterDummy)
383
416
        # now we should get the default InterRepository object again.
384
417
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
385
418
 
389
422
    def test_is_compatible_and_registered(self):
390
423
        # InterWeaveRepo is compatible when either side
391
424
        # is a format 5/6/7 branch
392
 
        formats = [repository.RepositoryFormat5(),
393
 
                   repository.RepositoryFormat6(),
394
 
                   repository.RepositoryFormat7()]
395
 
        incompatible_formats = [repository.RepositoryFormat4(),
396
 
                                repository.RepositoryFormatKnit1(),
 
425
        from bzrlib.repofmt import knitrepo, weaverepo
 
426
        formats = [weaverepo.RepositoryFormat5(),
 
427
                   weaverepo.RepositoryFormat6(),
 
428
                   weaverepo.RepositoryFormat7()]
 
429
        incompatible_formats = [weaverepo.RepositoryFormat4(),
 
430
                                knitrepo.RepositoryFormatKnit1(),
397
431
                                ]
398
432
        repo_a = self.make_repository('a')
399
433
        repo_b = self.make_repository('b')
420
454
        t = get_transport(self.get_url('.'))
421
455
        t.mkdir('repository')
422
456
        repo_dir = bzrdir.BzrDirMetaFormat1().initialize('repository')
423
 
        repo = repository.RepositoryFormat7().initialize(repo_dir)
424
 
        target_format = repository.RepositoryFormatKnit1()
 
457
        repo = weaverepo.RepositoryFormat7().initialize(repo_dir)
 
458
        target_format = knitrepo.RepositoryFormatKnit1()
425
459
        converter = repository.CopyConverter(target_format)
426
460
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
427
461
        try:
439
473
        self.assertRaises(KeyError, repository._unescape_xml, 'foo&bar;') 
440
474
 
441
475
 
442
 
class TestRepositoryFormatKnit2(TestCaseWithTransport):
 
476
class TestRepositoryFormatKnit3(TestCaseWithTransport):
443
477
 
444
478
    def test_convert(self):
445
479
        """Ensure the upgrade adds weaves for roots"""
446
480
        format = bzrdir.BzrDirMetaFormat1()
447
 
        format.repository_format = repository.RepositoryFormatKnit1()
 
481
        format.repository_format = knitrepo.RepositoryFormatKnit1()
448
482
        tree = self.make_branch_and_tree('.', format)
449
483
        tree.commit("Dull commit", rev_id="dull")
450
484
        revision_tree = tree.branch.repository.revision_tree('dull')
451
485
        self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
452
486
            revision_tree.inventory.root.file_id)
453
487
        format = bzrdir.BzrDirMetaFormat1()
454
 
        format.repository_format = repository.RepositoryFormatKnit2()
 
488
        format.repository_format = knitrepo.RepositoryFormatKnit3()
455
489
        upgrade.Convert('.', format)
456
490
        tree = workingtree.WorkingTree.open('.')
457
491
        revision_tree = tree.branch.repository.revision_tree('dull')
459
493
        tree.commit("Another dull commit", rev_id='dull2')
460
494
        revision_tree = tree.branch.repository.revision_tree('dull2')
461
495
        self.assertEqual('dull', revision_tree.inventory.root.revision)
 
496