1
# Copyright (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from cStringIO import StringIO
25
from bzrlib.tests import TestCase
26
from bzrlib.inventory import Inventory, InventoryEntry
27
from bzrlib.xml4 import serializer_v4
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
_revision_v5 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
53
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
54
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
55
timestamp="1125907235.212"
57
<message>- start splitting code for xml (de)serialization away from objects
58
preparatory to supporting multiple formats by a single library
61
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
66
_revision_v5_utc = """\
67
<revision committer="Martin Pool <mbp@sourcefrog.net>"
68
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
69
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
70
timestamp="1125907235.212"
72
<message>- start splitting code for xml (de)serialization away from objects
73
preparatory to supporting multiple formats by a single library
76
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
81
_committed_inv_v5 = """<inventory>
82
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
83
name="bar" parent_id="TREE_ROOT"
84
revision="mbp@foo-123123"
85
text_sha1="A" text_size="1"/>
86
<directory name="subdir"
87
file_id="foo-20050801201819-4139aa4a272f4250"
89
revision="mbp@foo-00"/>
90
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
91
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
93
text_sha1="B" text_size="0"/>
97
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
98
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
99
name="bar" parent_id="TREE_ROOT"
100
revision="mbp@foo-123123"/>
101
<directory name="subdir"
102
file_id="foo-20050801201819-4139aa4a272f4250"
103
parent_id="TREE_ROOT"
104
revision="mbp@foo-00"/>
105
<file file_id="bar-20050824000535-6bc48cfad47ed134"
106
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
107
revision="mbp@foo-00"/>
112
# DO NOT REFLOW THIS. Its the exact revision we want.
113
_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">
114
<message>- start splitting code for xml (de)serialization away from objects
115
preparatory to supporting multiple formats by a single library
118
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
124
# DO NOT REFLOW THIS. Its the exact inventory we want.
125
_expected_inv_v5 = """<inventory format="5">
126
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
127
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
128
<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" />
133
_expected_inv_v5_root = """<inventory file_id="f<" format="5" revision_id="mother!">
134
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f<" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
135
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f<" revision="mbp@foo-00" />
136
<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" />
137
<symlink file_id="link-1" name="link" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" symlink_target="a" />
141
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
142
<directory file_id="tree-root-321" name="" revision="rev_outer" />
143
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
144
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
145
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
146
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
150
_revision_utf8_v5 = """<revision committer="Erik Bågfors <erik@foo.net>"
151
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
152
revision_id="erik@bågfors-02"
153
timestamp="1125907235.212"
155
<message>Include µnicode characters
158
<revision_ref revision_id="erik@bågfors-01"/>
163
_inventory_utf8_v5 = """<inventory file_id="TREé_ROOT" format="5"
164
revision_id="erik@bågfors-02">
165
<file file_id="bår-01"
166
name="bår" parent_id="TREé_ROOT"
167
revision="erik@bågfors-01"/>
168
<directory name="sµbdir"
169
file_id="sµbdir-01"
170
parent_id="TREé_ROOT"
171
revision="erik@bågfors-01"/>
172
<file executable="yes" file_id="bår-02"
173
name="bår" parent_id="sµbdir-01"
174
revision="erik@bågfors-02"/>
178
# Before revision_id was always stored as an attribute
179
_inventory_v5a = """<inventory format="5">
183
# Before revision_id was always stored as an attribute
184
_inventory_v5b = """<inventory format="5" revision_id="a-rev-id">
189
class TestSerializer(TestCase):
190
"""Test XML serialization"""
192
def test_canned_inventory(self):
193
"""Test unpacked a canned inventory v4 file."""
194
inp = StringIO(_working_inventory_v4)
195
inv = serializer_v4.read_inventory(inp)
196
self.assertEqual(len(inv), 4)
197
self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
199
def test_unpack_revision(self):
200
"""Test unpacking a canned revision v4"""
201
inp = StringIO(_revision_v4)
202
rev = serializer_v4.read_revision(inp)
203
eq = self.assertEqual
205
"Martin Pool <mbp@sourcefrog.net>")
207
"mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
208
eq(len(rev.parent_ids), 1)
209
eq(rev.parent_ids[0],
210
"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
212
def test_unpack_revision_5(self):
213
"""Test unpacking a canned revision v5"""
214
inp = StringIO(_revision_v5)
215
rev = bzrlib.xml5.serializer_v5.read_revision(inp)
216
eq = self.assertEqual
218
"Martin Pool <mbp@sourcefrog.net>")
219
eq(len(rev.parent_ids), 1)
220
eq(rev.timezone, 36000)
221
eq(rev.parent_ids[0],
222
"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
224
def test_unpack_revision_5_utc(self):
225
inp = StringIO(_revision_v5_utc)
226
rev = bzrlib.xml5.serializer_v5.read_revision(inp)
227
eq = self.assertEqual
229
"Martin Pool <mbp@sourcefrog.net>")
230
eq(len(rev.parent_ids), 1)
232
eq(rev.parent_ids[0],
233
"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
235
def test_unpack_inventory_5(self):
236
"""Unpack canned new-style inventory"""
237
inp = StringIO(_committed_inv_v5)
238
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
239
eq = self.assertEqual
241
ie = inv['bar-20050824000535-6bc48cfad47ed134']
243
eq(ie.revision, 'mbp@foo-00')
245
eq(inv[ie.parent_id].kind, 'directory')
247
def test_unpack_basis_inventory_5(self):
248
"""Unpack canned new-style inventory"""
249
inp = StringIO(_basis_inv_v5)
250
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
251
eq = self.assertEqual
253
eq(inv.revision_id, 'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
254
ie = inv['bar-20050824000535-6bc48cfad47ed134']
256
eq(ie.revision, 'mbp@foo-00')
258
eq(inv[ie.parent_id].kind, 'directory')
260
def test_unpack_inventory_5a(self):
261
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
262
_inventory_v5a, revision_id='test-rev-id')
263
self.assertEqual('test-rev-id', inv.root.revision)
265
def test_unpack_inventory_5b(self):
266
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
267
_inventory_v5b, revision_id='test-rev-id')
268
self.assertEqual('a-rev-id', inv.root.revision)
270
def test_repack_inventory_5(self):
271
inp = StringIO(_committed_inv_v5)
272
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
274
bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
275
self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
276
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
277
self.assertEqual(inv, inv2)
279
def assertRoundTrips(self, xml_string):
280
inp = StringIO(xml_string)
281
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
283
bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
284
self.assertEqualDiff(xml_string, outp.getvalue())
285
lines = bzrlib.xml5.serializer_v5.write_inventory_to_lines(inv)
287
self.assertEqual(outp.readlines(), lines)
288
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
289
self.assertEqual(inv, inv2)
291
def tests_serialize_inventory_v5_with_root(self):
292
self.assertRoundTrips(_expected_inv_v5_root)
294
def check_repack_revision(self, txt):
295
"""Check that repacking a revision yields the same information"""
297
rev = bzrlib.xml5.serializer_v5.read_revision(inp)
299
bzrlib.xml5.serializer_v5.write_revision(rev, outp)
300
outfile_contents = outp.getvalue()
301
rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
302
self.assertEqual(rev, rev2)
304
def test_repack_revision_5(self):
305
"""Round-trip revision to XML v5"""
306
self.check_repack_revision(_revision_v5)
308
def test_repack_revision_5_utc(self):
309
self.check_repack_revision(_revision_v5_utc)
311
def test_pack_revision_5(self):
312
"""Pack revision to XML v5"""
313
# fixed 20051025, revisions should have final newline
314
rev = bzrlib.xml5.serializer_v5.read_revision_from_string(_revision_v5)
316
bzrlib.xml5.serializer_v5.write_revision(rev, outp)
317
outfile_contents = outp.getvalue()
318
self.assertEqual(outfile_contents[-1], '\n')
319
self.assertEqualDiff(outfile_contents, bzrlib.xml5.serializer_v5.write_revision_to_string(rev))
320
self.assertEqualDiff(outfile_contents, _expected_rev_v5)
322
def test_empty_property_value(self):
323
"""Create an empty property value check that it serializes correctly"""
324
s_v5 = bzrlib.xml5.serializer_v5
325
rev = s_v5.read_revision_from_string(_revision_v5)
327
props = {'empty':'', 'one':'one'}
328
rev.properties = props
329
txt = s_v5.write_revision_to_string(rev)
330
new_rev = s_v5.read_revision_from_string(txt)
331
self.assertEqual(props, new_rev.properties)
333
def test_roundtrip_inventory_v7(self):
334
inv = Inventory('tree-root-321', revision_id='rev_outer')
335
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
336
'rev_outer', 'rev_inner'))
337
inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
338
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
340
inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
341
inv['tree-root-321'].revision = 'rev_outer'
342
inv['dir-id'].revision = 'rev_outer'
343
inv['file-id'].revision = 'rev_outer'
344
inv['file-id'].text_sha1 = 'A'
345
inv['file-id'].text_size = 1
346
inv['link-id'].revision = 'rev_outer'
347
inv['link-id'].symlink_target = 'a'
348
txt = xml7.serializer_v7.write_inventory_to_string(inv)
349
lines = xml7.serializer_v7.write_inventory_to_lines(inv)
350
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
351
self.assertEqualDiff(_expected_inv_v7, txt)
352
inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
353
self.assertEqual(5, len(inv2))
354
for path, ie in inv.iter_entries():
355
self.assertEqual(ie, inv2[ie.file_id])
357
def test_wrong_format_v7(self):
358
"""Can't accidentally open a file with wrong serializer"""
359
s_v6 = bzrlib.xml6.serializer_v6
360
s_v7 = xml7.serializer_v7
361
self.assertRaises(errors.UnexpectedInventoryFormat,
362
s_v7.read_inventory_from_string, _expected_inv_v5)
363
self.assertRaises(errors.UnexpectedInventoryFormat,
364
s_v6.read_inventory_from_string, _expected_inv_v7)
366
def test_tree_reference(self):
367
s_v5 = bzrlib.xml5.serializer_v5
368
s_v6 = bzrlib.xml6.serializer_v6
369
s_v7 = xml7.serializer_v7
370
inv = Inventory('tree-root-321', revision_id='rev-outer')
371
inv.root.revision = 'root-rev'
372
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
373
'rev-outer', 'rev-inner'))
374
self.assertRaises(errors.UnsupportedInventoryKind,
375
s_v5.write_inventory_to_string, inv)
376
self.assertRaises(errors.UnsupportedInventoryKind,
377
s_v6.write_inventory_to_string, inv)
378
txt = s_v7.write_inventory_to_string(inv)
379
lines = s_v7.write_inventory_to_lines(inv)
380
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
381
inv2 = s_v7.read_inventory_from_string(txt)
382
self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
383
self.assertEqual('rev-outer', inv2['nested-id'].revision)
384
self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
385
self.assertRaises(errors.UnsupportedInventoryKind,
386
s_v6.read_inventory_from_string,
387
txt.replace('format="7"', 'format="6"'))
388
self.assertRaises(errors.UnsupportedInventoryKind,
389
s_v5.read_inventory_from_string,
390
txt.replace('format="7"', 'format="5"'))
392
def test_revision_ids_are_utf8(self):
393
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
394
s_v5 = bzrlib.xml5.serializer_v5
395
rev = s_v5.read_revision_from_string(_revision_utf8_v5)
396
self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
397
self.assertIsInstance(rev.revision_id, str)
398
self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
399
for parent_id in rev.parent_ids:
400
self.assertIsInstance(parent_id, str)
401
self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
402
self.assertIsInstance(rev.message, unicode)
404
# ie.revision should either be None or a utf-8 revision id
405
inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
406
rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
407
rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
408
fid_root = u'TRE\xe9_ROOT'.encode('utf8')
409
fid_bar1 = u'b\xe5r-01'.encode('utf8')
410
fid_sub = u's\xb5bdir-01'.encode('utf8')
411
fid_bar2 = u'b\xe5r-02'.encode('utf8')
412
expected = [(u'', fid_root, None, rev_id_2),
413
(u'b\xe5r', fid_bar1, fid_root, rev_id_1),
414
(u's\xb5bdir', fid_sub, fid_root, rev_id_1),
415
(u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
417
self.assertEqual(rev_id_2, inv.revision_id)
418
self.assertIsInstance(inv.revision_id, str)
420
actual = list(inv.iter_entries_by_dir())
421
for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
422
(act_path, act_ie)) in zip(expected, actual):
423
self.assertEqual(exp_path, act_path)
424
self.assertIsInstance(act_path, unicode)
425
self.assertEqual(exp_file_id, act_ie.file_id)
426
self.assertIsInstance(act_ie.file_id, str)
427
self.assertEqual(exp_parent_id, act_ie.parent_id)
428
if exp_parent_id is not None:
429
self.assertIsInstance(act_ie.parent_id, str)
430
self.assertEqual(exp_rev_id, act_ie.revision)
431
if exp_rev_id is not None:
432
self.assertIsInstance(act_ie.revision, str)
434
self.assertEqual(len(expected), len(actual))
436
def test_registry(self):
437
self.assertIs(serializer_v4,
438
xml_serializer.format_registry.get('4'))
439
self.assertIs(bzrlib.xml5.serializer_v5,
440
xml_serializer.format_registry.get('5'))
441
self.assertIs(bzrlib.xml6.serializer_v6,
442
xml_serializer.format_registry.get('6'))
443
self.assertIs(bzrlib.xml7.serializer_v7,
444
xml_serializer.format_registry.get('7'))
447
class TestEncodeAndEscape(TestCase):
448
"""Whitebox testing of the _encode_and_escape function."""
451
# Keep the cache clear before and after the test
452
bzrlib.xml5._ensure_utf8_re()
453
bzrlib.xml5._clear_cache()
454
self.addCleanup(bzrlib.xml5._clear_cache)
456
def test_simple_ascii(self):
457
# _encode_and_escape always appends a final ", because these parameters
458
# are being used in xml attributes, and by returning it now, we have to
459
# do fewer string operations later.
460
val = bzrlib.xml5._encode_and_escape('foo bar')
461
self.assertEqual('foo bar"', val)
462
# The second time should be cached
463
val2 = bzrlib.xml5._encode_and_escape('foo bar')
464
self.assertIs(val2, val)
466
def test_ascii_with_xml(self):
467
self.assertEqual('&'"<>"',
468
bzrlib.xml5._encode_and_escape('&\'"<>'))
470
def test_utf8_with_xml(self):
472
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
473
self.assertEqual('µå&ج"',
474
bzrlib.xml5._encode_and_escape(utf8_str))
476
def test_unicode(self):
477
uni_str = u'\xb5\xe5&\u062c'
478
self.assertEqual('µå&ج"',
479
bzrlib.xml5._encode_and_escape(uni_str))