/brz/remove-bazaar

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