13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
from cStringIO import StringIO
22
23
from bzrlib import (
27
31
revision as _mod_revision,
30
from bzrlib.builtins import _merge_helper
31
from bzrlib.bzrdir import BzrDir
35
from bzrlib.bundle import read_mergeable_from_url
32
36
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
33
37
from bzrlib.bundle.bundle_data import BundleTree
34
from bzrlib.bundle.serializer import write_bundle, read_bundle
38
from bzrlib.bzrdir import BzrDir
39
from bzrlib.directory_service import directories
40
from bzrlib.bundle.serializer import write_bundle, read_bundle, v09, v4
35
41
from bzrlib.bundle.serializer.v08 import BundleSerializerV08
36
42
from bzrlib.bundle.serializer.v09 import BundleSerializerV09
43
from bzrlib.bundle.serializer.v4 import BundleSerializerV4
37
44
from bzrlib.branch import Branch
38
from bzrlib.diff import internal_diff
39
from bzrlib.errors import (BzrError, TestamentMismatch, NotABundle, BadBundle,
41
from bzrlib.merge import Merge3Merger
42
45
from bzrlib.repofmt import knitrepo
43
from bzrlib.osutils import has_symlinks, sha_file
44
from bzrlib.tests import (TestCaseInTempDir, TestCaseWithTransport,
45
TestCase, TestSkipped)
46
from bzrlib.tests import (
46
50
from bzrlib.transform import TreeTransform
47
from bzrlib.workingtree import WorkingTree
50
53
class MockTree(object):
642
652
self.assertEqualDiff(tree1_inv, tree2_inv)
643
653
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
644
654
other.commit('rename file', rev_id='a@cset-0-6b')
645
_merge_helper([other.basedir, -1], [None, None],
646
this_dir=self.tree1.basedir)
655
self.tree1.merge_from_branch(other.branch)
647
656
self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
649
658
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
651
def test_symlink_bundle(self):
652
if not has_symlinks():
653
raise TestSkipped("No symlink support")
660
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
663
self.requireFeature(tests.SymlinkFeature)
654
664
self.tree1 = self.make_branch_and_tree('b1')
655
665
self.b1 = self.tree1.branch
656
667
tt = TreeTransform(self.tree1)
657
tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
668
tt.new_symlink(link_name, tt.root, link_target, link_id)
659
670
self.tree1.commit('add symlink', rev_id='l@cset-0-1')
660
self.get_valid_bundle('null:', 'l@cset-0-1')
671
bundle = self.get_valid_bundle('null:', 'l@cset-0-1')
672
if getattr(bundle ,'revision_tree', None) is not None:
673
# Not all bundle formats supports revision_tree
674
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-1')
675
self.assertEqual(link_target, bund_tree.get_symlink_target(link_id))
661
677
tt = TreeTransform(self.tree1)
662
trans_id = tt.trans_id_tree_file_id('link-1')
678
trans_id = tt.trans_id_tree_file_id(link_id)
663
679
tt.adjust_path('link2', tt.root, trans_id)
664
680
tt.delete_contents(trans_id)
665
tt.create_symlink('mars', trans_id)
681
tt.create_symlink(new_link_target, trans_id)
667
683
self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
668
self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
684
bundle = self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
685
if getattr(bundle ,'revision_tree', None) is not None:
686
# Not all bundle formats supports revision_tree
687
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-2')
688
self.assertEqual(new_link_target,
689
bund_tree.get_symlink_target(link_id))
669
691
tt = TreeTransform(self.tree1)
670
trans_id = tt.trans_id_tree_file_id('link-1')
692
trans_id = tt.trans_id_tree_file_id(link_id)
671
693
tt.delete_contents(trans_id)
672
694
tt.create_symlink('jupiter', trans_id)
674
696
self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
675
self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
697
bundle = self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
676
699
tt = TreeTransform(self.tree1)
677
trans_id = tt.trans_id_tree_file_id('link-1')
700
trans_id = tt.trans_id_tree_file_id(link_id)
678
701
tt.delete_contents(trans_id)
680
703
self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
681
self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
704
bundle = self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
706
def test_symlink_bundle(self):
707
self._test_symlink_bundle('link', 'bar/foo', 'mars')
709
def test_unicode_symlink_bundle(self):
710
self.requireFeature(tests.UnicodeFilenameFeature)
711
self._test_symlink_bundle(u'\N{Euro Sign}link',
712
u'bar/\N{Euro Sign}foo',
713
u'mars\N{Euro Sign}')
683
715
def test_binary_bundle(self):
684
716
self.tree1 = self.make_branch_and_tree('b1')
685
717
self.b1 = self.tree1.branch
686
718
tt = TreeTransform(self.tree1)
689
721
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')
722
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
692
725
self.tree1.commit('add binary', rev_id='b@cset-0-1')
693
726
self.get_valid_bundle('null:', 'b@cset-0-1')
785
827
u'William Dod\xe9\n').encode('utf-8'))
788
self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
830
self.tree1.add([u'with Dod\N{Euro Sign}'], ['withdod-id'])
789
831
self.tree1.commit(u'i18n commit from William Dod\xe9',
790
832
rev_id='i18n-1', committer=u'William Dod\xe9')
792
if sys.platform == 'darwin':
793
# On Mac the '\xe9' gets changed to 'e\u0301'
794
self.assertEqual([u'.bzr', u'with Dode\u0301'],
795
sorted(os.listdir(u'b1')))
796
delta = self.tree1.changes_from(self.tree1.basis_tree())
797
self.assertEqual([(u'with Dod\xe9', 'withdod-id', 'file')],
799
self.knownFailure("Mac OSX doesn't preserve unicode"
800
" combining characters.")
803
835
bundle = self.get_valid_bundle('null:', 'i18n-1')
806
f = open(u'b1/with Dod\xe9', 'wb')
838
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
807
839
f.write(u'Modified \xb5\n'.encode('utf8'))
809
841
self.tree1.commit(u'modified', rev_id='i18n-2')
811
843
bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
814
self.tree1.rename_one(u'with Dod\xe9', u'B\xe5gfors')
846
self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
815
847
self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
816
848
committer=u'Erik B\xe5gfors')
818
850
bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
821
self.tree1.remove([u'B\xe5gfors'])
853
self.tree1.remove([u'B\N{Euro Sign}gfors'])
822
854
self.tree1.commit(u'removed', rev_id='i18n-4')
824
856
bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
915
947
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
916
948
self.b1 = tree.branch
917
949
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
950
bundle = read_bundle(bundle_sio)
951
revision_info = bundle.revisions[0]
952
self.assertEqual('rev1', revision_info.revision_id)
953
rev = revision_info.as_revision()
954
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
957
def test_bundle_sorted_properties(self):
958
"""For stability the writer should write properties in sorted order."""
959
tree = self.make_branch_and_memory_tree('tree')
961
self.addCleanup(tree.unlock)
963
tree.add([''], ['TREE_ROOT'])
964
tree.commit('One', rev_id='rev1',
965
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
966
self.b1 = tree.branch
967
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
968
bundle = read_bundle(bundle_sio)
969
revision_info = bundle.revisions[0]
970
self.assertEqual('rev1', revision_info.revision_id)
971
rev = revision_info.as_revision()
972
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
973
'd':'1'}, rev.properties)
975
def test_bundle_unicode_properties(self):
976
"""We should be able to round trip a non-ascii property."""
977
tree = self.make_branch_and_memory_tree('tree')
979
self.addCleanup(tree.unlock)
981
tree.add([''], ['TREE_ROOT'])
982
# Revisions themselves do not require anything about revision property
983
# keys, other than that they are a basestring, and do not contain
985
# However, Testaments assert than they are str(), and thus should not
987
tree.commit('One', rev_id='rev1',
988
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
989
self.b1 = tree.branch
990
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
991
bundle = read_bundle(bundle_sio)
992
revision_info = bundle.revisions[0]
993
self.assertEqual('rev1', revision_info.revision_id)
994
rev = revision_info.as_revision()
995
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
996
'alpha':u'\u03b1'}, rev.properties)
998
def test_bundle_with_ghosts(self):
999
tree = self.make_branch_and_tree('tree')
1000
self.b1 = tree.branch
1001
self.build_tree_contents([('tree/file', 'content1')])
1004
self.build_tree_contents([('tree/file', 'content2')])
1005
tree.add_parent_tree_id('ghost')
1006
tree.commit('rev2', rev_id='rev2')
1007
bundle = self.get_valid_bundle('null:', 'rev2')
1009
def make_simple_tree(self, format=None):
1010
tree = self.make_branch_and_tree('b1', format=format)
1011
self.b1 = tree.branch
1012
self.build_tree(['b1/file'])
1016
def test_across_serializers(self):
1017
tree = self.make_simple_tree('knit')
1018
tree.commit('hello', rev_id='rev1')
1019
tree.commit('hello', rev_id='rev2')
1020
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1021
repo = self.make_repository('repo', format='dirstate-with-subtree')
1022
bundle.install_revisions(repo)
1023
inv_text = repo.get_inventory_xml('rev2')
1024
self.assertNotContainsRe(inv_text, 'format="5"')
1025
self.assertContainsRe(inv_text, 'format="7"')
1027
def make_repo_with_installed_revisions(self):
1028
tree = self.make_simple_tree('knit')
1029
tree.commit('hello', rev_id='rev1')
1030
tree.commit('hello', rev_id='rev2')
1031
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1032
repo = self.make_repository('repo', format='dirstate-with-subtree')
1033
bundle.install_revisions(repo)
1036
def test_across_models(self):
1037
repo = self.make_repo_with_installed_revisions()
1038
inv = repo.get_inventory('rev2')
1039
self.assertEqual('rev2', inv.root.revision)
1040
root_id = inv.root.file_id
1042
self.addCleanup(repo.unlock)
1043
self.assertEqual({(root_id, 'rev1'):(),
1044
(root_id, 'rev2'):((root_id, 'rev1'),)},
1045
repo.texts.get_parent_map([(root_id, 'rev1'), (root_id, 'rev2')]))
1047
def test_inv_hash_across_serializers(self):
1048
repo = self.make_repo_with_installed_revisions()
1049
recorded_inv_sha1 = repo.get_inventory_sha1('rev2')
1050
xml = repo.get_inventory_xml('rev2')
1051
self.assertEqual(osutils.sha_string(xml), recorded_inv_sha1)
1053
def test_across_models_incompatible(self):
1054
tree = self.make_simple_tree('dirstate-with-subtree')
1055
tree.commit('hello', rev_id='rev1')
1056
tree.commit('hello', rev_id='rev2')
1058
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1059
except errors.IncompatibleBundleFormat:
1060
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1061
repo = self.make_repository('repo', format='knit')
1062
bundle.install_revisions(repo)
1064
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1065
self.assertRaises(errors.IncompatibleRevision,
1066
bundle.install_revisions, repo)
1068
def test_get_merge_request(self):
1069
tree = self.make_simple_tree()
1070
tree.commit('hello', rev_id='rev1')
1071
tree.commit('hello', rev_id='rev2')
1072
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1073
result = bundle.get_merge_request(tree.branch.repository)
1074
self.assertEqual((None, 'rev1', 'inapplicable'), result)
1076
def test_with_subtree(self):
1077
tree = self.make_branch_and_tree('tree',
1078
format='dirstate-with-subtree')
1079
self.b1 = tree.branch
1080
subtree = self.make_branch_and_tree('tree/subtree',
1081
format='dirstate-with-subtree')
1083
tree.commit('hello', rev_id='rev1')
1085
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1086
except errors.IncompatibleBundleFormat:
1087
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1088
if isinstance(bundle, v09.BundleInfo09):
1089
raise tests.TestSkipped("Format 0.9 doesn't work with subtrees")
1090
repo = self.make_repository('repo', format='knit')
1091
self.assertRaises(errors.IncompatibleRevision,
1092
bundle.install_revisions, repo)
1093
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1094
bundle.install_revisions(repo2)
1096
def test_revision_id_with_slash(self):
1097
self.tree1 = self.make_branch_and_tree('tree')
1098
self.b1 = self.tree1.branch
1100
self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1102
raise tests.TestSkipped(
1103
"Repository doesn't support revision ids with slashes")
1104
bundle = self.get_valid_bundle('null:', 'rev/id')
1106
def test_skip_file(self):
1107
"""Make sure we don't accidentally write to the wrong versionedfile"""
1108
self.tree1 = self.make_branch_and_tree('tree')
1109
self.b1 = self.tree1.branch
1110
# rev1 is not present in bundle, done by fetch
1111
self.build_tree_contents([('tree/file2', 'contents1')])
1112
self.tree1.add('file2', 'file2-id')
1113
self.tree1.commit('rev1', rev_id='reva')
1114
self.build_tree_contents([('tree/file3', 'contents2')])
1115
# rev2 is present in bundle, and done by fetch
1116
# having file1 in the bunle causes file1's versionedfile to be opened.
1117
self.tree1.add('file3', 'file3-id')
1118
self.tree1.commit('rev2')
1119
# Updating file2 should not cause an attempt to add to file1's vf
1120
target = self.tree1.bzrdir.sprout('target').open_workingtree()
1121
self.build_tree_contents([('tree/file2', 'contents3')])
1122
self.tree1.commit('rev3', rev_id='rev3')
1123
bundle = self.get_valid_bundle('reva', 'rev3')
1124
if getattr(bundle, 'get_bundle_reader', None) is None:
1125
raise tests.TestSkipped('Bundle format cannot provide reader')
1126
# be sure that file1 comes before file2
1127
for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1130
self.assertNotEqual(f, 'file2-id')
1131
bundle.install_revisions(target.branch.repository)
1134
class V08BundleTester(BundleTester, tests.TestCaseWithTransport):
1138
def test_bundle_empty_property(self):
1139
"""Test serializing revision properties with an empty value."""
1140
tree = self.make_branch_and_memory_tree('tree')
1142
self.addCleanup(tree.unlock)
1143
tree.add([''], ['TREE_ROOT'])
1144
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1145
self.b1 = tree.branch
1146
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
918
1147
self.assertContainsRe(bundle_sio.getvalue(),
919
1148
'# properties:\n'
920
1149
'# branch-nick: tree\n'
1040
class MungedBundleTester(TestCaseWithTransport):
1273
class V4BundleTester(BundleTester, tests.TestCaseWithTransport):
1277
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1278
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1279
Make sure that the text generated is valid, and that it
1280
can be applied against the base, and generate the same information.
1282
:return: The in-memory bundle
1284
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1286
# This should also validate the generated bundle
1287
bundle = read_bundle(bundle_txt)
1288
repository = self.b1.repository
1289
for bundle_rev in bundle.real_revisions:
1290
# These really should have already been checked when we read the
1291
# bundle, since it computes the sha1 hash for the revision, which
1292
# only will match if everything is okay, but lets be explicit about
1294
branch_rev = repository.get_revision(bundle_rev.revision_id)
1295
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1296
'timestamp', 'timezone', 'message', 'committer',
1297
'parent_ids', 'properties'):
1298
self.assertEqual(getattr(branch_rev, a),
1299
getattr(bundle_rev, a))
1300
self.assertEqual(len(branch_rev.parent_ids),
1301
len(bundle_rev.parent_ids))
1302
self.assertEqual(set(rev_ids),
1303
set([r.revision_id for r in bundle.real_revisions]))
1304
self.valid_apply_bundle(base_rev_id, bundle,
1305
checkout_dir=checkout_dir)
1309
def get_invalid_bundle(self, base_rev_id, rev_id):
1310
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1311
Munge the text so that it's invalid.
1313
:return: The in-memory bundle
1315
from bzrlib.bundle import serializer
1316
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1317
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1318
new_text = new_text.replace('<file file_id="exe-1"',
1319
'<file executable="y" file_id="exe-1"')
1320
new_text = new_text.replace('B222', 'B237')
1321
bundle_txt = StringIO()
1322
bundle_txt.write(serializer._get_bundle_header('4'))
1323
bundle_txt.write('\n')
1324
bundle_txt.write(new_text.encode('bz2'))
1326
bundle = read_bundle(bundle_txt)
1327
self.valid_apply_bundle(base_rev_id, bundle)
1330
def create_bundle_text(self, base_rev_id, rev_id):
1331
bundle_txt = StringIO()
1332
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1333
bundle_txt, format=self.format)
1335
self.assertEqual(bundle_txt.readline(),
1336
'# Bazaar revision bundle v%s\n' % self.format)
1337
self.assertEqual(bundle_txt.readline(), '#\n')
1338
rev = self.b1.repository.get_revision(rev_id)
1340
return bundle_txt, rev_ids
1342
def get_bundle_tree(self, bundle, revision_id):
1343
repository = self.make_repository('repo')
1344
bundle.install_revisions(repository)
1345
return repository.revision_tree(revision_id)
1347
def test_creation(self):
1348
tree = self.make_branch_and_tree('tree')
1349
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1350
tree.add('file', 'fileid-2')
1351
tree.commit('added file', rev_id='rev1')
1352
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1353
tree.commit('changed file', rev_id='rev2')
1355
serializer = BundleSerializerV4('1.0')
1356
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1358
tree2 = self.make_branch_and_tree('target')
1359
target_repo = tree2.branch.repository
1360
install_bundle(target_repo, serializer.read(s))
1361
target_repo.lock_read()
1362
self.addCleanup(target_repo.unlock)
1363
# Turn the 'iterators_of_bytes' back into simple strings for comparison
1364
repo_texts = dict((i, ''.join(content)) for i, content
1365
in target_repo.iter_files_bytes(
1366
[('fileid-2', 'rev1', '1'),
1367
('fileid-2', 'rev2', '2')]))
1368
self.assertEqual({'1':'contents1\nstatic\n',
1369
'2':'contents2\nstatic\n'},
1371
rtree = target_repo.revision_tree('rev2')
1372
inventory_vf = target_repo.inventories
1373
# If the inventory store has a graph, it must match the revision graph.
1375
[inventory_vf.get_parent_map([('rev2',)])[('rev2',)]],
1376
[None, (('rev1',),)])
1377
self.assertEqual('changed file',
1378
target_repo.get_revision('rev2').message)
1381
def get_raw(bundle_file):
1383
line = bundle_file.readline()
1384
line = bundle_file.readline()
1385
lines = bundle_file.readlines()
1386
return ''.join(lines).decode('bz2')
1388
def test_copy_signatures(self):
1389
tree_a = self.make_branch_and_tree('tree_a')
1391
import bzrlib.commit as commit
1392
oldstrategy = bzrlib.gpg.GPGStrategy
1393
branch = tree_a.branch
1394
repo_a = branch.repository
1395
tree_a.commit("base", allow_pointless=True, rev_id='A')
1396
self.failIf(branch.repository.has_signature_for_revision_id('A'))
1398
from bzrlib.testament import Testament
1399
# monkey patch gpg signing mechanism
1400
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1401
new_config = test_commit.MustSignConfig(branch)
1402
commit.Commit(config=new_config).commit(message="base",
1403
allow_pointless=True,
1405
working_tree=tree_a)
1407
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1408
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1410
bzrlib.gpg.GPGStrategy = oldstrategy
1411
tree_b = self.make_branch_and_tree('tree_b')
1412
repo_b = tree_b.branch.repository
1414
serializer = BundleSerializerV4('4')
1415
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1417
install_bundle(repo_b, serializer.read(s))
1418
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1419
self.assertEqual(repo_b.get_signature_text('B'),
1420
repo_a.get_signature_text('B'))
1422
# ensure repeat installs are harmless
1423
install_bundle(repo_b, serializer.read(s))
1426
class V4WeaveBundleTester(V4BundleTester):
1428
def bzrdir_format(self):
1432
class MungedBundleTester(object):
1042
1434
def build_test_bundle(self):
1043
1435
wt = self.make_branch_and_tree('b1')
1121
1518
bundle = read_bundle(bundle_txt)
1122
1519
self.check_valid(bundle)
1522
class MungedBundleTesterV4(tests.TestCaseWithTransport, MungedBundleTester):
1527
class TestBundleWriterReader(tests.TestCase):
1529
def test_roundtrip_record(self):
1530
fileobj = StringIO()
1531
writer = v4.BundleWriter(fileobj)
1533
writer.add_info_record(foo='bar')
1534
writer._add_record("Record body", {'parents': ['1', '3'],
1535
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1538
reader = v4.BundleReader(fileobj, stream_input=True)
1539
record_iter = reader.iter_records()
1540
record = record_iter.next()
1541
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1542
'info', None, None), record)
1543
record = record_iter.next()
1544
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1545
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1548
def test_roundtrip_record_memory_hungry(self):
1549
fileobj = StringIO()
1550
writer = v4.BundleWriter(fileobj)
1552
writer.add_info_record(foo='bar')
1553
writer._add_record("Record body", {'parents': ['1', '3'],
1554
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1557
reader = v4.BundleReader(fileobj, stream_input=False)
1558
record_iter = reader.iter_records()
1559
record = record_iter.next()
1560
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1561
'info', None, None), record)
1562
record = record_iter.next()
1563
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1564
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1567
def test_encode_name(self):
1568
self.assertEqual('revision/rev1',
1569
v4.BundleWriter.encode_name('revision', 'rev1'))
1570
self.assertEqual('file/rev//1/file-id-1',
1571
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1572
self.assertEqual('info',
1573
v4.BundleWriter.encode_name('info', None, None))
1575
def test_decode_name(self):
1576
self.assertEqual(('revision', 'rev1', None),
1577
v4.BundleReader.decode_name('revision/rev1'))
1578
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1579
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1580
self.assertEqual(('info', None, None),
1581
v4.BundleReader.decode_name('info'))
1583
def test_too_many_names(self):
1584
fileobj = StringIO()
1585
writer = v4.BundleWriter(fileobj)
1587
writer.add_info_record(foo='bar')
1588
writer._container.add_bytes_record('blah', ['two', 'names'])
1591
record_iter = v4.BundleReader(fileobj).iter_records()
1592
record = record_iter.next()
1593
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1594
'info', None, None), record)
1595
self.assertRaises(errors.BadBundle, record_iter.next)
1598
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
1600
def test_read_mergeable_skips_local(self):
1601
"""A local bundle named like the URL should not be read.
1603
out, wt = test_read_bundle.create_bundle_file(self)
1604
class FooService(object):
1605
"""A directory service that always returns source"""
1607
def look_up(self, name, url):
1609
directories.register('foo:', FooService, 'Testing directory service')
1610
self.addCleanup(lambda: directories.remove('foo:'))
1611
self.build_tree_contents([('./foo:bar', out.getvalue())])
1612
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1615
def test_smart_server_connection_reset(self):
1616
"""If a smart server connection fails during the attempt to read a
1617
bundle, then the ConnectionReset error should be propagated.
1619
# Instantiate a server that will provoke a ConnectionReset
1620
sock_server = _DisconnectingTCPServer()
1622
self.addCleanup(sock_server.tearDown)
1623
# We don't really care what the url is since the server will close the
1624
# connection without interpreting it
1625
url = sock_server.get_url()
1626
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1629
class _DisconnectingTCPServer(object):
1630
"""A TCP server that immediately closes any connection made to it."""
1633
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1634
self.sock.bind(('127.0.0.1', 0))
1636
self.port = self.sock.getsockname()[1]
1637
self.thread = threading.Thread(
1638
name='%s (port %d)' % (self.__class__.__name__, self.port),
1639
target=self.accept_and_close)
1642
def accept_and_close(self):
1643
conn, addr = self.sock.accept()
1644
conn.shutdown(socket.SHUT_RDWR)
1648
return 'bzr://127.0.0.1:%d/' % (self.port,)
1652
# make sure the thread dies by connecting to the listening socket,
1653
# just in case the test failed to do so.
1654
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1655
conn.connect(self.sock.getsockname())
1657
except socket.error: