~bzr-pqm/bzr/bzr.dev

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