~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
178
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
179
class TestSerializer(TestCase):
180
    """Test XML serialization"""
2889.1.1 by Robert Collins
* The class ``bzrlib.repofmt.knitrepo.KnitRepository3`` has been folded into
181
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
182
    def test_canned_inventory(self):
183
        """Test unpacked a canned inventory v4 file."""
184
        inp = StringIO(_working_inventory_v4)
185
        inv = serializer_v4.read_inventory(inp)
186
        self.assertEqual(len(inv), 4)
187
        self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
1182 by Martin Pool
- more disentangling of xml storage format from objects
188
189
    def test_unpack_revision(self):
190
        """Test unpacking a canned revision v4"""
191
        inp = StringIO(_revision_v4)
192
        rev = serializer_v4.read_revision(inp)
193
        eq = self.assertEqual
194
        eq(rev.committer,
195
           "Martin Pool <mbp@sourcefrog.net>")
196
        eq(rev.inventory_id,
197
           "mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
198
        eq(len(rev.parent_ids), 1)
199
        eq(rev.parent_ids[0],
1182 by Martin Pool
- more disentangling of xml storage format from objects
200
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
1183 by Martin Pool
- implement version 5 xml storage, and tests
201
202
    def test_unpack_revision_5(self):
203
        """Test unpacking a canned revision v5"""
204
        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
205
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
1183 by Martin Pool
- implement version 5 xml storage, and tests
206
        eq = self.assertEqual
207
        eq(rev.committer,
208
           "Martin Pool <mbp@sourcefrog.net>")
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
209
        eq(len(rev.parent_ids), 1)
1183 by Martin Pool
- implement version 5 xml storage, and tests
210
        eq(rev.timezone, 36000)
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
211
        eq(rev.parent_ids[0],
1183 by Martin Pool
- implement version 5 xml storage, and tests
212
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
213
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
214
    def test_unpack_revision_5_utc(self):
215
        inp = StringIO(_revision_v5_utc)
216
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
217
        eq = self.assertEqual
218
        eq(rev.committer,
219
           "Martin Pool <mbp@sourcefrog.net>")
220
        eq(len(rev.parent_ids), 1)
221
        eq(rev.timezone, 0)
222
        eq(rev.parent_ids[0],
223
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
224
1183 by Martin Pool
- implement version 5 xml storage, and tests
225
    def test_unpack_inventory_5(self):
226
        """Unpack canned new-style inventory"""
227
        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
228
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
1183 by Martin Pool
- implement version 5 xml storage, and tests
229
        eq = self.assertEqual
230
        eq(len(inv), 4)
231
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
232
        eq(ie.kind, 'file')
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
233
        eq(ie.revision, 'mbp@foo-00')
1183 by Martin Pool
- implement version 5 xml storage, and tests
234
        eq(ie.name, 'bar')
235
        eq(inv[ie.parent_id].kind, 'directory')
1184 by Martin Pool
- fix v5 packing of inventory entries
236
1638.1.2 by Robert Collins
Change the basis-inventory file to not have the revision-id in the file name.
237
    def test_unpack_basis_inventory_5(self):
238
        """Unpack canned new-style inventory"""
239
        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
240
        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.
241
        eq = self.assertEqual
242
        eq(len(inv), 4)
243
        eq(inv.revision_id, 'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
244
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
245
        eq(ie.kind, 'file')
246
        eq(ie.revision, 'mbp@foo-00')
247
        eq(ie.name, 'bar')
248
        eq(inv[ie.parent_id].kind, 'directory')
249
1184 by Martin Pool
- fix v5 packing of inventory entries
250
    def test_repack_inventory_5(self):
251
        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
252
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
1184 by Martin Pool
- fix v5 packing of inventory entries
253
        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
254
        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
255
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
256
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
257
        self.assertEqual(inv, inv2)
258
    
259
    def assertRoundTrips(self, xml_string):
260
        inp = StringIO(xml_string)
261
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
262
        outp = StringIO()
263
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
264
        self.assertEqualDiff(xml_string, outp.getvalue())
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
265
        lines = bzrlib.xml5.serializer_v5.write_inventory_to_lines(inv)
266
        outp.seek(0)
267
        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
268
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
269
        self.assertEqual(inv, inv2)
270
271
    def tests_serialize_inventory_v5_with_root(self):
272
        self.assertRoundTrips(_expected_inv_v5_root)
1185 by Martin Pool
- add xml round-trip test for revisions
273
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
274
    def check_repack_revision(self, txt):
275
        """Check that repacking a revision yields the same information"""
276
        inp = StringIO(txt)
277
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
278
        outp = StringIO()
279
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
280
        outfile_contents = outp.getvalue()
281
        rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
282
        self.assertEqual(rev, rev2)
283
1185 by Martin Pool
- add xml round-trip test for revisions
284
    def test_repack_revision_5(self):
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
285
        """Round-trip revision to XML v5"""
1913.1.2 by John Arbash Meinel
Add direct tests to xml serializer
286
        self.check_repack_revision(_revision_v5)
287
288
    def test_repack_revision_5_utc(self):
289
        self.check_repack_revision(_revision_v5_utc)
1185 by Martin Pool
- add xml round-trip test for revisions
290
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
291
    def test_pack_revision_5(self):
292
        """Pack revision to XML v5"""
293
        # 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
294
        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
295
        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
296
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
1185.16.123 by Martin Pool
Fix syntax of serializer_v5.pack_revision_to_string
297
        outfile_contents = outp.getvalue()
298
        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
299
        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
300
        self.assertEqualDiff(outfile_contents, _expected_rev_v5)
1886.1.1 by John Arbash Meinel
Fix bug #47782,
301
302
    def test_empty_property_value(self):
303
        """Create an empty property value check that it serializes correctly"""
304
        s_v5 = bzrlib.xml5.serializer_v5
305
        rev = s_v5.read_revision_from_string(_revision_v5)
306
        outp = StringIO()
307
        props = {'empty':'', 'one':'one'}
308
        rev.properties = props
309
        txt = s_v5.write_revision_to_string(rev)
310
        new_rev = s_v5.read_revision_from_string(txt)
311
        self.assertEqual(props, new_rev.properties)
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
312
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
313
    def test_roundtrip_inventory_v7(self):
314
        inv = Inventory('tree-root-321', revision_id='rev_outer')
315
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
316
                                        'rev_outer', 'rev_inner'))
317
        inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
318
        inv.add(inventory.InventoryDirectory('dir-id', 'dir', 
319
                                             'tree-root-321'))
320
        inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
321
        inv['tree-root-321'].revision = 'rev_outer'
322
        inv['dir-id'].revision = 'rev_outer'
323
        inv['file-id'].revision = 'rev_outer'
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
324
        inv['file-id'].text_sha1 = 'A'
325
        inv['file-id'].text_size = 1
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
326
        inv['link-id'].revision = 'rev_outer'
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
327
        inv['link-id'].symlink_target = 'a'
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
328
        txt = xml7.serializer_v7.write_inventory_to_string(inv)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
329
        lines = xml7.serializer_v7.write_inventory_to_lines(inv)
330
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
331
        self.assertEqualDiff(_expected_inv_v7, txt)
332
        inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
333
        self.assertEqual(5, len(inv2))
334
        for path, ie in inv.iter_entries():
335
            self.assertEqual(ie, inv2[ie.file_id])
336
337
    def test_wrong_format_v7(self):
338
        """Can't accidentally open a file with wrong serializer"""
339
        s_v6 = bzrlib.xml6.serializer_v6
340
        s_v7 = xml7.serializer_v7
341
        self.assertRaises(errors.UnexpectedInventoryFormat, 
342
                          s_v7.read_inventory_from_string, _expected_inv_v5)
343
        self.assertRaises(errors.UnexpectedInventoryFormat, 
344
                          s_v6.read_inventory_from_string, _expected_inv_v7)
345
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
346
    def test_tree_reference(self):
347
        s_v5 = bzrlib.xml5.serializer_v5
348
        s_v6 = bzrlib.xml6.serializer_v6
349
        s_v7 = xml7.serializer_v7
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
350
        inv = Inventory('tree-root-321', revision_id='rev-outer')
351
        inv.root.revision = 'root-rev'
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
352
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
353
                                        'rev-outer', 'rev-inner'))
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
354
        self.assertRaises(errors.UnsupportedInventoryKind,
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
355
                          s_v5.write_inventory_to_string, inv)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
356
        self.assertRaises(errors.UnsupportedInventoryKind,
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
357
                          s_v6.write_inventory_to_string, inv)
358
        txt = s_v7.write_inventory_to_string(inv)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
359
        lines = s_v7.write_inventory_to_lines(inv)
360
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
361
        inv2 = s_v7.read_inventory_from_string(txt)
362
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
363
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
364
        self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
365
        self.assertRaises(errors.UnsupportedInventoryKind, 
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
366
                          s_v6.read_inventory_from_string,
367
                          txt.replace('format="7"', 'format="6"'))
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
368
        self.assertRaises(errors.UnsupportedInventoryKind, 
369
                          s_v5.read_inventory_from_string,
2100.3.2 by Aaron Bentley
Add tests for format 7, enforce number
370
                          txt.replace('format="7"', 'format="5"'))
2255.6.1 by Aaron Bentley
Merge from by-reference-trees
371
2249.5.4 by John Arbash Meinel
When reading XML, always return utf-8 revision ids.
372
    def test_revision_ids_are_utf8(self):
373
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
374
        s_v5 = bzrlib.xml5.serializer_v5
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
375
        rev = s_v5.read_revision_from_string(_revision_utf8_v5)
376
        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.
377
        self.assertIsInstance(rev.revision_id, str)
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
378
        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.
379
        for parent_id in rev.parent_ids:
380
            self.assertIsInstance(parent_id, str)
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
381
        self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
382
        self.assertIsInstance(rev.message, unicode)
2249.5.4 by John Arbash Meinel
When reading XML, always return utf-8 revision ids.
383
384
        # 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,
385
        inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
386
        rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
387
        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
388
        fid_root = u'TRE\xe9_ROOT'.encode('utf8')
389
        fid_bar1 = u'b\xe5r-01'.encode('utf8')
390
        fid_sub = u's\xb5bdir-01'.encode('utf8')
391
        fid_bar2 = u'b\xe5r-02'.encode('utf8')
2889.1.1 by Robert Collins
* The class ``bzrlib.repofmt.knitrepo.KnitRepository3`` has been folded into
392
        expected = [(u'', fid_root, None, rev_id_2),
2294.1.6 by John Arbash Meinel
Include parent_id checks in the xml serializer tests.
393
                    (u'b\xe5r', fid_bar1, fid_root, rev_id_1),
394
                    (u's\xb5bdir', fid_sub, fid_root, rev_id_1),
395
                    (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,
396
                   ]
397
        self.assertEqual(rev_id_2, inv.revision_id)
398
        self.assertIsInstance(inv.revision_id, str)
399
400
        actual = list(inv.iter_entries_by_dir())
2294.1.6 by John Arbash Meinel
Include parent_id checks in the xml serializer tests.
401
        for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
402
             (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,
403
            self.assertEqual(exp_path, act_path)
404
            self.assertIsInstance(act_path, unicode)
405
            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
406
            self.assertIsInstance(act_ie.file_id, str)
2294.1.6 by John Arbash Meinel
Include parent_id checks in the xml serializer tests.
407
            self.assertEqual(exp_parent_id, act_ie.parent_id)
408
            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
409
                self.assertIsInstance(act_ie.parent_id, str)
2294.1.5 by John Arbash Meinel
Fix Inventory.iter_entries_by_dir to return Unicode paths,
410
            self.assertEqual(exp_rev_id, act_ie.revision)
411
            if exp_rev_id is not None:
412
                self.assertIsInstance(act_ie.revision, str)
413
414
        self.assertEqual(len(expected), len(actual))
2249.5.10 by John Arbash Meinel
Make sure xml5 can handle unicode or utf8 strings
415
2520.4.101 by Aaron Bentley
Use a registry to look up xml serializers by format
416
    def test_registry(self):
417
        self.assertIs(serializer_v4,
418
                      xml_serializer.format_registry.get('4'))
419
        self.assertIs(bzrlib.xml5.serializer_v5,
420
                      xml_serializer.format_registry.get('5'))
421
        self.assertIs(bzrlib.xml6.serializer_v6,
422
                      xml_serializer.format_registry.get('6'))
423
        self.assertIs(bzrlib.xml7.serializer_v7,
424
                      xml_serializer.format_registry.get('7'))
425
2249.5.10 by John Arbash Meinel
Make sure xml5 can handle unicode or utf8 strings
426
427
class TestEncodeAndEscape(TestCase):
428
    """Whitebox testing of the _encode_and_escape function."""
429
430
    def setUp(self):
431
        # Keep the cache clear before and after the test
432
        bzrlib.xml5._ensure_utf8_re()
433
        bzrlib.xml5._clear_cache()
434
        self.addCleanup(bzrlib.xml5._clear_cache)
435
436
    def test_simple_ascii(self):
437
        # _encode_and_escape always appends a final ", because these parameters
438
        # are being used in xml attributes, and by returning it now, we have to
439
        # do fewer string operations later.
440
        val = bzrlib.xml5._encode_and_escape('foo bar')
441
        self.assertEqual('foo bar"', val)
442
        # The second time should be cached
443
        val2 = bzrlib.xml5._encode_and_escape('foo bar')
444
        self.assertIs(val2, val)
445
446
    def test_ascii_with_xml(self):
447
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
448
                         bzrlib.xml5._encode_and_escape('&\'"<>'))
449
450
    def test_utf8_with_xml(self):
451
        # u'\xb5\xe5&\u062c'
452
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
453
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
454
                         bzrlib.xml5._encode_and_escape(utf8_str))
455
456
    def test_unicode(self):
457
        uni_str = u'\xb5\xe5&\u062c'
458
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
459
                         bzrlib.xml5._encode_and_escape(uni_str))