80
65
file_id="foo-20050801201819-4139aa4a272f4250"
81
66
parent_id="TREE_ROOT"
82
67
revision="mbp@foo-00"/>
83
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
84
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
85
revision="mbp@foo-00"/>
89
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
90
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
91
name="bar" parent_id="TREE_ROOT"
92
revision="mbp@foo-123123"/>
93
<directory name="subdir"
94
file_id="foo-20050801201819-4139aa4a272f4250"
96
revision="mbp@foo-00"/>
97
68
<file file_id="bar-20050824000535-6bc48cfad47ed134"
98
69
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
99
70
revision="mbp@foo-00"/>
104
# DO NOT REFLOW THIS. Its the exact revision we want.
105
_expected_rev_v5 = """<revision committer="Martin Pool <mbp@sourcefrog.net>" format="5" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
106
<message>- start splitting code for xml (de)serialization away from objects
107
preparatory to supporting multiple formats by a single library
110
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
116
# DO NOT REFLOW THIS. Its the exact inventory we want.
117
_expected_inv_v5 = """<inventory format="5">
118
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" />
119
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
120
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
125
_expected_inv_v5_root = """<inventory file_id="f<" format="5" revision_id="mother!">
126
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f<" revision="mbp@foo-123123" />
127
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f<" revision="mbp@foo-00" />
128
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
133
_revision_utf8_v5 = """<revision committer="Erik Bågfors <erik@foo.net>"
134
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
135
revision_id="erik@bågfors-02"
136
timestamp="1125907235.212"
138
<message>Include µnicode characters
141
<revision_ref revision_id="erik@bågfors-01"/>
146
_inventory_utf8_v5 = """<inventory file_id="TREé_ROOT" format="5"
147
revision_id="erik@bågfors-02">
148
<file file_id="bår-01"
149
name="bår" parent_id="TREé_ROOT"
150
revision="erik@bågfors-01"/>
151
<directory name="sµbdir"
152
file_id="sµbdir-01"
153
parent_id="TREé_ROOT"
154
revision="erik@bågfors-01"/>
155
<file executable="yes" file_id="bår-02"
156
name="bår" parent_id="sµbdir-01"
157
revision="erik@bågfors-02"/>
162
74
class TestSerializer(TestCase):
163
75
"""Test XML serialization"""
164
76
def test_canned_inventory(self):
232
120
def test_repack_inventory_5(self):
233
121
inp = StringIO(_committed_inv_v5)
234
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
236
bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
237
self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
238
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
239
self.assertEqual(inv, inv2)
241
def assertRoundTrips(self, xml_string):
242
inp = StringIO(xml_string)
243
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
245
bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
246
self.assertEqualDiff(xml_string, outp.getvalue())
247
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
248
self.assertEqual(inv, inv2)
250
def tests_serialize_inventory_v5_with_root(self):
251
self.assertRoundTrips(_expected_inv_v5_root)
253
def check_repack_revision(self, txt):
254
"""Check that repacking a revision yields the same information"""
256
rev = bzrlib.xml5.serializer_v5.read_revision(inp)
258
bzrlib.xml5.serializer_v5.write_revision(rev, outp)
259
outfile_contents = outp.getvalue()
260
rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
122
inv = serializer_v5.read_inventory(inp)
124
serializer_v5.write_inventory(inv, outp)
125
inv2 = serializer_v5.read_inventory(StringIO(outp.getvalue()))
126
self.assertEqual(inv, inv2)
128
def test_repack_revision_5(self):
129
inp = StringIO(_revision_v5)
130
rev = serializer_v5.read_revision(inp)
132
serializer_v5.write_revision(rev, outp)
133
rev2 = serializer_v5.read_revision(StringIO(outp.getvalue()))
261
134
self.assertEqual(rev, rev2)
263
def test_repack_revision_5(self):
264
"""Round-trip revision to XML v5"""
265
self.check_repack_revision(_revision_v5)
267
def test_repack_revision_5_utc(self):
268
self.check_repack_revision(_revision_v5_utc)
270
def test_pack_revision_5(self):
271
"""Pack revision to XML v5"""
272
# fixed 20051025, revisions should have final newline
273
rev = bzrlib.xml5.serializer_v5.read_revision_from_string(_revision_v5)
275
bzrlib.xml5.serializer_v5.write_revision(rev, outp)
276
outfile_contents = outp.getvalue()
277
self.assertEqual(outfile_contents[-1], '\n')
278
self.assertEqualDiff(outfile_contents, bzrlib.xml5.serializer_v5.write_revision_to_string(rev))
279
self.assertEqualDiff(outfile_contents, _expected_rev_v5)
281
def test_empty_property_value(self):
282
"""Create an empty property value check that it serializes correctly"""
283
s_v5 = bzrlib.xml5.serializer_v5
284
rev = s_v5.read_revision_from_string(_revision_v5)
286
props = {'empty':'', 'one':'one'}
287
rev.properties = props
288
txt = s_v5.write_revision_to_string(rev)
289
new_rev = s_v5.read_revision_from_string(txt)
290
self.assertEqual(props, new_rev.properties)
292
def test_revision_ids_are_utf8(self):
293
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
294
s_v5 = bzrlib.xml5.serializer_v5
295
rev = s_v5.read_revision_from_string(_revision_utf8_v5)
296
self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
297
self.assertIsInstance(rev.revision_id, str)
298
self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
299
for parent_id in rev.parent_ids:
300
self.assertIsInstance(parent_id, str)
301
self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
302
self.assertIsInstance(rev.message, unicode)
304
# ie.revision should either be None or a utf-8 revision id
305
inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
306
rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
307
rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
308
fid_root = u'TRE\xe9_ROOT'.encode('utf8')
309
fid_bar1 = u'b\xe5r-01'.encode('utf8')
310
fid_sub = u's\xb5bdir-01'.encode('utf8')
311
fid_bar2 = u'b\xe5r-02'.encode('utf8')
312
expected = [(u'', fid_root, None, None),
313
(u'b\xe5r', fid_bar1, fid_root, rev_id_1),
314
(u's\xb5bdir', fid_sub, fid_root, rev_id_1),
315
(u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
317
self.assertEqual(rev_id_2, inv.revision_id)
318
self.assertIsInstance(inv.revision_id, str)
320
actual = list(inv.iter_entries_by_dir())
321
for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
322
(act_path, act_ie)) in zip(expected, actual):
323
self.assertEqual(exp_path, act_path)
324
self.assertIsInstance(act_path, unicode)
325
self.assertEqual(exp_file_id, act_ie.file_id)
326
self.assertIsInstance(act_ie.file_id, str)
327
self.assertEqual(exp_parent_id, act_ie.parent_id)
328
if exp_parent_id is not None:
329
self.assertIsInstance(act_ie.parent_id, str)
330
self.assertEqual(exp_rev_id, act_ie.revision)
331
if exp_rev_id is not None:
332
self.assertIsInstance(act_ie.revision, str)
334
self.assertEqual(len(expected), len(actual))
337
class TestEncodeAndEscape(TestCase):
338
"""Whitebox testing of the _encode_and_escape function."""
341
# Keep the cache clear before and after the test
342
bzrlib.xml5._ensure_utf8_re()
343
bzrlib.xml5._clear_cache()
344
self.addCleanup(bzrlib.xml5._clear_cache)
346
def test_simple_ascii(self):
347
# _encode_and_escape always appends a final ", because these parameters
348
# are being used in xml attributes, and by returning it now, we have to
349
# do fewer string operations later.
350
val = bzrlib.xml5._encode_and_escape('foo bar')
351
self.assertEqual('foo bar"', val)
352
# The second time should be cached
353
val2 = bzrlib.xml5._encode_and_escape('foo bar')
354
self.assertIs(val2, val)
356
def test_ascii_with_xml(self):
357
self.assertEqual('&'"<>"',
358
bzrlib.xml5._encode_and_escape('&\'"<>'))
360
def test_utf8_with_xml(self):
362
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
363
self.assertEqual('µå&ج"',
364
bzrlib.xml5._encode_and_escape(utf8_str))
366
def test_unicode(self):
367
uni_str = u'\xb5\xe5&\u062c'
368
self.assertEqual('µå&ج"',
369
bzrlib.xml5._encode_and_escape(uni_str))