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
19
19
from bzrlib import (
25
27
from bzrlib.tests import TestCase
26
from bzrlib.inventory import Inventory, InventoryEntry
27
from bzrlib.xml4 import serializer_v4
28
from bzrlib.inventory import Inventory
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" />
52
31
_revision_v5 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
53
32
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
54
33
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
81
60
_committed_inv_v5 = """<inventory>
82
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
83
name="bar" parent_id="TREE_ROOT"
84
revision="mbp@foo-123123"/>
61
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
62
name="bar" parent_id="TREE_ROOT"
63
revision="mbp@foo-123123"
64
text_sha1="A" text_size="1"/>
85
65
<directory name="subdir"
86
66
file_id="foo-20050801201819-4139aa4a272f4250"
88
68
revision="mbp@foo-00"/>
89
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
90
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
91
revision="mbp@foo-00"/>
69
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
70
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
72
text_sha1="B" text_size="0"/>
95
76
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
96
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
97
name="bar" parent_id="TREE_ROOT"
77
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
78
name="bar" parent_id="TREE_ROOT"
98
79
revision="mbp@foo-123123"/>
99
80
<directory name="subdir"
100
81
file_id="foo-20050801201819-4139aa4a272f4250"
101
parent_id="TREE_ROOT"
102
83
revision="mbp@foo-00"/>
103
<file file_id="bar-20050824000535-6bc48cfad47ed134"
104
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
84
<file file_id="bar-20050824000535-6bc48cfad47ed134"
85
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
105
86
revision="mbp@foo-00"/>
122
103
# DO NOT REFLOW THIS. Its the exact inventory we want.
123
104
_expected_inv_v5 = """<inventory format="5">
124
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" />
105
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
125
106
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
126
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
107
<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" />
131
112
_expected_inv_v5_root = """<inventory file_id="f<" format="5" revision_id="mother!">
132
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f<" revision="mbp@foo-123123" />
113
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f<" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
133
114
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f<" revision="mbp@foo-00" />
134
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
115
<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" />
116
<symlink file_id="link-1" name="link" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" symlink_target="a" />
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" />
138
128
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
139
129
<directory file_id="tree-root-321" name="" revision="rev_outer" />
140
130
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
141
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" />
142
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" />
131
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
132
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
143
133
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
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" />
147
155
_revision_utf8_v5 = """<revision committer="Erik Bågfors <erik@foo.net>"
148
156
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
149
157
revision_id="erik@bågfors-02"
243
242
eq(ie.name, 'bar')
244
243
eq(inv[ie.parent_id].kind, 'directory')
245
def test_unpack_inventory_5a(self):
246
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
247
_inventory_v5a, revision_id='test-rev-id')
248
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
def test_unpack_inventory_5b(self):
283
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
284
_inventory_v5b, revision_id='test-rev-id')
285
self.assertEqual('a-rev-id', inv.root.revision)
246
287
def test_repack_inventory_5(self):
247
288
inp = StringIO(_committed_inv_v5)
248
289
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
303
347
new_rev = s_v5.read_revision_from_string(txt)
304
348
self.assertEqual(props, new_rev.properties)
306
def test_roundtrip_inventory_v7(self):
350
def get_sample_inventory(self):
307
351
inv = Inventory('tree-root-321', revision_id='rev_outer')
308
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
309
'rev_outer', 'rev_inner'))
310
352
inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
311
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
353
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
312
354
'tree-root-321'))
313
355
inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
314
356
inv['tree-root-321'].revision = 'rev_outer'
315
357
inv['dir-id'].revision = 'rev_outer'
316
358
inv['file-id'].revision = 'rev_outer'
359
inv['file-id'].text_sha1 = 'A'
360
inv['file-id'].text_size = 1
317
361
inv['link-id'].revision = 'rev_outer'
362
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'))
318
369
txt = xml7.serializer_v7.write_inventory_to_string(inv)
370
lines = xml7.serializer_v7.write_inventory_to_lines(inv)
371
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
319
372
self.assertEqualDiff(_expected_inv_v7, txt)
320
373
inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
321
374
self.assertEqual(5, len(inv2))
322
375
for path, ie in inv.iter_entries():
323
376
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])
325
389
def test_wrong_format_v7(self):
326
390
"""Can't accidentally open a file with wrong serializer"""
327
391
s_v6 = bzrlib.xml6.serializer_v6
328
392
s_v7 = xml7.serializer_v7
329
self.assertRaises(errors.UnexpectedInventoryFormat,
393
self.assertRaises(errors.UnexpectedInventoryFormat,
330
394
s_v7.read_inventory_from_string, _expected_inv_v5)
331
self.assertRaises(errors.UnexpectedInventoryFormat,
395
self.assertRaises(errors.UnexpectedInventoryFormat,
332
396
s_v6.read_inventory_from_string, _expected_inv_v7)
334
398
def test_tree_reference(self):
335
399
s_v5 = bzrlib.xml5.serializer_v5
336
400
s_v6 = bzrlib.xml6.serializer_v6
337
401
s_v7 = xml7.serializer_v7
338
inv = Inventory('tree-root-321')
402
inv = Inventory('tree-root-321', revision_id='rev-outer')
403
inv.root.revision = 'root-rev'
339
404
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
340
405
'rev-outer', 'rev-inner'))
341
self.assertRaises(errors.UnsupportedInventoryKind,
406
self.assertRaises(errors.UnsupportedInventoryKind,
342
407
s_v5.write_inventory_to_string, inv)
343
self.assertRaises(errors.UnsupportedInventoryKind,
408
self.assertRaises(errors.UnsupportedInventoryKind,
344
409
s_v6.write_inventory_to_string, inv)
345
410
txt = s_v7.write_inventory_to_string(inv)
411
lines = s_v7.write_inventory_to_lines(inv)
412
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
346
413
inv2 = s_v7.read_inventory_from_string(txt)
347
414
self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
348
415
self.assertEqual('rev-outer', inv2['nested-id'].revision)
349
416
self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
350
self.assertRaises(errors.UnsupportedInventoryKind,
351
s_v6.read_inventory_from_string,
352
txt.replace('format="7"', 'format="6"'))
353
self.assertRaises(errors.UnsupportedInventoryKind,
354
s_v5.read_inventory_from_string,
355
txt.replace('format="7"', 'format="5"'))
418
def test_roundtrip_inventory_v8(self):
419
inv = self.get_sample_inventory()
420
txt = xml8.serializer_v8.write_inventory_to_string(inv)
421
inv2 = xml8.serializer_v8.read_inventory_from_string(txt)
422
self.assertEqual(4, len(inv2))
423
for path, ie in inv.iter_entries():
424
self.assertEqual(ie, inv2[ie.file_id])
426
def test_inventory_text_v8(self):
427
inv = self.get_sample_inventory()
428
txt = xml8.serializer_v8.write_inventory_to_string(inv)
429
lines = xml8.serializer_v8.write_inventory_to_lines(inv)
430
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
431
self.assertEqualDiff(_expected_inv_v8, txt)
433
def test_revision_text_v6(self):
434
"""Pack revision to XML v6"""
435
rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
437
serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
438
self.assertEqualDiff(serialized, _expected_rev_v5)
440
def test_revision_text_v7(self):
441
"""Pack revision to XML v7"""
442
rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
444
serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
445
self.assertEqualDiff(serialized, _expected_rev_v5)
447
def test_revision_text_v8(self):
448
"""Pack revision to XML v8"""
449
rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
451
serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
452
self.assertEqualDiff(serialized, _expected_rev_v8)
357
454
def test_revision_ids_are_utf8(self):
358
455
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
399
496
self.assertEqual(len(expected), len(actual))
401
def test_registry(self):
402
self.assertIs(serializer_v4,
403
xml_serializer.format_registry.get('4'))
404
self.assertIs(bzrlib.xml5.serializer_v5,
405
xml_serializer.format_registry.get('5'))
406
self.assertIs(bzrlib.xml6.serializer_v6,
407
xml_serializer.format_registry.get('6'))
408
self.assertIs(bzrlib.xml7.serializer_v7,
409
xml_serializer.format_registry.get('7'))
412
499
class TestEncodeAndEscape(TestCase):
413
500
"""Whitebox testing of the _encode_and_escape function."""
503
super(TestEncodeAndEscape, self).setUp()
416
504
# Keep the cache clear before and after the test
417
bzrlib.xml5._ensure_utf8_re()
418
bzrlib.xml5._clear_cache()
419
self.addCleanup(bzrlib.xml5._clear_cache)
505
bzrlib.xml_serializer._clear_cache()
506
self.addCleanup(bzrlib.xml_serializer._clear_cache)
421
508
def test_simple_ascii(self):
422
509
# _encode_and_escape always appends a final ", because these parameters
423
510
# are being used in xml attributes, and by returning it now, we have to
424
511
# do fewer string operations later.
425
val = bzrlib.xml5._encode_and_escape('foo bar')
512
val = bzrlib.xml_serializer.encode_and_escape('foo bar')
426
513
self.assertEqual('foo bar"', val)
427
514
# The second time should be cached
428
val2 = bzrlib.xml5._encode_and_escape('foo bar')
515
val2 = bzrlib.xml_serializer.encode_and_escape('foo bar')
429
516
self.assertIs(val2, val)
431
518
def test_ascii_with_xml(self):
432
519
self.assertEqual('&'"<>"',
433
bzrlib.xml5._encode_and_escape('&\'"<>'))
520
bzrlib.xml_serializer.encode_and_escape('&\'"<>'))
435
522
def test_utf8_with_xml(self):
436
523
# u'\xb5\xe5&\u062c'
437
524
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
438
525
self.assertEqual('µå&ج"',
439
bzrlib.xml5._encode_and_escape(utf8_str))
526
bzrlib.xml_serializer.encode_and_escape(utf8_str))
441
528
def test_unicode(self):
442
529
uni_str = u'\xb5\xe5&\u062c'
443
530
self.assertEqual('µå&ج"',
444
bzrlib.xml5._encode_and_escape(uni_str))
531
bzrlib.xml_serializer.encode_and_escape(uni_str))
534
class TestMisc(TestCase):
536
def test_unescape_xml(self):
537
"""We get some kind of error when malformed entities are passed"""
538
self.assertRaises(KeyError, bzrlib.xml8._unescape_xml, 'foo&bar;')