~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

  • Committer: Joe Julian
  • Date: 2010-01-10 02:25:31 UTC
  • mto: (4634.119.7 2.0)
  • mto: This revision was merged to the branch mainline in revision 4959.
  • Revision ID: joe@julianfamily.org-20100110022531-wqk61rsagz8xsiga
Added MANIFEST.in to allow bdist_rpm to have all the required include files and tools. bdist_rpm will still fail to build correctly on some distributions due to a disttools bug http://bugs.python.org/issue644744

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#
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
18
18
 
19
19
from bzrlib import (
20
20
    errors,
21
 
    fifo_cache,
22
21
    inventory,
23
22
    xml6,
24
23
    xml7,
25
24
    xml8,
 
25
    serializer,
26
26
    )
27
27
from bzrlib.tests import TestCase
28
 
from bzrlib.inventory import Inventory
 
28
from bzrlib.inventory import Inventory, InventoryEntry
 
29
from bzrlib.xml4 import serializer_v4
29
30
import bzrlib.xml5
30
31
 
 
32
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
 
33
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
 
34
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
 
35
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
 
36
</inventory>"""
 
37
 
 
38
 
 
39
_revision_v4 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
 
40
    inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
41
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
42
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
43
    timestamp="1125907235.212"
 
44
    timezone="36000">
 
45
<message>- start splitting code for xml (de)serialization away from objects
 
46
  preparatory to supporting multiple formats by a single library
 
47
</message>
 
48
<parents>
 
49
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
 
50
</parents>
 
51
</revision>
 
52
"""
 
53
 
31
54
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
32
55
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
33
56
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
194
217
class TestSerializer(TestCase):
195
218
    """Test XML serialization"""
196
219
 
 
220
    def test_canned_inventory(self):
 
221
        """Test unpacked a canned inventory v4 file."""
 
222
        inp = StringIO(_working_inventory_v4)
 
223
        inv = serializer_v4.read_inventory(inp)
 
224
        self.assertEqual(len(inv), 4)
 
225
        self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
 
226
 
 
227
    def test_unpack_revision(self):
 
228
        """Test unpacking a canned revision v4"""
 
229
        inp = StringIO(_revision_v4)
 
230
        rev = serializer_v4.read_revision(inp)
 
231
        eq = self.assertEqual
 
232
        eq(rev.committer,
 
233
           "Martin Pool <mbp@sourcefrog.net>")
 
234
        eq(rev.inventory_id,
 
235
           "mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
 
236
        eq(len(rev.parent_ids), 1)
 
237
        eq(rev.parent_ids[0],
 
238
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
 
239
 
197
240
    def test_unpack_revision_5(self):
198
241
        """Test unpacking a canned revision v5"""
199
242
        inp = StringIO(_revision_v5)
247
290
                _inventory_v5a, revision_id='test-rev-id')
248
291
        self.assertEqual('test-rev-id', inv.root.revision)
249
292
 
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
253
 
        # False
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)
263
 
            else:
264
 
                self.assertIsNot(entry, entry_cache[key])
265
 
 
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
269
 
        # True
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)
279
 
            else:
280
 
                self.assertIs(entry, entry_cache[key])
281
 
 
282
293
    def test_unpack_inventory_5b(self):
283
294
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
284
295
                _inventory_v5b, revision_id='test-rev-id')
508
519
    def setUp(self):
509
520
        TestCase.setUp(self)
510
521
        # Keep the cache clear before and after the test
511
 
        bzrlib.xml_serializer._clear_cache()
512
 
        self.addCleanup(bzrlib.xml_serializer._clear_cache)
 
522
        bzrlib.xml8._ensure_utf8_re()
 
523
        bzrlib.xml8._clear_cache()
 
524
        self.addCleanup(bzrlib.xml8._clear_cache)
513
525
 
514
526
    def test_simple_ascii(self):
515
527
        # _encode_and_escape always appends a final ", because these parameters
516
528
        # are being used in xml attributes, and by returning it now, we have to
517
529
        # do fewer string operations later.
518
 
        val = bzrlib.xml_serializer.encode_and_escape('foo bar')
 
530
        val = bzrlib.xml8._encode_and_escape('foo bar')
519
531
        self.assertEqual('foo bar"', val)
520
532
        # The second time should be cached
521
 
        val2 = bzrlib.xml_serializer.encode_and_escape('foo bar')
 
533
        val2 = bzrlib.xml8._encode_and_escape('foo bar')
522
534
        self.assertIs(val2, val)
523
535
 
524
536
    def test_ascii_with_xml(self):
525
537
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
526
 
                         bzrlib.xml_serializer.encode_and_escape('&\'"<>'))
 
538
                         bzrlib.xml8._encode_and_escape('&\'"<>'))
527
539
 
528
540
    def test_utf8_with_xml(self):
529
541
        # u'\xb5\xe5&\u062c'
530
542
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
531
543
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
532
 
                         bzrlib.xml_serializer.encode_and_escape(utf8_str))
 
544
                         bzrlib.xml8._encode_and_escape(utf8_str))
533
545
 
534
546
    def test_unicode(self):
535
547
        uni_str = u'\xb5\xe5&\u062c'
536
548
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
537
 
                         bzrlib.xml_serializer.encode_and_escape(uni_str))
538
 
 
539
 
 
540
 
class TestMisc(TestCase):
541
 
 
542
 
    def test_unescape_xml(self):
543
 
        """We get some kind of error when malformed entities are passed"""
544
 
        self.assertRaises(KeyError, bzrlib.xml8._unescape_xml, 'foo&bar;')
 
549
                         bzrlib.xml8._encode_and_escape(uni_str))