/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_repository.py

  • Committer: John Arbash Meinel
  • Date: 2008-07-08 14:55:19 UTC
  • mfrom: (3530 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3532.
  • Revision ID: john@arbash-meinel.com-20080708145519-paqg4kjwbpgs2xmq
Merge bzr.dev 3530

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
from bzrlib.tests import (
40
40
    TestCase,
41
41
    TestCaseWithTransport,
 
42
    TestSkipped,
42
43
    test_knit,
43
44
    )
44
 
from bzrlib.transport import get_transport
 
45
from bzrlib.transport import (
 
46
    fakenfs,
 
47
    get_transport,
 
48
    )
45
49
from bzrlib.transport.memory import MemoryServer
46
50
from bzrlib.util import bencode
47
51
from bzrlib import (
167
171
                          control.transport.get,
168
172
                          'ancestry.weave')
169
173
 
170
 
    def test_exposed_versioned_files_are_marked_dirty(self):
171
 
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
172
 
        repo = weaverepo.RepositoryFormat6().initialize(control)
173
 
        repo.lock_write()
174
 
        inv = repo.get_inventory_weave()
175
 
        repo.unlock()
176
 
        self.assertRaises(errors.OutSideTransaction,
177
 
            inv.add_lines, 'foo', [], [])
178
 
 
179
174
    def test_supports_external_lookups(self):
180
175
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
181
176
        repo = weaverepo.RepositoryFormat6().initialize(control)
205
200
                             'w\n'
206
201
                             'W\n',
207
202
                             t.get('inventory.weave').read())
 
203
        # Creating a file with id Foo:Bar results in a non-escaped file name on
 
204
        # disk.
 
205
        control.create_branch()
 
206
        tree = control.create_workingtree()
 
207
        tree.add(['foo'], ['Foo:Bar'], ['file'])
 
208
        tree.put_file_bytes_non_atomic('Foo:Bar', 'content\n')
 
209
        tree.commit('first post', rev_id='first')
 
210
        self.assertEqualDiff(
 
211
            '# bzr weave file v5\n'
 
212
            'i\n'
 
213
            '1 7fe70820e08a1aac0ef224d9c66ab66831cc4ab1\n'
 
214
            'n first\n'
 
215
            '\n'
 
216
            'w\n'
 
217
            '{ 0\n'
 
218
            '. content\n'
 
219
            '}\n'
 
220
            'W\n',
 
221
            t.get('weaves/74/Foo%3ABar.weave').read())
208
222
 
209
223
    def test_shared_disk_layout(self):
210
224
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
285
299
                             'W\n',
286
300
                             t.get('inventory.weave').read())
287
301
 
288
 
    def test_exposed_versioned_files_are_marked_dirty(self):
289
 
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
290
 
        repo = weaverepo.RepositoryFormat7().initialize(control)
291
 
        repo.lock_write()
292
 
        inv = repo.get_inventory_weave()
293
 
        repo.unlock()
294
 
        self.assertRaises(errors.OutSideTransaction,
295
 
            inv.add_lines, 'foo', [], [])
296
 
 
297
302
    def test_supports_external_lookups(self):
298
303
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
299
304
        repo = weaverepo.RepositoryFormat7().initialize(control)
321
326
        # self.assertEqualDiff('', t.get('lock').read())
322
327
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
323
328
        self.check_knits(t)
 
329
        # Check per-file knits.
 
330
        branch = control.create_branch()
 
331
        tree = control.create_workingtree()
 
332
        tree.add(['foo'], ['Nasty-IdC:'], ['file'])
 
333
        tree.put_file_bytes_non_atomic('Nasty-IdC:', '')
 
334
        tree.commit('1st post', rev_id='foo')
 
335
        self.assertHasKnit(t, 'knits/e8/%254easty-%2549d%2543%253a',
 
336
            '\nfoo fulltext 0 81  :')
324
337
 
325
 
    def assertHasKnit(self, t, knit_name):
 
338
    def assertHasKnit(self, t, knit_name, extra_content=''):
326
339
        """Assert that knit_name exists on t."""
327
 
        self.assertEqualDiff('# bzr knit index 8\n',
 
340
        self.assertEqualDiff('# bzr knit index 8\n' + extra_content,
328
341
                             t.get(knit_name + '.kndx').read())
329
 
        # no default content
330
 
        self.assertTrue(t.has(knit_name + '.knit'))
331
342
 
332
343
    def check_knits(self, t):
333
344
        """check knit content for a repository."""
377
388
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
378
389
        self.check_knits(t)
379
390
 
380
 
    def test_exposed_versioned_files_are_marked_dirty(self):
381
 
        format = bzrdir.BzrDirMetaFormat1()
382
 
        format.repository_format = knitrepo.RepositoryFormatKnit1()
383
 
        repo = self.make_repository('.', format=format)
384
 
        repo.lock_write()
385
 
        inv = repo.get_inventory_weave()
386
 
        repo.unlock()
387
 
        self.assertRaises(errors.OutSideTransaction,
388
 
            inv.add_lines, 'foo', [], [])
389
 
 
390
391
    def test_deserialise_sets_root_revision(self):
391
392
        """We must have a inventory.root.revision
392
393
 
420
421
        self.assertFalse(repo._format.supports_external_lookups)
421
422
 
422
423
 
423
 
class KnitRepositoryStreamTests(test_knit.KnitTests):
424
 
    """Tests for knitrepo._get_stream_as_bytes."""
425
 
 
426
 
    def test_get_stream_as_bytes(self):
427
 
        # Make a simple knit
428
 
        k1 = self.make_test_knit()
429
 
        k1.add_lines('text-a', [], test_knit.split_lines(test_knit.TEXT_1))
430
 
        
431
 
        # Serialise it, check the output.
432
 
        bytes = knitrepo._get_stream_as_bytes(k1, ['text-a'])
433
 
        data = bencode.bdecode(bytes)
434
 
        format, record = data
435
 
        self.assertEqual('knit-plain', format)
436
 
        self.assertEqual(['text-a', ['fulltext'], []], record[:3])
437
 
        self.assertRecordContentEqual(k1, 'text-a', record[3])
438
 
 
439
 
    def test_get_stream_as_bytes_all(self):
440
 
        """Get a serialised data stream for all the records in a knit.
441
 
 
442
 
        Much like test_get_stream_all, except for get_stream_as_bytes.
443
 
        """
444
 
        k1 = self.make_test_knit()
445
 
        # Insert the same data as BasicKnitTests.test_knit_join, as they seem
446
 
        # to cover a range of cases (no parents, one parent, multiple parents).
447
 
        test_data = [
448
 
            ('text-a', [], test_knit.TEXT_1),
449
 
            ('text-b', ['text-a'], test_knit.TEXT_1),
450
 
            ('text-c', [], test_knit.TEXT_1),
451
 
            ('text-d', ['text-c'], test_knit.TEXT_1),
452
 
            ('text-m', ['text-b', 'text-d'], test_knit.TEXT_1),
453
 
           ]
454
 
        # This test is actually a bit strict as the order in which they're
455
 
        # returned is not defined.  This matches the current (deterministic)
456
 
        # behaviour.
457
 
        expected_data_list = [
458
 
            # version, options, parents
459
 
            ('text-a', ['fulltext'], []),
460
 
            ('text-b', ['line-delta'], ['text-a']),
461
 
            ('text-m', ['line-delta'], ['text-b', 'text-d']),
462
 
            ('text-c', ['fulltext'], []),
463
 
            ('text-d', ['line-delta'], ['text-c']),
464
 
            ]
465
 
        for version_id, parents, lines in test_data:
466
 
            k1.add_lines(version_id, parents, test_knit.split_lines(lines))
467
 
 
468
 
        bytes = knitrepo._get_stream_as_bytes(
469
 
            k1, ['text-a', 'text-b', 'text-m', 'text-c', 'text-d', ])
470
 
 
471
 
        data = bencode.bdecode(bytes)
472
 
        format = data.pop(0)
473
 
        self.assertEqual('knit-plain', format)
474
 
 
475
 
        for expected, actual in zip(expected_data_list, data):
476
 
            expected_version = expected[0]
477
 
            expected_options = expected[1]
478
 
            expected_parents = expected[2]
479
 
            version, options, parents, bytes = actual
480
 
            self.assertEqual(expected_version, version)
481
 
            self.assertEqual(expected_options, options)
482
 
            self.assertEqual(expected_parents, parents)
483
 
            self.assertRecordContentEqual(k1, version, bytes)
484
 
 
485
 
 
486
424
class DummyRepository(object):
487
425
    """A dummy repository for testing."""
488
426
 
593
531
                                                        repo_b).__class__)
594
532
 
595
533
 
596
 
class TestInterRemoteToOther(TestCaseWithTransport):
597
 
 
598
 
    def make_remote_repository(self, path, backing_format=None):
599
 
        """Make a RemoteRepository object backed by a real repository that will
600
 
        be created at the given path."""
601
 
        self.make_repository(path, format=backing_format)
602
 
        smart_server = server.SmartTCPServer_for_testing()
603
 
        smart_server.setUp()
604
 
        remote_transport = get_transport(smart_server.get_url()).clone(path)
605
 
        self.addCleanup(smart_server.tearDown)
606
 
        remote_bzrdir = bzrdir.BzrDir.open_from_transport(remote_transport)
607
 
        remote_repo = remote_bzrdir.open_repository()
608
 
        return remote_repo
609
 
 
610
 
    def test_is_compatible_same_format(self):
611
 
        """InterRemoteToOther is compatible with a remote repository and a
612
 
        second repository that have the same format."""
613
 
        local_repo = self.make_repository('local')
614
 
        remote_repo = self.make_remote_repository('remote')
615
 
        is_compatible = repository.InterRemoteToOther.is_compatible
616
 
        self.assertTrue(
617
 
            is_compatible(remote_repo, local_repo),
618
 
            "InterRemoteToOther(%r, %r) is false" % (remote_repo, local_repo))
619
 
          
620
 
    def test_is_incompatible_different_format(self):
621
 
        local_repo = self.make_repository('local', 'dirstate')
622
 
        remote_repo = self.make_remote_repository('a', 'dirstate-with-subtree')
623
 
        is_compatible = repository.InterRemoteToOther.is_compatible
624
 
        self.assertFalse(
625
 
            is_compatible(remote_repo, local_repo),
626
 
            "InterRemoteToOther(%r, %r) is true" % (local_repo, remote_repo))
627
 
 
628
 
    def test_is_incompatible_different_format_both_remote(self):
629
 
        remote_repo_a = self.make_remote_repository(
630
 
            'a', 'dirstate-with-subtree')
631
 
        remote_repo_b = self.make_remote_repository('b', 'dirstate')
632
 
        is_compatible = repository.InterRemoteToOther.is_compatible
633
 
        self.assertFalse(
634
 
            is_compatible(remote_repo_a, remote_repo_b),
635
 
            "InterRemoteToOther(%r, %r) is true"
636
 
            % (remote_repo_a, remote_repo_b))
637
 
 
638
 
 
639
534
class TestRepositoryConverter(TestCaseWithTransport):
640
535
 
641
536
    def test_convert_empty(self):
670
565
        tree = self.make_branch_and_tree('.', format)
671
566
        tree.commit("Dull commit", rev_id="dull")
672
567
        revision_tree = tree.branch.repository.revision_tree('dull')
673
 
        self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
674
 
            revision_tree.inventory.root.file_id)
 
568
        revision_tree.lock_read()
 
569
        try:
 
570
            self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
 
571
                revision_tree.inventory.root.file_id)
 
572
        finally:
 
573
            revision_tree.unlock()
675
574
        format = bzrdir.BzrDirMetaFormat1()
676
575
        format.repository_format = knitrepo.RepositoryFormatKnit3()
677
576
        upgrade.Convert('.', format)
678
577
        tree = workingtree.WorkingTree.open('.')
679
578
        revision_tree = tree.branch.repository.revision_tree('dull')
680
 
        revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
 
579
        revision_tree.lock_read()
 
580
        try:
 
581
            revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
 
582
        finally:
 
583
            revision_tree.unlock()
681
584
        tree.commit("Another dull commit", rev_id='dull2')
682
585
        revision_tree = tree.branch.repository.revision_tree('dull2')
 
586
        revision_tree.lock_read()
 
587
        self.addCleanup(revision_tree.unlock)
683
588
        self.assertEqual('dull', revision_tree.inventory.root.revision)
684
589
 
685
 
    def test_exposed_versioned_files_are_marked_dirty(self):
686
 
        format = bzrdir.BzrDirMetaFormat1()
687
 
        format.repository_format = knitrepo.RepositoryFormatKnit3()
688
 
        repo = self.make_repository('.', format=format)
689
 
        repo.lock_write()
690
 
        inv = repo.get_inventory_weave()
691
 
        repo.unlock()
692
 
        self.assertRaises(errors.OutSideTransaction,
693
 
            inv.add_lines, 'foo', [], [])
694
 
 
695
590
    def test_supports_external_lookups(self):
696
591
        format = bzrdir.BzrDirMetaFormat1()
697
592
        format.repository_format = knitrepo.RepositoryFormatKnit3()
769
664
        entry.revision = revision
770
665
        entry.text_size = 0
771
666
        inv.add(entry)
772
 
        vf = repo.weave_store.get_weave_or_empty(file_id,
773
 
                                                 repo.get_transaction())
774
 
        vf.add_lines(revision, parents, ['line\n'])
 
667
        text_key = (file_id, revision)
 
668
        parent_keys = [(file_id, parent) for parent in parents]
 
669
        repo.texts.add_lines(text_key, parent_keys, ['line\n'])
775
670
 
776
671
    def test_insert_from_broken_repo(self):
777
672
        """Inserting a data stream from a broken repository won't silently
779
674
        """
780
675
        broken_repo = self.make_broken_repository()
781
676
        empty_repo = self.make_repository('empty-repo')
782
 
        search = graph.SearchResult(set(['rev1a', 'rev2', 'rev3']),
783
 
            set(), 3, ['rev1a', 'rev2', 'rev3'])
784
 
        stream = broken_repo.get_data_stream_for_search(search)
785
 
        empty_repo.lock_write()
786
 
        self.addCleanup(empty_repo.unlock)
787
 
        empty_repo.start_write_group()
788
 
        try:
789
 
            self.assertRaises(
790
 
                errors.KnitCorrupt, empty_repo.insert_data_stream, stream)
791
 
        finally:
792
 
            empty_repo.abort_write_group()
 
677
        self.assertRaises(errors.RevisionNotPresent, empty_repo.fetch, broken_repo)
793
678
 
794
679
 
795
680
class TestKnitPackNoSubtrees(TestCaseWithTransport):
814
699
            "Bazaar pack repository format 1 (needs bzr 0.92)\n",
815
700
                             t.get('format').read())
816
701
 
817
 
    def assertHasKndx(self, t, knit_name):
818
 
        """Assert that knit_name exists on t."""
819
 
        self.assertEqualDiff('# bzr knit index 8\n',
820
 
                             t.get(knit_name + '.kndx').read())
821
 
 
822
702
    def assertHasNoKndx(self, t, knit_name):
823
703
        """Assert that knit_name has no index on t."""
824
704
        self.assertFalse(t.has(knit_name + '.kndx'))
944
824
        pack_names = [node[1][0] for node in index.iter_all_entries()]
945
825
        self.assertTrue(large_pack_name in pack_names)
946
826
 
 
827
    def test_fail_obsolete_deletion(self):
 
828
        # failing to delete obsolete packs is not fatal
 
829
        format = self.get_format()
 
830
        server = fakenfs.FakeNFSServer()
 
831
        server.setUp()
 
832
        self.addCleanup(server.tearDown)
 
833
        transport = get_transport(server.get_url())
 
834
        bzrdir = self.get_format().initialize_on_transport(transport)
 
835
        repo = bzrdir.create_repository()
 
836
        repo_transport = bzrdir.get_repository_transport(None)
 
837
        self.assertTrue(repo_transport.has('obsolete_packs'))
 
838
        # these files are in use by another client and typically can't be deleted
 
839
        repo_transport.put_bytes('obsolete_packs/.nfsblahblah', 'contents')
 
840
        repo._pack_collection._clear_obsolete_packs()
 
841
        self.assertTrue(repo_transport.has('obsolete_packs/.nfsblahblah'))
 
842
 
947
843
    def test_pack_after_two_commits_packs_everything(self):
948
844
        format = self.get_format()
949
845
        tree = self.make_branch_and_tree('.', format=format)
988
884
 
989
885
    def _add_text(self, repo, fileid):
990
886
        """Add a text to the repository within a write group."""
991
 
        vf =repo.weave_store.get_weave(fileid, repo.get_transaction())
992
 
        vf.add_lines('samplerev+' + fileid, [], [])
 
887
        repo.texts.add_lines((fileid, 'samplerev+'+fileid), [], [])
993
888
 
994
889
    def test_concurrent_writers_merge_new_packs(self):
995
890
        format = self.get_format()
1166
1061
            inv.root.revision = revision_id
1167
1062
            root_id = inv.root.file_id
1168
1063
            sha1 = repo.add_inventory(revision_id, inv, [])
1169
 
            vf = repo.weave_store.get_weave_or_empty(root_id,
1170
 
                repo.get_transaction())
1171
 
            vf.add_lines(revision_id, [], [])
 
1064
            repo.texts.add_lines((root_id, revision_id), [], [])
1172
1065
            rev = bzrlib.revision.Revision(timestamp=0,
1173
1066
                                           timezone=None,
1174
1067
                                           committer="Foo Bar <foo@example.com>",
1189
1082
        inv = missing_ghost.get_inventory('tip')
1190
1083
        self.assertRaises(errors.NoSuchRevision,
1191
1084
            missing_ghost.get_revision, 'ghost')
1192
 
        self.assertRaises(errors.RevisionNotPresent,
 
1085
        self.assertRaises(errors.NoSuchRevision,
1193
1086
            missing_ghost.get_inventory, 'ghost')
1194
1087
 
1195
1088
    def test_supports_external_lookups(self):