/breezy/unstable

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy/unstable

« back to all changes in this revision

Viewing changes to breezy/tests/test_xml.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-24 01:39:33 UTC
  • mfrom: (3815.3776.6)
  • Revision ID: jelmer@jelmer.uk-20170524013933-ir4y4tqtrsiz2ka2
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from cStringIO import StringIO
18
17
 
19
 
from bzrlib import (
 
18
from .. import (
20
19
    errors,
21
20
    fifo_cache,
22
21
    inventory,
24
23
    xml7,
25
24
    xml8,
26
25
    )
27
 
from bzrlib.tests import TestCase
28
 
from bzrlib.inventory import Inventory
29
 
import bzrlib.xml5
 
26
from ..sixish import (
 
27
    BytesIO,
 
28
    )
 
29
from ..inventory import Inventory
 
30
from . import TestCase
 
31
import breezy.xml5
30
32
 
31
33
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
32
34
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
196
198
 
197
199
    def test_unpack_revision_5(self):
198
200
        """Test unpacking a canned revision v5"""
199
 
        inp = StringIO(_revision_v5)
200
 
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
 
201
        inp = BytesIO(_revision_v5)
 
202
        rev = breezy.xml5.serializer_v5.read_revision(inp)
201
203
        eq = self.assertEqual
202
204
        eq(rev.committer,
203
205
           "Martin Pool <mbp@sourcefrog.net>")
207
209
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
208
210
 
209
211
    def test_unpack_revision_5_utc(self):
210
 
        inp = StringIO(_revision_v5_utc)
211
 
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
 
212
        inp = BytesIO(_revision_v5_utc)
 
213
        rev = breezy.xml5.serializer_v5.read_revision(inp)
212
214
        eq = self.assertEqual
213
215
        eq(rev.committer,
214
216
           "Martin Pool <mbp@sourcefrog.net>")
219
221
 
220
222
    def test_unpack_inventory_5(self):
221
223
        """Unpack canned new-style inventory"""
222
 
        inp = StringIO(_committed_inv_v5)
223
 
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
 
224
        inp = BytesIO(_committed_inv_v5)
 
225
        inv = breezy.xml5.serializer_v5.read_inventory(inp)
224
226
        eq = self.assertEqual
225
227
        eq(len(inv), 4)
226
228
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
231
233
 
232
234
    def test_unpack_basis_inventory_5(self):
233
235
        """Unpack canned new-style inventory"""
234
 
        inp = StringIO(_basis_inv_v5)
235
 
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
 
236
        inp = BytesIO(_basis_inv_v5)
 
237
        inv = breezy.xml5.serializer_v5.read_inventory(inp)
236
238
        eq = self.assertEqual
237
239
        eq(len(inv), 4)
238
240
        eq(inv.revision_id, 'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
243
245
        eq(inv[ie.parent_id].kind, 'directory')
244
246
 
245
247
    def test_unpack_inventory_5a(self):
246
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
 
248
        inv = breezy.xml5.serializer_v5.read_inventory_from_string(
247
249
                _inventory_v5a, revision_id='test-rev-id')
248
250
        self.assertEqual('test-rev-id', inv.root.revision)
249
251
 
252
254
        # But the returned objects should be copies if return_from_cache is
253
255
        # False
254
256
        entry_cache = fifo_cache.FIFOCache()
255
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
 
257
        inv = breezy.xml5.serializer_v5.read_inventory_from_string(
256
258
            _inventory_v5a, revision_id='test-rev-id',
257
259
            entry_cache=entry_cache, return_from_cache=False)
258
260
        for entry in inv.iter_just_entries():
268
270
        # The returned objects should be exact if return_from_cache is
269
271
        # True
270
272
        entry_cache = fifo_cache.FIFOCache()
271
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
 
273
        inv = breezy.xml5.serializer_v5.read_inventory_from_string(
272
274
            _inventory_v5a, revision_id='test-rev-id',
273
275
            entry_cache=entry_cache, return_from_cache=True)
274
276
        for entry in inv.iter_just_entries():
280
282
                self.assertIs(entry, entry_cache[key])
281
283
 
282
284
    def test_unpack_inventory_5b(self):
283
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
 
285
        inv = breezy.xml5.serializer_v5.read_inventory_from_string(
284
286
                _inventory_v5b, revision_id='test-rev-id')
285
287
        self.assertEqual('a-rev-id', inv.root.revision)
286
288
 
287
289
    def test_repack_inventory_5(self):
288
 
        inp = StringIO(_committed_inv_v5)
289
 
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
290
 
        outp = StringIO()
291
 
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
 
290
        inp = BytesIO(_committed_inv_v5)
 
291
        inv = breezy.xml5.serializer_v5.read_inventory(inp)
 
292
        outp = BytesIO()
 
293
        breezy.xml5.serializer_v5.write_inventory(inv, outp)
292
294
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
293
 
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
 
295
        inv2 = breezy.xml5.serializer_v5.read_inventory(BytesIO(outp.getvalue()))
294
296
        self.assertEqual(inv, inv2)
295
297
 
296
298
    def assertRoundTrips(self, xml_string):
297
 
        inp = StringIO(xml_string)
298
 
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
299
 
        outp = StringIO()
300
 
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
 
299
        inp = BytesIO(xml_string)
 
300
        inv = breezy.xml5.serializer_v5.read_inventory(inp)
 
301
        outp = BytesIO()
 
302
        breezy.xml5.serializer_v5.write_inventory(inv, outp)
301
303
        self.assertEqualDiff(xml_string, outp.getvalue())
302
 
        lines = bzrlib.xml5.serializer_v5.write_inventory_to_lines(inv)
 
304
        lines = breezy.xml5.serializer_v5.write_inventory_to_lines(inv)
303
305
        outp.seek(0)
304
306
        self.assertEqual(outp.readlines(), lines)
305
 
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
 
307
        inv2 = breezy.xml5.serializer_v5.read_inventory(BytesIO(outp.getvalue()))
306
308
        self.assertEqual(inv, inv2)
307
309
 
308
310
    def tests_serialize_inventory_v5_with_root(self):
310
312
 
311
313
    def check_repack_revision(self, txt):
312
314
        """Check that repacking a revision yields the same information"""
313
 
        inp = StringIO(txt)
314
 
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
315
 
        outp = StringIO()
316
 
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
 
315
        inp = BytesIO(txt)
 
316
        rev = breezy.xml5.serializer_v5.read_revision(inp)
 
317
        outp = BytesIO()
 
318
        breezy.xml5.serializer_v5.write_revision(rev, outp)
317
319
        outfile_contents = outp.getvalue()
318
 
        rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
 
320
        rev2 = breezy.xml5.serializer_v5.read_revision(BytesIO(outfile_contents))
319
321
        self.assertEqual(rev, rev2)
320
322
 
321
323
    def test_repack_revision_5(self):
328
330
    def test_pack_revision_5(self):
329
331
        """Pack revision to XML v5"""
330
332
        # fixed 20051025, revisions should have final newline
331
 
        rev = bzrlib.xml5.serializer_v5.read_revision_from_string(_revision_v5)
332
 
        outp = StringIO()
333
 
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
 
333
        rev = breezy.xml5.serializer_v5.read_revision_from_string(_revision_v5)
 
334
        outp = BytesIO()
 
335
        breezy.xml5.serializer_v5.write_revision(rev, outp)
334
336
        outfile_contents = outp.getvalue()
335
337
        self.assertEqual(outfile_contents[-1], '\n')
336
 
        self.assertEqualDiff(outfile_contents, bzrlib.xml5.serializer_v5.write_revision_to_string(rev))
 
338
        self.assertEqualDiff(outfile_contents, breezy.xml5.serializer_v5.write_revision_to_string(rev))
337
339
        self.assertEqualDiff(outfile_contents, _expected_rev_v5)
338
340
 
339
341
    def test_empty_property_value(self):
340
342
        """Create an empty property value check that it serializes correctly"""
341
 
        s_v5 = bzrlib.xml5.serializer_v5
 
343
        s_v5 = breezy.xml5.serializer_v5
342
344
        rev = s_v5.read_revision_from_string(_revision_v5)
343
 
        outp = StringIO()
 
345
        outp = BytesIO()
344
346
        props = {'empty':'', 'one':'one'}
345
347
        rev.properties = props
346
348
        txt = s_v5.write_revision_to_string(rev)
368
370
                                        'rev_outer', 'rev_inner'))
369
371
        txt = xml7.serializer_v7.write_inventory_to_string(inv)
370
372
        lines = xml7.serializer_v7.write_inventory_to_lines(inv)
371
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
 
373
        self.assertEqual(breezy.osutils.split_lines(txt), lines)
372
374
        self.assertEqualDiff(_expected_inv_v7, txt)
373
375
        inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
374
376
        self.assertEqual(5, len(inv2))
379
381
        inv = self.get_sample_inventory()
380
382
        txt = xml6.serializer_v6.write_inventory_to_string(inv)
381
383
        lines = xml6.serializer_v6.write_inventory_to_lines(inv)
382
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
 
384
        self.assertEqual(breezy.osutils.split_lines(txt), lines)
383
385
        self.assertEqualDiff(_expected_inv_v6, txt)
384
386
        inv2 = xml6.serializer_v6.read_inventory_from_string(txt)
385
387
        self.assertEqual(4, len(inv2))
388
390
 
389
391
    def test_wrong_format_v7(self):
390
392
        """Can't accidentally open a file with wrong serializer"""
391
 
        s_v6 = bzrlib.xml6.serializer_v6
 
393
        s_v6 = breezy.xml6.serializer_v6
392
394
        s_v7 = xml7.serializer_v7
393
395
        self.assertRaises(errors.UnexpectedInventoryFormat,
394
396
                          s_v7.read_inventory_from_string, _expected_inv_v5)
396
398
                          s_v6.read_inventory_from_string, _expected_inv_v7)
397
399
 
398
400
    def test_tree_reference(self):
399
 
        s_v5 = bzrlib.xml5.serializer_v5
400
 
        s_v6 = bzrlib.xml6.serializer_v6
 
401
        s_v5 = breezy.xml5.serializer_v5
 
402
        s_v6 = breezy.xml6.serializer_v6
401
403
        s_v7 = xml7.serializer_v7
402
404
        inv = Inventory('tree-root-321', revision_id='rev-outer')
403
405
        inv.root.revision = 'root-rev'
409
411
                          s_v6.write_inventory_to_string, inv)
410
412
        txt = s_v7.write_inventory_to_string(inv)
411
413
        lines = s_v7.write_inventory_to_lines(inv)
412
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
 
414
        self.assertEqual(breezy.osutils.split_lines(txt), lines)
413
415
        inv2 = s_v7.read_inventory_from_string(txt)
414
416
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
415
417
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
427
429
        inv = self.get_sample_inventory()
428
430
        txt = xml8.serializer_v8.write_inventory_to_string(inv)
429
431
        lines = xml8.serializer_v8.write_inventory_to_lines(inv)
430
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
 
432
        self.assertEqual(breezy.osutils.split_lines(txt), lines)
431
433
        self.assertEqualDiff(_expected_inv_v8, txt)
432
434
 
433
435
    def test_revision_text_v6(self):
434
436
        """Pack revision to XML v6"""
435
 
        rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
 
437
        rev = breezy.xml6.serializer_v6.read_revision_from_string(
436
438
            _expected_rev_v5)
437
 
        serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
 
439
        serialized = breezy.xml6.serializer_v6.write_revision_to_string(rev)
438
440
        self.assertEqualDiff(serialized, _expected_rev_v5)
439
441
 
440
442
    def test_revision_text_v7(self):
441
443
        """Pack revision to XML v7"""
442
 
        rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
 
444
        rev = breezy.xml7.serializer_v7.read_revision_from_string(
443
445
            _expected_rev_v5)
444
 
        serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
 
446
        serialized = breezy.xml7.serializer_v7.write_revision_to_string(rev)
445
447
        self.assertEqualDiff(serialized, _expected_rev_v5)
446
448
 
447
449
    def test_revision_text_v8(self):
448
450
        """Pack revision to XML v8"""
449
 
        rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
 
451
        rev = breezy.xml8.serializer_v8.read_revision_from_string(
450
452
            _expected_rev_v8)
451
 
        serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
 
453
        serialized = breezy.xml8.serializer_v8.write_revision_to_string(rev)
452
454
        self.assertEqualDiff(serialized, _expected_rev_v8)
453
455
 
454
456
    def test_revision_ids_are_utf8(self):
455
457
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
456
 
        s_v5 = bzrlib.xml5.serializer_v5
 
458
        s_v5 = breezy.xml5.serializer_v5
457
459
        rev = s_v5.read_revision_from_string(_revision_utf8_v5)
458
460
        self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
459
461
        self.assertIsInstance(rev.revision_id, str)
502
504
    def setUp(self):
503
505
        super(TestEncodeAndEscape, self).setUp()
504
506
        # Keep the cache clear before and after the test
505
 
        bzrlib.xml_serializer._clear_cache()
506
 
        self.addCleanup(bzrlib.xml_serializer._clear_cache)
 
507
        breezy.xml_serializer._clear_cache()
 
508
        self.addCleanup(breezy.xml_serializer._clear_cache)
507
509
 
508
510
    def test_simple_ascii(self):
509
511
        # _encode_and_escape always appends a final ", because these parameters
510
512
        # are being used in xml attributes, and by returning it now, we have to
511
513
        # do fewer string operations later.
512
 
        val = bzrlib.xml_serializer.encode_and_escape('foo bar')
 
514
        val = breezy.xml_serializer.encode_and_escape('foo bar')
513
515
        self.assertEqual('foo bar"', val)
514
516
        # The second time should be cached
515
 
        val2 = bzrlib.xml_serializer.encode_and_escape('foo bar')
 
517
        val2 = breezy.xml_serializer.encode_and_escape('foo bar')
516
518
        self.assertIs(val2, val)
517
519
 
518
520
    def test_ascii_with_xml(self):
519
521
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
520
 
                         bzrlib.xml_serializer.encode_and_escape('&\'"<>'))
 
522
                         breezy.xml_serializer.encode_and_escape('&\'"<>'))
521
523
 
522
524
    def test_utf8_with_xml(self):
523
525
        # u'\xb5\xe5&\u062c'
524
526
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
525
527
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
526
 
                         bzrlib.xml_serializer.encode_and_escape(utf8_str))
 
528
                         breezy.xml_serializer.encode_and_escape(utf8_str))
527
529
 
528
530
    def test_unicode(self):
529
531
        uni_str = u'\xb5\xe5&\u062c'
530
532
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
531
 
                         bzrlib.xml_serializer.encode_and_escape(uni_str))
 
533
                         breezy.xml_serializer.encode_and_escape(uni_str))
532
534
 
533
535
 
534
536
class TestMisc(TestCase):
535
537
 
536
538
    def test_unescape_xml(self):
537
539
        """We get some kind of error when malformed entities are passed"""
538
 
        self.assertRaises(KeyError, bzrlib.xml8._unescape_xml, 'foo&bar;')
 
540
        self.assertRaises(KeyError, breezy.xml8._unescape_xml, 'foo&bar;')