67
_revision_v5_utc = """\
68
<revision committer="Martin Pool <mbp@sourcefrog.net>"
69
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
70
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
71
timestamp="1125907235.212"
73
<message>- start splitting code for xml (de)serialization away from objects
74
preparatory to supporting multiple formats by a single library
77
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
60
82
_committed_inv_v5 = """<inventory>
61
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
62
name="bar" parent_id="TREE_ROOT"
63
revision="mbp@foo-123123"/>
83
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
84
name="bar" parent_id="TREE_ROOT"
85
revision="mbp@foo-123123"
86
text_sha1="A" text_size="1"/>
64
87
<directory name="subdir"
65
88
file_id="foo-20050801201819-4139aa4a272f4250"
67
90
revision="mbp@foo-00"/>
68
<file file_id="bar-20050824000535-6bc48cfad47ed134"
69
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
70
revision="mbp@foo-00"/>
91
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
92
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
94
text_sha1="B" text_size="0"/>
74
98
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
75
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
76
name="bar" parent_id="TREE_ROOT"
99
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
100
name="bar" parent_id="TREE_ROOT"
77
101
revision="mbp@foo-123123"/>
78
102
<directory name="subdir"
79
103
file_id="foo-20050801201819-4139aa4a272f4250"
104
parent_id="TREE_ROOT"
81
105
revision="mbp@foo-00"/>
82
<file file_id="bar-20050824000535-6bc48cfad47ed134"
83
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
106
<file file_id="bar-20050824000535-6bc48cfad47ed134"
107
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
84
108
revision="mbp@foo-00"/>
113
# DO NOT REFLOW THIS. Its the exact revision we want.
114
_expected_rev_v5 = """<revision committer="Martin Pool <mbp@sourcefrog.net>" format="5" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
115
<message>- start splitting code for xml (de)serialization away from objects
116
preparatory to supporting multiple formats by a single library
119
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
125
# DO NOT REFLOW THIS. Its the exact inventory we want.
126
_expected_inv_v5 = """<inventory format="5">
127
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
128
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
129
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" text_sha1="B" text_size="0" />
134
_expected_inv_v5_root = """<inventory file_id="f<" format="5" revision_id="mother!">
135
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f<" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
136
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f<" revision="mbp@foo-00" />
137
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" text_sha1="B" text_size="0" />
138
<symlink file_id="link-1" name="link" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" symlink_target="a" />
142
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
143
<directory file_id="tree-root-321" name="" revision="rev_outer" />
144
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
145
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
146
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
147
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
151
_expected_rev_v8 = """<revision committer="Martin Pool <mbp@sourcefrog.net>" format="8" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
152
<message>- start splitting code for xml (de)serialization away from objects
153
preparatory to supporting multiple formats by a single library
156
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
161
_expected_inv_v8 = """<inventory format="8" revision_id="rev_outer">
162
<directory file_id="tree-root-321" name="" revision="rev_outer" />
163
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
164
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
165
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
169
_revision_utf8_v5 = """<revision committer="Erik Bågfors <erik@foo.net>"
170
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
171
revision_id="erik@bågfors-02"
172
timestamp="1125907235.212"
174
<message>Include µnicode characters
177
<revision_ref revision_id="erik@bågfors-01"/>
182
_inventory_utf8_v5 = """<inventory file_id="TREé_ROOT" format="5"
183
revision_id="erik@bågfors-02">
184
<file file_id="bår-01"
185
name="bår" parent_id="TREé_ROOT"
186
revision="erik@bågfors-01"/>
187
<directory name="sµbdir"
188
file_id="sµbdir-01"
189
parent_id="TREé_ROOT"
190
revision="erik@bågfors-01"/>
191
<file executable="yes" file_id="bår-02"
192
name="bår" parent_id="sµbdir-01"
193
revision="erik@bågfors-02"/>
197
# Before revision_id was always stored as an attribute
198
_inventory_v5a = """<inventory format="5">
202
# Before revision_id was always stored as an attribute
203
_inventory_v5b = """<inventory format="5" revision_id="a-rev-id">
89
208
class TestSerializer(TestCase):
90
209
"""Test XML serialization"""
91
211
def test_canned_inventory(self):
92
212
"""Test unpacked a canned inventory v4 file."""
93
213
inp = StringIO(_working_inventory_v4)
145
276
eq(ie.name, 'bar')
146
277
eq(inv[ie.parent_id].kind, 'directory')
279
def test_unpack_inventory_5a(self):
280
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
281
_inventory_v5a, revision_id='test-rev-id')
282
self.assertEqual('test-rev-id', inv.root.revision)
284
def test_unpack_inventory_5b(self):
285
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
286
_inventory_v5b, revision_id='test-rev-id')
287
self.assertEqual('a-rev-id', inv.root.revision)
148
289
def test_repack_inventory_5(self):
149
290
inp = StringIO(_committed_inv_v5)
150
291
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
151
292
outp = StringIO()
152
293
bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
153
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
154
self.assertEqual(inv, inv2)
294
self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
295
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
296
self.assertEqual(inv, inv2)
298
def assertRoundTrips(self, xml_string):
299
inp = StringIO(xml_string)
300
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
302
bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
303
self.assertEqualDiff(xml_string, outp.getvalue())
304
lines = bzrlib.xml5.serializer_v5.write_inventory_to_lines(inv)
306
self.assertEqual(outp.readlines(), lines)
307
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
308
self.assertEqual(inv, inv2)
310
def tests_serialize_inventory_v5_with_root(self):
311
self.assertRoundTrips(_expected_inv_v5_root)
313
def check_repack_revision(self, txt):
314
"""Check that repacking a revision yields the same information"""
316
rev = bzrlib.xml5.serializer_v5.read_revision(inp)
318
bzrlib.xml5.serializer_v5.write_revision(rev, outp)
319
outfile_contents = outp.getvalue()
320
rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
321
self.assertEqual(rev, rev2)
156
323
def test_repack_revision_5(self):
157
324
"""Round-trip revision to XML v5"""
158
inp = StringIO(_revision_v5)
159
rev = bzrlib.xml5.serializer_v5.read_revision(inp)
161
bzrlib.xml5.serializer_v5.write_revision(rev, outp)
162
outfile_contents = outp.getvalue()
163
rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
164
self.assertEqual(rev, rev2)
325
self.check_repack_revision(_revision_v5)
327
def test_repack_revision_5_utc(self):
328
self.check_repack_revision(_revision_v5_utc)
166
330
def test_pack_revision_5(self):
167
331
"""Pack revision to XML v5"""
183
348
txt = s_v5.write_revision_to_string(rev)
184
349
new_rev = s_v5.read_revision_from_string(txt)
185
350
self.assertEqual(props, new_rev.properties)
352
def get_sample_inventory(self):
353
inv = Inventory('tree-root-321', revision_id='rev_outer')
354
inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
355
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
357
inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
358
inv['tree-root-321'].revision = 'rev_outer'
359
inv['dir-id'].revision = 'rev_outer'
360
inv['file-id'].revision = 'rev_outer'
361
inv['file-id'].text_sha1 = 'A'
362
inv['file-id'].text_size = 1
363
inv['link-id'].revision = 'rev_outer'
364
inv['link-id'].symlink_target = 'a'
367
def test_roundtrip_inventory_v7(self):
368
inv = self.get_sample_inventory()
369
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
370
'rev_outer', 'rev_inner'))
371
txt = xml7.serializer_v7.write_inventory_to_string(inv)
372
lines = xml7.serializer_v7.write_inventory_to_lines(inv)
373
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
374
self.assertEqualDiff(_expected_inv_v7, txt)
375
inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
376
self.assertEqual(5, len(inv2))
377
for path, ie in inv.iter_entries():
378
self.assertEqual(ie, inv2[ie.file_id])
380
def test_wrong_format_v7(self):
381
"""Can't accidentally open a file with wrong serializer"""
382
s_v6 = bzrlib.xml6.serializer_v6
383
s_v7 = xml7.serializer_v7
384
self.assertRaises(errors.UnexpectedInventoryFormat,
385
s_v7.read_inventory_from_string, _expected_inv_v5)
386
self.assertRaises(errors.UnexpectedInventoryFormat,
387
s_v6.read_inventory_from_string, _expected_inv_v7)
389
def test_tree_reference(self):
390
s_v5 = bzrlib.xml5.serializer_v5
391
s_v6 = bzrlib.xml6.serializer_v6
392
s_v7 = xml7.serializer_v7
393
inv = Inventory('tree-root-321', revision_id='rev-outer')
394
inv.root.revision = 'root-rev'
395
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
396
'rev-outer', 'rev-inner'))
397
self.assertRaises(errors.UnsupportedInventoryKind,
398
s_v5.write_inventory_to_string, inv)
399
self.assertRaises(errors.UnsupportedInventoryKind,
400
s_v6.write_inventory_to_string, inv)
401
txt = s_v7.write_inventory_to_string(inv)
402
lines = s_v7.write_inventory_to_lines(inv)
403
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
404
inv2 = s_v7.read_inventory_from_string(txt)
405
self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
406
self.assertEqual('rev-outer', inv2['nested-id'].revision)
407
self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
408
self.assertRaises(errors.UnsupportedInventoryKind,
409
s_v6.read_inventory_from_string,
410
txt.replace('format="7"', 'format="6"'))
411
self.assertRaises(errors.UnsupportedInventoryKind,
412
s_v5.read_inventory_from_string,
413
txt.replace('format="7"', 'format="5"'))
415
def test_roundtrip_inventory_v8(self):
416
inv = self.get_sample_inventory()
417
txt = xml8.serializer_v8.write_inventory_to_string(inv)
418
inv2 = xml8.serializer_v8.read_inventory_from_string(txt)
419
self.assertEqual(4, len(inv2))
420
for path, ie in inv.iter_entries():
421
self.assertEqual(ie, inv2[ie.file_id])
423
def test_inventory_text_v8(self):
424
inv = self.get_sample_inventory()
425
txt = xml8.serializer_v8.write_inventory_to_string(inv)
426
lines = xml8.serializer_v8.write_inventory_to_lines(inv)
427
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
428
self.assertEqualDiff(_expected_inv_v8, txt)
430
def test_revision_text_v6(self):
431
"""Pack revision to XML v6"""
432
rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
434
serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
435
self.assertEqualDiff(serialized, _expected_rev_v5)
437
def test_revision_text_v7(self):
438
"""Pack revision to XML v7"""
439
rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
441
serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
442
self.assertEqualDiff(serialized, _expected_rev_v5)
444
def test_revision_text_v8(self):
445
"""Pack revision to XML v8"""
446
rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
448
serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
449
self.assertEqualDiff(serialized, _expected_rev_v8)
451
def test_revision_ids_are_utf8(self):
452
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
453
s_v5 = bzrlib.xml5.serializer_v5
454
rev = s_v5.read_revision_from_string(_revision_utf8_v5)
455
self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
456
self.assertIsInstance(rev.revision_id, str)
457
self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
458
for parent_id in rev.parent_ids:
459
self.assertIsInstance(parent_id, str)
460
self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
461
self.assertIsInstance(rev.message, unicode)
463
# ie.revision should either be None or a utf-8 revision id
464
inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
465
rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
466
rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
467
fid_root = u'TRE\xe9_ROOT'.encode('utf8')
468
fid_bar1 = u'b\xe5r-01'.encode('utf8')
469
fid_sub = u's\xb5bdir-01'.encode('utf8')
470
fid_bar2 = u'b\xe5r-02'.encode('utf8')
471
expected = [(u'', fid_root, None, rev_id_2),
472
(u'b\xe5r', fid_bar1, fid_root, rev_id_1),
473
(u's\xb5bdir', fid_sub, fid_root, rev_id_1),
474
(u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
476
self.assertEqual(rev_id_2, inv.revision_id)
477
self.assertIsInstance(inv.revision_id, str)
479
actual = list(inv.iter_entries_by_dir())
480
for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
481
(act_path, act_ie)) in zip(expected, actual):
482
self.assertEqual(exp_path, act_path)
483
self.assertIsInstance(act_path, unicode)
484
self.assertEqual(exp_file_id, act_ie.file_id)
485
self.assertIsInstance(act_ie.file_id, str)
486
self.assertEqual(exp_parent_id, act_ie.parent_id)
487
if exp_parent_id is not None:
488
self.assertIsInstance(act_ie.parent_id, str)
489
self.assertEqual(exp_rev_id, act_ie.revision)
490
if exp_rev_id is not None:
491
self.assertIsInstance(act_ie.revision, str)
493
self.assertEqual(len(expected), len(actual))
496
class TestEncodeAndEscape(TestCase):
497
"""Whitebox testing of the _encode_and_escape function."""
501
# Keep the cache clear before and after the test
502
bzrlib.xml8._ensure_utf8_re()
503
bzrlib.xml8._clear_cache()
504
self.addCleanup(bzrlib.xml8._clear_cache)
506
def test_simple_ascii(self):
507
# _encode_and_escape always appends a final ", because these parameters
508
# are being used in xml attributes, and by returning it now, we have to
509
# do fewer string operations later.
510
val = bzrlib.xml8._encode_and_escape('foo bar')
511
self.assertEqual('foo bar"', val)
512
# The second time should be cached
513
val2 = bzrlib.xml8._encode_and_escape('foo bar')
514
self.assertIs(val2, val)
516
def test_ascii_with_xml(self):
517
self.assertEqual('&'"<>"',
518
bzrlib.xml8._encode_and_escape('&\'"<>'))
520
def test_utf8_with_xml(self):
522
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
523
self.assertEqual('µå&ج"',
524
bzrlib.xml8._encode_and_escape(utf8_str))
526
def test_unicode(self):
527
uni_str = u'\xb5\xe5&\u062c'
528
self.assertEqual('µå&ج"',
529
bzrlib.xml8._encode_and_escape(uni_str))