1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
# Copyright (C) 2005 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from cStringIO import StringIO
from bzrlib import (
errors,
inventory,
xml7,
)
from bzrlib.tests import TestCase
from bzrlib.inventory import Inventory, InventoryEntry
from bzrlib.xml4 import serializer_v4
import bzrlib.xml5
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
</inventory>"""
_revision_v4 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
timestamp="1125907235.212"
timezone="36000">
<message>- start splitting code for xml (de)serialization away from objects
preparatory to supporting multiple formats by a single library
</message>
<parents>
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
</parents>
</revision>
"""
_revision_v5 = """<revision committer="Martin Pool <mbp@sourcefrog.net>"
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
timestamp="1125907235.212"
timezone="36000">
<message>- start splitting code for xml (de)serialization away from objects
preparatory to supporting multiple formats by a single library
</message>
<parents>
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
</parents>
</revision>
"""
_revision_v5_utc = """\
<revision committer="Martin Pool <mbp@sourcefrog.net>"
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
timestamp="1125907235.212"
timezone="0">
<message>- start splitting code for xml (de)serialization away from objects
preparatory to supporting multiple formats by a single library
</message>
<parents>
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
</parents>
</revision>
"""
_committed_inv_v5 = """<inventory>
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
name="bar" parent_id="TREE_ROOT"
revision="mbp@foo-123123"/>
<directory name="subdir"
file_id="foo-20050801201819-4139aa4a272f4250"
parent_id="TREE_ROOT"
revision="mbp@foo-00"/>
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
revision="mbp@foo-00"/>
</inventory>
"""
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
name="bar" parent_id="TREE_ROOT"
revision="mbp@foo-123123"/>
<directory name="subdir"
file_id="foo-20050801201819-4139aa4a272f4250"
parent_id="TREE_ROOT"
revision="mbp@foo-00"/>
<file file_id="bar-20050824000535-6bc48cfad47ed134"
name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
revision="mbp@foo-00"/>
</inventory>
"""
# DO NOT REFLOW THIS. Its the exact revision we want.
_expected_rev_v5 = """<revision committer="Martin Pool <mbp@sourcefrog.net>" format="5" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
<message>- start splitting code for xml (de)serialization away from objects
preparatory to supporting multiple formats by a single library
</message>
<parents>
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
</parents>
</revision>
"""
# DO NOT REFLOW THIS. Its the exact inventory we want.
_expected_inv_v5 = """<inventory format="5">
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" />
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
</inventory>
"""
_expected_inv_v5_root = """<inventory file_id="f<" format="5" revision_id="mother!">
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f<" revision="mbp@foo-123123" />
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f<" revision="mbp@foo-00" />
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
</inventory>
"""
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
<directory file_id="tree-root-321" name="" revision="rev_outer" />
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" />
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" />
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
</inventory>
"""
_revision_utf8_v5 = """<revision committer="Erik Bågfors <erik@foo.net>"
inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
revision_id="erik@bågfors-02"
timestamp="1125907235.212"
timezone="36000">
<message>Include µnicode characters
</message>
<parents>
<revision_ref revision_id="erik@bågfors-01"/>
</parents>
</revision>
"""
_inventory_utf8_v5 = """<inventory file_id="TREé_ROOT" format="5"
revision_id="erik@bågfors-02">
<file file_id="bår-01"
name="bår" parent_id="TREé_ROOT"
revision="erik@bågfors-01"/>
<directory name="sµbdir"
file_id="sµbdir-01"
parent_id="TREé_ROOT"
revision="erik@bågfors-01"/>
<file executable="yes" file_id="bår-02"
name="bår" parent_id="sµbdir-01"
revision="erik@bågfors-02"/>
</inventory>
"""
class TestSerializer(TestCase):
"""Test XML serialization"""
def test_canned_inventory(self):
"""Test unpacked a canned inventory v4 file."""
inp = StringIO(_working_inventory_v4)
inv = serializer_v4.read_inventory(inp)
self.assertEqual(len(inv), 4)
self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
def test_unpack_revision(self):
"""Test unpacking a canned revision v4"""
inp = StringIO(_revision_v4)
rev = serializer_v4.read_revision(inp)
eq = self.assertEqual
eq(rev.committer,
"Martin Pool <mbp@sourcefrog.net>")
eq(rev.inventory_id,
"mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
eq(len(rev.parent_ids), 1)
eq(rev.parent_ids[0],
"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
def test_unpack_revision_5(self):
"""Test unpacking a canned revision v5"""
inp = StringIO(_revision_v5)
rev = bzrlib.xml5.serializer_v5.read_revision(inp)
eq = self.assertEqual
eq(rev.committer,
"Martin Pool <mbp@sourcefrog.net>")
eq(len(rev.parent_ids), 1)
eq(rev.timezone, 36000)
eq(rev.parent_ids[0],
"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
def test_unpack_revision_5_utc(self):
inp = StringIO(_revision_v5_utc)
rev = bzrlib.xml5.serializer_v5.read_revision(inp)
eq = self.assertEqual
eq(rev.committer,
"Martin Pool <mbp@sourcefrog.net>")
eq(len(rev.parent_ids), 1)
eq(rev.timezone, 0)
eq(rev.parent_ids[0],
"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
def test_unpack_inventory_5(self):
"""Unpack canned new-style inventory"""
inp = StringIO(_committed_inv_v5)
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
eq = self.assertEqual
eq(len(inv), 4)
ie = inv['bar-20050824000535-6bc48cfad47ed134']
eq(ie.kind, 'file')
eq(ie.revision, 'mbp@foo-00')
eq(ie.name, 'bar')
eq(inv[ie.parent_id].kind, 'directory')
def test_unpack_basis_inventory_5(self):
"""Unpack canned new-style inventory"""
inp = StringIO(_basis_inv_v5)
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
eq = self.assertEqual
eq(len(inv), 4)
eq(inv.revision_id, 'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
ie = inv['bar-20050824000535-6bc48cfad47ed134']
eq(ie.kind, 'file')
eq(ie.revision, 'mbp@foo-00')
eq(ie.name, 'bar')
eq(inv[ie.parent_id].kind, 'directory')
def test_repack_inventory_5(self):
inp = StringIO(_committed_inv_v5)
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
outp = StringIO()
bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
self.assertEqual(inv, inv2)
def assertRoundTrips(self, xml_string):
inp = StringIO(xml_string)
inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
outp = StringIO()
bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
self.assertEqualDiff(xml_string, outp.getvalue())
inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
self.assertEqual(inv, inv2)
def tests_serialize_inventory_v5_with_root(self):
self.assertRoundTrips(_expected_inv_v5_root)
def check_repack_revision(self, txt):
"""Check that repacking a revision yields the same information"""
inp = StringIO(txt)
rev = bzrlib.xml5.serializer_v5.read_revision(inp)
outp = StringIO()
bzrlib.xml5.serializer_v5.write_revision(rev, outp)
outfile_contents = outp.getvalue()
rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
self.assertEqual(rev, rev2)
def test_repack_revision_5(self):
"""Round-trip revision to XML v5"""
self.check_repack_revision(_revision_v5)
def test_repack_revision_5_utc(self):
self.check_repack_revision(_revision_v5_utc)
def test_pack_revision_5(self):
"""Pack revision to XML v5"""
# fixed 20051025, revisions should have final newline
rev = bzrlib.xml5.serializer_v5.read_revision_from_string(_revision_v5)
outp = StringIO()
bzrlib.xml5.serializer_v5.write_revision(rev, outp)
outfile_contents = outp.getvalue()
self.assertEqual(outfile_contents[-1], '\n')
self.assertEqualDiff(outfile_contents, bzrlib.xml5.serializer_v5.write_revision_to_string(rev))
self.assertEqualDiff(outfile_contents, _expected_rev_v5)
def test_empty_property_value(self):
"""Create an empty property value check that it serializes correctly"""
s_v5 = bzrlib.xml5.serializer_v5
rev = s_v5.read_revision_from_string(_revision_v5)
outp = StringIO()
props = {'empty':'', 'one':'one'}
rev.properties = props
txt = s_v5.write_revision_to_string(rev)
new_rev = s_v5.read_revision_from_string(txt)
self.assertEqual(props, new_rev.properties)
def test_roundtrip_inventory_v7(self):
inv = Inventory('tree-root-321', revision_id='rev_outer')
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
'rev_outer', 'rev_inner'))
inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
inv.add(inventory.InventoryDirectory('dir-id', 'dir',
'tree-root-321'))
inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
inv['tree-root-321'].revision = 'rev_outer'
inv['dir-id'].revision = 'rev_outer'
inv['file-id'].revision = 'rev_outer'
inv['link-id'].revision = 'rev_outer'
txt = xml7.serializer_v7.write_inventory_to_string(inv)
self.assertEqualDiff(_expected_inv_v7, txt)
inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
self.assertEqual(5, len(inv2))
for path, ie in inv.iter_entries():
self.assertEqual(ie, inv2[ie.file_id])
def test_wrong_format_v7(self):
"""Can't accidentally open a file with wrong serializer"""
s_v6 = bzrlib.xml6.serializer_v6
s_v7 = xml7.serializer_v7
self.assertRaises(errors.UnexpectedInventoryFormat,
s_v7.read_inventory_from_string, _expected_inv_v5)
self.assertRaises(errors.UnexpectedInventoryFormat,
s_v6.read_inventory_from_string, _expected_inv_v7)
def test_tree_reference(self):
s_v5 = bzrlib.xml5.serializer_v5
s_v6 = bzrlib.xml6.serializer_v6
s_v7 = xml7.serializer_v7
inv = Inventory('tree-root-321')
inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
'rev-outer', 'rev-inner'))
self.assertRaises(errors.UnsupportedInventoryKind,
s_v5.write_inventory_to_string, inv)
self.assertRaises(errors.UnsupportedInventoryKind,
s_v6.write_inventory_to_string, inv)
txt = s_v7.write_inventory_to_string(inv)
inv2 = s_v7.read_inventory_from_string(txt)
self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
self.assertEqual('rev-outer', inv2['nested-id'].revision)
self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
self.assertRaises(errors.UnsupportedInventoryKind,
s_v6.read_inventory_from_string,
txt.replace('format="7"', 'format="6"'))
self.assertRaises(errors.UnsupportedInventoryKind,
s_v5.read_inventory_from_string,
txt.replace('format="7"', 'format="5"'))
def test_revision_ids_are_utf8(self):
"""Parsed revision_ids should all be utf-8 strings, not unicode."""
s_v5 = bzrlib.xml5.serializer_v5
rev = s_v5.read_revision_from_string(_revision_utf8_v5)
self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
self.assertIsInstance(rev.revision_id, str)
self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
for parent_id in rev.parent_ids:
self.assertIsInstance(parent_id, str)
self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
self.assertIsInstance(rev.message, unicode)
# ie.revision should either be None or a utf-8 revision id
inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
fid_root = u'TRE\xe9_ROOT'.encode('utf8')
fid_bar1 = u'b\xe5r-01'.encode('utf8')
fid_sub = u's\xb5bdir-01'.encode('utf8')
fid_bar2 = u'b\xe5r-02'.encode('utf8')
expected = [(u'', fid_root, None, None),
(u'b\xe5r', fid_bar1, fid_root, rev_id_1),
(u's\xb5bdir', fid_sub, fid_root, rev_id_1),
(u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
]
self.assertEqual(rev_id_2, inv.revision_id)
self.assertIsInstance(inv.revision_id, str)
actual = list(inv.iter_entries_by_dir())
for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
(act_path, act_ie)) in zip(expected, actual):
self.assertEqual(exp_path, act_path)
self.assertIsInstance(act_path, unicode)
self.assertEqual(exp_file_id, act_ie.file_id)
self.assertIsInstance(act_ie.file_id, str)
self.assertEqual(exp_parent_id, act_ie.parent_id)
if exp_parent_id is not None:
self.assertIsInstance(act_ie.parent_id, str)
self.assertEqual(exp_rev_id, act_ie.revision)
if exp_rev_id is not None:
self.assertIsInstance(act_ie.revision, str)
self.assertEqual(len(expected), len(actual))
class TestEncodeAndEscape(TestCase):
"""Whitebox testing of the _encode_and_escape function."""
def setUp(self):
# Keep the cache clear before and after the test
bzrlib.xml5._ensure_utf8_re()
bzrlib.xml5._clear_cache()
self.addCleanup(bzrlib.xml5._clear_cache)
def test_simple_ascii(self):
# _encode_and_escape always appends a final ", because these parameters
# are being used in xml attributes, and by returning it now, we have to
# do fewer string operations later.
val = bzrlib.xml5._encode_and_escape('foo bar')
self.assertEqual('foo bar"', val)
# The second time should be cached
val2 = bzrlib.xml5._encode_and_escape('foo bar')
self.assertIs(val2, val)
def test_ascii_with_xml(self):
self.assertEqual('&'"<>"',
bzrlib.xml5._encode_and_escape('&\'"<>'))
def test_utf8_with_xml(self):
# u'\xb5\xe5&\u062c'
utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
self.assertEqual('µå&ج"',
bzrlib.xml5._encode_and_escape(utf8_str))
def test_unicode(self):
uni_str = u'\xb5\xe5&\u062c'
self.assertEqual('µå&ج"',
bzrlib.xml5._encode_and_escape(uni_str))
|