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