/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-05-24 00:39:50 UTC
  • mto: This revision was merged to the branch mainline in revision 7504.
  • Revision ID: jelmer@jelmer.uk-20200524003950-bbc545r76vc5yajg
Add github action.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from io import BytesIO
 
18
 
 
19
from .. import (
 
20
    errors,
 
21
    fifo_cache,
 
22
    )
 
23
from ..bzr import (
 
24
    inventory,
 
25
    xml6,
 
26
    xml7,
 
27
    xml8,
 
28
    )
 
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;"
 
34
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
35
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
36
    timestamp="1125907235.212"
 
37
    timezone="36000">
 
38
<message>- start splitting code for xml (de)serialization away from objects
 
39
  preparatory to supporting multiple formats by a single library
 
40
</message>
 
41
<parents>
 
42
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
 
43
</parents>
 
44
</revision>
 
45
"""
 
46
 
 
47
_revision_v5_utc = b"""\
 
48
<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
 
49
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
50
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
51
    timestamp="1125907235.212"
 
52
    timezone="0">
 
53
<message>- start splitting code for xml (de)serialization away from objects
 
54
  preparatory to supporting multiple formats by a single library
 
55
</message>
 
56
<parents>
 
57
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
 
58
</parents>
 
59
</revision>
 
60
"""
 
61
 
 
62
_committed_inv_v5 = b"""<inventory>
 
63
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
 
64
      name="bar" parent_id="TREE_ROOT"
 
65
      revision="mbp@foo-123123"
 
66
      text_sha1="A" text_size="1"/>
 
67
<directory name="subdir"
 
68
           file_id="foo-20050801201819-4139aa4a272f4250"
 
69
           parent_id="TREE_ROOT"
 
70
           revision="mbp@foo-00"/>
 
71
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
 
72
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
 
73
      revision="mbp@foo-00"
 
74
      text_sha1="B" text_size="0"/>
 
75
</inventory>
 
76
"""
 
77
 
 
78
_basis_inv_v5 = b"""<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
 
79
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
 
80
      name="bar" parent_id="TREE_ROOT"
 
81
      revision="mbp@foo-123123"/>
 
82
<directory name="subdir"
 
83
           file_id="foo-20050801201819-4139aa4a272f4250"
 
84
           parent_id="TREE_ROOT"
 
85
           revision="mbp@foo-00"/>
 
86
<file file_id="bar-20050824000535-6bc48cfad47ed134"
 
87
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
 
88
      revision="mbp@foo-00"/>
 
89
</inventory>
 
90
"""
 
91
 
 
92
 
 
93
# DO NOT REFLOW THIS. Its the exact revision we want.
 
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">
 
95
<message>- start splitting code for xml (de)serialization away from objects
 
96
  preparatory to supporting multiple formats by a single library
 
97
</message>
 
98
<parents>
 
99
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
 
100
</parents>
 
101
</revision>
 
102
"""
 
103
 
 
104
 
 
105
# DO NOT REFLOW THIS. Its the exact inventory we want.
 
106
_expected_inv_v5 = b"""<inventory format="5">
 
107
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
 
108
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
 
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" />
 
110
</inventory>
 
111
"""
 
112
 
 
113
 
 
114
_expected_inv_v5_root = b"""<inventory file_id="f&lt;" format="5" revision_id="mother!">
 
115
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
 
116
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f&lt;" revision="mbp@foo-00" />
 
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" />
 
118
<symlink file_id="link-1" name="link" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" symlink_target="a" />
 
119
</inventory>
 
120
"""
 
121
 
 
122
_expected_inv_v6 = b"""<inventory format="6" revision_id="rev_outer">
 
123
<directory file_id="tree-root-321" name="" revision="rev_outer" />
 
124
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
 
125
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
 
126
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
 
127
</inventory>
 
128
"""
 
129
 
 
130
_expected_inv_v7 = b"""<inventory format="7" revision_id="rev_outer">
 
131
<directory file_id="tree-root-321" name="" revision="rev_outer" />
 
132
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
 
133
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
 
134
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
 
135
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
 
136
</inventory>
 
137
"""
 
138
 
 
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">
 
140
<message>- start splitting code for xml (de)serialization away from objects
 
141
  preparatory to supporting multiple formats by a single library
 
142
</message>
 
143
<parents>
 
144
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
 
145
</parents>
 
146
</revision>
 
147
"""
 
148
 
 
149
_expected_inv_v8 = b"""<inventory format="8" revision_id="rev_outer">
 
150
<directory file_id="tree-root-321" name="" revision="rev_outer" />
 
151
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
 
152
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
 
153
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
 
154
</inventory>
 
155
"""
 
156
 
 
157
_revision_utf8_v5 = b"""<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
 
158
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
159
    revision_id="erik@b&#229;gfors-02"
 
160
    timestamp="1125907235.212"
 
161
    timezone="36000">
 
162
<message>Include &#181;nicode characters
 
163
</message>
 
164
<parents>
 
165
<revision_ref revision_id="erik@b&#229;gfors-01"/>
 
166
</parents>
 
167
</revision>
 
168
"""
 
169
 
 
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"
 
187
                                   revision_id="erik@b&#229;gfors-02">
 
188
<file file_id="b&#229;r-01"
 
189
      name="b&#229;r" parent_id="TRE&#233;_ROOT"
 
190
      revision="erik@b&#229;gfors-01"/>
 
191
<directory name="s&#181;bdir"
 
192
           file_id="s&#181;bdir-01"
 
193
           parent_id="TRE&#233;_ROOT"
 
194
           revision="erik@b&#229;gfors-01"/>
 
195
<file executable="yes" file_id="b&#229;r-02"
 
196
      name="b&#229;r" parent_id="s&#181;bdir-01"
 
197
      revision="erik@b&#229;gfors-02"/>
 
198
</inventory>
 
199
"""
 
200
 
 
201
# Before revision_id was always stored as an attribute
 
202
_inventory_v5a = b"""<inventory format="5">
 
203
</inventory>
 
204
"""
 
205
 
 
206
# Before revision_id was always stored as an attribute
 
207
_inventory_v5b = b"""<inventory format="5" revision_id="a-rev-id">
 
208
</inventory>
 
209
"""
 
210
 
 
211
 
 
212
class TestSerializer(TestCase):
 
213
    """Test XML serialization"""
 
214
 
 
215
    def test_unpack_revision_5(self):
 
216
        """Test unpacking a canned revision v5"""
 
217
        inp = BytesIO(_revision_v5)
 
218
        rev = breezy.bzr.xml5.serializer_v5.read_revision(inp)
 
219
        eq = self.assertEqual
 
220
        eq(rev.committer,
 
221
           "Martin Pool <mbp@sourcefrog.net>")
 
222
        eq(len(rev.parent_ids), 1)
 
223
        eq(rev.timezone, 36000)
 
224
        eq(rev.parent_ids[0],
 
225
           b"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
 
226
 
 
227
    def test_unpack_revision_5_utc(self):
 
228
        inp = BytesIO(_revision_v5_utc)
 
229
        rev = breezy.bzr.xml5.serializer_v5.read_revision(inp)
 
230
        eq = self.assertEqual
 
231
        eq(rev.committer,
 
232
           "Martin Pool <mbp@sourcefrog.net>")
 
233
        eq(len(rev.parent_ids), 1)
 
234
        eq(rev.timezone, 0)
 
235
        eq(rev.parent_ids[0],
 
236
           b"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
 
237
 
 
238
    def test_unpack_inventory_5(self):
 
239
        """Unpack canned new-style inventory"""
 
240
        inp = BytesIO(_committed_inv_v5)
 
241
        inv = breezy.bzr.xml5.serializer_v5.read_inventory(inp)
 
242
        eq = self.assertEqual
 
243
        eq(len(inv), 4)
 
244
        ie = inv.get_entry(b'bar-20050824000535-6bc48cfad47ed134')
 
245
        eq(ie.kind, 'file')
 
246
        eq(ie.revision, b'mbp@foo-00')
 
247
        eq(ie.name, 'bar')
 
248
        eq(inv.get_entry(ie.parent_id).kind, 'directory')
 
249
 
 
250
    def test_unpack_basis_inventory_5(self):
 
251
        """Unpack canned new-style inventory"""
 
252
        inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
253
            breezy.osutils.split_lines(_basis_inv_v5))
 
254
        eq = self.assertEqual
 
255
        eq(len(inv), 4)
 
256
        eq(inv.revision_id,
 
257
           b'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
 
258
        ie = inv.get_entry(b'bar-20050824000535-6bc48cfad47ed134')
 
259
        eq(ie.kind, 'file')
 
260
        eq(ie.revision, b'mbp@foo-00')
 
261
        eq(ie.name, 'bar')
 
262
        eq(inv.get_entry(ie.parent_id).kind, 'directory')
 
263
 
 
264
    def test_unpack_inventory_5a(self):
 
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)
 
268
 
 
269
    def test_unpack_inventory_5a_cache_and_copy(self):
 
270
        # Passing an entry_cache should get populated with the objects
 
271
        # But the returned objects should be copies if return_from_cache is
 
272
        # False
 
273
        entry_cache = fifo_cache.FIFOCache()
 
274
        inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
275
            breezy.osutils.split_lines(_inventory_v5a), revision_id=b'test-rev-id',
 
276
            entry_cache=entry_cache, return_from_cache=False)
 
277
        for entry in inv.iter_just_entries():
 
278
            key = (entry.file_id, entry.revision)
 
279
            if entry.file_id is inv.root.file_id:
 
280
                # The root id is inferred for xml v5
 
281
                self.assertFalse(key in entry_cache)
 
282
            else:
 
283
                self.assertIsNot(entry, entry_cache[key])
 
284
 
 
285
    def test_unpack_inventory_5a_cache_no_copy(self):
 
286
        # Passing an entry_cache should get populated with the objects
 
287
        # The returned objects should be exact if return_from_cache is
 
288
        # True
 
289
        entry_cache = fifo_cache.FIFOCache()
 
290
        inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
291
            breezy.osutils.split_lines(_inventory_v5a), revision_id=b'test-rev-id',
 
292
            entry_cache=entry_cache, return_from_cache=True)
 
293
        for entry in inv.iter_just_entries():
 
294
            key = (entry.file_id, entry.revision)
 
295
            if entry.file_id is inv.root.file_id:
 
296
                # The root id is inferred for xml v5
 
297
                self.assertFalse(key in entry_cache)
 
298
            else:
 
299
                self.assertIs(entry, entry_cache[key])
 
300
 
 
301
    def test_unpack_inventory_5b(self):
 
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)
 
305
 
 
306
    def test_repack_inventory_5(self):
 
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)
 
311
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
 
312
        inv2 = breezy.bzr.xml5.serializer_v5.read_inventory_from_lines(
 
313
            breezy.osutils.split_lines(outp.getvalue()))
 
314
        self.assertEqual(inv, inv2)
 
315
 
 
316
    def assertRoundTrips(self, xml_string):
 
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)
 
321
        self.assertEqualDiff(xml_string, outp.getvalue())
 
322
        lines = breezy.bzr.xml5.serializer_v5.write_inventory_to_lines(inv)
 
323
        outp.seek(0)
 
324
        self.assertEqual(outp.readlines(), lines)
 
325
        inv2 = breezy.bzr.xml5.serializer_v5.read_inventory(
 
326
            BytesIO(outp.getvalue()))
 
327
        self.assertEqual(inv, inv2)
 
328
 
 
329
    def tests_serialize_inventory_v5_with_root(self):
 
330
        self.assertRoundTrips(_expected_inv_v5_root)
 
331
 
 
332
    def check_repack_revision(self, txt):
 
333
        """Check that repacking a revision yields the same information"""
 
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))
 
339
        self.assertEqual(rev, rev2)
 
340
 
 
341
    def test_repack_revision_5(self):
 
342
        """Round-trip revision to XML v5"""
 
343
        self.check_repack_revision(_revision_v5)
 
344
 
 
345
    def test_repack_revision_5_utc(self):
 
346
        self.check_repack_revision(_revision_v5_utc)
 
347
 
 
348
    def test_pack_revision_5(self):
 
349
        """Pack revision to XML v5"""
 
350
        # fixed 20051025, revisions should have final newline
 
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)))
 
358
        self.assertEqualDiff(outfile_contents, _expected_rev_v5)
 
359
 
 
360
    def test_empty_property_value(self):
 
361
        """Create an empty property value check that it serializes correctly"""
 
362
        s_v5 = breezy.bzr.xml5.serializer_v5
 
363
        rev = s_v5.read_revision_from_string(_revision_v5)
 
364
        props = {'empty': '', 'one': 'one'}
 
365
        rev.properties = props
 
366
        txt = b''.join(s_v5.write_revision_to_lines(rev))
 
367
        new_rev = s_v5.read_revision_from_string(txt)
 
368
        self.assertEqual(props, new_rev.properties)
 
369
 
 
370
    def get_sample_inventory(self):
 
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'
 
383
        return inv
 
384
 
 
385
    def test_roundtrip_inventory_v7(self):
 
386
        inv = self.get_sample_inventory()
 
387
        inv.add(inventory.TreeReference(b'nested-id', 'nested', b'tree-root-321',
 
388
                                        b'rev_outer', b'rev_inner'))
 
389
        lines = xml7.serializer_v7.write_inventory_to_lines(inv)
 
390
        self.assertEqualDiff(_expected_inv_v7, b''.join(lines))
 
391
        inv2 = xml7.serializer_v7.read_inventory_from_lines(lines)
 
392
        self.assertEqual(5, len(inv2))
 
393
        for path, ie in inv.iter_entries():
 
394
            self.assertEqual(ie, inv2.get_entry(ie.file_id))
 
395
 
 
396
    def test_roundtrip_inventory_v6(self):
 
397
        inv = self.get_sample_inventory()
 
398
        lines = xml6.serializer_v6.write_inventory_to_lines(inv)
 
399
        self.assertEqualDiff(_expected_inv_v6, b''.join(lines))
 
400
        inv2 = xml6.serializer_v6.read_inventory_from_lines(lines)
 
401
        self.assertEqual(4, len(inv2))
 
402
        for path, ie in inv.iter_entries():
 
403
            self.assertEqual(ie, inv2.get_entry(ie.file_id))
 
404
 
 
405
    def test_wrong_format_v7(self):
 
406
        """Can't accidentally open a file with wrong serializer"""
 
407
        s_v6 = breezy.bzr.xml6.serializer_v6
 
408
        s_v7 = xml7.serializer_v7
 
409
        self.assertRaises(errors.UnexpectedInventoryFormat,
 
410
                          s_v7.read_inventory_from_lines,
 
411
                          breezy.osutils.split_lines(_expected_inv_v5))
 
412
        self.assertRaises(errors.UnexpectedInventoryFormat,
 
413
                          s_v6.read_inventory_from_lines,
 
414
                          breezy.osutils.split_lines(_expected_inv_v7))
 
415
 
 
416
    def test_tree_reference(self):
 
417
        s_v5 = breezy.bzr.xml5.serializer_v5
 
418
        s_v6 = breezy.bzr.xml6.serializer_v6
 
419
        s_v7 = xml7.serializer_v7
 
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)
 
435
 
 
436
    def test_roundtrip_inventory_v8(self):
 
437
        inv = self.get_sample_inventory()
 
438
        lines = xml8.serializer_v8.write_inventory_to_lines(inv)
 
439
        inv2 = xml8.serializer_v8.read_inventory_from_lines(lines)
 
440
        self.assertEqual(4, len(inv2))
 
441
        for path, ie in inv.iter_entries():
 
442
            self.assertEqual(ie, inv2.get_entry(ie.file_id))
 
443
 
 
444
    def test_inventory_text_v8(self):
 
445
        inv = self.get_sample_inventory()
 
446
        lines = xml8.serializer_v8.write_inventory_to_lines(inv)
 
447
        self.assertEqualDiff(_expected_inv_v8, b''.join(lines))
 
448
 
 
449
    def test_revision_text_v6(self):
 
450
        """Pack revision to XML v6"""
 
451
        rev = breezy.bzr.xml6.serializer_v6.read_revision_from_string(
 
452
            _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)
 
456
 
 
457
    def test_revision_text_v7(self):
 
458
        """Pack revision to XML v7"""
 
459
        rev = breezy.bzr.xml7.serializer_v7.read_revision_from_string(
 
460
            _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)
 
464
 
 
465
    def test_revision_text_v8(self):
 
466
        """Pack revision to XML v8"""
 
467
        rev = breezy.bzr.xml8.serializer_v8.read_revision_from_string(
 
468
            _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)
 
480
 
 
481
    def test_revision_ids_are_utf8(self):
 
482
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
 
483
        s_v5 = breezy.bzr.xml5.serializer_v5
 
484
        rev = s_v5.read_revision_from_string(_revision_utf8_v5)
 
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)
 
488
        for parent_id in rev.parent_ids:
 
489
            self.assertIsInstance(parent_id, bytes)
 
490
        self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
 
491
        self.assertIsInstance(rev.message, str)
 
492
 
 
493
        # ie.revision should either be None or a utf-8 revision id
 
494
        inv = s_v5.read_inventory_from_lines(breezy.osutils.split_lines(_inventory_utf8_v5))
 
495
        rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
 
496
        rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
 
497
        fid_root = u'TRE\xe9_ROOT'.encode('utf8')
 
498
        fid_bar1 = u'b\xe5r-01'.encode('utf8')
 
499
        fid_sub = u's\xb5bdir-01'.encode('utf8')
 
500
        fid_bar2 = u'b\xe5r-02'.encode('utf8')
 
501
        expected = [(u'', fid_root, None, rev_id_2),
 
502
                    (u'b\xe5r', fid_bar1, fid_root, rev_id_1),
 
503
                    (u's\xb5bdir', fid_sub, fid_root, rev_id_1),
 
504
                    (u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
 
505
                    ]
 
506
        self.assertEqual(rev_id_2, inv.revision_id)
 
507
        self.assertIsInstance(inv.revision_id, bytes)
 
508
 
 
509
        actual = list(inv.iter_entries_by_dir())
 
510
        for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
 
511
             (act_path, act_ie)) in zip(expected, actual):
 
512
            self.assertEqual(exp_path, act_path)
 
513
            self.assertIsInstance(act_path, str)
 
514
            self.assertEqual(exp_file_id, act_ie.file_id)
 
515
            self.assertIsInstance(act_ie.file_id, bytes)
 
516
            self.assertEqual(exp_parent_id, act_ie.parent_id)
 
517
            if exp_parent_id is not None:
 
518
                self.assertIsInstance(act_ie.parent_id, bytes)
 
519
            self.assertEqual(exp_rev_id, act_ie.revision)
 
520
            if exp_rev_id is not None:
 
521
                self.assertIsInstance(act_ie.revision, bytes)
 
522
 
 
523
        self.assertEqual(len(expected), len(actual))
 
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
 
 
532
 
 
533
class TestEncodeAndEscape(TestCase):
 
534
    """Whitebox testing of the _encode_and_escape function."""
 
535
 
 
536
    def setUp(self):
 
537
        super(TestEncodeAndEscape, self).setUp()
 
538
        # Keep the cache clear before and after the test
 
539
        breezy.bzr.xml_serializer._clear_cache()
 
540
        self.addCleanup(breezy.bzr.xml_serializer._clear_cache)
 
541
 
 
542
    def test_simple_ascii(self):
 
543
        # _encode_and_escape always appends a final ", because these parameters
 
544
        # are being used in xml attributes, and by returning it now, we have to
 
545
        # do fewer string operations later.
 
546
        val = breezy.bzr.xml_serializer.encode_and_escape('foo bar')
 
547
        self.assertEqual(b'foo bar', val)
 
548
        # The second time should be cached
 
549
        val2 = breezy.bzr.xml_serializer.encode_and_escape('foo bar')
 
550
        self.assertIs(val2, val)
 
551
 
 
552
    def test_ascii_with_xml(self):
 
553
        self.assertEqual(b'&amp;&apos;&quot;&lt;&gt;',
 
554
                         breezy.bzr.xml_serializer.encode_and_escape('&\'"<>'))
 
555
 
 
556
    def test_utf8_with_xml(self):
 
557
        # u'\xb5\xe5&\u062c'
 
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))
 
561
 
 
562
    def test_unicode(self):
 
563
        uni_str = u'\xb5\xe5&\u062c'
 
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;')