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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
from cStringIO import StringIO
23
22
from bzrlib import (
29
revision as _mod_revision,
32
from bzrlib.bundle import read_mergeable_from_url
29
from bzrlib.builtins import _merge_helper
30
from bzrlib.bzrdir import BzrDir
33
31
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
34
32
from bzrlib.bundle.bundle_data import BundleTree
35
from bzrlib.bzrdir import BzrDir
36
from bzrlib.directory_service import directories
37
from bzrlib.bundle.serializer import write_bundle, read_bundle, v09, v4
33
from bzrlib.bundle.serializer import write_bundle, read_bundle
38
34
from bzrlib.bundle.serializer.v08 import BundleSerializerV08
39
35
from bzrlib.bundle.serializer.v09 import BundleSerializerV09
40
from bzrlib.bundle.serializer.v4 import BundleSerializerV4
41
36
from bzrlib.branch import Branch
42
37
from bzrlib.diff import internal_diff
38
from bzrlib.errors import (BzrError, TestamentMismatch, NotABundle, BadBundle,
43
40
from bzrlib.merge import Merge3Merger
44
41
from bzrlib.repofmt import knitrepo
45
from bzrlib.osutils import sha_file, sha_string
46
from bzrlib.tests import (
50
TestCaseWithTransport,
42
from bzrlib.osutils import has_symlinks, sha_file
43
from bzrlib.tests import (TestCaseInTempDir, TestCaseWithTransport,
44
TestCase, TestSkipped)
55
45
from bzrlib.transform import TreeTransform
46
from bzrlib.workingtree import WorkingTree
58
49
class MockTree(object):
458
452
if checkout_dir is None:
459
checkout_dir = osutils.mkdtemp(prefix='test-branch-', dir='.')
453
checkout_dir = tempfile.mkdtemp(prefix='test-branch-', dir='.')
461
455
if not os.path.exists(checkout_dir):
462
456
os.mkdir(checkout_dir)
463
457
tree = self.make_branch_and_tree(checkout_dir)
465
ancestors = write_bundle(self.b1.repository, rev_id, 'null:', s,
459
ancestors = write_bundle(self.b1.repository, rev_id, None, s,
466
460
format=self.format)
468
self.assertIsInstance(s.getvalue(), str)
462
assert isinstance(s.getvalue(), str), (
463
"Bundle isn't a bytestring:\n %s..." % repr(s.getvalue())[:40])
469
464
install_bundle(tree.branch.repository, read_bundle(s))
470
465
for ancestor in ancestors:
471
466
old = self.b1.repository.revision_tree(ancestor)
472
467
new = tree.branch.repository.revision_tree(ancestor)
476
# Check that there aren't any inventory level changes
477
delta = new.changes_from(old)
478
self.assertFalse(delta.has_changed(),
479
'Revision %s not copied correctly.'
482
# Now check that the file contents are all correct
483
for inventory_id in old:
485
old_file = old.get_file(inventory_id)
486
except errors.NoSuchFile:
490
self.assertEqual(old_file.read(),
491
new.get_file(inventory_id).read())
495
if not _mod_revision.is_null(rev_id):
469
# Check that there aren't any inventory level changes
470
delta = new.changes_from(old)
471
self.assertFalse(delta.has_changed(),
472
'Revision %s not copied correctly.'
475
# Now check that the file contents are all correct
476
for inventory_id in old:
478
old_file = old.get_file(inventory_id)
483
self.assertEqual(old_file.read(),
484
new.get_file(inventory_id).read())
485
if rev_id is not None:
496
486
rh = self.b1.revision_history()
497
487
tree.branch.set_revision_history(rh[:rh.index(rev_id)+1])
809
784
u'William Dod\xe9\n').encode('utf-8'))
812
self.tree1.add([u'with Dod\N{Euro Sign}'], ['withdod-id'])
787
self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
813
788
self.tree1.commit(u'i18n commit from William Dod\xe9',
814
789
rev_id='i18n-1', committer=u'William Dod\xe9')
791
if sys.platform == 'darwin':
792
# On Mac the '\xe9' gets changed to 'e\u0301'
793
self.assertEqual([u'.bzr', u'with Dode\u0301'],
794
sorted(os.listdir(u'b1')))
795
delta = self.tree1.changes_from(self.tree1.basis_tree())
796
self.assertEqual([(u'with Dod\xe9', 'withdod-id', 'file')],
798
self.knownFailure("Mac OSX doesn't preserve unicode"
799
" combining characters.")
817
bundle = self.get_valid_bundle('null:', 'i18n-1')
802
bundle = self.get_valid_bundle(None, 'i18n-1')
820
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
805
f = open(u'b1/with Dod\xe9', 'wb')
821
806
f.write(u'Modified \xb5\n'.encode('utf8'))
823
808
self.tree1.commit(u'modified', rev_id='i18n-2')
825
810
bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
828
self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
813
self.tree1.rename_one(u'with Dod\xe9', u'B\xe5gfors')
829
814
self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
830
815
committer=u'Erik B\xe5gfors')
832
817
bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
835
self.tree1.remove([u'B\N{Euro Sign}gfors'])
820
self.tree1.remove([u'B\xe5gfors'])
836
821
self.tree1.commit(u'removed', rev_id='i18n-4')
838
823
bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
841
bundle = self.get_valid_bundle('null:', 'i18n-4')
826
bundle = self.get_valid_bundle(None, 'i18n-4')
844
829
def test_whitespace_bundle(self):
928
913
tree.add([''], ['TREE_ROOT'])
929
914
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
930
915
self.b1 = tree.branch
931
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
932
bundle = read_bundle(bundle_sio)
933
revision_info = bundle.revisions[0]
934
self.assertEqual('rev1', revision_info.revision_id)
935
rev = revision_info.as_revision()
936
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
939
def test_bundle_sorted_properties(self):
940
"""For stability the writer should write properties in sorted order."""
941
tree = self.make_branch_and_memory_tree('tree')
943
self.addCleanup(tree.unlock)
945
tree.add([''], ['TREE_ROOT'])
946
tree.commit('One', rev_id='rev1',
947
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
948
self.b1 = tree.branch
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', 'a':'4', 'b':'3', 'c':'2',
955
'd':'1'}, rev.properties)
957
def test_bundle_unicode_properties(self):
958
"""We should be able to round trip a non-ascii property."""
959
tree = self.make_branch_and_memory_tree('tree')
961
self.addCleanup(tree.unlock)
963
tree.add([''], ['TREE_ROOT'])
964
# Revisions themselves do not require anything about revision property
965
# keys, other than that they are a basestring, and do not contain
967
# However, Testaments assert than they are str(), and thus should not
969
tree.commit('One', rev_id='rev1',
970
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
971
self.b1 = tree.branch
972
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
973
bundle = read_bundle(bundle_sio)
974
revision_info = bundle.revisions[0]
975
self.assertEqual('rev1', revision_info.revision_id)
976
rev = revision_info.as_revision()
977
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
978
'alpha':u'\u03b1'}, rev.properties)
980
def test_bundle_with_ghosts(self):
981
tree = self.make_branch_and_tree('tree')
982
self.b1 = tree.branch
983
self.build_tree_contents([('tree/file', 'content1')])
986
self.build_tree_contents([('tree/file', 'content2')])
987
tree.add_parent_tree_id('ghost')
988
tree.commit('rev2', rev_id='rev2')
989
bundle = self.get_valid_bundle('null:', 'rev2')
991
def make_simple_tree(self, format=None):
992
tree = self.make_branch_and_tree('b1', format=format)
993
self.b1 = tree.branch
994
self.build_tree(['b1/file'])
998
def test_across_serializers(self):
999
tree = self.make_simple_tree('knit')
1000
tree.commit('hello', rev_id='rev1')
1001
tree.commit('hello', rev_id='rev2')
1002
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1003
repo = self.make_repository('repo', format='dirstate-with-subtree')
1004
bundle.install_revisions(repo)
1005
inv_text = repo.get_inventory_xml('rev2')
1006
self.assertNotContainsRe(inv_text, 'format="5"')
1007
self.assertContainsRe(inv_text, 'format="7"')
1009
def make_repo_with_installed_revisions(self):
1010
tree = self.make_simple_tree('knit')
1011
tree.commit('hello', rev_id='rev1')
1012
tree.commit('hello', rev_id='rev2')
1013
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1014
repo = self.make_repository('repo', format='dirstate-with-subtree')
1015
bundle.install_revisions(repo)
1018
def test_across_models(self):
1019
repo = self.make_repo_with_installed_revisions()
1020
inv = repo.get_inventory('rev2')
1021
self.assertEqual('rev2', inv.root.revision)
1022
root_id = inv.root.file_id
1024
self.addCleanup(repo.unlock)
1025
self.assertEqual({(root_id, 'rev1'):(),
1026
(root_id, 'rev2'):((root_id, 'rev1'),)},
1027
repo.texts.get_parent_map([(root_id, 'rev1'), (root_id, 'rev2')]))
1029
def test_inv_hash_across_serializers(self):
1030
repo = self.make_repo_with_installed_revisions()
1031
recorded_inv_sha1 = repo.get_inventory_sha1('rev2')
1032
xml = repo.get_inventory_xml('rev2')
1033
self.assertEqual(sha_string(xml), recorded_inv_sha1)
1035
def test_across_models_incompatible(self):
1036
tree = self.make_simple_tree('dirstate-with-subtree')
1037
tree.commit('hello', rev_id='rev1')
1038
tree.commit('hello', rev_id='rev2')
1040
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1041
except errors.IncompatibleBundleFormat:
1042
raise TestSkipped("Format 0.8 doesn't work with knit3")
1043
repo = self.make_repository('repo', format='knit')
1044
bundle.install_revisions(repo)
1046
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1047
self.assertRaises(errors.IncompatibleRevision,
1048
bundle.install_revisions, repo)
1050
def test_get_merge_request(self):
1051
tree = self.make_simple_tree()
1052
tree.commit('hello', rev_id='rev1')
1053
tree.commit('hello', rev_id='rev2')
1054
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1055
result = bundle.get_merge_request(tree.branch.repository)
1056
self.assertEqual((None, 'rev1', 'inapplicable'), result)
1058
def test_with_subtree(self):
1059
tree = self.make_branch_and_tree('tree',
1060
format='dirstate-with-subtree')
1061
self.b1 = tree.branch
1062
subtree = self.make_branch_and_tree('tree/subtree',
1063
format='dirstate-with-subtree')
1065
tree.commit('hello', rev_id='rev1')
1067
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1068
except errors.IncompatibleBundleFormat:
1069
raise TestSkipped("Format 0.8 doesn't work with knit3")
1070
if isinstance(bundle, v09.BundleInfo09):
1071
raise TestSkipped("Format 0.9 doesn't work with subtrees")
1072
repo = self.make_repository('repo', format='knit')
1073
self.assertRaises(errors.IncompatibleRevision,
1074
bundle.install_revisions, repo)
1075
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1076
bundle.install_revisions(repo2)
1078
def test_revision_id_with_slash(self):
1079
self.tree1 = self.make_branch_and_tree('tree')
1080
self.b1 = self.tree1.branch
1082
self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1084
raise TestSkipped("Repository doesn't support revision ids with"
1086
bundle = self.get_valid_bundle('null:', 'rev/id')
1088
def test_skip_file(self):
1089
"""Make sure we don't accidentally write to the wrong versionedfile"""
1090
self.tree1 = self.make_branch_and_tree('tree')
1091
self.b1 = self.tree1.branch
1092
# rev1 is not present in bundle, done by fetch
1093
self.build_tree_contents([('tree/file2', 'contents1')])
1094
self.tree1.add('file2', 'file2-id')
1095
self.tree1.commit('rev1', rev_id='reva')
1096
self.build_tree_contents([('tree/file3', 'contents2')])
1097
# rev2 is present in bundle, and done by fetch
1098
# having file1 in the bunle causes file1's versionedfile to be opened.
1099
self.tree1.add('file3', 'file3-id')
1100
self.tree1.commit('rev2')
1101
# Updating file2 should not cause an attempt to add to file1's vf
1102
target = self.tree1.bzrdir.sprout('target').open_workingtree()
1103
self.build_tree_contents([('tree/file2', 'contents3')])
1104
self.tree1.commit('rev3', rev_id='rev3')
1105
bundle = self.get_valid_bundle('reva', 'rev3')
1106
if getattr(bundle, 'get_bundle_reader', None) is None:
1107
raise TestSkipped('Bundle format cannot provide reader')
1108
# be sure that file1 comes before file2
1109
for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1112
self.assertNotEqual(f, 'file2-id')
1113
bundle.install_revisions(target.branch.repository)
1116
class V08BundleTester(BundleTester, TestCaseWithTransport):
1120
def test_bundle_empty_property(self):
1121
"""Test serializing revision properties with an empty value."""
1122
tree = self.make_branch_and_memory_tree('tree')
1124
self.addCleanup(tree.unlock)
1125
tree.add([''], ['TREE_ROOT'])
1126
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1127
self.b1 = tree.branch
1128
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
916
bundle_sio, revision_ids = self.create_bundle_text(None, 'rev1')
1129
917
self.assertContainsRe(bundle_sio.getvalue(),
1130
918
'# properties:\n'
1131
919
'# branch-nick: tree\n'
1255
class V4BundleTester(BundleTester, TestCaseWithTransport):
1259
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1260
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1261
Make sure that the text generated is valid, and that it
1262
can be applied against the base, and generate the same information.
1264
:return: The in-memory bundle
1266
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1268
# This should also validate the generated bundle
1269
bundle = read_bundle(bundle_txt)
1270
repository = self.b1.repository
1271
for bundle_rev in bundle.real_revisions:
1272
# These really should have already been checked when we read the
1273
# bundle, since it computes the sha1 hash for the revision, which
1274
# only will match if everything is okay, but lets be explicit about
1276
branch_rev = repository.get_revision(bundle_rev.revision_id)
1277
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1278
'timestamp', 'timezone', 'message', 'committer',
1279
'parent_ids', 'properties'):
1280
self.assertEqual(getattr(branch_rev, a),
1281
getattr(bundle_rev, a))
1282
self.assertEqual(len(branch_rev.parent_ids),
1283
len(bundle_rev.parent_ids))
1284
self.assertEqual(set(rev_ids),
1285
set([r.revision_id for r in bundle.real_revisions]))
1286
self.valid_apply_bundle(base_rev_id, bundle,
1287
checkout_dir=checkout_dir)
1291
def get_invalid_bundle(self, base_rev_id, rev_id):
1292
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1293
Munge the text so that it's invalid.
1295
:return: The in-memory bundle
1297
from bzrlib.bundle import serializer
1298
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1299
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1300
new_text = new_text.replace('<file file_id="exe-1"',
1301
'<file executable="y" file_id="exe-1"')
1302
new_text = new_text.replace('B222', 'B237')
1303
bundle_txt = StringIO()
1304
bundle_txt.write(serializer._get_bundle_header('4'))
1305
bundle_txt.write('\n')
1306
bundle_txt.write(new_text.encode('bz2'))
1308
bundle = read_bundle(bundle_txt)
1309
self.valid_apply_bundle(base_rev_id, bundle)
1312
def create_bundle_text(self, base_rev_id, rev_id):
1313
bundle_txt = StringIO()
1314
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1315
bundle_txt, format=self.format)
1317
self.assertEqual(bundle_txt.readline(),
1318
'# Bazaar revision bundle v%s\n' % self.format)
1319
self.assertEqual(bundle_txt.readline(), '#\n')
1320
rev = self.b1.repository.get_revision(rev_id)
1322
return bundle_txt, rev_ids
1324
def get_bundle_tree(self, bundle, revision_id):
1325
repository = self.make_repository('repo')
1326
bundle.install_revisions(repository)
1327
return repository.revision_tree(revision_id)
1329
def test_creation(self):
1330
tree = self.make_branch_and_tree('tree')
1331
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1332
tree.add('file', 'fileid-2')
1333
tree.commit('added file', rev_id='rev1')
1334
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1335
tree.commit('changed file', rev_id='rev2')
1337
serializer = BundleSerializerV4('1.0')
1338
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1340
tree2 = self.make_branch_and_tree('target')
1341
target_repo = tree2.branch.repository
1342
install_bundle(target_repo, serializer.read(s))
1343
target_repo.lock_read()
1344
self.addCleanup(target_repo.unlock)
1345
# Turn the 'iterators_of_bytes' back into simple strings for comparison
1346
repo_texts = dict((i, ''.join(content)) for i, content
1347
in target_repo.iter_files_bytes(
1348
[('fileid-2', 'rev1', '1'),
1349
('fileid-2', 'rev2', '2')]))
1350
self.assertEqual({'1':'contents1\nstatic\n',
1351
'2':'contents2\nstatic\n'},
1353
rtree = target_repo.revision_tree('rev2')
1354
inventory_vf = target_repo.inventories
1355
# If the inventory store has a graph, it must match the revision graph.
1357
[inventory_vf.get_parent_map([('rev2',)])[('rev2',)]],
1358
[None, (('rev1',),)])
1359
self.assertEqual('changed file',
1360
target_repo.get_revision('rev2').message)
1363
def get_raw(bundle_file):
1365
line = bundle_file.readline()
1366
line = bundle_file.readline()
1367
lines = bundle_file.readlines()
1368
return ''.join(lines).decode('bz2')
1370
def test_copy_signatures(self):
1371
tree_a = self.make_branch_and_tree('tree_a')
1373
import bzrlib.commit as commit
1374
oldstrategy = bzrlib.gpg.GPGStrategy
1375
branch = tree_a.branch
1376
repo_a = branch.repository
1377
tree_a.commit("base", allow_pointless=True, rev_id='A')
1378
self.failIf(branch.repository.has_signature_for_revision_id('A'))
1380
from bzrlib.testament import Testament
1381
# monkey patch gpg signing mechanism
1382
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1383
new_config = test_commit.MustSignConfig(branch)
1384
commit.Commit(config=new_config).commit(message="base",
1385
allow_pointless=True,
1387
working_tree=tree_a)
1389
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1390
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1392
bzrlib.gpg.GPGStrategy = oldstrategy
1393
tree_b = self.make_branch_and_tree('tree_b')
1394
repo_b = tree_b.branch.repository
1396
serializer = BundleSerializerV4('4')
1397
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1399
install_bundle(repo_b, serializer.read(s))
1400
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1401
self.assertEqual(repo_b.get_signature_text('B'),
1402
repo_a.get_signature_text('B'))
1404
# ensure repeat installs are harmless
1405
install_bundle(repo_b, serializer.read(s))
1408
class V4WeaveBundleTester(V4BundleTester):
1410
def bzrdir_format(self):
1414
class MungedBundleTester(object):
1039
class MungedBundleTester(TestCaseWithTransport):
1416
1041
def build_test_bundle(self):
1417
1042
wt = self.make_branch_and_tree('b1')
1500
1120
bundle = read_bundle(bundle_txt)
1501
1121
self.check_valid(bundle)
1504
class MungedBundleTesterV4(TestCaseWithTransport, MungedBundleTester):
1509
class TestBundleWriterReader(TestCase):
1511
def test_roundtrip_record(self):
1512
fileobj = StringIO()
1513
writer = v4.BundleWriter(fileobj)
1515
writer.add_info_record(foo='bar')
1516
writer._add_record("Record body", {'parents': ['1', '3'],
1517
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1520
reader = v4.BundleReader(fileobj, stream_input=True)
1521
record_iter = reader.iter_records()
1522
record = record_iter.next()
1523
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1524
'info', None, None), record)
1525
record = record_iter.next()
1526
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1527
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1530
def test_roundtrip_record_memory_hungry(self):
1531
fileobj = StringIO()
1532
writer = v4.BundleWriter(fileobj)
1534
writer.add_info_record(foo='bar')
1535
writer._add_record("Record body", {'parents': ['1', '3'],
1536
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1539
reader = v4.BundleReader(fileobj, stream_input=False)
1540
record_iter = reader.iter_records()
1541
record = record_iter.next()
1542
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1543
'info', None, None), record)
1544
record = record_iter.next()
1545
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1546
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1549
def test_encode_name(self):
1550
self.assertEqual('revision/rev1',
1551
v4.BundleWriter.encode_name('revision', 'rev1'))
1552
self.assertEqual('file/rev//1/file-id-1',
1553
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1554
self.assertEqual('info',
1555
v4.BundleWriter.encode_name('info', None, None))
1557
def test_decode_name(self):
1558
self.assertEqual(('revision', 'rev1', None),
1559
v4.BundleReader.decode_name('revision/rev1'))
1560
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1561
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1562
self.assertEqual(('info', None, None),
1563
v4.BundleReader.decode_name('info'))
1565
def test_too_many_names(self):
1566
fileobj = StringIO()
1567
writer = v4.BundleWriter(fileobj)
1569
writer.add_info_record(foo='bar')
1570
writer._container.add_bytes_record('blah', ['two', 'names'])
1573
record_iter = v4.BundleReader(fileobj).iter_records()
1574
record = record_iter.next()
1575
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1576
'info', None, None), record)
1577
self.assertRaises(errors.BadBundle, record_iter.next)
1580
class TestReadMergeableFromUrl(TestCaseWithTransport):
1582
def test_read_mergeable_skips_local(self):
1583
"""A local bundle named like the URL should not be read.
1585
out, wt = test_read_bundle.create_bundle_file(self)
1586
class FooService(object):
1587
"""A directory service that always returns source"""
1589
def look_up(self, name, url):
1591
directories.register('foo:', FooService, 'Testing directory service')
1592
self.addCleanup(lambda: directories.remove('foo:'))
1593
self.build_tree_contents([('./foo:bar', out.getvalue())])
1594
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1597
def test_smart_server_connection_reset(self):
1598
"""If a smart server connection fails during the attempt to read a
1599
bundle, then the ConnectionReset error should be propagated.
1601
# Instantiate a server that will provoke a ConnectionReset
1602
sock_server = _DisconnectingTCPServer()
1604
self.addCleanup(sock_server.tearDown)
1605
# We don't really care what the url is since the server will close the
1606
# connection without interpreting it
1607
url = sock_server.get_url()
1608
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1611
class _DisconnectingTCPServer(object):
1612
"""A TCP server that immediately closes any connection made to it."""
1615
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1616
self.sock.bind(('127.0.0.1', 0))
1618
self.port = self.sock.getsockname()[1]
1619
self.thread = threading.Thread(
1620
name='%s (port %d)' % (self.__class__.__name__, self.port),
1621
target=self.accept_and_close)
1624
def accept_and_close(self):
1625
conn, addr = self.sock.accept()
1626
conn.shutdown(socket.SHUT_RDWR)
1630
return 'bzr://127.0.0.1:%d/' % (self.port,)
1634
# make sure the thread dies by connecting to the listening socket,
1635
# just in case the test failed to do so.
1636
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1637
conn.connect(self.sock.getsockname())
1639
except socket.error: