302
259
txt = s_v5.write_revision_to_string(rev)
303
260
new_rev = s_v5.read_revision_from_string(txt)
304
261
self.assertEqual(props, new_rev.properties)
306
def test_roundtrip_inventory_v7(self):
307
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
inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
311
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
313
inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
314
inv['tree-root-321'].revision = 'rev_outer'
315
inv['dir-id'].revision = 'rev_outer'
316
inv['file-id'].revision = 'rev_outer'
317
inv['link-id'].revision = 'rev_outer'
318
txt = xml7.serializer_v7.write_inventory_to_string(inv)
319
self.assertEqualDiff(_expected_inv_v7, txt)
320
inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
321
self.assertEqual(5, len(inv2))
322
for path, ie in inv.iter_entries():
323
self.assertEqual(ie, inv2[ie.file_id])
325
def test_wrong_format_v7(self):
326
"""Can't accidentally open a file with wrong serializer"""
327
s_v6 = bzrlib.xml6.serializer_v6
328
s_v7 = xml7.serializer_v7
329
self.assertRaises(errors.UnexpectedInventoryFormat,
330
s_v7.read_inventory_from_string, _expected_inv_v5)
331
self.assertRaises(errors.UnexpectedInventoryFormat,
332
s_v6.read_inventory_from_string, _expected_inv_v7)
334
def test_tree_reference(self):
335
s_v5 = bzrlib.xml5.serializer_v5
336
s_v6 = bzrlib.xml6.serializer_v6
337
s_v7 = xml7.serializer_v7
338
inv = Inventory('tree-root-321')
339
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
340
'rev-outer', 'rev-inner'))
341
self.assertRaises(errors.UnsupportedInventoryKind,
342
s_v5.write_inventory_to_string, inv)
343
self.assertRaises(errors.UnsupportedInventoryKind,
344
s_v6.write_inventory_to_string, inv)
345
txt = s_v7.write_inventory_to_string(inv)
346
inv2 = s_v7.read_inventory_from_string(txt)
347
self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
348
self.assertEqual('rev-outer', inv2['nested-id'].revision)
349
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"'))
357
def test_revision_ids_are_utf8(self):
358
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
359
s_v5 = bzrlib.xml5.serializer_v5
360
rev = s_v5.read_revision_from_string(_revision_utf8_v5)
361
self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
362
self.assertIsInstance(rev.revision_id, str)
363
self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
364
for parent_id in rev.parent_ids:
365
self.assertIsInstance(parent_id, str)
366
self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
367
self.assertIsInstance(rev.message, unicode)
369
# ie.revision should either be None or a utf-8 revision id
370
inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
371
rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
372
rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
373
fid_root = u'TRE\xe9_ROOT'.encode('utf8')
374
fid_bar1 = u'b\xe5r-01'.encode('utf8')
375
fid_sub = u's\xb5bdir-01'.encode('utf8')
376
fid_bar2 = u'b\xe5r-02'.encode('utf8')
377
expected = [(u'', fid_root, None, None),
378
(u'b\xe5r', fid_bar1, fid_root, rev_id_1),
379
(u's\xb5bdir', fid_sub, fid_root, rev_id_1),
380
(u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
382
self.assertEqual(rev_id_2, inv.revision_id)
383
self.assertIsInstance(inv.revision_id, str)
385
actual = list(inv.iter_entries_by_dir())
386
for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
387
(act_path, act_ie)) in zip(expected, actual):
388
self.assertEqual(exp_path, act_path)
389
self.assertIsInstance(act_path, unicode)
390
self.assertEqual(exp_file_id, act_ie.file_id)
391
self.assertIsInstance(act_ie.file_id, str)
392
self.assertEqual(exp_parent_id, act_ie.parent_id)
393
if exp_parent_id is not None:
394
self.assertIsInstance(act_ie.parent_id, str)
395
self.assertEqual(exp_rev_id, act_ie.revision)
396
if exp_rev_id is not None:
397
self.assertIsInstance(act_ie.revision, str)
399
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
class TestEncodeAndEscape(TestCase):
413
"""Whitebox testing of the _encode_and_escape function."""
416
# 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)
421
def test_simple_ascii(self):
422
# _encode_and_escape always appends a final ", because these parameters
423
# are being used in xml attributes, and by returning it now, we have to
424
# do fewer string operations later.
425
val = bzrlib.xml5._encode_and_escape('foo bar')
426
self.assertEqual('foo bar"', val)
427
# The second time should be cached
428
val2 = bzrlib.xml5._encode_and_escape('foo bar')
429
self.assertIs(val2, val)
431
def test_ascii_with_xml(self):
432
self.assertEqual('&'"<>"',
433
bzrlib.xml5._encode_and_escape('&\'"<>'))
435
def test_utf8_with_xml(self):
437
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
438
self.assertEqual('µå&ج"',
439
bzrlib.xml5._encode_and_escape(utf8_str))
441
def test_unicode(self):
442
uni_str = u'\xb5\xe5&\u062c'
443
self.assertEqual('µå&ج"',
444
bzrlib.xml5._encode_and_escape(uni_str))