1
# Copyright (C) 2005-2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
17
from io import BytesIO
29
from ..bzr.inventory import Inventory
30
from . import TestCase
31
import breezy.bzr.xml5
33
_revision_v5 = b"""<revision committer="Martin Pool <mbp@sourcefrog.net>"
34
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
35
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
36
timestamp="1125907235.212"
38
<message>- start splitting code for xml (de)serialization away from objects
39
preparatory to supporting multiple formats by a single library
42
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
47
_revision_v5_utc = b"""\
48
<revision committer="Martin Pool <mbp@sourcefrog.net>"
49
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
50
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
51
timestamp="1125907235.212"
53
<message>- start splitting code for xml (de)serialization away from objects
54
preparatory to supporting multiple formats by a single library
57
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
62
_committed_inv_v5 = b"""<inventory>
63
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
64
name="bar" parent_id="TREE_ROOT"
65
revision="mbp@foo-123123"
66
text_sha1="A" text_size="1"/>
67
<directory name="subdir"
68
file_id="foo-20050801201819-4139aa4a272f4250"
70
revision="mbp@foo-00"/>
71
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
72
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
74
text_sha1="B" text_size="0"/>
78
_basis_inv_v5 = b"""<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
79
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
80
name="bar" parent_id="TREE_ROOT"
81
revision="mbp@foo-123123"/>
82
<directory name="subdir"
83
file_id="foo-20050801201819-4139aa4a272f4250"
85
revision="mbp@foo-00"/>
86
<file file_id="bar-20050824000535-6bc48cfad47ed134"
87
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
88
revision="mbp@foo-00"/>
93
# DO NOT REFLOW THIS. Its the exact revision we want.
94
_expected_rev_v5 = b"""<revision committer="Martin Pool <mbp@sourcefrog.net>" format="5" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
95
<message>- start splitting code for xml (de)serialization away from objects
96
preparatory to supporting multiple formats by a single library
99
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
105
# DO NOT REFLOW THIS. Its the exact inventory we want.
106
_expected_inv_v5 = b"""<inventory format="5">
107
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
108
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
109
<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" />
114
_expected_inv_v5_root = b"""<inventory file_id="f<" format="5" revision_id="mother!">
115
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f<" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
116
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f<" revision="mbp@foo-00" />
117
<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" />
118
<symlink file_id="link-1" name="link" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" symlink_target="a" />
122
_expected_inv_v6 = b"""<inventory format="6" revision_id="rev_outer">
123
<directory file_id="tree-root-321" name="" revision="rev_outer" />
124
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
125
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
126
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
130
_expected_inv_v7 = b"""<inventory format="7" revision_id="rev_outer">
131
<directory file_id="tree-root-321" name="" revision="rev_outer" />
132
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
133
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
134
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
135
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
139
_expected_rev_v8 = b"""<revision committer="Martin Pool <mbp@sourcefrog.net>" format="8" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
140
<message>- start splitting code for xml (de)serialization away from objects
141
preparatory to supporting multiple formats by a single library
144
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
149
_expected_inv_v8 = b"""<inventory format="8" revision_id="rev_outer">
150
<directory file_id="tree-root-321" name="" revision="rev_outer" />
151
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
152
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
153
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
157
_revision_utf8_v5 = b"""<revision committer="Erik Bågfors <erik@foo.net>"
158
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
159
revision_id="erik@bågfors-02"
160
timestamp="1125907235.212"
162
<message>Include µnicode characters
165
<revision_ref revision_id="erik@bågfors-01"/>
170
_expected_rev_v8_complex = b"""<revision committer="Erik Bågfors <erik@foo.net>" format="8" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="erik@bågfors-02" timestamp="1125907235.212" timezone="36000">
171
<message>Include µnicode characters
174
<revision_ref revision_id="erik@bågfors-01" />
175
<revision_ref revision_id="erik@bagfors-02" />
177
<properties><property name="bar" />
178
<property name="foo">this has a
179
newline in it</property>
186
_inventory_utf8_v5 = b"""<inventory file_id="TREé_ROOT" format="5"
187
revision_id="erik@bågfors-02">
188
<file file_id="bår-01"
189
name="bår" parent_id="TREé_ROOT"
190
revision="erik@bågfors-01"/>
191
<directory name="sµbdir"
192
file_id="sµbdir-01"
193
parent_id="TREé_ROOT"
194
revision="erik@bågfors-01"/>
195
<file executable="yes" file_id="bår-02"
196
name="bår" parent_id="sµbdir-01"
197
revision="erik@bågfors-02"/>
201
# Before revision_id was always stored as an attribute
202
_inventory_v5a = b"""<inventory format="5">
206
# Before revision_id was always stored as an attribute
207
_inventory_v5b = b"""<inventory format="5" revision_id="a-rev-id">
212
class TestSerializer(TestCase):
213
"""Test XML serialization"""
215
def test_unpack_revision_5(self):
216
"""Test unpacking a canned revision v5"""
217
inp = BytesIO(_revision_v5)
218
rev = breezy.bzr.xml5.serializer_v5.read_revision(inp)
219
eq = self.assertEqual
221
"Martin Pool <mbp@sourcefrog.net>")
222
eq(len(rev.parent_ids), 1)
223
eq(rev.timezone, 36000)
224
eq(rev.parent_ids[0],
225
b"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
227
def test_unpack_revision_5_utc(self):
228
inp = BytesIO(_revision_v5_utc)
229
rev = breezy.bzr.xml5.serializer_v5.read_revision(inp)
230
eq = self.assertEqual
232
"Martin Pool <mbp@sourcefrog.net>")
233
eq(len(rev.parent_ids), 1)
235
eq(rev.parent_ids[0],
236
b"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
238
def test_unpack_inventory_5(self):
239
"""Unpack canned new-style inventory"""
240
inp = BytesIO(_committed_inv_v5)
241
inv = breezy.bzr.xml5.serializer_v5.read_inventory(inp)
242
eq = self.assertEqual
244
ie = inv.get_entry(b'bar-20050824000535-6bc48cfad47ed134')
246
eq(ie.revision, b'mbp@foo-00')
248
eq(inv.get_entry(ie.parent_id).kind, 'directory')
250
def test_unpack_basis_inventory_5(self):
251
"""Unpack canned new-style inventory"""
252
inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
253
breezy.osutils.split_lines(_basis_inv_v5))
254
eq = self.assertEqual
257
b'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
258
ie = inv.get_entry(b'bar-20050824000535-6bc48cfad47ed134')
260
eq(ie.revision, b'mbp@foo-00')
262
eq(inv.get_entry(ie.parent_id).kind, 'directory')
264
def test_unpack_inventory_5a(self):
265
inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
266
breezy.osutils.split_lines(_inventory_v5a), revision_id=b'test-rev-id')
267
self.assertEqual(b'test-rev-id', inv.root.revision)
269
def test_unpack_inventory_5a_cache_and_copy(self):
270
# Passing an entry_cache should get populated with the objects
271
# But the returned objects should be copies if return_from_cache is
273
entry_cache = fifo_cache.FIFOCache()
274
inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
275
breezy.osutils.split_lines(_inventory_v5a), revision_id=b'test-rev-id',
276
entry_cache=entry_cache, return_from_cache=False)
277
for entry in inv.iter_just_entries():
278
key = (entry.file_id, entry.revision)
279
if entry.file_id is inv.root.file_id:
280
# The root id is inferred for xml v5
281
self.assertFalse(key in entry_cache)
283
self.assertIsNot(entry, entry_cache[key])
285
def test_unpack_inventory_5a_cache_no_copy(self):
286
# Passing an entry_cache should get populated with the objects
287
# The returned objects should be exact if return_from_cache is
289
entry_cache = fifo_cache.FIFOCache()
290
inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
291
breezy.osutils.split_lines(_inventory_v5a), revision_id=b'test-rev-id',
292
entry_cache=entry_cache, return_from_cache=True)
293
for entry in inv.iter_just_entries():
294
key = (entry.file_id, entry.revision)
295
if entry.file_id is inv.root.file_id:
296
# The root id is inferred for xml v5
297
self.assertFalse(key in entry_cache)
299
self.assertIs(entry, entry_cache[key])
301
def test_unpack_inventory_5b(self):
302
inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
303
breezy.osutils.split_lines(_inventory_v5b), revision_id=b'test-rev-id')
304
self.assertEqual(b'a-rev-id', inv.root.revision)
306
def test_repack_inventory_5(self):
307
inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
308
breezy.osutils.split_lines(_committed_inv_v5))
310
breezy.bzr.xml5.serializer_v5.write_inventory(inv, outp)
311
self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
312
inv2 = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
313
breezy.osutils.split_lines(outp.getvalue()))
314
self.assertEqual(inv, inv2)
316
def assertRoundTrips(self, xml_string):
317
inp = BytesIO(xml_string)
318
inv = breezy.bzr.xml5.serializer_v5.read_inventory(inp)
320
breezy.bzr.xml5.serializer_v5.write_inventory(inv, outp)
321
self.assertEqualDiff(xml_string, outp.getvalue())
322
lines = breezy.bzr.xml5.serializer_v5.write_inventory_to_lines(inv)
324
self.assertEqual(outp.readlines(), lines)
325
inv2 = breezy.bzr.xml5.serializer_v5.read_inventory(
326
BytesIO(outp.getvalue()))
327
self.assertEqual(inv, inv2)
329
def tests_serialize_inventory_v5_with_root(self):
330
self.assertRoundTrips(_expected_inv_v5_root)
332
def check_repack_revision(self, txt):
333
"""Check that repacking a revision yields the same information"""
335
rev = breezy.bzr.xml5.serializer_v5.read_revision(inp)
336
outfile_contents = breezy.bzr.xml5.serializer_v5.write_revision_to_string(rev)
337
rev2 = breezy.bzr.xml5.serializer_v5.read_revision(
338
BytesIO(outfile_contents))
339
self.assertEqual(rev, rev2)
341
def test_repack_revision_5(self):
342
"""Round-trip revision to XML v5"""
343
self.check_repack_revision(_revision_v5)
345
def test_repack_revision_5_utc(self):
346
self.check_repack_revision(_revision_v5_utc)
348
def test_pack_revision_5(self):
349
"""Pack revision to XML v5"""
350
# fixed 20051025, revisions should have final newline
351
rev = breezy.bzr.xml5.serializer_v5.read_revision_from_string(
353
outfile_contents = breezy.bzr.xml5.serializer_v5.write_revision_to_string(rev)
354
self.assertEqual(outfile_contents[-1:], b'\n')
355
self.assertEqualDiff(
357
b''.join(breezy.bzr.xml5.serializer_v5.write_revision_to_lines(rev)))
358
self.assertEqualDiff(outfile_contents, _expected_rev_v5)
360
def test_empty_property_value(self):
361
"""Create an empty property value check that it serializes correctly"""
362
s_v5 = breezy.bzr.xml5.serializer_v5
363
rev = s_v5.read_revision_from_string(_revision_v5)
364
props = {'empty': '', 'one': 'one'}
365
rev.properties = props
366
txt = b''.join(s_v5.write_revision_to_lines(rev))
367
new_rev = s_v5.read_revision_from_string(txt)
368
self.assertEqual(props, new_rev.properties)
370
def get_sample_inventory(self):
371
inv = Inventory(b'tree-root-321', revision_id=b'rev_outer')
372
inv.add(inventory.InventoryFile(b'file-id', 'file', b'tree-root-321'))
373
inv.add(inventory.InventoryDirectory(b'dir-id', 'dir',
375
inv.add(inventory.InventoryLink(b'link-id', 'link', b'tree-root-321'))
376
inv.get_entry(b'tree-root-321').revision = b'rev_outer'
377
inv.get_entry(b'dir-id').revision = b'rev_outer'
378
inv.get_entry(b'file-id').revision = b'rev_outer'
379
inv.get_entry(b'file-id').text_sha1 = b'A'
380
inv.get_entry(b'file-id').text_size = 1
381
inv.get_entry(b'link-id').revision = b'rev_outer'
382
inv.get_entry(b'link-id').symlink_target = 'a'
385
def test_roundtrip_inventory_v7(self):
386
inv = self.get_sample_inventory()
387
inv.add(inventory.TreeReference(b'nested-id', 'nested', b'tree-root-321',
388
b'rev_outer', b'rev_inner'))
389
lines = xml7.serializer_v7.write_inventory_to_lines(inv)
390
self.assertEqualDiff(_expected_inv_v7, b''.join(lines))
391
inv2 = xml7.serializer_v7.read_inventory_from_lines(lines)
392
self.assertEqual(5, len(inv2))
393
for path, ie in inv.iter_entries():
394
self.assertEqual(ie, inv2.get_entry(ie.file_id))
396
def test_roundtrip_inventory_v6(self):
397
inv = self.get_sample_inventory()
398
lines = xml6.serializer_v6.write_inventory_to_lines(inv)
399
self.assertEqualDiff(_expected_inv_v6, b''.join(lines))
400
inv2 = xml6.serializer_v6.read_inventory_from_lines(lines)
401
self.assertEqual(4, len(inv2))
402
for path, ie in inv.iter_entries():
403
self.assertEqual(ie, inv2.get_entry(ie.file_id))
405
def test_wrong_format_v7(self):
406
"""Can't accidentally open a file with wrong serializer"""
407
s_v6 = breezy.bzr.xml6.serializer_v6
408
s_v7 = xml7.serializer_v7
409
self.assertRaises(errors.UnexpectedInventoryFormat,
410
s_v7.read_inventory_from_lines,
411
breezy.osutils.split_lines(_expected_inv_v5))
412
self.assertRaises(errors.UnexpectedInventoryFormat,
413
s_v6.read_inventory_from_lines,
414
breezy.osutils.split_lines(_expected_inv_v7))
416
def test_tree_reference(self):
417
s_v5 = breezy.bzr.xml5.serializer_v5
418
s_v6 = breezy.bzr.xml6.serializer_v6
419
s_v7 = xml7.serializer_v7
420
inv = Inventory(b'tree-root-321', revision_id=b'rev-outer')
421
inv.root.revision = b'root-rev'
422
inv.add(inventory.TreeReference(b'nested-id', 'nested', b'tree-root-321',
423
b'rev-outer', b'rev-inner'))
424
self.assertRaises(errors.UnsupportedInventoryKind,
425
s_v5.write_inventory_to_lines, inv)
426
self.assertRaises(errors.UnsupportedInventoryKind,
427
s_v6.write_inventory_to_lines, inv)
428
lines = s_v7.write_inventory_to_chunks(inv)
429
inv2 = s_v7.read_inventory_from_lines(lines)
430
self.assertEqual(b'tree-root-321',
431
inv2.get_entry(b'nested-id').parent_id)
432
self.assertEqual(b'rev-outer', inv2.get_entry(b'nested-id').revision)
434
b'rev-inner', inv2.get_entry(b'nested-id').reference_revision)
436
def test_roundtrip_inventory_v8(self):
437
inv = self.get_sample_inventory()
438
lines = xml8.serializer_v8.write_inventory_to_lines(inv)
439
inv2 = xml8.serializer_v8.read_inventory_from_lines(lines)
440
self.assertEqual(4, len(inv2))
441
for path, ie in inv.iter_entries():
442
self.assertEqual(ie, inv2.get_entry(ie.file_id))
444
def test_inventory_text_v8(self):
445
inv = self.get_sample_inventory()
446
lines = xml8.serializer_v8.write_inventory_to_lines(inv)
447
self.assertEqualDiff(_expected_inv_v8, b''.join(lines))
449
def test_revision_text_v6(self):
450
"""Pack revision to XML v6"""
451
rev = breezy.bzr.xml6.serializer_v6.read_revision_from_string(
453
serialized = breezy.bzr.xml6.serializer_v6.write_revision_to_lines(
455
self.assertEqualDiff(b''.join(serialized), _expected_rev_v5)
457
def test_revision_text_v7(self):
458
"""Pack revision to XML v7"""
459
rev = breezy.bzr.xml7.serializer_v7.read_revision_from_string(
461
serialized = breezy.bzr.xml7.serializer_v7.write_revision_to_lines(
463
self.assertEqualDiff(b''.join(serialized), _expected_rev_v5)
465
def test_revision_text_v8(self):
466
"""Pack revision to XML v8"""
467
rev = breezy.bzr.xml8.serializer_v8.read_revision_from_string(
469
serialized = breezy.bzr.xml8.serializer_v8.write_revision_to_lines(
471
self.assertEqualDiff(b''.join(serialized), _expected_rev_v8)
473
def test_revision_text_v8_complex(self):
474
"""Pack revision to XML v8"""
475
rev = breezy.bzr.xml8.serializer_v8.read_revision_from_string(
476
_expected_rev_v8_complex)
477
serialized = breezy.bzr.xml8.serializer_v8.write_revision_to_lines(
479
self.assertEqualDiff(b''.join(serialized), _expected_rev_v8_complex)
481
def test_revision_ids_are_utf8(self):
482
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
483
s_v5 = breezy.bzr.xml5.serializer_v5
484
rev = s_v5.read_revision_from_string(_revision_utf8_v5)
485
self.assertEqual(b'erik@b\xc3\xa5gfors-02', rev.revision_id)
486
self.assertIsInstance(rev.revision_id, bytes)
487
self.assertEqual([b'erik@b\xc3\xa5gfors-01'], rev.parent_ids)
488
for parent_id in rev.parent_ids:
489
self.assertIsInstance(parent_id, bytes)
490
self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
491
self.assertIsInstance(rev.message, str)
493
# ie.revision should either be None or a utf-8 revision id
494
inv = s_v5.read_inventory_from_lines(breezy.osutils.split_lines(_inventory_utf8_v5))
495
rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
496
rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
497
fid_root = u'TRE\xe9_ROOT'.encode('utf8')
498
fid_bar1 = u'b\xe5r-01'.encode('utf8')
499
fid_sub = u's\xb5bdir-01'.encode('utf8')
500
fid_bar2 = u'b\xe5r-02'.encode('utf8')
501
expected = [(u'', fid_root, None, rev_id_2),
502
(u'b\xe5r', fid_bar1, fid_root, rev_id_1),
503
(u's\xb5bdir', fid_sub, fid_root, rev_id_1),
504
(u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
506
self.assertEqual(rev_id_2, inv.revision_id)
507
self.assertIsInstance(inv.revision_id, bytes)
509
actual = list(inv.iter_entries_by_dir())
510
for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
511
(act_path, act_ie)) in zip(expected, actual):
512
self.assertEqual(exp_path, act_path)
513
self.assertIsInstance(act_path, str)
514
self.assertEqual(exp_file_id, act_ie.file_id)
515
self.assertIsInstance(act_ie.file_id, bytes)
516
self.assertEqual(exp_parent_id, act_ie.parent_id)
517
if exp_parent_id is not None:
518
self.assertIsInstance(act_ie.parent_id, bytes)
519
self.assertEqual(exp_rev_id, act_ie.revision)
520
if exp_rev_id is not None:
521
self.assertIsInstance(act_ie.revision, bytes)
523
self.assertEqual(len(expected), len(actual))
525
def test_serialization_error(self):
526
s_v5 = breezy.bzr.xml5.serializer_v5
527
e = self.assertRaises(
528
errors.UnexpectedInventoryFormat,
529
s_v5.read_inventory_from_lines, [b"<Notquitexml"])
530
self.assertEqual(str(e), "unclosed token: line 1, column 0")
533
class TestEncodeAndEscape(TestCase):
534
"""Whitebox testing of the _encode_and_escape function."""
537
super(TestEncodeAndEscape, self).setUp()
538
# Keep the cache clear before and after the test
539
breezy.bzr.xml_serializer._clear_cache()
540
self.addCleanup(breezy.bzr.xml_serializer._clear_cache)
542
def test_simple_ascii(self):
543
# _encode_and_escape always appends a final ", because these parameters
544
# are being used in xml attributes, and by returning it now, we have to
545
# do fewer string operations later.
546
val = breezy.bzr.xml_serializer.encode_and_escape('foo bar')
547
self.assertEqual(b'foo bar', val)
548
# The second time should be cached
549
val2 = breezy.bzr.xml_serializer.encode_and_escape('foo bar')
550
self.assertIs(val2, val)
552
def test_ascii_with_xml(self):
553
self.assertEqual(b'&'"<>',
554
breezy.bzr.xml_serializer.encode_and_escape('&\'"<>'))
556
def test_utf8_with_xml(self):
558
utf8_str = b'\xc2\xb5\xc3\xa5&\xd8\xac'
559
self.assertEqual(b'µå&ج',
560
breezy.bzr.xml_serializer.encode_and_escape(utf8_str))
562
def test_unicode(self):
563
uni_str = u'\xb5\xe5&\u062c'
564
self.assertEqual(b'µå&ج',
565
breezy.bzr.xml_serializer.encode_and_escape(uni_str))
568
class TestMisc(TestCase):
570
def test_unescape_xml(self):
571
"""We get some kind of error when malformed entities are passed"""
572
self.assertRaises(KeyError, breezy.bzr.xml8._unescape_xml, b'foo&bar;')