13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
from cStringIO import StringIO
19
19
from bzrlib import (
26
28
from bzrlib.tests import TestCase
27
29
from bzrlib.inventory import Inventory, InventoryEntry
82
84
_committed_inv_v5 = """<inventory>
83
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
84
name="bar" parent_id="TREE_ROOT"
85
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
86
name="bar" parent_id="TREE_ROOT"
85
87
revision="mbp@foo-123123"
86
88
text_sha1="A" text_size="1"/>
87
89
<directory name="subdir"
88
90
file_id="foo-20050801201819-4139aa4a272f4250"
90
92
revision="mbp@foo-00"/>
91
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
92
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
93
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
94
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
93
95
revision="mbp@foo-00"
94
96
text_sha1="B" text_size="0"/>
98
100
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
99
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
100
name="bar" parent_id="TREE_ROOT"
101
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
102
name="bar" parent_id="TREE_ROOT"
101
103
revision="mbp@foo-123123"/>
102
104
<directory name="subdir"
103
105
file_id="foo-20050801201819-4139aa4a272f4250"
104
parent_id="TREE_ROOT"
106
parent_id="TREE_ROOT"
105
107
revision="mbp@foo-00"/>
106
<file file_id="bar-20050824000535-6bc48cfad47ed134"
107
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
108
<file file_id="bar-20050824000535-6bc48cfad47ed134"
109
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
108
110
revision="mbp@foo-00"/>
144
_expected_inv_v6 = """<inventory format="6" revision_id="rev_outer">
145
<directory file_id="tree-root-321" name="" revision="rev_outer" />
146
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
147
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
148
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
142
152
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
143
153
<directory file_id="tree-root-321" name="" revision="rev_outer" />
144
154
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
281
291
_inventory_v5a, revision_id='test-rev-id')
282
292
self.assertEqual('test-rev-id', inv.root.revision)
294
def test_unpack_inventory_5a_cache_and_copy(self):
295
# Passing an entry_cache should get populated with the objects
296
# But the returned objects should be copies if return_from_cache is
298
entry_cache = fifo_cache.FIFOCache()
299
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
300
_inventory_v5a, revision_id='test-rev-id',
301
entry_cache=entry_cache, return_from_cache=False)
302
for entry in inv.iter_just_entries():
303
key = (entry.file_id, entry.revision)
304
if entry.file_id is inv.root.file_id:
305
# The root id is inferred for xml v5
306
self.assertFalse(key in entry_cache)
308
self.assertIsNot(entry, entry_cache[key])
310
def test_unpack_inventory_5a_cache_no_copy(self):
311
# Passing an entry_cache should get populated with the objects
312
# The returned objects should be exact if return_from_cache is
314
entry_cache = fifo_cache.FIFOCache()
315
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
316
_inventory_v5a, revision_id='test-rev-id',
317
entry_cache=entry_cache, return_from_cache=True)
318
for entry in inv.iter_just_entries():
319
key = (entry.file_id, entry.revision)
320
if entry.file_id is inv.root.file_id:
321
# The root id is inferred for xml v5
322
self.assertFalse(key in entry_cache)
324
self.assertIs(entry, entry_cache[key])
284
326
def test_unpack_inventory_5b(self):
285
327
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
286
328
_inventory_v5b, revision_id='test-rev-id')
294
336
self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
295
337
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
296
338
self.assertEqual(inv, inv2)
298
340
def assertRoundTrips(self, xml_string):
299
341
inp = StringIO(xml_string)
300
342
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
352
394
def get_sample_inventory(self):
353
395
inv = Inventory('tree-root-321', revision_id='rev_outer')
354
396
inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
355
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
397
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
356
398
'tree-root-321'))
357
399
inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
358
400
inv['tree-root-321'].revision = 'rev_outer'
377
419
for path, ie in inv.iter_entries():
378
420
self.assertEqual(ie, inv2[ie.file_id])
422
def test_roundtrip_inventory_v6(self):
423
inv = self.get_sample_inventory()
424
txt = xml6.serializer_v6.write_inventory_to_string(inv)
425
lines = xml6.serializer_v6.write_inventory_to_lines(inv)
426
self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
427
self.assertEqualDiff(_expected_inv_v6, txt)
428
inv2 = xml6.serializer_v6.read_inventory_from_string(txt)
429
self.assertEqual(4, len(inv2))
430
for path, ie in inv.iter_entries():
431
self.assertEqual(ie, inv2[ie.file_id])
380
433
def test_wrong_format_v7(self):
381
434
"""Can't accidentally open a file with wrong serializer"""
382
435
s_v6 = bzrlib.xml6.serializer_v6
383
436
s_v7 = xml7.serializer_v7
384
self.assertRaises(errors.UnexpectedInventoryFormat,
437
self.assertRaises(errors.UnexpectedInventoryFormat,
385
438
s_v7.read_inventory_from_string, _expected_inv_v5)
386
self.assertRaises(errors.UnexpectedInventoryFormat,
439
self.assertRaises(errors.UnexpectedInventoryFormat,
387
440
s_v6.read_inventory_from_string, _expected_inv_v7)
389
442
def test_tree_reference(self):
405
458
self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
406
459
self.assertEqual('rev-outer', inv2['nested-id'].revision)
407
460
self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
408
self.assertRaises(errors.UnsupportedInventoryKind,
461
self.assertRaises(errors.UnsupportedInventoryKind,
409
462
s_v6.read_inventory_from_string,
410
463
txt.replace('format="7"', 'format="6"'))
411
self.assertRaises(errors.UnsupportedInventoryKind,
464
self.assertRaises(errors.UnsupportedInventoryKind,
412
465
s_v5.read_inventory_from_string,
413
466
txt.replace('format="7"', 'format="5"'))
493
546
self.assertEqual(len(expected), len(actual))
495
def test_registry(self):
496
self.assertIs(serializer_v4,
497
xml_serializer.format_registry.get('4'))
498
self.assertIs(bzrlib.xml5.serializer_v5,
499
xml_serializer.format_registry.get('5'))
500
self.assertIs(bzrlib.xml6.serializer_v6,
501
xml_serializer.format_registry.get('6'))
502
self.assertIs(bzrlib.xml7.serializer_v7,
503
xml_serializer.format_registry.get('7'))
504
self.assertIs(bzrlib.xml8.serializer_v8,
505
xml_serializer.format_registry.get('8'))
508
549
class TestEncodeAndEscape(TestCase):
509
550
"""Whitebox testing of the _encode_and_escape function."""
512
554
# Keep the cache clear before and after the test
513
555
bzrlib.xml8._ensure_utf8_re()
514
556
bzrlib.xml8._clear_cache()