/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
from cStringIO import StringIO
18
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
19
from bzrlib import (
20
    errors, 
21
    inventory, 
22
    xml7,
23
    )
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
24
from bzrlib.tests import TestCase
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
25
from bzrlib.inventory import Inventory, InventoryEntry
1304 by Martin Pool
- fix up imports of serializer_v4
26
from bzrlib.xml4 import serializer_v4
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
27
import bzrlib.xml5
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
28
29
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
30
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
31
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
32
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
33
</inventory>"""
34
1182 by Martin Pool
- more disentangling of xml storage format from objects
35
36
_revision_v4 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
37
    inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
38
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
39
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
2102.4.1 by John Arbash Meinel
Switch to using millisecond resolution in Revision XML
40
    timestamp="1125907235.212"
1182 by Martin Pool
- more disentangling of xml storage format from objects
41
    timezone="36000">
42
<message>- start splitting code for xml (de)serialization away from objects
43
  preparatory to supporting multiple formats by a single library
44
</message>
45
<parents>
46
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
47
</parents>
48
</revision>
49
"""
50
1183 by Martin Pool
- implement version 5 xml storage, and tests
51
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
52
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
53
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
2102.4.1 by John Arbash Meinel
Switch to using millisecond resolution in Revision XML
54
    timestamp="1125907235.212"
1183 by Martin Pool
- implement version 5 xml storage, and tests
55
    timezone="36000">
56
<message>- start splitting code for xml (de)serialization away from objects
57
  preparatory to supporting multiple formats by a single library
58
</message>
59
<parents>
60
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
61
</parents>
62
</revision>
63
"""
64
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
65
_revision_v5_utc = """\
66
<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
67
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
68
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
2102.4.1 by John Arbash Meinel
Switch to using millisecond resolution in Revision XML
69
    timestamp="1125907235.212"
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
70
    timezone="0">
71
<message>- start splitting code for xml (de)serialization away from objects
72
  preparatory to supporting multiple formats by a single library
73
</message>
74
<parents>
75
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
76
</parents>
77
</revision>
78
"""
79
1183 by Martin Pool
- implement version 5 xml storage, and tests
80
_committed_inv_v5 = """<inventory>
81
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
82
      name="bar" parent_id="TREE_ROOT" 
1092.2.22 by Robert Collins
text_version and name_version unification looking reasonable
83
      revision="mbp@foo-123123"/>
1183 by Martin Pool
- implement version 5 xml storage, and tests
84
<directory name="subdir"
85
           file_id="foo-20050801201819-4139aa4a272f4250"
86
           parent_id="TREE_ROOT" 
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
87
           revision="mbp@foo-00"/>
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
88
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" 
1183 by Martin Pool
- implement version 5 xml storage, and tests
89
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
1092.2.22 by Robert Collins
text_version and name_version unification looking reasonable
90
      revision="mbp@foo-00"/>
1183 by Martin Pool
- implement version 5 xml storage, and tests
91
</inventory>
92
"""
93
1638.1.2 by Robert Collins
Change the basis-inventory file to not have the revision-id in the file name.
94
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
95
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
96
      name="bar" parent_id="TREE_ROOT" 
97
      revision="mbp@foo-123123"/>
98
<directory name="subdir"
99
           file_id="foo-20050801201819-4139aa4a272f4250"
100
           parent_id="TREE_ROOT" 
101
           revision="mbp@foo-00"/>
102
<file file_id="bar-20050824000535-6bc48cfad47ed134" 
103
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
104
      revision="mbp@foo-00"/>
105
</inventory>
106
"""
107
1886.1.1 by John Arbash Meinel
Fix bug #47782,
108
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
109
# DO NOT REFLOW THIS. Its the exact revision we want.
2102.4.1 by John Arbash Meinel
Switch to using millisecond resolution in Revision XML
110
_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">
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
111
<message>- start splitting code for xml (de)serialization away from objects
112
  preparatory to supporting multiple formats by a single library
113
</message>
114
<parents>
115
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
116
</parents>
117
</revision>
118
"""
119
120
121
# DO NOT REFLOW THIS. Its the exact inventory we want.
122
_expected_inv_v5 = """<inventory format="5">
123
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" />
124
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
125
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
126
</inventory>
127
"""
128
129
130
_expected_inv_v5_root = """<inventory file_id="f&lt;" format="5" revision_id="mother!">
131
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" />
132
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f&lt;" revision="mbp@foo-00" />
133
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
134
</inventory>
135
"""
136
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
137
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
138
<directory file_id="tree-root-321" name="" revision="rev_outer" />
139
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
140
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" />
141
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" />
142
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
143
</inventory>
144
"""
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
145
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
146
class TestSerializer(TestCase):
147
    """Test XML serialization"""
148
    def test_canned_inventory(self):
149
        """Test unpacked a canned inventory v4 file."""
150
        inp = StringIO(_working_inventory_v4)
151
        inv = serializer_v4.read_inventory(inp)
152
        self.assertEqual(len(inv), 4)
153
        self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
1182 by Martin Pool
- more disentangling of xml storage format from objects
154
155
    def test_unpack_revision(self):
156
        """Test unpacking a canned revision v4"""
157
        inp = StringIO(_revision_v4)
158
        rev = serializer_v4.read_revision(inp)
159
        eq = self.assertEqual
160
        eq(rev.committer,
161
           "Martin Pool <mbp@sourcefrog.net>")
162
        eq(rev.inventory_id,
163
           "mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
164
        eq(len(rev.parent_ids), 1)
165
        eq(rev.parent_ids[0],
1182 by Martin Pool
- more disentangling of xml storage format from objects
166
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
1183 by Martin Pool
- implement version 5 xml storage, and tests
167
168
    def test_unpack_revision_5(self):
169
        """Test unpacking a canned revision v5"""
170
        inp = StringIO(_revision_v5)
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
171
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
1183 by Martin Pool
- implement version 5 xml storage, and tests
172
        eq = self.assertEqual
173
        eq(rev.committer,
174
           "Martin Pool <mbp@sourcefrog.net>")
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
175
        eq(len(rev.parent_ids), 1)
1183 by Martin Pool
- implement version 5 xml storage, and tests
176
        eq(rev.timezone, 36000)
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
177
        eq(rev.parent_ids[0],
1183 by Martin Pool
- implement version 5 xml storage, and tests
178
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
179
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
180
    def test_unpack_revision_5_utc(self):
181
        inp = StringIO(_revision_v5_utc)
182
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
183
        eq = self.assertEqual
184
        eq(rev.committer,
185
           "Martin Pool <mbp@sourcefrog.net>")
186
        eq(len(rev.parent_ids), 1)
187
        eq(rev.timezone, 0)
188
        eq(rev.parent_ids[0],
189
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
190
1183 by Martin Pool
- implement version 5 xml storage, and tests
191
    def test_unpack_inventory_5(self):
192
        """Unpack canned new-style inventory"""
193
        inp = StringIO(_committed_inv_v5)
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
194
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
1183 by Martin Pool
- implement version 5 xml storage, and tests
195
        eq = self.assertEqual
196
        eq(len(inv), 4)
197
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
198
        eq(ie.kind, 'file')
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
199
        eq(ie.revision, 'mbp@foo-00')
1183 by Martin Pool
- implement version 5 xml storage, and tests
200
        eq(ie.name, 'bar')
201
        eq(inv[ie.parent_id].kind, 'directory')
1184 by Martin Pool
- fix v5 packing of inventory entries
202
1638.1.2 by Robert Collins
Change the basis-inventory file to not have the revision-id in the file name.
203
    def test_unpack_basis_inventory_5(self):
204
        """Unpack canned new-style inventory"""
205
        inp = StringIO(_basis_inv_v5)
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
206
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
1638.1.2 by Robert Collins
Change the basis-inventory file to not have the revision-id in the file name.
207
        eq = self.assertEqual
208
        eq(len(inv), 4)
209
        eq(inv.revision_id, 'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
210
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
211
        eq(ie.kind, 'file')
212
        eq(ie.revision, 'mbp@foo-00')
213
        eq(ie.name, 'bar')
214
        eq(inv[ie.parent_id].kind, 'directory')
215
1184 by Martin Pool
- fix v5 packing of inventory entries
216
    def test_repack_inventory_5(self):
217
        inp = StringIO(_committed_inv_v5)
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
218
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
1184 by Martin Pool
- fix v5 packing of inventory entries
219
        outp = StringIO()
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
220
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
221
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
222
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
223
        self.assertEqual(inv, inv2)
224
    
225
    def assertRoundTrips(self, xml_string):
226
        inp = StringIO(xml_string)
227
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
228
        outp = StringIO()
229
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
230
        self.assertEqualDiff(xml_string, outp.getvalue())
231
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
232
        self.assertEqual(inv, inv2)
233
234
    def tests_serialize_inventory_v5_with_root(self):
235
        self.assertRoundTrips(_expected_inv_v5_root)
1185 by Martin Pool
- add xml round-trip test for revisions
236
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
237
    def check_repack_revision(self, txt):
238
        """Check that repacking a revision yields the same information"""
239
        inp = StringIO(txt)
240
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
241
        outp = StringIO()
242
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
243
        outfile_contents = outp.getvalue()
244
        rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
245
        self.assertEqual(rev, rev2)
246
1185 by Martin Pool
- add xml round-trip test for revisions
247
    def test_repack_revision_5(self):
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
248
        """Round-trip revision to XML v5"""
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
249
        self.check_repack_revision(_revision_v5)
250
251
    def test_repack_revision_5_utc(self):
252
        self.check_repack_revision(_revision_v5_utc)
1185 by Martin Pool
- add xml round-trip test for revisions
253
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
254
    def test_pack_revision_5(self):
255
        """Pack revision to XML v5"""
256
        # fixed 20051025, revisions should have final newline
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
257
        rev = bzrlib.xml5.serializer_v5.read_revision_from_string(_revision_v5)
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
258
        outp = StringIO()
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
259
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
260
        outfile_contents = outp.getvalue()
261
        self.assertEqual(outfile_contents[-1], '\n')
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
262
        self.assertEqualDiff(outfile_contents, bzrlib.xml5.serializer_v5.write_revision_to_string(rev))
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
263
        self.assertEqualDiff(outfile_contents, _expected_rev_v5)
1886.1.1 by John Arbash Meinel
Fix bug #47782,
264
265
    def test_empty_property_value(self):
266
        """Create an empty property value check that it serializes correctly"""
267
        s_v5 = bzrlib.xml5.serializer_v5
268
        rev = s_v5.read_revision_from_string(_revision_v5)
269
        outp = StringIO()
270
        props = {'empty':'', 'one':'one'}
271
        rev.properties = props
272
        txt = s_v5.write_revision_to_string(rev)
273
        new_rev = s_v5.read_revision_from_string(txt)
274
        self.assertEqual(props, new_rev.properties)
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
275
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
276
    def test_roundtrip_inventory_v7(self):
277
        inv = Inventory('tree-root-321', revision_id='rev_outer')
278
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
279
                                        'rev_outer', 'rev_inner'))
280
        inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
281
        inv.add(inventory.InventoryDirectory('dir-id', 'dir', 
282
                                             'tree-root-321'))
283
        inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
284
        inv['tree-root-321'].revision = 'rev_outer'
285
        inv['dir-id'].revision = 'rev_outer'
286
        inv['file-id'].revision = 'rev_outer'
287
        inv['link-id'].revision = 'rev_outer'
288
        txt = xml7.serializer_v7.write_inventory_to_string(inv)
289
        self.assertEqualDiff(_expected_inv_v7, txt)
290
        inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
291
        self.assertEqual(5, len(inv2))
292
        for path, ie in inv.iter_entries():
293
            self.assertEqual(ie, inv2[ie.file_id])
294
295
    def test_wrong_format_v7(self):
296
        """Can't accidentally open a file with wrong serializer"""
297
        s_v6 = bzrlib.xml6.serializer_v6
298
        s_v7 = xml7.serializer_v7
299
        self.assertRaises(errors.UnexpectedInventoryFormat, 
300
                          s_v7.read_inventory_from_string, _expected_inv_v5)
301
        self.assertRaises(errors.UnexpectedInventoryFormat, 
302
                          s_v6.read_inventory_from_string, _expected_inv_v7)
303
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
304
    def test_tree_reference(self):
305
        s_v5 = bzrlib.xml5.serializer_v5
306
        s_v6 = bzrlib.xml6.serializer_v6
307
        s_v7 = xml7.serializer_v7
308
        inv = Inventory('tree-root-321')
309
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
310
                                        'rev-outer', 'rev-inner'))
311
        self.assertRaises(errors.UnsupportedInventoryKind, 
312
                          s_v5.write_inventory_to_string, inv)
313
        self.assertRaises(errors.UnsupportedInventoryKind, 
314
                          s_v6.write_inventory_to_string, inv)
315
        txt = s_v7.write_inventory_to_string(inv)
316
        inv2 = s_v7.read_inventory_from_string(txt)
317
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
318
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
319
        self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
320
        self.assertRaises(errors.UnsupportedInventoryKind, 
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
321
                          s_v6.read_inventory_from_string,
322
                          txt.replace('format="7"', 'format="6"'))
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
323
        self.assertRaises(errors.UnsupportedInventoryKind, 
324
                          s_v5.read_inventory_from_string,
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
325
                          txt.replace('format="7"', 'format="5"'))
2255.6.1 by Aaron Bentley
Merge from by-reference-trees
326
2249.5.4 by John Arbash Meinel
When reading XML, always return utf-8 revision ids.
327
    def test_revision_ids_are_utf8(self):
328
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
329
        s_v5 = bzrlib.xml5.serializer_v5
330
        rev = s_v5.read_revision_from_string(_revision_v5)
331
        self.assertIsInstance(rev.revision_id, str)
332
        for parent_id in rev.parent_ids:
333
            self.assertIsInstance(parent_id, str)
334
335
        # ie.revision should either be None or a utf-8 revision id
336
        inv = s_v5.read_inventory_from_string(_committed_inv_v5)
337
        for path, ie in inv.iter_entries():
338
            if ie.revision is None:
339
                continue
340
            self.assertIsInstance(ie.revision, str)
2249.5.10 by John Arbash Meinel
Make sure xml5 can handle unicode or utf8 strings
341
342
343
class TestEncodeAndEscape(TestCase):
344
    """Whitebox testing of the _encode_and_escape function."""
345
346
    def setUp(self):
347
        # Keep the cache clear before and after the test
348
        bzrlib.xml5._ensure_utf8_re()
349
        bzrlib.xml5._clear_cache()
350
        self.addCleanup(bzrlib.xml5._clear_cache)
351
352
    def test_simple_ascii(self):
353
        # _encode_and_escape always appends a final ", because these parameters
354
        # are being used in xml attributes, and by returning it now, we have to
355
        # do fewer string operations later.
356
        val = bzrlib.xml5._encode_and_escape('foo bar')
357
        self.assertEqual('foo bar"', val)
358
        # The second time should be cached
359
        val2 = bzrlib.xml5._encode_and_escape('foo bar')
360
        self.assertIs(val2, val)
361
362
    def test_ascii_with_xml(self):
363
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
364
                         bzrlib.xml5._encode_and_escape('&\'"<>'))
365
366
    def test_utf8_with_xml(self):
367
        # u'\xb5\xe5&\u062c'
368
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
369
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
370
                         bzrlib.xml5._encode_and_escape(utf8_str))
371
372
    def test_unicode(self):
373
        uni_str = u'\xb5\xe5&\u062c'
374
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
375
                         bzrlib.xml5._encode_and_escape(uni_str))