1
# Copyright (C) 2005-2011 Canonical Ltd
1
# Copyright (C) 2005 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
19
19
from bzrlib import (
27
26
from bzrlib.tests import TestCase
28
from bzrlib.inventory import Inventory
27
from bzrlib.inventory import Inventory, InventoryEntry
28
from bzrlib.xml4 import serializer_v4
31
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
32
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
33
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
34
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
38
_revision_v4 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
39
inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
40
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
41
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
42
timestamp="1125907235.212"
44
<message>- start splitting code for xml (de)serialization away from objects
45
preparatory to supporting multiple formats by a single library
48
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
31
53
_revision_v5 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
32
54
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
33
55
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
120
_expected_inv_v6 = """<inventory format="6" revision_id="rev_outer">
121
<directory file_id="tree-root-321" name="" revision="rev_outer" />
122
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
123
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
124
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
128
142
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
129
143
<directory file_id="tree-root-321" name="" revision="rev_outer" />
130
144
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
194
208
class TestSerializer(TestCase):
195
209
"""Test XML serialization"""
211
def test_canned_inventory(self):
212
"""Test unpacked a canned inventory v4 file."""
213
inp = StringIO(_working_inventory_v4)
214
inv = serializer_v4.read_inventory(inp)
215
self.assertEqual(len(inv), 4)
216
self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
218
def test_unpack_revision(self):
219
"""Test unpacking a canned revision v4"""
220
inp = StringIO(_revision_v4)
221
rev = serializer_v4.read_revision(inp)
222
eq = self.assertEqual
224
"Martin Pool <mbp@sourcefrog.net>")
226
"mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
227
eq(len(rev.parent_ids), 1)
228
eq(rev.parent_ids[0],
229
"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
197
231
def test_unpack_revision_5(self):
198
232
"""Test unpacking a canned revision v5"""
199
233
inp = StringIO(_revision_v5)
247
281
_inventory_v5a, revision_id='test-rev-id')
248
282
self.assertEqual('test-rev-id', inv.root.revision)
250
def test_unpack_inventory_5a_cache_and_copy(self):
251
# Passing an entry_cache should get populated with the objects
252
# But the returned objects should be copies if return_from_cache is
254
entry_cache = fifo_cache.FIFOCache()
255
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
256
_inventory_v5a, revision_id='test-rev-id',
257
entry_cache=entry_cache, return_from_cache=False)
258
for entry in inv.iter_just_entries():
259
key = (entry.file_id, entry.revision)
260
if entry.file_id is inv.root.file_id:
261
# The root id is inferred for xml v5
262
self.assertFalse(key in entry_cache)
264
self.assertIsNot(entry, entry_cache[key])
266
def test_unpack_inventory_5a_cache_no_copy(self):
267
# Passing an entry_cache should get populated with the objects
268
# The returned objects should be exact if return_from_cache is
270
entry_cache = fifo_cache.FIFOCache()
271
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
272
_inventory_v5a, revision_id='test-rev-id',
273
entry_cache=entry_cache, return_from_cache=True)
274
for entry in inv.iter_just_entries():
275
key = (entry.file_id, entry.revision)
276
if entry.file_id is inv.root.file_id:
277
# The root id is inferred for xml v5
278
self.assertFalse(key in entry_cache)
280
self.assertIs(entry, entry_cache[key])
282
284
def test_unpack_inventory_5b(self):
283
285
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
284
286
_inventory_v5b, revision_id='test-rev-id')
375
377
for path, ie in inv.iter_entries():
376
378
self.assertEqual(ie, inv2[ie.file_id])
378
def test_roundtrip_inventory_v6(self):
379
inv = self.get_sample_inventory()
380
txt = xml6.serializer_v6.write_inventory_to_string(inv)
381
lines = xml6.serializer_v6.write_inventory_to_lines(inv)
382
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
383
self.assertEqualDiff(_expected_inv_v6, txt)
384
inv2 = xml6.serializer_v6.read_inventory_from_string(txt)
385
self.assertEqual(4, len(inv2))
386
for path, ie in inv.iter_entries():
387
self.assertEqual(ie, inv2[ie.file_id])
389
380
def test_wrong_format_v7(self):
390
381
"""Can't accidentally open a file with wrong serializer"""
391
382
s_v6 = bzrlib.xml6.serializer_v6
536
527
uni_str = u'\xb5\xe5&\u062c'
537
528
self.assertEqual('µå&ج"',
538
529
bzrlib.xml8._encode_and_escape(uni_str))
541
class TestMisc(TestCase):
543
def test_unescape_xml(self):
544
"""We get some kind of error when malformed entities are passed"""
545
self.assertRaises(KeyError, bzrlib.xml8._unescape_xml, 'foo&bar;')