~bzr-pqm/bzr/bzr.dev

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,
2520.4.101 by Aaron Bentley
Use a registry to look up xml serializers by format
23
    xml_serializer,
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
24
    )
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
25
from bzrlib.tests import TestCase
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
26
from bzrlib.inventory import Inventory, InventoryEntry
1304 by Martin Pool
- fix up imports of serializer_v4
27
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
28
import bzrlib.xml5
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
29
30
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
31
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
32
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
33
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
34
</inventory>"""
35
1182 by Martin Pool
- more disentangling of xml storage format from objects
36
37
_revision_v4 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
38
    inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
39
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
40
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
2102.4.1 by John Arbash Meinel
Switch to using millisecond resolution in Revision XML
41
    timestamp="1125907235.212"
1182 by Martin Pool
- more disentangling of xml storage format from objects
42
    timezone="36000">
43
<message>- start splitting code for xml (de)serialization away from objects
44
  preparatory to supporting multiple formats by a single library
45
</message>
46
<parents>
47
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
48
</parents>
49
</revision>
50
"""
51
1183 by Martin Pool
- implement version 5 xml storage, and tests
52
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
53
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
54
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
2102.4.1 by John Arbash Meinel
Switch to using millisecond resolution in Revision XML
55
    timestamp="1125907235.212"
1183 by Martin Pool
- implement version 5 xml storage, and tests
56
    timezone="36000">
57
<message>- start splitting code for xml (de)serialization away from objects
58
  preparatory to supporting multiple formats by a single library
59
</message>
60
<parents>
61
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
62
</parents>
63
</revision>
64
"""
65
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
66
_revision_v5_utc = """\
67
<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
68
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
69
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
2102.4.1 by John Arbash Meinel
Switch to using millisecond resolution in Revision XML
70
    timestamp="1125907235.212"
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
71
    timezone="0">
72
<message>- start splitting code for xml (de)serialization away from objects
73
  preparatory to supporting multiple formats by a single library
74
</message>
75
<parents>
76
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
77
</parents>
78
</revision>
79
"""
80
1183 by Martin Pool
- implement version 5 xml storage, and tests
81
_committed_inv_v5 = """<inventory>
82
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
83
      name="bar" parent_id="TREE_ROOT" 
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
84
      revision="mbp@foo-123123"
85
      text_sha1="A" text_size="1"/>
1183 by Martin Pool
- implement version 5 xml storage, and tests
86
<directory name="subdir"
87
           file_id="foo-20050801201819-4139aa4a272f4250"
88
           parent_id="TREE_ROOT" 
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
89
           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
90
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" 
1183 by Martin Pool
- implement version 5 xml storage, and tests
91
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
92
      revision="mbp@foo-00"
93
      text_sha1="B" text_size="0"/>
1183 by Martin Pool
- implement version 5 xml storage, and tests
94
</inventory>
95
"""
96
1638.1.2 by Robert Collins
Change the basis-inventory file to not have the revision-id in the file name.
97
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
98
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
99
      name="bar" parent_id="TREE_ROOT" 
100
      revision="mbp@foo-123123"/>
101
<directory name="subdir"
102
           file_id="foo-20050801201819-4139aa4a272f4250"
103
           parent_id="TREE_ROOT" 
104
           revision="mbp@foo-00"/>
105
<file file_id="bar-20050824000535-6bc48cfad47ed134" 
106
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
107
      revision="mbp@foo-00"/>
108
</inventory>
109
"""
110
1886.1.1 by John Arbash Meinel
Fix bug #47782,
111
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
112
# 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
113
_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
114
<message>- start splitting code for xml (de)serialization away from objects
115
  preparatory to supporting multiple formats by a single library
116
</message>
117
<parents>
118
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
119
</parents>
120
</revision>
121
"""
122
123
124
# DO NOT REFLOW THIS. Its the exact inventory we want.
125
_expected_inv_v5 = """<inventory format="5">
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
126
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
127
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
128
<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" />
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
129
</inventory>
130
"""
131
132
133
_expected_inv_v5_root = """<inventory file_id="f&lt;" format="5" revision_id="mother!">
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
134
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
135
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f&lt;" revision="mbp@foo-00" />
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
136
<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" />
137
<symlink file_id="link-1" name="link" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" symlink_target="a" />
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
138
</inventory>
139
"""
140
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
141
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
142
<directory file_id="tree-root-321" name="" revision="rev_outer" />
143
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
144
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
145
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
146
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
147
</inventory>
148
"""
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
149
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
150
_revision_utf8_v5 = """<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
151
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
152
    revision_id="erik@b&#229;gfors-02"
153
    timestamp="1125907235.212"
154
    timezone="36000">
155
<message>Include &#181;nicode characters
156
</message>
157
<parents>
158
<revision_ref revision_id="erik@b&#229;gfors-01"/>
159
</parents>
160
</revision>
161
"""
162
163
_inventory_utf8_v5 = """<inventory file_id="TRE&#233;_ROOT" format="5"
164
                                   revision_id="erik@b&#229;gfors-02">
165
<file file_id="b&#229;r-01"
2294.1.9 by John Arbash Meinel
Minor performance improvement, use None as signal rather than ROOT_ID
166
      name="b&#229;r" parent_id="TRE&#233;_ROOT"
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
167
      revision="erik@b&#229;gfors-01"/>
168
<directory name="s&#181;bdir"
169
           file_id="s&#181;bdir-01"
2294.1.9 by John Arbash Meinel
Minor performance improvement, use None as signal rather than ROOT_ID
170
           parent_id="TRE&#233;_ROOT"
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
171
           revision="erik@b&#229;gfors-01"/>
172
<file executable="yes" file_id="b&#229;r-02"
173
      name="b&#229;r" parent_id="s&#181;bdir-01"
174
      revision="erik@b&#229;gfors-02"/>
175
</inventory>
176
"""
177
2917.2.1 by John Arbash Meinel
Fix bug #152360. The xml5 serializer should be using
178
# Before revision_id was always stored as an attribute
179
_inventory_v5a = """<inventory format="5">
180
</inventory>
181
"""
182
183
# Before revision_id was always stored as an attribute
184
_inventory_v5b = """<inventory format="5" revision_id="a-rev-id">
185
</inventory>
186
"""
187
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
188
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
189
class TestSerializer(TestCase):
190
    """Test XML serialization"""
2889.1.1 by Robert Collins
* The class ``bzrlib.repofmt.knitrepo.KnitRepository3`` has been folded into
191
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
192
    def test_canned_inventory(self):
193
        """Test unpacked a canned inventory v4 file."""
194
        inp = StringIO(_working_inventory_v4)
195
        inv = serializer_v4.read_inventory(inp)
196
        self.assertEqual(len(inv), 4)
197
        self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
1182 by Martin Pool
- more disentangling of xml storage format from objects
198
199
    def test_unpack_revision(self):
200
        """Test unpacking a canned revision v4"""
201
        inp = StringIO(_revision_v4)
202
        rev = serializer_v4.read_revision(inp)
203
        eq = self.assertEqual
204
        eq(rev.committer,
205
           "Martin Pool <mbp@sourcefrog.net>")
206
        eq(rev.inventory_id,
207
           "mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
208
        eq(len(rev.parent_ids), 1)
209
        eq(rev.parent_ids[0],
1182 by Martin Pool
- more disentangling of xml storage format from objects
210
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
1183 by Martin Pool
- implement version 5 xml storage, and tests
211
212
    def test_unpack_revision_5(self):
213
        """Test unpacking a canned revision v5"""
214
        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
215
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
1183 by Martin Pool
- implement version 5 xml storage, and tests
216
        eq = self.assertEqual
217
        eq(rev.committer,
218
           "Martin Pool <mbp@sourcefrog.net>")
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
219
        eq(len(rev.parent_ids), 1)
1183 by Martin Pool
- implement version 5 xml storage, and tests
220
        eq(rev.timezone, 36000)
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
221
        eq(rev.parent_ids[0],
1183 by Martin Pool
- implement version 5 xml storage, and tests
222
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
223
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
224
    def test_unpack_revision_5_utc(self):
225
        inp = StringIO(_revision_v5_utc)
226
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
227
        eq = self.assertEqual
228
        eq(rev.committer,
229
           "Martin Pool <mbp@sourcefrog.net>")
230
        eq(len(rev.parent_ids), 1)
231
        eq(rev.timezone, 0)
232
        eq(rev.parent_ids[0],
233
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
234
1183 by Martin Pool
- implement version 5 xml storage, and tests
235
    def test_unpack_inventory_5(self):
236
        """Unpack canned new-style inventory"""
237
        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
238
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
1183 by Martin Pool
- implement version 5 xml storage, and tests
239
        eq = self.assertEqual
240
        eq(len(inv), 4)
241
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
242
        eq(ie.kind, 'file')
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
243
        eq(ie.revision, 'mbp@foo-00')
1183 by Martin Pool
- implement version 5 xml storage, and tests
244
        eq(ie.name, 'bar')
245
        eq(inv[ie.parent_id].kind, 'directory')
1184 by Martin Pool
- fix v5 packing of inventory entries
246
1638.1.2 by Robert Collins
Change the basis-inventory file to not have the revision-id in the file name.
247
    def test_unpack_basis_inventory_5(self):
248
        """Unpack canned new-style inventory"""
249
        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
250
        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.
251
        eq = self.assertEqual
252
        eq(len(inv), 4)
253
        eq(inv.revision_id, 'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
254
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
255
        eq(ie.kind, 'file')
256
        eq(ie.revision, 'mbp@foo-00')
257
        eq(ie.name, 'bar')
258
        eq(inv[ie.parent_id].kind, 'directory')
259
2917.2.1 by John Arbash Meinel
Fix bug #152360. The xml5 serializer should be using
260
    def test_unpack_inventory_5a(self):
261
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
262
                _inventory_v5a, revision_id='test-rev-id')
263
        self.assertEqual('test-rev-id', inv.root.revision)
264
265
    def test_unpack_inventory_5b(self):
266
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
267
                _inventory_v5b, revision_id='test-rev-id')
268
        self.assertEqual('a-rev-id', inv.root.revision)
269
1184 by Martin Pool
- fix v5 packing of inventory entries
270
    def test_repack_inventory_5(self):
271
        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
272
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
1184 by Martin Pool
- fix v5 packing of inventory entries
273
        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
274
        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
275
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
276
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
277
        self.assertEqual(inv, inv2)
278
    
279
    def assertRoundTrips(self, xml_string):
280
        inp = StringIO(xml_string)
281
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
282
        outp = StringIO()
283
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
284
        self.assertEqualDiff(xml_string, outp.getvalue())
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
285
        lines = bzrlib.xml5.serializer_v5.write_inventory_to_lines(inv)
286
        outp.seek(0)
287
        self.assertEqual(outp.readlines(), lines)
1934.1.3 by John Arbash Meinel
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences
288
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
289
        self.assertEqual(inv, inv2)
290
291
    def tests_serialize_inventory_v5_with_root(self):
292
        self.assertRoundTrips(_expected_inv_v5_root)
1185 by Martin Pool
- add xml round-trip test for revisions
293
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
294
    def check_repack_revision(self, txt):
295
        """Check that repacking a revision yields the same information"""
296
        inp = StringIO(txt)
297
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
298
        outp = StringIO()
299
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
300
        outfile_contents = outp.getvalue()
301
        rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
302
        self.assertEqual(rev, rev2)
303
1185 by Martin Pool
- add xml round-trip test for revisions
304
    def test_repack_revision_5(self):
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
305
        """Round-trip revision to XML v5"""
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
306
        self.check_repack_revision(_revision_v5)
307
308
    def test_repack_revision_5_utc(self):
309
        self.check_repack_revision(_revision_v5_utc)
1185 by Martin Pool
- add xml round-trip test for revisions
310
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
311
    def test_pack_revision_5(self):
312
        """Pack revision to XML v5"""
313
        # 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
314
        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
315
        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
316
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
317
        outfile_contents = outp.getvalue()
318
        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
319
        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
320
        self.assertEqualDiff(outfile_contents, _expected_rev_v5)
1886.1.1 by John Arbash Meinel
Fix bug #47782,
321
322
    def test_empty_property_value(self):
323
        """Create an empty property value check that it serializes correctly"""
324
        s_v5 = bzrlib.xml5.serializer_v5
325
        rev = s_v5.read_revision_from_string(_revision_v5)
326
        outp = StringIO()
327
        props = {'empty':'', 'one':'one'}
328
        rev.properties = props
329
        txt = s_v5.write_revision_to_string(rev)
330
        new_rev = s_v5.read_revision_from_string(txt)
331
        self.assertEqual(props, new_rev.properties)
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
332
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
333
    def test_roundtrip_inventory_v7(self):
334
        inv = Inventory('tree-root-321', revision_id='rev_outer')
335
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
336
                                        'rev_outer', 'rev_inner'))
337
        inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
338
        inv.add(inventory.InventoryDirectory('dir-id', 'dir', 
339
                                             'tree-root-321'))
340
        inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
341
        inv['tree-root-321'].revision = 'rev_outer'
342
        inv['dir-id'].revision = 'rev_outer'
343
        inv['file-id'].revision = 'rev_outer'
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
344
        inv['file-id'].text_sha1 = 'A'
345
        inv['file-id'].text_size = 1
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
346
        inv['link-id'].revision = 'rev_outer'
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
347
        inv['link-id'].symlink_target = 'a'
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
348
        txt = xml7.serializer_v7.write_inventory_to_string(inv)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
349
        lines = xml7.serializer_v7.write_inventory_to_lines(inv)
350
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
351
        self.assertEqualDiff(_expected_inv_v7, txt)
352
        inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
353
        self.assertEqual(5, len(inv2))
354
        for path, ie in inv.iter_entries():
355
            self.assertEqual(ie, inv2[ie.file_id])
356
357
    def test_wrong_format_v7(self):
358
        """Can't accidentally open a file with wrong serializer"""
359
        s_v6 = bzrlib.xml6.serializer_v6
360
        s_v7 = xml7.serializer_v7
361
        self.assertRaises(errors.UnexpectedInventoryFormat, 
362
                          s_v7.read_inventory_from_string, _expected_inv_v5)
363
        self.assertRaises(errors.UnexpectedInventoryFormat, 
364
                          s_v6.read_inventory_from_string, _expected_inv_v7)
365
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
366
    def test_tree_reference(self):
367
        s_v5 = bzrlib.xml5.serializer_v5
368
        s_v6 = bzrlib.xml6.serializer_v6
369
        s_v7 = xml7.serializer_v7
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
370
        inv = Inventory('tree-root-321', revision_id='rev-outer')
371
        inv.root.revision = 'root-rev'
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
372
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
373
                                        'rev-outer', 'rev-inner'))
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
374
        self.assertRaises(errors.UnsupportedInventoryKind,
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
375
                          s_v5.write_inventory_to_string, inv)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
376
        self.assertRaises(errors.UnsupportedInventoryKind,
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
377
                          s_v6.write_inventory_to_string, inv)
378
        txt = s_v7.write_inventory_to_string(inv)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
379
        lines = s_v7.write_inventory_to_lines(inv)
380
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
381
        inv2 = s_v7.read_inventory_from_string(txt)
382
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
383
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
384
        self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
385
        self.assertRaises(errors.UnsupportedInventoryKind, 
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
386
                          s_v6.read_inventory_from_string,
387
                          txt.replace('format="7"', 'format="6"'))
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
388
        self.assertRaises(errors.UnsupportedInventoryKind, 
389
                          s_v5.read_inventory_from_string,
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
390
                          txt.replace('format="7"', 'format="5"'))
2255.6.1 by Aaron Bentley
Merge from by-reference-trees
391
2249.5.4 by John Arbash Meinel
When reading XML, always return utf-8 revision ids.
392
    def test_revision_ids_are_utf8(self):
393
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
394
        s_v5 = bzrlib.xml5.serializer_v5
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
395
        rev = s_v5.read_revision_from_string(_revision_utf8_v5)
396
        self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
2249.5.4 by John Arbash Meinel
When reading XML, always return utf-8 revision ids.
397
        self.assertIsInstance(rev.revision_id, str)
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
398
        self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
2249.5.4 by John Arbash Meinel
When reading XML, always return utf-8 revision ids.
399
        for parent_id in rev.parent_ids:
400
            self.assertIsInstance(parent_id, str)
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
401
        self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
402
        self.assertIsInstance(rev.message, unicode)
2249.5.4 by John Arbash Meinel
When reading XML, always return utf-8 revision ids.
403
404
        # ie.revision should either be None or a utf-8 revision id
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
405
        inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
406
        rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
407
        rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
408
        fid_root = u'TRE\xe9_ROOT'.encode('utf8')
409
        fid_bar1 = u'b\xe5r-01'.encode('utf8')
410
        fid_sub = u's\xb5bdir-01'.encode('utf8')
411
        fid_bar2 = u'b\xe5r-02'.encode('utf8')
2889.1.1 by Robert Collins
* The class ``bzrlib.repofmt.knitrepo.KnitRepository3`` has been folded into
412
        expected = [(u'', fid_root, None, rev_id_2),
2294.1.6 by John Arbash Meinel
Include parent_id checks in the xml serializer tests.
413
                    (u'b\xe5r', fid_bar1, fid_root, rev_id_1),
414
                    (u's\xb5bdir', fid_sub, fid_root, rev_id_1),
415
                    (u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
416
                   ]
417
        self.assertEqual(rev_id_2, inv.revision_id)
418
        self.assertIsInstance(inv.revision_id, str)
419
420
        actual = list(inv.iter_entries_by_dir())
2294.1.6 by John Arbash Meinel
Include parent_id checks in the xml serializer tests.
421
        for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
422
             (act_path, act_ie)) in zip(expected, actual):
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
423
            self.assertEqual(exp_path, act_path)
424
            self.assertIsInstance(act_path, unicode)
425
            self.assertEqual(exp_file_id, act_ie.file_id)
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
426
            self.assertIsInstance(act_ie.file_id, str)
2294.1.6 by John Arbash Meinel
Include parent_id checks in the xml serializer tests.
427
            self.assertEqual(exp_parent_id, act_ie.parent_id)
428
            if exp_parent_id is not None:
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
429
                self.assertIsInstance(act_ie.parent_id, str)
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
430
            self.assertEqual(exp_rev_id, act_ie.revision)
431
            if exp_rev_id is not None:
432
                self.assertIsInstance(act_ie.revision, str)
433
434
        self.assertEqual(len(expected), len(actual))
2249.5.10 by John Arbash Meinel
Make sure xml5 can handle unicode or utf8 strings
435
2520.4.101 by Aaron Bentley
Use a registry to look up xml serializers by format
436
    def test_registry(self):
437
        self.assertIs(serializer_v4,
438
                      xml_serializer.format_registry.get('4'))
439
        self.assertIs(bzrlib.xml5.serializer_v5,
440
                      xml_serializer.format_registry.get('5'))
441
        self.assertIs(bzrlib.xml6.serializer_v6,
442
                      xml_serializer.format_registry.get('6'))
443
        self.assertIs(bzrlib.xml7.serializer_v7,
444
                      xml_serializer.format_registry.get('7'))
445
2249.5.10 by John Arbash Meinel
Make sure xml5 can handle unicode or utf8 strings
446
447
class TestEncodeAndEscape(TestCase):
448
    """Whitebox testing of the _encode_and_escape function."""
449
450
    def setUp(self):
451
        # Keep the cache clear before and after the test
452
        bzrlib.xml5._ensure_utf8_re()
453
        bzrlib.xml5._clear_cache()
454
        self.addCleanup(bzrlib.xml5._clear_cache)
455
456
    def test_simple_ascii(self):
457
        # _encode_and_escape always appends a final ", because these parameters
458
        # are being used in xml attributes, and by returning it now, we have to
459
        # do fewer string operations later.
460
        val = bzrlib.xml5._encode_and_escape('foo bar')
461
        self.assertEqual('foo bar"', val)
462
        # The second time should be cached
463
        val2 = bzrlib.xml5._encode_and_escape('foo bar')
464
        self.assertIs(val2, val)
465
466
    def test_ascii_with_xml(self):
467
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
468
                         bzrlib.xml5._encode_and_escape('&\'"<>'))
469
470
    def test_utf8_with_xml(self):
471
        # u'\xb5\xe5&\u062c'
472
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
473
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
474
                         bzrlib.xml5._encode_and_escape(utf8_str))
475
476
    def test_unicode(self):
477
        uni_str = u'\xb5\xe5&\u062c'
478
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
479
                         bzrlib.xml5._encode_and_escape(uni_str))