~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

  • Committer: Robert Collins
  • Date: 2009-07-07 04:32:13 UTC
  • mto: This revision was merged to the branch mainline in revision 4524.
  • Revision ID: robertc@robertcollins.net-20090707043213-4hjjhgr40iq7gk2d
More informative assertions in xml serialisation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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
16
16
 
17
17
from cStringIO import StringIO
18
18
 
19
19
from bzrlib import (
20
 
    errors, 
21
 
    inventory, 
 
20
    errors,
 
21
    inventory,
22
22
    xml7,
 
23
    xml8,
 
24
    serializer,
23
25
    )
24
26
from bzrlib.tests import TestCase
25
27
from bzrlib.inventory import Inventory, InventoryEntry
78
80
"""
79
81
 
80
82
_committed_inv_v5 = """<inventory>
81
 
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
82
 
      name="bar" parent_id="TREE_ROOT" 
83
 
      revision="mbp@foo-123123"/>
 
83
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
 
84
      name="bar" parent_id="TREE_ROOT"
 
85
      revision="mbp@foo-123123"
 
86
      text_sha1="A" text_size="1"/>
84
87
<directory name="subdir"
85
88
           file_id="foo-20050801201819-4139aa4a272f4250"
86
 
           parent_id="TREE_ROOT" 
 
89
           parent_id="TREE_ROOT"
87
90
           revision="mbp@foo-00"/>
88
 
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" 
89
 
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
90
 
      revision="mbp@foo-00"/>
 
91
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
 
92
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
 
93
      revision="mbp@foo-00"
 
94
      text_sha1="B" text_size="0"/>
91
95
</inventory>
92
96
"""
93
97
 
94
98
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
95
 
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
96
 
      name="bar" parent_id="TREE_ROOT" 
 
99
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
 
100
      name="bar" parent_id="TREE_ROOT"
97
101
      revision="mbp@foo-123123"/>
98
102
<directory name="subdir"
99
103
           file_id="foo-20050801201819-4139aa4a272f4250"
100
 
           parent_id="TREE_ROOT" 
 
104
           parent_id="TREE_ROOT"
101
105
           revision="mbp@foo-00"/>
102
 
<file file_id="bar-20050824000535-6bc48cfad47ed134" 
103
 
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
 
106
<file file_id="bar-20050824000535-6bc48cfad47ed134"
 
107
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
104
108
      revision="mbp@foo-00"/>
105
109
</inventory>
106
110
"""
120
124
 
121
125
# DO NOT REFLOW THIS. Its the exact inventory we want.
122
126
_expected_inv_v5 = """<inventory format="5">
123
 
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" />
 
127
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
124
128
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
125
 
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
 
129
<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" />
126
130
</inventory>
127
131
"""
128
132
 
129
133
 
130
134
_expected_inv_v5_root = """<inventory file_id="f&lt;" format="5" revision_id="mother!">
131
 
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" />
 
135
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
132
136
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f&lt;" revision="mbp@foo-00" />
133
 
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
 
137
<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" />
 
138
<symlink file_id="link-1" name="link" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" symlink_target="a" />
134
139
</inventory>
135
140
"""
136
141
 
137
142
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
138
143
<directory file_id="tree-root-321" name="" revision="rev_outer" />
139
144
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
140
 
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" />
141
 
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" />
 
145
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
 
146
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
142
147
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
143
148
</inventory>
144
149
"""
145
150
 
 
151
_expected_rev_v8 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;" format="8" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
 
152
<message>- start splitting code for xml (de)serialization away from objects
 
153
  preparatory to supporting multiple formats by a single library
 
154
</message>
 
155
<parents>
 
156
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
 
157
</parents>
 
158
</revision>
 
159
"""
 
160
 
 
161
_expected_inv_v8 = """<inventory format="8" revision_id="rev_outer">
 
162
<directory file_id="tree-root-321" name="" revision="rev_outer" />
 
163
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
 
164
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
 
165
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
 
166
</inventory>
 
167
"""
 
168
 
146
169
_revision_utf8_v5 = """<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
147
170
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
148
171
    revision_id="erik@b&#229;gfors-02"
171
194
</inventory>
172
195
"""
173
196
 
 
197
# Before revision_id was always stored as an attribute
 
198
_inventory_v5a = """<inventory format="5">
 
199
</inventory>
 
200
"""
 
201
 
 
202
# Before revision_id was always stored as an attribute
 
203
_inventory_v5b = """<inventory format="5" revision_id="a-rev-id">
 
204
</inventory>
 
205
"""
 
206
 
174
207
 
175
208
class TestSerializer(TestCase):
176
209
    """Test XML serialization"""
 
210
 
177
211
    def test_canned_inventory(self):
178
212
        """Test unpacked a canned inventory v4 file."""
179
213
        inp = StringIO(_working_inventory_v4)
242
276
        eq(ie.name, 'bar')
243
277
        eq(inv[ie.parent_id].kind, 'directory')
244
278
 
 
279
    def test_unpack_inventory_5a(self):
 
280
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
 
281
                _inventory_v5a, revision_id='test-rev-id')
 
282
        self.assertEqual('test-rev-id', inv.root.revision)
 
283
 
 
284
    def test_unpack_inventory_5b(self):
 
285
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
 
286
                _inventory_v5b, revision_id='test-rev-id')
 
287
        self.assertEqual('a-rev-id', inv.root.revision)
 
288
 
245
289
    def test_repack_inventory_5(self):
246
290
        inp = StringIO(_committed_inv_v5)
247
291
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
250
294
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
251
295
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
252
296
        self.assertEqual(inv, inv2)
253
 
    
 
297
 
254
298
    def assertRoundTrips(self, xml_string):
255
299
        inp = StringIO(xml_string)
256
300
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
257
301
        outp = StringIO()
258
302
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
259
303
        self.assertEqualDiff(xml_string, outp.getvalue())
 
304
        lines = bzrlib.xml5.serializer_v5.write_inventory_to_lines(inv)
 
305
        outp.seek(0)
 
306
        self.assertEqual(outp.readlines(), lines)
260
307
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
261
308
        self.assertEqual(inv, inv2)
262
309
 
302
349
        new_rev = s_v5.read_revision_from_string(txt)
303
350
        self.assertEqual(props, new_rev.properties)
304
351
 
305
 
    def test_roundtrip_inventory_v7(self):
 
352
    def get_sample_inventory(self):
306
353
        inv = Inventory('tree-root-321', revision_id='rev_outer')
307
 
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
308
 
                                        'rev_outer', 'rev_inner'))
309
354
        inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
310
 
        inv.add(inventory.InventoryDirectory('dir-id', 'dir', 
 
355
        inv.add(inventory.InventoryDirectory('dir-id', 'dir',
311
356
                                             'tree-root-321'))
312
357
        inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
313
358
        inv['tree-root-321'].revision = 'rev_outer'
314
359
        inv['dir-id'].revision = 'rev_outer'
315
360
        inv['file-id'].revision = 'rev_outer'
 
361
        inv['file-id'].text_sha1 = 'A'
 
362
        inv['file-id'].text_size = 1
316
363
        inv['link-id'].revision = 'rev_outer'
 
364
        inv['link-id'].symlink_target = 'a'
 
365
        return inv
 
366
 
 
367
    def test_roundtrip_inventory_v7(self):
 
368
        inv = self.get_sample_inventory()
 
369
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
 
370
                                        'rev_outer', 'rev_inner'))
317
371
        txt = xml7.serializer_v7.write_inventory_to_string(inv)
 
372
        lines = xml7.serializer_v7.write_inventory_to_lines(inv)
 
373
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
318
374
        self.assertEqualDiff(_expected_inv_v7, txt)
319
375
        inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
320
376
        self.assertEqual(5, len(inv2))
325
381
        """Can't accidentally open a file with wrong serializer"""
326
382
        s_v6 = bzrlib.xml6.serializer_v6
327
383
        s_v7 = xml7.serializer_v7
328
 
        self.assertRaises(errors.UnexpectedInventoryFormat, 
 
384
        self.assertRaises(errors.UnexpectedInventoryFormat,
329
385
                          s_v7.read_inventory_from_string, _expected_inv_v5)
330
 
        self.assertRaises(errors.UnexpectedInventoryFormat, 
 
386
        self.assertRaises(errors.UnexpectedInventoryFormat,
331
387
                          s_v6.read_inventory_from_string, _expected_inv_v7)
332
388
 
333
389
    def test_tree_reference(self):
334
390
        s_v5 = bzrlib.xml5.serializer_v5
335
391
        s_v6 = bzrlib.xml6.serializer_v6
336
392
        s_v7 = xml7.serializer_v7
337
 
        inv = Inventory('tree-root-321')
 
393
        inv = Inventory('tree-root-321', revision_id='rev-outer')
 
394
        inv.root.revision = 'root-rev'
338
395
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
339
396
                                        'rev-outer', 'rev-inner'))
340
 
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
397
        self.assertRaises(errors.UnsupportedInventoryKind,
341
398
                          s_v5.write_inventory_to_string, inv)
342
 
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
399
        self.assertRaises(errors.UnsupportedInventoryKind,
343
400
                          s_v6.write_inventory_to_string, inv)
344
401
        txt = s_v7.write_inventory_to_string(inv)
 
402
        lines = s_v7.write_inventory_to_lines(inv)
 
403
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
345
404
        inv2 = s_v7.read_inventory_from_string(txt)
346
405
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
347
406
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
348
407
        self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
349
 
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
408
        self.assertRaises(errors.UnsupportedInventoryKind,
350
409
                          s_v6.read_inventory_from_string,
351
410
                          txt.replace('format="7"', 'format="6"'))
352
 
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
411
        self.assertRaises(errors.UnsupportedInventoryKind,
353
412
                          s_v5.read_inventory_from_string,
354
413
                          txt.replace('format="7"', 'format="5"'))
355
414
 
 
415
    def test_roundtrip_inventory_v8(self):
 
416
        inv = self.get_sample_inventory()
 
417
        txt = xml8.serializer_v8.write_inventory_to_string(inv)
 
418
        inv2 = xml8.serializer_v8.read_inventory_from_string(txt)
 
419
        self.assertEqual(4, len(inv2))
 
420
        for path, ie in inv.iter_entries():
 
421
            self.assertEqual(ie, inv2[ie.file_id])
 
422
 
 
423
    def test_inventory_text_v8(self):
 
424
        inv = self.get_sample_inventory()
 
425
        txt = xml8.serializer_v8.write_inventory_to_string(inv)
 
426
        lines = xml8.serializer_v8.write_inventory_to_lines(inv)
 
427
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
 
428
        self.assertEqualDiff(_expected_inv_v8, txt)
 
429
 
 
430
    def test_revision_text_v6(self):
 
431
        """Pack revision to XML v6"""
 
432
        rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
 
433
            _expected_rev_v5)
 
434
        serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
 
435
        self.assertEqualDiff(serialized, _expected_rev_v5)
 
436
 
 
437
    def test_revision_text_v7(self):
 
438
        """Pack revision to XML v7"""
 
439
        rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
 
440
            _expected_rev_v5)
 
441
        serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
 
442
        self.assertEqualDiff(serialized, _expected_rev_v5)
 
443
 
 
444
    def test_revision_text_v8(self):
 
445
        """Pack revision to XML v8"""
 
446
        rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
 
447
            _expected_rev_v8)
 
448
        serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
 
449
        self.assertEqualDiff(serialized, _expected_rev_v8)
 
450
 
356
451
    def test_revision_ids_are_utf8(self):
357
452
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
358
453
        s_v5 = bzrlib.xml5.serializer_v5
373
468
        fid_bar1 = u'b\xe5r-01'.encode('utf8')
374
469
        fid_sub = u's\xb5bdir-01'.encode('utf8')
375
470
        fid_bar2 = u'b\xe5r-02'.encode('utf8')
376
 
        expected = [(u'', fid_root, None, None),
 
471
        expected = [(u'', fid_root, None, rev_id_2),
377
472
                    (u'b\xe5r', fid_bar1, fid_root, rev_id_1),
378
473
                    (u's\xb5bdir', fid_sub, fid_root, rev_id_1),
379
474
                    (u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
402
497
    """Whitebox testing of the _encode_and_escape function."""
403
498
 
404
499
    def setUp(self):
 
500
        TestCase.setUp(self)
405
501
        # Keep the cache clear before and after the test
406
 
        bzrlib.xml5._ensure_utf8_re()
407
 
        bzrlib.xml5._clear_cache()
408
 
        self.addCleanup(bzrlib.xml5._clear_cache)
 
502
        bzrlib.xml8._ensure_utf8_re()
 
503
        bzrlib.xml8._clear_cache()
 
504
        self.addCleanup(bzrlib.xml8._clear_cache)
409
505
 
410
506
    def test_simple_ascii(self):
411
507
        # _encode_and_escape always appends a final ", because these parameters
412
508
        # are being used in xml attributes, and by returning it now, we have to
413
509
        # do fewer string operations later.
414
 
        val = bzrlib.xml5._encode_and_escape('foo bar')
 
510
        val = bzrlib.xml8._encode_and_escape('foo bar')
415
511
        self.assertEqual('foo bar"', val)
416
512
        # The second time should be cached
417
 
        val2 = bzrlib.xml5._encode_and_escape('foo bar')
 
513
        val2 = bzrlib.xml8._encode_and_escape('foo bar')
418
514
        self.assertIs(val2, val)
419
515
 
420
516
    def test_ascii_with_xml(self):
421
517
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
422
 
                         bzrlib.xml5._encode_and_escape('&\'"<>'))
 
518
                         bzrlib.xml8._encode_and_escape('&\'"<>'))
423
519
 
424
520
    def test_utf8_with_xml(self):
425
521
        # u'\xb5\xe5&\u062c'
426
522
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
427
523
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
428
 
                         bzrlib.xml5._encode_and_escape(utf8_str))
 
524
                         bzrlib.xml8._encode_and_escape(utf8_str))
429
525
 
430
526
    def test_unicode(self):
431
527
        uni_str = u'\xb5\xe5&\u062c'
432
528
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
433
 
                         bzrlib.xml5._encode_and_escape(uni_str))
 
529
                         bzrlib.xml8._encode_and_escape(uni_str))