/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_xml.py

  • Committer: Jelmer Vernooij
  • Date: 2020-02-18 01:57:45 UTC
  • mto: This revision was merged to the branch mainline in revision 7493.
  • Revision ID: jelmer@jelmer.uk-20200218015745-q2ss9tsk74h4nh61
drop use of future.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
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
 
17
from io import BytesIO
18
18
 
19
 
from bzrlib import (
 
19
from .. import (
20
20
    errors,
21
21
    fifo_cache,
 
22
    )
 
23
from ..bzr import (
22
24
    inventory,
23
25
    xml6,
24
26
    xml7,
25
27
    xml8,
26
 
    serializer,
27
28
    )
28
 
from bzrlib.tests import TestCase
29
 
from bzrlib.inventory import Inventory, InventoryEntry
30
 
from bzrlib.xml4 import serializer_v4
31
 
import bzrlib.xml5
32
 
 
33
 
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
34
 
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
35
 
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
36
 
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
37
 
</inventory>"""
38
 
 
39
 
 
40
 
_revision_v4 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
41
 
    inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
42
 
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
43
 
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
44
 
    timestamp="1125907235.212"
45
 
    timezone="36000">
46
 
<message>- start splitting code for xml (de)serialization away from objects
47
 
  preparatory to supporting multiple formats by a single library
48
 
</message>
49
 
<parents>
50
 
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
51
 
</parents>
52
 
</revision>
53
 
"""
54
 
 
55
 
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
 
29
from ..bzr.inventory import Inventory
 
30
from . import TestCase
 
31
import breezy.bzr.xml5
 
32
 
 
33
_revision_v5 = b"""<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
56
34
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
57
35
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
58
36
    timestamp="1125907235.212"
66
44
</revision>
67
45
"""
68
46
 
69
 
_revision_v5_utc = """\
 
47
_revision_v5_utc = b"""\
70
48
<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
71
49
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
72
50
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
81
59
</revision>
82
60
"""
83
61
 
84
 
_committed_inv_v5 = """<inventory>
 
62
_committed_inv_v5 = b"""<inventory>
85
63
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
86
64
      name="bar" parent_id="TREE_ROOT"
87
65
      revision="mbp@foo-123123"
97
75
</inventory>
98
76
"""
99
77
 
100
 
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
 
78
_basis_inv_v5 = b"""<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
101
79
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
102
80
      name="bar" parent_id="TREE_ROOT"
103
81
      revision="mbp@foo-123123"/>
113
91
 
114
92
 
115
93
# DO NOT REFLOW THIS. Its the exact revision we want.
116
 
_expected_rev_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;" format="5" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
 
94
_expected_rev_v5 = b"""<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;" format="5" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
117
95
<message>- start splitting code for xml (de)serialization away from objects
118
96
  preparatory to supporting multiple formats by a single library
119
97
</message>
125
103
 
126
104
 
127
105
# DO NOT REFLOW THIS. Its the exact inventory we want.
128
 
_expected_inv_v5 = """<inventory format="5">
 
106
_expected_inv_v5 = b"""<inventory format="5">
129
107
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
130
108
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
131
109
<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" />
133
111
"""
134
112
 
135
113
 
136
 
_expected_inv_v5_root = """<inventory file_id="f&lt;" format="5" revision_id="mother!">
 
114
_expected_inv_v5_root = b"""<inventory file_id="f&lt;" format="5" revision_id="mother!">
137
115
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
138
116
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f&lt;" revision="mbp@foo-00" />
139
117
<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" />
141
119
</inventory>
142
120
"""
143
121
 
144
 
_expected_inv_v6 = """<inventory format="6" revision_id="rev_outer">
 
122
_expected_inv_v6 = b"""<inventory format="6" revision_id="rev_outer">
145
123
<directory file_id="tree-root-321" name="" revision="rev_outer" />
146
124
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
147
125
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
149
127
</inventory>
150
128
"""
151
129
 
152
 
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
 
130
_expected_inv_v7 = b"""<inventory format="7" revision_id="rev_outer">
153
131
<directory file_id="tree-root-321" name="" revision="rev_outer" />
154
132
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
155
133
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
158
136
</inventory>
159
137
"""
160
138
 
161
 
_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">
 
139
_expected_rev_v8 = b"""<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">
162
140
<message>- start splitting code for xml (de)serialization away from objects
163
141
  preparatory to supporting multiple formats by a single library
164
142
</message>
168
146
</revision>
169
147
"""
170
148
 
171
 
_expected_inv_v8 = """<inventory format="8" revision_id="rev_outer">
 
149
_expected_inv_v8 = b"""<inventory format="8" revision_id="rev_outer">
172
150
<directory file_id="tree-root-321" name="" revision="rev_outer" />
173
151
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
174
152
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
176
154
</inventory>
177
155
"""
178
156
 
179
 
_revision_utf8_v5 = """<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
 
157
_revision_utf8_v5 = b"""<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
180
158
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
181
159
    revision_id="erik@b&#229;gfors-02"
182
160
    timestamp="1125907235.212"
189
167
</revision>
190
168
"""
191
169
 
192
 
_inventory_utf8_v5 = """<inventory file_id="TRE&#233;_ROOT" format="5"
 
170
_expected_rev_v8_complex = b"""<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;" format="8" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="erik@b&#229;gfors-02" timestamp="1125907235.212" timezone="36000">
 
171
<message>Include &#181;nicode characters
 
172
</message>
 
173
<parents>
 
174
<revision_ref revision_id="erik@b&#229;gfors-01" />
 
175
<revision_ref revision_id="erik@bagfors-02" />
 
176
</parents>
 
177
<properties><property name="bar" />
 
178
<property name="foo">this has a
 
179
newline in it</property>
 
180
</properties>
 
181
</revision>
 
182
"""
 
183
 
 
184
 
 
185
 
 
186
_inventory_utf8_v5 = b"""<inventory file_id="TRE&#233;_ROOT" format="5"
193
187
                                   revision_id="erik@b&#229;gfors-02">
194
188
<file file_id="b&#229;r-01"
195
189
      name="b&#229;r" parent_id="TRE&#233;_ROOT"
205
199
"""
206
200
 
207
201
# Before revision_id was always stored as an attribute
208
 
_inventory_v5a = """<inventory format="5">
 
202
_inventory_v5a = b"""<inventory format="5">
209
203
</inventory>
210
204
"""
211
205
 
212
206
# Before revision_id was always stored as an attribute
213
 
_inventory_v5b = """<inventory format="5" revision_id="a-rev-id">
 
207
_inventory_v5b = b"""<inventory format="5" revision_id="a-rev-id">
214
208
</inventory>
215
209
"""
216
210
 
218
212
class TestSerializer(TestCase):
219
213
    """Test XML serialization"""
220
214
 
221
 
    def test_canned_inventory(self):
222
 
        """Test unpacked a canned inventory v4 file."""
223
 
        inp = StringIO(_working_inventory_v4)
224
 
        inv = serializer_v4.read_inventory(inp)
225
 
        self.assertEqual(len(inv), 4)
226
 
        self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
227
 
 
228
 
    def test_unpack_revision(self):
229
 
        """Test unpacking a canned revision v4"""
230
 
        inp = StringIO(_revision_v4)
231
 
        rev = serializer_v4.read_revision(inp)
232
 
        eq = self.assertEqual
233
 
        eq(rev.committer,
234
 
           "Martin Pool <mbp@sourcefrog.net>")
235
 
        eq(rev.inventory_id,
236
 
           "mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
237
 
        eq(len(rev.parent_ids), 1)
238
 
        eq(rev.parent_ids[0],
239
 
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
240
 
 
241
215
    def test_unpack_revision_5(self):
242
216
        """Test unpacking a canned revision v5"""
243
 
        inp = StringIO(_revision_v5)
244
 
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
 
217
        inp = BytesIO(_revision_v5)
 
218
        rev = breezy.bzr.xml5.serializer_v5.read_revision(inp)
245
219
        eq = self.assertEqual
246
220
        eq(rev.committer,
247
221
           "Martin Pool <mbp@sourcefrog.net>")
248
222
        eq(len(rev.parent_ids), 1)
249
223
        eq(rev.timezone, 36000)
250
224
        eq(rev.parent_ids[0],
251
 
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
 
225
           b"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
252
226
 
253
227
    def test_unpack_revision_5_utc(self):
254
 
        inp = StringIO(_revision_v5_utc)
255
 
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
 
228
        inp = BytesIO(_revision_v5_utc)
 
229
        rev = breezy.bzr.xml5.serializer_v5.read_revision(inp)
256
230
        eq = self.assertEqual
257
231
        eq(rev.committer,
258
232
           "Martin Pool <mbp@sourcefrog.net>")
259
233
        eq(len(rev.parent_ids), 1)
260
234
        eq(rev.timezone, 0)
261
235
        eq(rev.parent_ids[0],
262
 
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
 
236
           b"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
263
237
 
264
238
    def test_unpack_inventory_5(self):
265
239
        """Unpack canned new-style inventory"""
266
 
        inp = StringIO(_committed_inv_v5)
267
 
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
 
240
        inp = BytesIO(_committed_inv_v5)
 
241
        inv = breezy.bzr.xml5.serializer_v5.read_inventory(inp)
268
242
        eq = self.assertEqual
269
243
        eq(len(inv), 4)
270
 
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
 
244
        ie = inv.get_entry(b'bar-20050824000535-6bc48cfad47ed134')
271
245
        eq(ie.kind, 'file')
272
 
        eq(ie.revision, 'mbp@foo-00')
 
246
        eq(ie.revision, b'mbp@foo-00')
273
247
        eq(ie.name, 'bar')
274
 
        eq(inv[ie.parent_id].kind, 'directory')
 
248
        eq(inv.get_entry(ie.parent_id).kind, 'directory')
275
249
 
276
250
    def test_unpack_basis_inventory_5(self):
277
251
        """Unpack canned new-style inventory"""
278
 
        inp = StringIO(_basis_inv_v5)
279
 
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
 
252
        inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
253
            breezy.osutils.split_lines(_basis_inv_v5))
280
254
        eq = self.assertEqual
281
255
        eq(len(inv), 4)
282
 
        eq(inv.revision_id, 'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
283
 
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
 
256
        eq(inv.revision_id,
 
257
           b'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
 
258
        ie = inv.get_entry(b'bar-20050824000535-6bc48cfad47ed134')
284
259
        eq(ie.kind, 'file')
285
 
        eq(ie.revision, 'mbp@foo-00')
 
260
        eq(ie.revision, b'mbp@foo-00')
286
261
        eq(ie.name, 'bar')
287
 
        eq(inv[ie.parent_id].kind, 'directory')
 
262
        eq(inv.get_entry(ie.parent_id).kind, 'directory')
288
263
 
289
264
    def test_unpack_inventory_5a(self):
290
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
291
 
                _inventory_v5a, revision_id='test-rev-id')
292
 
        self.assertEqual('test-rev-id', inv.root.revision)
 
265
        inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
266
            breezy.osutils.split_lines(_inventory_v5a), revision_id=b'test-rev-id')
 
267
        self.assertEqual(b'test-rev-id', inv.root.revision)
293
268
 
294
269
    def test_unpack_inventory_5a_cache_and_copy(self):
295
270
        # Passing an entry_cache should get populated with the objects
296
271
        # But the returned objects should be copies if return_from_cache is
297
272
        # False
298
273
        entry_cache = fifo_cache.FIFOCache()
299
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
300
 
            _inventory_v5a, revision_id='test-rev-id',
 
274
        inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
275
            breezy.osutils.split_lines(_inventory_v5a), revision_id=b'test-rev-id',
301
276
            entry_cache=entry_cache, return_from_cache=False)
302
277
        for entry in inv.iter_just_entries():
303
278
            key = (entry.file_id, entry.revision)
312
287
        # The returned objects should be exact if return_from_cache is
313
288
        # True
314
289
        entry_cache = fifo_cache.FIFOCache()
315
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
316
 
            _inventory_v5a, revision_id='test-rev-id',
 
290
        inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
291
            breezy.osutils.split_lines(_inventory_v5a), revision_id=b'test-rev-id',
317
292
            entry_cache=entry_cache, return_from_cache=True)
318
293
        for entry in inv.iter_just_entries():
319
294
            key = (entry.file_id, entry.revision)
324
299
                self.assertIs(entry, entry_cache[key])
325
300
 
326
301
    def test_unpack_inventory_5b(self):
327
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
328
 
                _inventory_v5b, revision_id='test-rev-id')
329
 
        self.assertEqual('a-rev-id', inv.root.revision)
 
302
        inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
303
            breezy.osutils.split_lines(_inventory_v5b), revision_id=b'test-rev-id')
 
304
        self.assertEqual(b'a-rev-id', inv.root.revision)
330
305
 
331
306
    def test_repack_inventory_5(self):
332
 
        inp = StringIO(_committed_inv_v5)
333
 
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
334
 
        outp = StringIO()
335
 
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
 
307
        inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
308
            breezy.osutils.split_lines(_committed_inv_v5))
 
309
        outp = BytesIO()
 
310
        breezy.bzr.xml5.serializer_v5.write_inventory(inv, outp)
336
311
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
337
 
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
 
312
        inv2 = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
313
            breezy.osutils.split_lines(outp.getvalue()))
338
314
        self.assertEqual(inv, inv2)
339
315
 
340
316
    def assertRoundTrips(self, xml_string):
341
 
        inp = StringIO(xml_string)
342
 
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
343
 
        outp = StringIO()
344
 
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
 
317
        inp = BytesIO(xml_string)
 
318
        inv = breezy.bzr.xml5.serializer_v5.read_inventory(inp)
 
319
        outp = BytesIO()
 
320
        breezy.bzr.xml5.serializer_v5.write_inventory(inv, outp)
345
321
        self.assertEqualDiff(xml_string, outp.getvalue())
346
 
        lines = bzrlib.xml5.serializer_v5.write_inventory_to_lines(inv)
 
322
        lines = breezy.bzr.xml5.serializer_v5.write_inventory_to_lines(inv)
347
323
        outp.seek(0)
348
324
        self.assertEqual(outp.readlines(), lines)
349
 
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
 
325
        inv2 = breezy.bzr.xml5.serializer_v5.read_inventory(
 
326
            BytesIO(outp.getvalue()))
350
327
        self.assertEqual(inv, inv2)
351
328
 
352
329
    def tests_serialize_inventory_v5_with_root(self):
354
331
 
355
332
    def check_repack_revision(self, txt):
356
333
        """Check that repacking a revision yields the same information"""
357
 
        inp = StringIO(txt)
358
 
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
359
 
        outp = StringIO()
360
 
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
361
 
        outfile_contents = outp.getvalue()
362
 
        rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
 
334
        inp = BytesIO(txt)
 
335
        rev = breezy.bzr.xml5.serializer_v5.read_revision(inp)
 
336
        outfile_contents = breezy.bzr.xml5.serializer_v5.write_revision_to_string(rev)
 
337
        rev2 = breezy.bzr.xml5.serializer_v5.read_revision(
 
338
            BytesIO(outfile_contents))
363
339
        self.assertEqual(rev, rev2)
364
340
 
365
341
    def test_repack_revision_5(self):
372
348
    def test_pack_revision_5(self):
373
349
        """Pack revision to XML v5"""
374
350
        # fixed 20051025, revisions should have final newline
375
 
        rev = bzrlib.xml5.serializer_v5.read_revision_from_string(_revision_v5)
376
 
        outp = StringIO()
377
 
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
378
 
        outfile_contents = outp.getvalue()
379
 
        self.assertEqual(outfile_contents[-1], '\n')
380
 
        self.assertEqualDiff(outfile_contents, bzrlib.xml5.serializer_v5.write_revision_to_string(rev))
 
351
        rev = breezy.bzr.xml5.serializer_v5.read_revision_from_string(
 
352
            _revision_v5)
 
353
        outfile_contents = breezy.bzr.xml5.serializer_v5.write_revision_to_string(rev)
 
354
        self.assertEqual(outfile_contents[-1:], b'\n')
 
355
        self.assertEqualDiff(
 
356
            outfile_contents,
 
357
            b''.join(breezy.bzr.xml5.serializer_v5.write_revision_to_lines(rev)))
381
358
        self.assertEqualDiff(outfile_contents, _expected_rev_v5)
382
359
 
383
360
    def test_empty_property_value(self):
384
361
        """Create an empty property value check that it serializes correctly"""
385
 
        s_v5 = bzrlib.xml5.serializer_v5
 
362
        s_v5 = breezy.bzr.xml5.serializer_v5
386
363
        rev = s_v5.read_revision_from_string(_revision_v5)
387
 
        outp = StringIO()
388
 
        props = {'empty':'', 'one':'one'}
 
364
        props = {'empty': '', 'one': 'one'}
389
365
        rev.properties = props
390
 
        txt = s_v5.write_revision_to_string(rev)
 
366
        txt = b''.join(s_v5.write_revision_to_lines(rev))
391
367
        new_rev = s_v5.read_revision_from_string(txt)
392
368
        self.assertEqual(props, new_rev.properties)
393
369
 
394
370
    def get_sample_inventory(self):
395
 
        inv = Inventory('tree-root-321', revision_id='rev_outer')
396
 
        inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
397
 
        inv.add(inventory.InventoryDirectory('dir-id', 'dir',
398
 
                                             'tree-root-321'))
399
 
        inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
400
 
        inv['tree-root-321'].revision = 'rev_outer'
401
 
        inv['dir-id'].revision = 'rev_outer'
402
 
        inv['file-id'].revision = 'rev_outer'
403
 
        inv['file-id'].text_sha1 = 'A'
404
 
        inv['file-id'].text_size = 1
405
 
        inv['link-id'].revision = 'rev_outer'
406
 
        inv['link-id'].symlink_target = 'a'
 
371
        inv = Inventory(b'tree-root-321', revision_id=b'rev_outer')
 
372
        inv.add(inventory.InventoryFile(b'file-id', 'file', b'tree-root-321'))
 
373
        inv.add(inventory.InventoryDirectory(b'dir-id', 'dir',
 
374
                                             b'tree-root-321'))
 
375
        inv.add(inventory.InventoryLink(b'link-id', 'link', b'tree-root-321'))
 
376
        inv.get_entry(b'tree-root-321').revision = b'rev_outer'
 
377
        inv.get_entry(b'dir-id').revision = b'rev_outer'
 
378
        inv.get_entry(b'file-id').revision = b'rev_outer'
 
379
        inv.get_entry(b'file-id').text_sha1 = b'A'
 
380
        inv.get_entry(b'file-id').text_size = 1
 
381
        inv.get_entry(b'link-id').revision = b'rev_outer'
 
382
        inv.get_entry(b'link-id').symlink_target = 'a'
407
383
        return inv
408
384
 
409
385
    def test_roundtrip_inventory_v7(self):
410
386
        inv = self.get_sample_inventory()
411
 
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
412
 
                                        'rev_outer', 'rev_inner'))
413
 
        txt = xml7.serializer_v7.write_inventory_to_string(inv)
 
387
        inv.add(inventory.TreeReference(b'nested-id', 'nested', b'tree-root-321',
 
388
                                        b'rev_outer', b'rev_inner'))
414
389
        lines = xml7.serializer_v7.write_inventory_to_lines(inv)
415
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
416
 
        self.assertEqualDiff(_expected_inv_v7, txt)
417
 
        inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
 
390
        self.assertEqualDiff(_expected_inv_v7, b''.join(lines))
 
391
        inv2 = xml7.serializer_v7.read_inventory_from_lines(lines)
418
392
        self.assertEqual(5, len(inv2))
419
393
        for path, ie in inv.iter_entries():
420
 
            self.assertEqual(ie, inv2[ie.file_id])
 
394
            self.assertEqual(ie, inv2.get_entry(ie.file_id))
421
395
 
422
396
    def test_roundtrip_inventory_v6(self):
423
397
        inv = self.get_sample_inventory()
424
 
        txt = xml6.serializer_v6.write_inventory_to_string(inv)
425
398
        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)
 
399
        self.assertEqualDiff(_expected_inv_v6, b''.join(lines))
 
400
        inv2 = xml6.serializer_v6.read_inventory_from_lines(lines)
429
401
        self.assertEqual(4, len(inv2))
430
402
        for path, ie in inv.iter_entries():
431
 
            self.assertEqual(ie, inv2[ie.file_id])
 
403
            self.assertEqual(ie, inv2.get_entry(ie.file_id))
432
404
 
433
405
    def test_wrong_format_v7(self):
434
406
        """Can't accidentally open a file with wrong serializer"""
435
 
        s_v6 = bzrlib.xml6.serializer_v6
 
407
        s_v6 = breezy.bzr.xml6.serializer_v6
436
408
        s_v7 = xml7.serializer_v7
437
409
        self.assertRaises(errors.UnexpectedInventoryFormat,
438
 
                          s_v7.read_inventory_from_string, _expected_inv_v5)
 
410
                          s_v7.read_inventory_from_lines,
 
411
                          breezy.osutils.split_lines(_expected_inv_v5))
439
412
        self.assertRaises(errors.UnexpectedInventoryFormat,
440
 
                          s_v6.read_inventory_from_string, _expected_inv_v7)
 
413
                          s_v6.read_inventory_from_lines,
 
414
                          breezy.osutils.split_lines(_expected_inv_v7))
441
415
 
442
416
    def test_tree_reference(self):
443
 
        s_v5 = bzrlib.xml5.serializer_v5
444
 
        s_v6 = bzrlib.xml6.serializer_v6
 
417
        s_v5 = breezy.bzr.xml5.serializer_v5
 
418
        s_v6 = breezy.bzr.xml6.serializer_v6
445
419
        s_v7 = xml7.serializer_v7
446
 
        inv = Inventory('tree-root-321', revision_id='rev-outer')
447
 
        inv.root.revision = 'root-rev'
448
 
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
449
 
                                        'rev-outer', 'rev-inner'))
450
 
        self.assertRaises(errors.UnsupportedInventoryKind,
451
 
                          s_v5.write_inventory_to_string, inv)
452
 
        self.assertRaises(errors.UnsupportedInventoryKind,
453
 
                          s_v6.write_inventory_to_string, inv)
454
 
        txt = s_v7.write_inventory_to_string(inv)
455
 
        lines = s_v7.write_inventory_to_lines(inv)
456
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
457
 
        inv2 = s_v7.read_inventory_from_string(txt)
458
 
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
459
 
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
460
 
        self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
461
 
        self.assertRaises(errors.UnsupportedInventoryKind,
462
 
                          s_v6.read_inventory_from_string,
463
 
                          txt.replace('format="7"', 'format="6"'))
464
 
        self.assertRaises(errors.UnsupportedInventoryKind,
465
 
                          s_v5.read_inventory_from_string,
466
 
                          txt.replace('format="7"', 'format="5"'))
 
420
        inv = Inventory(b'tree-root-321', revision_id=b'rev-outer')
 
421
        inv.root.revision = b'root-rev'
 
422
        inv.add(inventory.TreeReference(b'nested-id', 'nested', b'tree-root-321',
 
423
                                        b'rev-outer', b'rev-inner'))
 
424
        self.assertRaises(errors.UnsupportedInventoryKind,
 
425
                          s_v5.write_inventory_to_lines, inv)
 
426
        self.assertRaises(errors.UnsupportedInventoryKind,
 
427
                          s_v6.write_inventory_to_lines, inv)
 
428
        lines = s_v7.write_inventory_to_chunks(inv)
 
429
        inv2 = s_v7.read_inventory_from_lines(lines)
 
430
        self.assertEqual(b'tree-root-321',
 
431
                         inv2.get_entry(b'nested-id').parent_id)
 
432
        self.assertEqual(b'rev-outer', inv2.get_entry(b'nested-id').revision)
 
433
        self.assertEqual(
 
434
            b'rev-inner', inv2.get_entry(b'nested-id').reference_revision)
467
435
 
468
436
    def test_roundtrip_inventory_v8(self):
469
437
        inv = self.get_sample_inventory()
470
 
        txt = xml8.serializer_v8.write_inventory_to_string(inv)
471
 
        inv2 = xml8.serializer_v8.read_inventory_from_string(txt)
 
438
        lines = xml8.serializer_v8.write_inventory_to_lines(inv)
 
439
        inv2 = xml8.serializer_v8.read_inventory_from_lines(lines)
472
440
        self.assertEqual(4, len(inv2))
473
441
        for path, ie in inv.iter_entries():
474
 
            self.assertEqual(ie, inv2[ie.file_id])
 
442
            self.assertEqual(ie, inv2.get_entry(ie.file_id))
475
443
 
476
444
    def test_inventory_text_v8(self):
477
445
        inv = self.get_sample_inventory()
478
 
        txt = xml8.serializer_v8.write_inventory_to_string(inv)
479
446
        lines = xml8.serializer_v8.write_inventory_to_lines(inv)
480
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
481
 
        self.assertEqualDiff(_expected_inv_v8, txt)
 
447
        self.assertEqualDiff(_expected_inv_v8, b''.join(lines))
482
448
 
483
449
    def test_revision_text_v6(self):
484
450
        """Pack revision to XML v6"""
485
 
        rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
 
451
        rev = breezy.bzr.xml6.serializer_v6.read_revision_from_string(
486
452
            _expected_rev_v5)
487
 
        serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
488
 
        self.assertEqualDiff(serialized, _expected_rev_v5)
 
453
        serialized = breezy.bzr.xml6.serializer_v6.write_revision_to_lines(
 
454
            rev)
 
455
        self.assertEqualDiff(b''.join(serialized), _expected_rev_v5)
489
456
 
490
457
    def test_revision_text_v7(self):
491
458
        """Pack revision to XML v7"""
492
 
        rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
 
459
        rev = breezy.bzr.xml7.serializer_v7.read_revision_from_string(
493
460
            _expected_rev_v5)
494
 
        serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
495
 
        self.assertEqualDiff(serialized, _expected_rev_v5)
 
461
        serialized = breezy.bzr.xml7.serializer_v7.write_revision_to_lines(
 
462
            rev)
 
463
        self.assertEqualDiff(b''.join(serialized), _expected_rev_v5)
496
464
 
497
465
    def test_revision_text_v8(self):
498
466
        """Pack revision to XML v8"""
499
 
        rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
 
467
        rev = breezy.bzr.xml8.serializer_v8.read_revision_from_string(
500
468
            _expected_rev_v8)
501
 
        serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
502
 
        self.assertEqualDiff(serialized, _expected_rev_v8)
 
469
        serialized = breezy.bzr.xml8.serializer_v8.write_revision_to_lines(
 
470
            rev)
 
471
        self.assertEqualDiff(b''.join(serialized), _expected_rev_v8)
 
472
 
 
473
    def test_revision_text_v8_complex(self):
 
474
        """Pack revision to XML v8"""
 
475
        rev = breezy.bzr.xml8.serializer_v8.read_revision_from_string(
 
476
            _expected_rev_v8_complex)
 
477
        serialized = breezy.bzr.xml8.serializer_v8.write_revision_to_lines(
 
478
            rev)
 
479
        self.assertEqualDiff(b''.join(serialized), _expected_rev_v8_complex)
503
480
 
504
481
    def test_revision_ids_are_utf8(self):
505
482
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
506
 
        s_v5 = bzrlib.xml5.serializer_v5
 
483
        s_v5 = breezy.bzr.xml5.serializer_v5
507
484
        rev = s_v5.read_revision_from_string(_revision_utf8_v5)
508
 
        self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
509
 
        self.assertIsInstance(rev.revision_id, str)
510
 
        self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
 
485
        self.assertEqual(b'erik@b\xc3\xa5gfors-02', rev.revision_id)
 
486
        self.assertIsInstance(rev.revision_id, bytes)
 
487
        self.assertEqual([b'erik@b\xc3\xa5gfors-01'], rev.parent_ids)
511
488
        for parent_id in rev.parent_ids:
512
 
            self.assertIsInstance(parent_id, str)
 
489
            self.assertIsInstance(parent_id, bytes)
513
490
        self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
514
 
        self.assertIsInstance(rev.message, unicode)
 
491
        self.assertIsInstance(rev.message, str)
515
492
 
516
493
        # ie.revision should either be None or a utf-8 revision id
517
 
        inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
 
494
        inv = s_v5.read_inventory_from_lines(breezy.osutils.split_lines(_inventory_utf8_v5))
518
495
        rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
519
496
        rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
520
497
        fid_root = u'TRE\xe9_ROOT'.encode('utf8')
525
502
                    (u'b\xe5r', fid_bar1, fid_root, rev_id_1),
526
503
                    (u's\xb5bdir', fid_sub, fid_root, rev_id_1),
527
504
                    (u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
528
 
                   ]
 
505
                    ]
529
506
        self.assertEqual(rev_id_2, inv.revision_id)
530
 
        self.assertIsInstance(inv.revision_id, str)
 
507
        self.assertIsInstance(inv.revision_id, bytes)
531
508
 
532
509
        actual = list(inv.iter_entries_by_dir())
533
510
        for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
534
511
             (act_path, act_ie)) in zip(expected, actual):
535
512
            self.assertEqual(exp_path, act_path)
536
 
            self.assertIsInstance(act_path, unicode)
 
513
            self.assertIsInstance(act_path, str)
537
514
            self.assertEqual(exp_file_id, act_ie.file_id)
538
 
            self.assertIsInstance(act_ie.file_id, str)
 
515
            self.assertIsInstance(act_ie.file_id, bytes)
539
516
            self.assertEqual(exp_parent_id, act_ie.parent_id)
540
517
            if exp_parent_id is not None:
541
 
                self.assertIsInstance(act_ie.parent_id, str)
 
518
                self.assertIsInstance(act_ie.parent_id, bytes)
542
519
            self.assertEqual(exp_rev_id, act_ie.revision)
543
520
            if exp_rev_id is not None:
544
 
                self.assertIsInstance(act_ie.revision, str)
 
521
                self.assertIsInstance(act_ie.revision, bytes)
545
522
 
546
523
        self.assertEqual(len(expected), len(actual))
547
524
 
 
525
    def test_serialization_error(self):
 
526
        s_v5 = breezy.bzr.xml5.serializer_v5
 
527
        e = self.assertRaises(
 
528
            errors.UnexpectedInventoryFormat,
 
529
            s_v5.read_inventory_from_lines, [b"<Notquitexml"])
 
530
        self.assertEqual(str(e), "unclosed token: line 1, column 0")
 
531
 
548
532
 
549
533
class TestEncodeAndEscape(TestCase):
550
534
    """Whitebox testing of the _encode_and_escape function."""
551
535
 
552
536
    def setUp(self):
553
 
        TestCase.setUp(self)
 
537
        super(TestEncodeAndEscape, self).setUp()
554
538
        # Keep the cache clear before and after the test
555
 
        bzrlib.xml8._ensure_utf8_re()
556
 
        bzrlib.xml8._clear_cache()
557
 
        self.addCleanup(bzrlib.xml8._clear_cache)
 
539
        breezy.bzr.xml_serializer._clear_cache()
 
540
        self.addCleanup(breezy.bzr.xml_serializer._clear_cache)
558
541
 
559
542
    def test_simple_ascii(self):
560
543
        # _encode_and_escape always appends a final ", because these parameters
561
544
        # are being used in xml attributes, and by returning it now, we have to
562
545
        # do fewer string operations later.
563
 
        val = bzrlib.xml8._encode_and_escape('foo bar')
564
 
        self.assertEqual('foo bar"', val)
 
546
        val = breezy.bzr.xml_serializer.encode_and_escape('foo bar')
 
547
        self.assertEqual(b'foo bar', val)
565
548
        # The second time should be cached
566
 
        val2 = bzrlib.xml8._encode_and_escape('foo bar')
 
549
        val2 = breezy.bzr.xml_serializer.encode_and_escape('foo bar')
567
550
        self.assertIs(val2, val)
568
551
 
569
552
    def test_ascii_with_xml(self):
570
 
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
571
 
                         bzrlib.xml8._encode_and_escape('&\'"<>'))
 
553
        self.assertEqual(b'&amp;&apos;&quot;&lt;&gt;',
 
554
                         breezy.bzr.xml_serializer.encode_and_escape('&\'"<>'))
572
555
 
573
556
    def test_utf8_with_xml(self):
574
557
        # u'\xb5\xe5&\u062c'
575
 
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
576
 
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
577
 
                         bzrlib.xml8._encode_and_escape(utf8_str))
 
558
        utf8_str = b'\xc2\xb5\xc3\xa5&\xd8\xac'
 
559
        self.assertEqual(b'&#181;&#229;&amp;&#1580;',
 
560
                         breezy.bzr.xml_serializer.encode_and_escape(utf8_str))
578
561
 
579
562
    def test_unicode(self):
580
563
        uni_str = u'\xb5\xe5&\u062c'
581
 
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
582
 
                         bzrlib.xml8._encode_and_escape(uni_str))
 
564
        self.assertEqual(b'&#181;&#229;&amp;&#1580;',
 
565
                         breezy.bzr.xml_serializer.encode_and_escape(uni_str))
 
566
 
 
567
 
 
568
class TestMisc(TestCase):
 
569
 
 
570
    def test_unescape_xml(self):
 
571
        """We get some kind of error when malformed entities are passed"""
 
572
        self.assertRaises(KeyError, breezy.bzr.xml8._unescape_xml, b'foo&bar;')