~bzr-pqm/bzr/bzr.dev

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