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
19
19
from bzrlib import (
27
25
from bzrlib.tests import TestCase
28
from bzrlib.inventory import Inventory
26
from bzrlib.inventory import Inventory, InventoryEntry
27
from bzrlib.xml4 import serializer_v4
30
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
31
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
32
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
33
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
37
_revision_v4 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
38
inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
39
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
40
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
41
timestamp="1125907235.212"
43
<message>- start splitting code for xml (de)serialization away from objects
44
preparatory to supporting multiple formats by a single library
47
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
31
52
_revision_v5 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
32
53
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
33
54
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
60
81
_committed_inv_v5 = """<inventory>
61
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
62
name="bar" parent_id="TREE_ROOT"
82
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
83
name="bar" parent_id="TREE_ROOT"
63
84
revision="mbp@foo-123123"
64
85
text_sha1="A" text_size="1"/>
65
86
<directory name="subdir"
66
87
file_id="foo-20050801201819-4139aa4a272f4250"
68
89
revision="mbp@foo-00"/>
69
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
70
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
90
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
91
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
71
92
revision="mbp@foo-00"
72
93
text_sha1="B" text_size="0"/>
76
97
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
77
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
78
name="bar" parent_id="TREE_ROOT"
98
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
99
name="bar" parent_id="TREE_ROOT"
79
100
revision="mbp@foo-123123"/>
80
101
<directory name="subdir"
81
102
file_id="foo-20050801201819-4139aa4a272f4250"
103
parent_id="TREE_ROOT"
83
104
revision="mbp@foo-00"/>
84
<file file_id="bar-20050824000535-6bc48cfad47ed134"
85
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
105
<file file_id="bar-20050824000535-6bc48cfad47ed134"
106
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
86
107
revision="mbp@foo-00"/>
120
_expected_inv_v6 = """<inventory format="6" revision_id="rev_outer">
121
<directory file_id="tree-root-321" name="" revision="rev_outer" />
122
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
123
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
124
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
128
141
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
129
142
<directory file_id="tree-root-321" name="" revision="rev_outer" />
130
143
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
137
_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">
138
<message>- start splitting code for xml (de)serialization away from objects
139
preparatory to supporting multiple formats by a single library
142
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
147
_expected_inv_v8 = """<inventory format="8" revision_id="rev_outer">
148
<directory file_id="tree-root-321" name="" revision="rev_outer" />
149
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
150
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
151
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
155
150
_revision_utf8_v5 = """<revision committer="Erik Bågfors <erik@foo.net>"
156
151
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
157
152
revision_id="erik@bågfors-02"
194
189
class TestSerializer(TestCase):
195
190
"""Test XML serialization"""
192
def test_canned_inventory(self):
193
"""Test unpacked a canned inventory v4 file."""
194
inp = StringIO(_working_inventory_v4)
195
inv = serializer_v4.read_inventory(inp)
196
self.assertEqual(len(inv), 4)
197
self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
199
def test_unpack_revision(self):
200
"""Test unpacking a canned revision v4"""
201
inp = StringIO(_revision_v4)
202
rev = serializer_v4.read_revision(inp)
203
eq = self.assertEqual
205
"Martin Pool <mbp@sourcefrog.net>")
207
"mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
208
eq(len(rev.parent_ids), 1)
209
eq(rev.parent_ids[0],
210
"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
197
212
def test_unpack_revision_5(self):
198
213
"""Test unpacking a canned revision v5"""
199
214
inp = StringIO(_revision_v5)
247
262
_inventory_v5a, revision_id='test-rev-id')
248
263
self.assertEqual('test-rev-id', inv.root.revision)
250
def test_unpack_inventory_5a_cache_and_copy(self):
251
# Passing an entry_cache should get populated with the objects
252
# But the returned objects should be copies if return_from_cache is
254
entry_cache = fifo_cache.FIFOCache()
255
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
256
_inventory_v5a, revision_id='test-rev-id',
257
entry_cache=entry_cache, return_from_cache=False)
258
for entry in inv.iter_just_entries():
259
key = (entry.file_id, entry.revision)
260
if entry.file_id is inv.root.file_id:
261
# The root id is inferred for xml v5
262
self.assertFalse(key in entry_cache)
264
self.assertIsNot(entry, entry_cache[key])
266
def test_unpack_inventory_5a_cache_no_copy(self):
267
# Passing an entry_cache should get populated with the objects
268
# The returned objects should be exact if return_from_cache is
270
entry_cache = fifo_cache.FIFOCache()
271
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
272
_inventory_v5a, revision_id='test-rev-id',
273
entry_cache=entry_cache, return_from_cache=True)
274
for entry in inv.iter_just_entries():
275
key = (entry.file_id, entry.revision)
276
if entry.file_id is inv.root.file_id:
277
# The root id is inferred for xml v5
278
self.assertFalse(key in entry_cache)
280
self.assertIs(entry, entry_cache[key])
282
265
def test_unpack_inventory_5b(self):
283
266
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
284
267
_inventory_v5b, revision_id='test-rev-id')
292
275
self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
293
276
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
294
277
self.assertEqual(inv, inv2)
296
279
def assertRoundTrips(self, xml_string):
297
280
inp = StringIO(xml_string)
298
281
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
347
330
new_rev = s_v5.read_revision_from_string(txt)
348
331
self.assertEqual(props, new_rev.properties)
350
def get_sample_inventory(self):
333
def test_roundtrip_inventory_v7(self):
351
334
inv = Inventory('tree-root-321', revision_id='rev_outer')
335
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
336
'rev_outer', 'rev_inner'))
352
337
inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
353
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
338
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
354
339
'tree-root-321'))
355
340
inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
356
341
inv['tree-root-321'].revision = 'rev_outer'
360
345
inv['file-id'].text_size = 1
361
346
inv['link-id'].revision = 'rev_outer'
362
347
inv['link-id'].symlink_target = 'a'
365
def test_roundtrip_inventory_v7(self):
366
inv = self.get_sample_inventory()
367
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
368
'rev_outer', 'rev_inner'))
369
348
txt = xml7.serializer_v7.write_inventory_to_string(inv)
370
349
lines = xml7.serializer_v7.write_inventory_to_lines(inv)
371
350
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
375
354
for path, ie in inv.iter_entries():
376
355
self.assertEqual(ie, inv2[ie.file_id])
378
def test_roundtrip_inventory_v6(self):
379
inv = self.get_sample_inventory()
380
txt = xml6.serializer_v6.write_inventory_to_string(inv)
381
lines = xml6.serializer_v6.write_inventory_to_lines(inv)
382
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
383
self.assertEqualDiff(_expected_inv_v6, txt)
384
inv2 = xml6.serializer_v6.read_inventory_from_string(txt)
385
self.assertEqual(4, len(inv2))
386
for path, ie in inv.iter_entries():
387
self.assertEqual(ie, inv2[ie.file_id])
389
357
def test_wrong_format_v7(self):
390
358
"""Can't accidentally open a file with wrong serializer"""
391
359
s_v6 = bzrlib.xml6.serializer_v6
392
360
s_v7 = xml7.serializer_v7
393
self.assertRaises(errors.UnexpectedInventoryFormat,
361
self.assertRaises(errors.UnexpectedInventoryFormat,
394
362
s_v7.read_inventory_from_string, _expected_inv_v5)
395
self.assertRaises(errors.UnexpectedInventoryFormat,
363
self.assertRaises(errors.UnexpectedInventoryFormat,
396
364
s_v6.read_inventory_from_string, _expected_inv_v7)
398
366
def test_tree_reference(self):
414
382
self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
415
383
self.assertEqual('rev-outer', inv2['nested-id'].revision)
416
384
self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
417
self.assertRaises(errors.UnsupportedInventoryKind,
385
self.assertRaises(errors.UnsupportedInventoryKind,
418
386
s_v6.read_inventory_from_string,
419
387
txt.replace('format="7"', 'format="6"'))
420
self.assertRaises(errors.UnsupportedInventoryKind,
388
self.assertRaises(errors.UnsupportedInventoryKind,
421
389
s_v5.read_inventory_from_string,
422
390
txt.replace('format="7"', 'format="5"'))
424
def test_roundtrip_inventory_v8(self):
425
inv = self.get_sample_inventory()
426
txt = xml8.serializer_v8.write_inventory_to_string(inv)
427
inv2 = xml8.serializer_v8.read_inventory_from_string(txt)
428
self.assertEqual(4, len(inv2))
429
for path, ie in inv.iter_entries():
430
self.assertEqual(ie, inv2[ie.file_id])
432
def test_inventory_text_v8(self):
433
inv = self.get_sample_inventory()
434
txt = xml8.serializer_v8.write_inventory_to_string(inv)
435
lines = xml8.serializer_v8.write_inventory_to_lines(inv)
436
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
437
self.assertEqualDiff(_expected_inv_v8, txt)
439
def test_revision_text_v6(self):
440
"""Pack revision to XML v6"""
441
rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
443
serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
444
self.assertEqualDiff(serialized, _expected_rev_v5)
446
def test_revision_text_v7(self):
447
"""Pack revision to XML v7"""
448
rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
450
serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
451
self.assertEqualDiff(serialized, _expected_rev_v5)
453
def test_revision_text_v8(self):
454
"""Pack revision to XML v8"""
455
rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
457
serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
458
self.assertEqualDiff(serialized, _expected_rev_v8)
460
392
def test_revision_ids_are_utf8(self):
461
393
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
462
394
s_v5 = bzrlib.xml5.serializer_v5
502
434
self.assertEqual(len(expected), len(actual))
436
def test_registry(self):
437
self.assertIs(serializer_v4,
438
xml_serializer.format_registry.get('4'))
439
self.assertIs(bzrlib.xml5.serializer_v5,
440
xml_serializer.format_registry.get('5'))
441
self.assertIs(bzrlib.xml6.serializer_v6,
442
xml_serializer.format_registry.get('6'))
443
self.assertIs(bzrlib.xml7.serializer_v7,
444
xml_serializer.format_registry.get('7'))
505
447
class TestEncodeAndEscape(TestCase):
506
448
"""Whitebox testing of the _encode_and_escape function."""
510
451
# Keep the cache clear before and after the test
511
bzrlib.xml8._ensure_utf8_re()
512
bzrlib.xml8._clear_cache()
513
self.addCleanup(bzrlib.xml8._clear_cache)
452
bzrlib.xml5._ensure_utf8_re()
453
bzrlib.xml5._clear_cache()
454
self.addCleanup(bzrlib.xml5._clear_cache)
515
456
def test_simple_ascii(self):
516
457
# _encode_and_escape always appends a final ", because these parameters
517
458
# are being used in xml attributes, and by returning it now, we have to
518
459
# do fewer string operations later.
519
val = bzrlib.xml8._encode_and_escape('foo bar')
460
val = bzrlib.xml5._encode_and_escape('foo bar')
520
461
self.assertEqual('foo bar"', val)
521
462
# The second time should be cached
522
val2 = bzrlib.xml8._encode_and_escape('foo bar')
463
val2 = bzrlib.xml5._encode_and_escape('foo bar')
523
464
self.assertIs(val2, val)
525
466
def test_ascii_with_xml(self):
526
467
self.assertEqual('&'"<>"',
527
bzrlib.xml8._encode_and_escape('&\'"<>'))
468
bzrlib.xml5._encode_and_escape('&\'"<>'))
529
470
def test_utf8_with_xml(self):
530
471
# u'\xb5\xe5&\u062c'
531
472
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
532
473
self.assertEqual('µå&ج"',
533
bzrlib.xml8._encode_and_escape(utf8_str))
474
bzrlib.xml5._encode_and_escape(utf8_str))
535
476
def test_unicode(self):
536
477
uni_str = u'\xb5\xe5&\u062c'
537
478
self.assertEqual('µå&ج"',
538
bzrlib.xml8._encode_and_escape(uni_str))
541
class TestMisc(TestCase):
543
def test_unescape_xml(self):
544
"""We get some kind of error when malformed entities are passed"""
545
self.assertRaises(KeyError, bzrlib.xml8._unescape_xml, 'foo&bar;')
479
bzrlib.xml5._encode_and_escape(uni_str))