~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

  • Committer: John Arbash Meinel
  • Date: 2010-01-13 16:23:07 UTC
  • mto: (4634.119.7 2.0)
  • mto: This revision was merged to the branch mainline in revision 4959.
  • Revision ID: john@arbash-meinel.com-20100113162307-0bs82td16gzih827
Update the MANIFEST.in file.

Show diffs side-by-side

added added

removed removed

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