~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bundle.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-07-19 16:09:34 UTC
  • mfrom: (2520.4.135 bzr.mpbundle)
  • Revision ID: pqm@pqm.ubuntu.com-20070719160934-d51fyijw69oto88p
Add new bundle and merge-directive formats

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from bzrlib.bzrdir import BzrDir
32
32
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
33
33
from bzrlib.bundle.bundle_data import BundleTree
34
 
from bzrlib.bundle.serializer import write_bundle, read_bundle
 
34
from bzrlib.bundle.serializer import write_bundle, read_bundle, v09, v4
35
35
from bzrlib.bundle.serializer.v08 import BundleSerializerV08
36
36
from bzrlib.bundle.serializer.v09 import BundleSerializerV09
 
37
from bzrlib.bundle.serializer.v4 import BundleSerializerV4
37
38
from bzrlib.branch import Branch
38
39
from bzrlib.diff import internal_diff
39
40
from bzrlib.errors import (BzrError, TestamentMismatch, NotABundle, BadBundle, 
42
43
from bzrlib.repofmt import knitrepo
43
44
from bzrlib.osutils import has_symlinks, sha_file
44
45
from bzrlib.tests import (TestCaseInTempDir, TestCaseWithTransport,
45
 
                          TestCase, TestSkipped)
 
46
                          TestCase, TestSkipped, test_commit)
46
47
from bzrlib.transform import TreeTransform
47
48
from bzrlib.workingtree import WorkingTree
48
49
 
348
349
                          target.repository, read_bundle(text))
349
350
 
350
351
 
351
 
class V08BundleTester(TestCaseWithTransport):
352
 
 
353
 
    format = '0.8'
 
352
class BundleTester(object):
354
353
 
355
354
    def bzrdir_format(self):
356
355
        format = bzrdir.BzrDirMetaFormat1()
379
378
        rev = self.b1.repository.get_revision(rev_id)
380
379
        self.assertEqual(bundle_txt.readline().decode('utf-8'),
381
380
                         u'# message:\n')
382
 
 
383
 
        open(',,bundle', 'wb').write(bundle_txt.getvalue())
384
381
        bundle_txt.seek(0)
385
382
        return bundle_txt, rev_ids
386
383
 
551
548
        self.tree1.commit('add one', rev_id='a@cset-0-1')
552
549
 
553
550
        bundle = self.get_valid_bundle('null:', 'a@cset-0-1')
554
 
        # FIXME: The current write_bundle api no longer supports
555
 
        #        setting a custom summary message
556
 
        #        We should re-introduce the ability, and update
557
 
        #        the tests to make sure it works.
558
 
        # bundle = self.get_valid_bundle(None, 'a@cset-0-1',
559
 
        #         message='With a specialized message')
560
551
 
561
552
        # Make sure we can handle files with spaces, tabs, other
562
553
        # bogus characters
575
566
        tt = TreeTransform(self.tree1)
576
567
        tt.new_file('executable', tt.root, '#!/bin/sh\n', 'exe-1', True)
577
568
        tt.apply()
 
569
        # have to fix length of file-id so that we can predictably rewrite
 
570
        # a (length-prefixed) record containing it later.
 
571
        self.tree1.add('with space.txt', 'withspace-id')
578
572
        self.tree1.add([
579
 
                'with space.txt'
580
 
                , 'dir'
 
573
                  'dir'
581
574
                , 'dir/filein subdir.c'
582
575
                , 'dir/WithCaps.txt'
583
576
                , 'dir/ pre space'
607
600
        self.tree1.commit('removed', rev_id='a@cset-0-3')
608
601
        
609
602
        bundle = self.get_valid_bundle('a@cset-0-2', 'a@cset-0-3')
610
 
        self.assertRaises(TestamentMismatch, self.get_invalid_bundle, 
611
 
                          'a@cset-0-2', 'a@cset-0-3')
 
603
        self.assertRaises((TestamentMismatch,
 
604
            errors.VersionedFileInvalidChecksum), self.get_invalid_bundle,
 
605
            'a@cset-0-2', 'a@cset-0-3')
612
606
        # Check a rollup bundle 
613
607
        bundle = self.get_valid_bundle('null:', 'a@cset-0-3')
614
608
 
622
616
 
623
617
        # Modified files
624
618
        open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
625
 
        open('b1/sub/dir/ pre space', 'ab').write('\r\nAdding some\r\nDOS format lines\r\n')
 
619
        open('b1/sub/dir/ pre space', 'ab').write(
 
620
             '\r\nAdding some\r\nDOS format lines\r\n')
626
621
        open('b1/sub/dir/nolastnewline.txt', 'ab').write('\n')
627
622
        self.tree1.rename_one('sub/dir/ pre space', 
628
623
                              'sub/ start space')
687
682
        
688
683
        # Add
689
684
        tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
690
 
        tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff', 'binary-2')
 
685
        tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
 
686
            'binary-2')
691
687
        tt.apply()
692
688
        self.tree1.commit('add binary', rev_id='b@cset-0-1')
693
689
        self.get_valid_bundle('null:', 'b@cset-0-1')
766
762
        rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
767
763
                               'a@cset-0-1', bundle_file, format=self.format)
768
764
        self.assertNotContainsRe(bundle_file.getvalue(), '\btwo\b')
769
 
        self.assertContainsRe(bundle_file.getvalue(), 'one')
770
 
        self.assertContainsRe(bundle_file.getvalue(), 'three')
 
765
        self.assertContainsRe(self.get_raw(bundle_file), 'one')
 
766
        self.assertContainsRe(self.get_raw(bundle_file), 'three')
 
767
 
 
768
    def test_bundle_same_basis(self):
 
769
        """Ensure using the basis as the target doesn't cause an error"""
 
770
        self.tree1 = self.make_branch_and_tree('b1')
 
771
        self.tree1.commit('add file', rev_id='a@cset-0-1')
 
772
        bundle_file = StringIO()
 
773
        rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-1',
 
774
                               'a@cset-0-1', bundle_file)
 
775
 
 
776
    @staticmethod
 
777
    def get_raw(bundle_file):
 
778
        return bundle_file.getvalue()
771
779
 
772
780
    def test_unicode_bundle(self):
773
781
        # Handle international characters
892
900
        self.b1 = self.tree1.branch
893
901
        self.tree1.commit('message', rev_id='revid1')
894
902
        bundle = self.get_valid_bundle('null:', 'revid1')
895
 
        tree = bundle.revision_tree(self.b1.repository, 'revid1')
 
903
        tree = self.get_bundle_tree(bundle, 'revid1')
896
904
        self.assertEqual('revid1', tree.inventory.root.revision)
897
905
 
898
906
    def test_install_revisions(self):
915
923
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
916
924
        self.b1 = tree.branch
917
925
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
926
        bundle = read_bundle(bundle_sio)
 
927
        revision_info = bundle.revisions[0]
 
928
        self.assertEqual('rev1', revision_info.revision_id)
 
929
        rev = revision_info.as_revision()
 
930
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
 
931
                         rev.properties)
 
932
 
 
933
    def test_bundle_sorted_properties(self):
 
934
        """For stability the writer should write properties in sorted order."""
 
935
        tree = self.make_branch_and_memory_tree('tree')
 
936
        tree.lock_write()
 
937
        self.addCleanup(tree.unlock)
 
938
 
 
939
        tree.add([''], ['TREE_ROOT'])
 
940
        tree.commit('One', rev_id='rev1',
 
941
                    revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
 
942
        self.b1 = tree.branch
 
943
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
944
        bundle = read_bundle(bundle_sio)
 
945
        revision_info = bundle.revisions[0]
 
946
        self.assertEqual('rev1', revision_info.revision_id)
 
947
        rev = revision_info.as_revision()
 
948
        self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
 
949
                          'd':'1'}, rev.properties)
 
950
 
 
951
    def test_bundle_unicode_properties(self):
 
952
        """We should be able to round trip a non-ascii property."""
 
953
        tree = self.make_branch_and_memory_tree('tree')
 
954
        tree.lock_write()
 
955
        self.addCleanup(tree.unlock)
 
956
 
 
957
        tree.add([''], ['TREE_ROOT'])
 
958
        # Revisions themselves do not require anything about revision property
 
959
        # keys, other than that they are a basestring, and do not contain
 
960
        # whitespace.
 
961
        # However, Testaments assert than they are str(), and thus should not
 
962
        # be Unicode.
 
963
        tree.commit('One', rev_id='rev1',
 
964
                    revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
 
965
        self.b1 = tree.branch
 
966
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
967
        bundle = read_bundle(bundle_sio)
 
968
        revision_info = bundle.revisions[0]
 
969
        self.assertEqual('rev1', revision_info.revision_id)
 
970
        rev = revision_info.as_revision()
 
971
        self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
 
972
                          'alpha':u'\u03b1'}, rev.properties)
 
973
 
 
974
    def test_bundle_with_ghosts(self):
 
975
        tree = self.make_branch_and_tree('tree')
 
976
        self.b1 = tree.branch
 
977
        self.build_tree_contents([('tree/file', 'content1')])
 
978
        tree.add(['file'])
 
979
        tree.commit('rev1')
 
980
        self.build_tree_contents([('tree/file', 'content2')])
 
981
        tree.add_parent_tree_id('ghost')
 
982
        tree.commit('rev2', rev_id='rev2')
 
983
        bundle = self.get_valid_bundle('null:', 'rev2')
 
984
 
 
985
    def make_simple_tree(self, format=None):
 
986
        tree = self.make_branch_and_tree('b1', format=format)
 
987
        self.b1 = tree.branch
 
988
        self.build_tree(['b1/file'])
 
989
        tree.add('file')
 
990
        return tree
 
991
 
 
992
    def test_across_serializers(self):
 
993
        tree = self.make_simple_tree('knit')
 
994
        tree.commit('hello', rev_id='rev1')
 
995
        tree.commit('hello', rev_id='rev2')
 
996
        bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
 
997
        repo = self.make_repository('repo', format='dirstate-with-subtree')
 
998
        bundle.install_revisions(repo)
 
999
        inv_text = repo.get_inventory_xml('rev2')
 
1000
        self.assertNotContainsRe(inv_text, 'format="5"')
 
1001
        self.assertContainsRe(inv_text, 'format="7"')
 
1002
 
 
1003
    def test_across_models(self):
 
1004
        tree = self.make_simple_tree('knit')
 
1005
        tree.commit('hello', rev_id='rev1')
 
1006
        tree.commit('hello', rev_id='rev2')
 
1007
        bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
 
1008
        repo = self.make_repository('repo', format='dirstate-with-subtree')
 
1009
        bundle.install_revisions(repo)
 
1010
        inv = repo.get_inventory('rev2')
 
1011
        self.assertEqual('rev2', inv.root.revision)
 
1012
        root_vf = repo.weave_store.get_weave(inv.root.file_id,
 
1013
                                             repo.get_transaction())
 
1014
        self.assertEqual(root_vf.versions(), ['rev1', 'rev2'])
 
1015
 
 
1016
    def test_across_models_incompatible(self):
 
1017
        tree = self.make_simple_tree('dirstate-with-subtree')
 
1018
        tree.commit('hello', rev_id='rev1')
 
1019
        tree.commit('hello', rev_id='rev2')
 
1020
        try:
 
1021
            bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
 
1022
        except errors.IncompatibleBundleFormat:
 
1023
            raise TestSkipped("Format 0.8 doesn't work with knit3")
 
1024
        repo = self.make_repository('repo', format='knit')
 
1025
        bundle.install_revisions(repo)
 
1026
 
 
1027
        bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
 
1028
        self.assertRaises(errors.IncompatibleRevision,
 
1029
                          bundle.install_revisions, repo)
 
1030
 
 
1031
    def test_get_merge_request(self):
 
1032
        tree = self.make_simple_tree()
 
1033
        tree.commit('hello', rev_id='rev1')
 
1034
        tree.commit('hello', rev_id='rev2')
 
1035
        bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
 
1036
        result = bundle.get_merge_request(tree.branch.repository)
 
1037
        self.assertEqual((None, 'rev1', 'inapplicable'), result)
 
1038
 
 
1039
    def test_with_subtree(self):
 
1040
        tree = self.make_branch_and_tree('tree',
 
1041
                                         format='dirstate-with-subtree')
 
1042
        self.b1 = tree.branch
 
1043
        subtree = self.make_branch_and_tree('tree/subtree',
 
1044
                                            format='dirstate-with-subtree')
 
1045
        tree.add('subtree')
 
1046
        tree.commit('hello', rev_id='rev1')
 
1047
        try:
 
1048
            bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
 
1049
        except errors.IncompatibleBundleFormat:
 
1050
            raise TestSkipped("Format 0.8 doesn't work with knit3")
 
1051
        if isinstance(bundle, v09.BundleInfo09):
 
1052
            raise TestSkipped("Format 0.9 doesn't work with subtrees")
 
1053
        repo = self.make_repository('repo', format='knit')
 
1054
        self.assertRaises(errors.IncompatibleRevision,
 
1055
                          bundle.install_revisions, repo)
 
1056
        repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
 
1057
        bundle.install_revisions(repo2)
 
1058
 
 
1059
    def test_revision_id_with_slash(self):
 
1060
        self.tree1 = self.make_branch_and_tree('tree')
 
1061
        self.b1 = self.tree1.branch
 
1062
        try:
 
1063
            self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
 
1064
        except ValueError:
 
1065
            raise TestSkipped("Repository doesn't support revision ids with"
 
1066
                              " slashes")
 
1067
        bundle = self.get_valid_bundle('null:', 'rev/id')
 
1068
 
 
1069
 
 
1070
class V08BundleTester(BundleTester, TestCaseWithTransport):
 
1071
 
 
1072
    format = '0.8'
 
1073
 
 
1074
    def test_bundle_empty_property(self):
 
1075
        """Test serializing revision properties with an empty value."""
 
1076
        tree = self.make_branch_and_memory_tree('tree')
 
1077
        tree.lock_write()
 
1078
        self.addCleanup(tree.unlock)
 
1079
        tree.add([''], ['TREE_ROOT'])
 
1080
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
 
1081
        self.b1 = tree.branch
 
1082
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
918
1083
        self.assertContainsRe(bundle_sio.getvalue(),
919
1084
                              '# properties:\n'
920
1085
                              '#   branch-nick: tree\n'
928
1093
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
929
1094
                         rev.properties)
930
1095
 
 
1096
    def get_bundle_tree(self, bundle, revision_id):
 
1097
        repository = self.make_repository('repo')
 
1098
        return bundle.revision_tree(repository, 'revid1')
 
1099
 
931
1100
    def test_bundle_empty_property_alt(self):
932
1101
        """Test serializing revision properties with an empty value.
933
1102
 
1037
1206
        return format
1038
1207
 
1039
1208
 
1040
 
class MungedBundleTester(TestCaseWithTransport):
 
1209
class V4BundleTester(BundleTester, TestCaseWithTransport):
 
1210
 
 
1211
    format = '4alpha'
 
1212
 
 
1213
    def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
 
1214
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
 
1215
        Make sure that the text generated is valid, and that it
 
1216
        can be applied against the base, and generate the same information.
 
1217
        
 
1218
        :return: The in-memory bundle 
 
1219
        """
 
1220
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
 
1221
 
 
1222
        # This should also validate the generated bundle 
 
1223
        bundle = read_bundle(bundle_txt)
 
1224
        repository = self.b1.repository
 
1225
        for bundle_rev in bundle.real_revisions:
 
1226
            # These really should have already been checked when we read the
 
1227
            # bundle, since it computes the sha1 hash for the revision, which
 
1228
            # only will match if everything is okay, but lets be explicit about
 
1229
            # it
 
1230
            branch_rev = repository.get_revision(bundle_rev.revision_id)
 
1231
            for a in ('inventory_sha1', 'revision_id', 'parent_ids',
 
1232
                      'timestamp', 'timezone', 'message', 'committer', 
 
1233
                      'parent_ids', 'properties'):
 
1234
                self.assertEqual(getattr(branch_rev, a), 
 
1235
                                 getattr(bundle_rev, a))
 
1236
            self.assertEqual(len(branch_rev.parent_ids), 
 
1237
                             len(bundle_rev.parent_ids))
 
1238
        self.assertEqual(set(rev_ids),
 
1239
                         set([r.revision_id for r in bundle.real_revisions]))
 
1240
        self.valid_apply_bundle(base_rev_id, bundle,
 
1241
                                   checkout_dir=checkout_dir)
 
1242
 
 
1243
        return bundle
 
1244
 
 
1245
    def get_invalid_bundle(self, base_rev_id, rev_id):
 
1246
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
 
1247
        Munge the text so that it's invalid.
 
1248
 
 
1249
        :return: The in-memory bundle
 
1250
        """
 
1251
        from bzrlib.bundle import serializer
 
1252
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
 
1253
        new_text = self.get_raw(StringIO(''.join(bundle_txt)))
 
1254
        new_text = new_text.replace('<file file_id="exe-1"',
 
1255
                                    '<file executable="y" file_id="exe-1"')
 
1256
        new_text = new_text.replace('B372', 'B387')
 
1257
        bundle_txt = StringIO()
 
1258
        bundle_txt.write(serializer._get_bundle_header('4alpha'))
 
1259
        bundle_txt.write('\n')
 
1260
        bundle_txt.write(new_text.encode('bz2'))
 
1261
        bundle_txt.seek(0)
 
1262
        bundle = read_bundle(bundle_txt)
 
1263
        self.valid_apply_bundle(base_rev_id, bundle)
 
1264
        return bundle
 
1265
 
 
1266
    def create_bundle_text(self, base_rev_id, rev_id):
 
1267
        bundle_txt = StringIO()
 
1268
        rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id, 
 
1269
                               bundle_txt, format=self.format)
 
1270
        bundle_txt.seek(0)
 
1271
        self.assertEqual(bundle_txt.readline(), 
 
1272
                         '# Bazaar revision bundle v%s\n' % self.format)
 
1273
        self.assertEqual(bundle_txt.readline(), '#\n')
 
1274
        rev = self.b1.repository.get_revision(rev_id)
 
1275
        bundle_txt.seek(0)
 
1276
        return bundle_txt, rev_ids
 
1277
 
 
1278
    def get_bundle_tree(self, bundle, revision_id):
 
1279
        repository = self.make_repository('repo')
 
1280
        bundle.install_revisions(repository)
 
1281
        return repository.revision_tree(revision_id)
 
1282
 
 
1283
    def test_creation(self):
 
1284
        tree = self.make_branch_and_tree('tree')
 
1285
        self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
 
1286
        tree.add('file', 'fileid-2')
 
1287
        tree.commit('added file', rev_id='rev1')
 
1288
        self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
 
1289
        tree.commit('changed file', rev_id='rev2')
 
1290
        s = StringIO()
 
1291
        serializer = BundleSerializerV4('1.0')
 
1292
        serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
 
1293
        s.seek(0)
 
1294
        tree2 = self.make_branch_and_tree('target')
 
1295
        target_repo = tree2.branch.repository
 
1296
        install_bundle(target_repo, serializer.read(s))
 
1297
        vf = target_repo.weave_store.get_weave('fileid-2',
 
1298
            target_repo.get_transaction())
 
1299
        self.assertEqual('contents1\nstatic\n', vf.get_text('rev1'))
 
1300
        self.assertEqual('contents2\nstatic\n', vf.get_text('rev2'))
 
1301
        rtree = target_repo.revision_tree('rev2')
 
1302
        inventory_vf = target_repo.get_inventory_weave()
 
1303
        self.assertEqual(['rev1'], inventory_vf.get_parents('rev2'))
 
1304
        self.assertEqual('changed file',
 
1305
                         target_repo.get_revision('rev2').message)
 
1306
 
 
1307
    @staticmethod
 
1308
    def get_raw(bundle_file):
 
1309
        bundle_file.seek(0)
 
1310
        line = bundle_file.readline()
 
1311
        line = bundle_file.readline()
 
1312
        lines = bundle_file.readlines()
 
1313
        return ''.join(lines).decode('bz2')
 
1314
 
 
1315
    def test_copy_signatures(self):
 
1316
        tree_a = self.make_branch_and_tree('tree_a')
 
1317
        import bzrlib.gpg
 
1318
        import bzrlib.commit as commit
 
1319
        oldstrategy = bzrlib.gpg.GPGStrategy
 
1320
        branch = tree_a.branch
 
1321
        repo_a = branch.repository
 
1322
        tree_a.commit("base", allow_pointless=True, rev_id='A')
 
1323
        self.failIf(branch.repository.has_signature_for_revision_id('A'))
 
1324
        try:
 
1325
            from bzrlib.testament import Testament
 
1326
            # monkey patch gpg signing mechanism
 
1327
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
 
1328
            new_config = test_commit.MustSignConfig(branch)
 
1329
            commit.Commit(config=new_config).commit(message="base",
 
1330
                                                    allow_pointless=True,
 
1331
                                                    rev_id='B',
 
1332
                                                    working_tree=tree_a)
 
1333
            def sign(text):
 
1334
                return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
 
1335
            self.assertTrue(repo_a.has_signature_for_revision_id('B'))
 
1336
        finally:
 
1337
            bzrlib.gpg.GPGStrategy = oldstrategy
 
1338
        tree_b = self.make_branch_and_tree('tree_b')
 
1339
        repo_b = tree_b.branch.repository
 
1340
        s = StringIO()
 
1341
        serializer = BundleSerializerV4('4alpha')
 
1342
        serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
 
1343
        s.seek(0)
 
1344
        install_bundle(repo_b, serializer.read(s))
 
1345
        self.assertTrue(repo_b.has_signature_for_revision_id('B'))
 
1346
        self.assertEqual(repo_b.get_signature_text('B'),
 
1347
                         repo_a.get_signature_text('B'))
 
1348
        s.seek(0)
 
1349
        # ensure repeat installs are harmless
 
1350
        install_bundle(repo_b, serializer.read(s))
 
1351
 
 
1352
 
 
1353
class V4WeaveBundleTester(V4BundleTester):
 
1354
 
 
1355
    def bzrdir_format(self):
 
1356
        return 'metaweave'
 
1357
 
 
1358
 
 
1359
class MungedBundleTester(object):
1041
1360
 
1042
1361
    def build_test_bundle(self):
1043
1362
        wt = self.make_branch_and_tree('b1')
1052
1371
 
1053
1372
        bundle_txt = StringIO()
1054
1373
        rev_ids = write_bundle(wt.branch.repository, 'a@cset-0-2',
1055
 
                               'a@cset-0-1', bundle_txt)
1056
 
        self.assertEqual(['a@cset-0-2'], rev_ids)
 
1374
                               'a@cset-0-1', bundle_txt, self.format)
 
1375
        self.assertEqual(set(['a@cset-0-2']), set(rev_ids))
1057
1376
        bundle_txt.seek(0, 0)
1058
1377
        return bundle_txt
1059
1378
 
1088
1407
        bundle = read_bundle(bundle_txt)
1089
1408
        self.check_valid(bundle)
1090
1409
 
 
1410
 
 
1411
class MungedBundleTesterV09(TestCaseWithTransport, MungedBundleTester):
 
1412
 
 
1413
    format = '0.9'
 
1414
 
1091
1415
    def test_missing_trailing_whitespace(self):
1092
1416
        bundle_txt = self.build_test_bundle()
1093
1417
 
1121
1445
        bundle = read_bundle(bundle_txt)
1122
1446
        self.check_valid(bundle)
1123
1447
 
 
1448
 
 
1449
class MungedBundleTesterV4(TestCaseWithTransport, MungedBundleTester):
 
1450
 
 
1451
    format = '4alpha'
 
1452
 
 
1453
 
 
1454
class TestBundleWriterReader(TestCase):
 
1455
 
 
1456
    def test_roundtrip_record(self):
 
1457
        fileobj = StringIO()
 
1458
        writer = v4.BundleWriter(fileobj)
 
1459
        writer.begin()
 
1460
        writer.add_info_record(foo='bar')
 
1461
        writer._add_record("Record body", {'parents': ['1', '3'],
 
1462
            'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
 
1463
        writer.end()
 
1464
        fileobj.seek(0)
 
1465
        record_iter = v4.BundleReader(fileobj).iter_records()
 
1466
        record = record_iter.next()
 
1467
        self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
 
1468
            'info', None, None), record)
 
1469
        record = record_iter.next()
 
1470
        self.assertEqual(("Record body", {'storage_kind': 'fulltext',
 
1471
                          'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
 
1472
                          record)
 
1473
 
 
1474
    def test_encode_name(self):
 
1475
        self.assertEqual('revision/rev1',
 
1476
            v4.BundleWriter.encode_name('revision', 'rev1'))
 
1477
        self.assertEqual('file/rev//1/file-id-1',
 
1478
            v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
 
1479
        self.assertEqual('info',
 
1480
            v4.BundleWriter.encode_name('info', None, None))
 
1481
 
 
1482
    def test_decode_name(self):
 
1483
        self.assertEqual(('revision', 'rev1', None),
 
1484
            v4.BundleReader.decode_name('revision/rev1'))
 
1485
        self.assertEqual(('file', 'rev/1', 'file-id-1'),
 
1486
            v4.BundleReader.decode_name('file/rev//1/file-id-1'))
 
1487
        self.assertEqual(('info', None, None),
 
1488
                         v4.BundleReader.decode_name('info'))
 
1489
 
 
1490
    def test_too_many_names(self):
 
1491
        fileobj = StringIO()
 
1492
        writer = v4.BundleWriter(fileobj)
 
1493
        writer.begin()
 
1494
        writer.add_info_record(foo='bar')
 
1495
        writer._container.add_bytes_record('blah', ['two', 'names'])
 
1496
        writer.end()
 
1497
        fileobj.seek(0)
 
1498
        record_iter = v4.BundleReader(fileobj).iter_records()
 
1499
        record = record_iter.next()
 
1500
        self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
 
1501
            'info', None, None), record)
 
1502
        self.assertRaises(BadBundle, record_iter.next)