/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2007, 2009, 2011, 2012, 2016 Canonical Ltd
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
2
#
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.
7
#
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.
12
#
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
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
16
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
17
"""Tests for breezy.pack."""
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
18
6670.4.1 by Jelmer Vernooij
Update imports.
19
from .. import errors, tests
20
from ..bzr import (
21
    pack,
22
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
23
from ..sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
24
    BytesIO,
25
    )
2506.3.2 by Andrew Bennetts
Test docstring tweaks, inspired by looking over the output of jml's testdoc tool.
26
27
2916.2.11 by Andrew Bennetts
Add direct unit tests for ContainerSerialiser.
28
class TestContainerSerialiser(tests.TestCase):
29
    """Tests for the ContainerSerialiser class."""
30
31
    def test_construct(self):
32
        """Test constructing a ContainerSerialiser."""
33
        pack.ContainerSerialiser()
34
35
    def test_begin(self):
36
        serialiser = pack.ContainerSerialiser()
6803.2.5 by Martin
Make test_pack pass on Python 3
37
        self.assertEqual(b'Bazaar pack format 1 (introduced in 0.18)\n',
2916.2.11 by Andrew Bennetts
Add direct unit tests for ContainerSerialiser.
38
                         serialiser.begin())
39
40
    def test_end(self):
41
        serialiser = pack.ContainerSerialiser()
6803.2.5 by Martin
Make test_pack pass on Python 3
42
        self.assertEqual(b'E', serialiser.end())
2916.2.11 by Andrew Bennetts
Add direct unit tests for ContainerSerialiser.
43
44
    def test_bytes_record_no_name(self):
45
        serialiser = pack.ContainerSerialiser()
6803.2.5 by Martin
Make test_pack pass on Python 3
46
        record = serialiser.bytes_record(b'bytes', [])
47
        self.assertEqual(b'B5\n\nbytes', record)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
48
2916.2.11 by Andrew Bennetts
Add direct unit tests for ContainerSerialiser.
49
    def test_bytes_record_one_name_with_one_part(self):
50
        serialiser = pack.ContainerSerialiser()
6803.2.5 by Martin
Make test_pack pass on Python 3
51
        record = serialiser.bytes_record(b'bytes', [(b'name',)])
52
        self.assertEqual(b'B5\nname\n\nbytes', record)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
53
2916.2.11 by Andrew Bennetts
Add direct unit tests for ContainerSerialiser.
54
    def test_bytes_record_one_name_with_two_parts(self):
55
        serialiser = pack.ContainerSerialiser()
6803.2.5 by Martin
Make test_pack pass on Python 3
56
        record = serialiser.bytes_record(b'bytes', [(b'part1', b'part2')])
57
        self.assertEqual(b'B5\npart1\x00part2\n\nbytes', record)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
58
2916.2.11 by Andrew Bennetts
Add direct unit tests for ContainerSerialiser.
59
    def test_bytes_record_two_names(self):
60
        serialiser = pack.ContainerSerialiser()
6803.2.5 by Martin
Make test_pack pass on Python 3
61
        record = serialiser.bytes_record(b'bytes', [(b'name1',), (b'name2',)])
62
        self.assertEqual(b'B5\nname1\nname2\n\nbytes', record)
2916.2.11 by Andrew Bennetts
Add direct unit tests for ContainerSerialiser.
63
64
    def test_bytes_record_whitespace_in_name_part(self):
65
        serialiser = pack.ContainerSerialiser()
66
        self.assertRaises(
67
            errors.InvalidRecordError,
6803.2.5 by Martin
Make test_pack pass on Python 3
68
            serialiser.bytes_record, b'bytes', [(b'bad name',)])
2916.2.11 by Andrew Bennetts
Add direct unit tests for ContainerSerialiser.
69
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
70
    def test_bytes_record_header(self):
71
        serialiser = pack.ContainerSerialiser()
6803.2.5 by Martin
Make test_pack pass on Python 3
72
        record = serialiser.bytes_header(32, [(b'name1',), (b'name2',)])
73
        self.assertEqual(b'B32\nname1\nname2\n\n', record)
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
74
2916.2.11 by Andrew Bennetts
Add direct unit tests for ContainerSerialiser.
75
2506.3.2 by Andrew Bennetts
Test docstring tweaks, inspired by looking over the output of jml's testdoc tool.
76
class TestContainerWriter(tests.TestCase):
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
77
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
78
    def setUp(self):
6552.1.3 by Vincent Ladeuil
Use super() instead of calling <base>.setup(self), as the original fix illustrated a too-easy-to-fall-into trap.
79
        super(TestContainerWriter, self).setUp()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
80
        self.output = BytesIO()
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
81
        self.writer = pack.ContainerWriter(self.output.write)
82
83
    def assertOutput(self, expected_output):
84
        """Assert that the output of self.writer ContainerWriter is equal to
85
        expected_output.
86
        """
87
        self.assertEqual(expected_output, self.output.getvalue())
88
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
89
    def test_construct(self):
90
        """Test constructing a ContainerWriter.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
91
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
92
        This uses None as the output stream to show that the constructor
93
        doesn't try to use the output stream.
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
94
        """
2535.3.26 by Andrew Bennetts
Revert merge of container-format changes rejected for bzr.dev (i.e. undo andrew.bennetts@canonical.com-20070717044423-cetp5spep142xsr4).
95
        writer = pack.ContainerWriter(None)
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
96
97
    def test_begin(self):
2506.3.2 by Andrew Bennetts
Test docstring tweaks, inspired by looking over the output of jml's testdoc tool.
98
        """The begin() method writes the container format marker line."""
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
99
        self.writer.begin()
6803.2.5 by Martin
Make test_pack pass on Python 3
100
        self.assertOutput(b'Bazaar pack format 1 (introduced in 0.18)\n')
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
101
2698.1.1 by Robert Collins
Add records_written attribute to ContainerWriter's. (Robert Collins).
102
    def test_zero_records_written_after_begin(self):
103
        """After begin is written, 0 records have been written."""
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
104
        self.writer.begin()
105
        self.assertEqual(0, self.writer.records_written)
2698.1.1 by Robert Collins
Add records_written attribute to ContainerWriter's. (Robert Collins).
106
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
107
    def test_end(self):
2506.3.2 by Andrew Bennetts
Test docstring tweaks, inspired by looking over the output of jml's testdoc tool.
108
        """The end() method writes an End Marker record."""
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
109
        self.writer.begin()
110
        self.writer.end()
6803.2.5 by Martin
Make test_pack pass on Python 3
111
        self.assertOutput(b'Bazaar pack format 1 (introduced in 0.18)\nE')
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
112
2698.1.1 by Robert Collins
Add records_written attribute to ContainerWriter's. (Robert Collins).
113
    def test_empty_end_does_not_add_a_record_to_records_written(self):
114
        """The end() method does not count towards the records written."""
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
115
        self.writer.begin()
116
        self.writer.end()
117
        self.assertEqual(0, self.writer.records_written)
2698.1.1 by Robert Collins
Add records_written attribute to ContainerWriter's. (Robert Collins).
118
119
    def test_non_empty_end_does_not_add_a_record_to_records_written(self):
120
        """The end() method does not count towards the records written."""
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
121
        self.writer.begin()
6803.2.5 by Martin
Make test_pack pass on Python 3
122
        self.writer.add_bytes_record(b'foo', names=[])
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
123
        self.writer.end()
124
        self.assertEqual(1, self.writer.records_written)
2698.1.1 by Robert Collins
Add records_written attribute to ContainerWriter's. (Robert Collins).
125
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
126
    def test_add_bytes_record_no_name(self):
127
        """Add a bytes record with no name."""
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
128
        self.writer.begin()
6803.2.5 by Martin
Make test_pack pass on Python 3
129
        offset, length = self.writer.add_bytes_record(b'abc', names=[])
2661.2.1 by Robert Collins
* ``bzrlib.pack.ContainerWriter`` now returns an offset, length tuple to
130
        self.assertEqual((42, 7), (offset, length))
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
131
        self.assertOutput(
6803.2.5 by Martin
Make test_pack pass on Python 3
132
            b'Bazaar pack format 1 (introduced in 0.18)\nB3\n\nabc')
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
133
134
    def test_add_bytes_record_one_name(self):
135
        """Add a bytes record with one name."""
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
136
        self.writer.begin()
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
137
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
138
        offset, length = self.writer.add_bytes_record(
6803.2.5 by Martin
Make test_pack pass on Python 3
139
            b'abc', names=[(b'name1', )])
2661.2.1 by Robert Collins
* ``bzrlib.pack.ContainerWriter`` now returns an offset, length tuple to
140
        self.assertEqual((42, 13), (offset, length))
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
141
        self.assertOutput(
6803.2.5 by Martin
Make test_pack pass on Python 3
142
            b'Bazaar pack format 1 (introduced in 0.18)\n'
143
            b'B3\nname1\n\nabc')
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
144
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
145
    def test_add_bytes_record_split_writes(self):
146
        """Write a large record which does multiple IOs"""
147
148
        writes = []
149
        real_write = self.writer.write_func
150
6803.2.5 by Martin
Make test_pack pass on Python 3
151
        def record_writes(data):
152
            writes.append(data)
153
            return real_write(data)
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
154
155
        self.writer.write_func = record_writes
156
        self.writer._JOIN_WRITES_THRESHOLD = 2
157
158
        self.writer.begin()
159
        offset, length = self.writer.add_bytes_record(
6803.2.5 by Martin
Make test_pack pass on Python 3
160
            b'abcabc', names=[(b'name1', )])
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
161
        self.assertEqual((42, 16), (offset, length))
162
        self.assertOutput(
6803.2.5 by Martin
Make test_pack pass on Python 3
163
            b'Bazaar pack format 1 (introduced in 0.18)\n'
164
            b'B6\nname1\n\nabcabc')
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
165
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
166
        self.assertEqual([
6803.2.5 by Martin
Make test_pack pass on Python 3
167
            b'Bazaar pack format 1 (introduced in 0.18)\n',
168
            b'B6\nname1\n\n',
169
            b'abcabc'],
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
170
            writes)
171
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
172
    def test_add_bytes_record_two_names(self):
173
        """Add a bytes record with two names."""
174
        self.writer.begin()
175
        offset, length = self.writer.add_bytes_record(
6803.2.5 by Martin
Make test_pack pass on Python 3
176
            b'abc', names=[(b'name1', ), (b'name2', )])
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
177
        self.assertEqual((42, 19), (offset, length))
178
        self.assertOutput(
6803.2.5 by Martin
Make test_pack pass on Python 3
179
            b'Bazaar pack format 1 (introduced in 0.18)\n'
180
            b'B3\nname1\nname2\n\nabc')
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
181
182
    def test_add_bytes_record_two_names(self):
183
        """Add a bytes record with two names."""
184
        self.writer.begin()
185
        offset, length = self.writer.add_bytes_record(
6803.2.5 by Martin
Make test_pack pass on Python 3
186
            b'abc', names=[(b'name1', ), (b'name2', )])
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
187
        self.assertEqual((42, 19), (offset, length))
188
        self.assertOutput(
6803.2.5 by Martin
Make test_pack pass on Python 3
189
            b'Bazaar pack format 1 (introduced in 0.18)\n'
190
            b'B3\nname1\nname2\n\nabc')
2682.1.1 by Robert Collins
* The ``bzrlib.pack`` interface has changed to use tuples of bytestrings
191
192
    def test_add_bytes_record_two_element_name(self):
193
        """Add a bytes record with a two-element name."""
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
194
        self.writer.begin()
195
        offset, length = self.writer.add_bytes_record(
6803.2.5 by Martin
Make test_pack pass on Python 3
196
            b'abc', names=[(b'name1', b'name2')])
2682.1.1 by Robert Collins
* The ``bzrlib.pack`` interface has changed to use tuples of bytestrings
197
        self.assertEqual((42, 19), (offset, length))
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
198
        self.assertOutput(
6803.2.5 by Martin
Make test_pack pass on Python 3
199
            b'Bazaar pack format 1 (introduced in 0.18)\n'
200
            b'B3\nname1\x00name2\n\nabc')
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
201
2661.2.1 by Robert Collins
* ``bzrlib.pack.ContainerWriter`` now returns an offset, length tuple to
202
    def test_add_second_bytes_record_gets_higher_offset(self):
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
203
        self.writer.begin()
6803.2.5 by Martin
Make test_pack pass on Python 3
204
        self.writer.add_bytes_record(b'abc', names=[])
205
        offset, length = self.writer.add_bytes_record(b'abc', names=[])
2661.2.1 by Robert Collins
* ``bzrlib.pack.ContainerWriter`` now returns an offset, length tuple to
206
        self.assertEqual((49, 7), (offset, length))
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
207
        self.assertOutput(
6803.2.5 by Martin
Make test_pack pass on Python 3
208
            b'Bazaar pack format 1 (introduced in 0.18)\n'
209
            b'B3\n\nabc'
210
            b'B3\n\nabc')
2661.2.1 by Robert Collins
* ``bzrlib.pack.ContainerWriter`` now returns an offset, length tuple to
211
2506.5.2 by Andrew Bennetts
Raise InvalidRecordError on invalid names.
212
    def test_add_bytes_record_invalid_name(self):
213
        """Adding a Bytes record with a name with whitespace in it raises
214
        InvalidRecordError.
215
        """
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
216
        self.writer.begin()
2506.5.2 by Andrew Bennetts
Raise InvalidRecordError on invalid names.
217
        self.assertRaises(
218
            errors.InvalidRecordError,
6803.2.5 by Martin
Make test_pack pass on Python 3
219
            self.writer.add_bytes_record, b'abc', names=[(b'bad name', )])
2506.5.2 by Andrew Bennetts
Raise InvalidRecordError on invalid names.
220
2698.1.1 by Robert Collins
Add records_written attribute to ContainerWriter's. (Robert Collins).
221
    def test_add_bytes_records_add_to_records_written(self):
222
        """Adding a Bytes record increments the records_written counter."""
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
223
        self.writer.begin()
6803.2.5 by Martin
Make test_pack pass on Python 3
224
        self.writer.add_bytes_record(b'foo', names=[])
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
225
        self.assertEqual(1, self.writer.records_written)
6803.2.5 by Martin
Make test_pack pass on Python 3
226
        self.writer.add_bytes_record(b'foo', names=[])
2916.2.12 by Andrew Bennetts
Refactor TestContainerWriter to be a little more concise.
227
        self.assertEqual(2, self.writer.records_written)
2698.1.1 by Robert Collins
Add records_written attribute to ContainerWriter's. (Robert Collins).
228
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
229
2506.3.2 by Andrew Bennetts
Test docstring tweaks, inspired by looking over the output of jml's testdoc tool.
230
class TestContainerReader(tests.TestCase):
2916.2.13 by Andrew Bennetts
Improve some docstrings.
231
    """Tests for the ContainerReader.
232
233
    The ContainerReader reads format 1 containers, so these tests explicitly
234
    test how it reacts to format 1 data.  If a new version of the format is
235
    added, then separate tests for that format should be added.
236
    """
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
237
6803.2.5 by Martin
Make test_pack pass on Python 3
238
    def get_reader_for(self, data):
239
        stream = BytesIO(data)
2535.3.26 by Andrew Bennetts
Revert merge of container-format changes rejected for bzr.dev (i.e. undo andrew.bennetts@canonical.com-20070717044423-cetp5spep142xsr4).
240
        reader = pack.ContainerReader(stream)
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
241
        return reader
242
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
243
    def test_construct(self):
244
        """Test constructing a ContainerReader.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
245
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
246
        This uses None as the output stream to show that the constructor
247
        doesn't try to use the input stream.
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
248
        """
2535.3.26 by Andrew Bennetts
Revert merge of container-format changes rejected for bzr.dev (i.e. undo andrew.bennetts@canonical.com-20070717044423-cetp5spep142xsr4).
249
        reader = pack.ContainerReader(None)
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
250
251
    def test_empty_container(self):
252
        """Read an empty container."""
2506.2.10 by Andrew Bennetts
Add '(introduced in 0.18)' to pack format string.
253
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
254
            b"Bazaar pack format 1 (introduced in 0.18)\nE")
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
255
        self.assertEqual([], list(reader.iter_records()))
256
257
    def test_unknown_format(self):
258
        """Unrecognised container formats raise UnknownContainerFormatError."""
6803.2.5 by Martin
Make test_pack pass on Python 3
259
        reader = self.get_reader_for(b"unknown format\n")
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
260
        self.assertRaises(
261
            errors.UnknownContainerFormatError, reader.iter_records)
262
263
    def test_unexpected_end_of_container(self):
264
        """Containers that don't end with an End Marker record should cause
265
        UnexpectedEndOfContainerError to be raised.
266
        """
2506.2.10 by Andrew Bennetts
Add '(introduced in 0.18)' to pack format string.
267
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
268
            b"Bazaar pack format 1 (introduced in 0.18)\n")
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
269
        iterator = reader.iter_records()
270
        self.assertRaises(
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
271
            errors.UnexpectedEndOfContainerError, next, iterator)
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
272
273
    def test_unknown_record_type(self):
274
        """Unknown record types cause UnknownRecordTypeError to be raised."""
2506.2.10 by Andrew Bennetts
Add '(introduced in 0.18)' to pack format string.
275
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
276
            b"Bazaar pack format 1 (introduced in 0.18)\nX")
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
277
        iterator = reader.iter_records()
278
        self.assertRaises(
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
279
            errors.UnknownRecordTypeError, next, iterator)
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
280
2506.3.1 by Andrew Bennetts
More progress:
281
    def test_container_with_one_unnamed_record(self):
282
        """Read a container with one Bytes record.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
283
2506.3.2 by Andrew Bennetts
Test docstring tweaks, inspired by looking over the output of jml's testdoc tool.
284
        Parsing Bytes records is more thoroughly exercised by
285
        TestBytesRecordReader.  This test is here to ensure that
286
        ContainerReader's integration with BytesRecordReader is working.
2506.3.1 by Andrew Bennetts
More progress:
287
        """
2506.2.10 by Andrew Bennetts
Add '(introduced in 0.18)' to pack format string.
288
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
289
            b"Bazaar pack format 1 (introduced in 0.18)\nB5\n\naaaaaE")
290
        expected_records = [([], b'aaaaa')]
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
291
        self.assertEqual(
292
            expected_records,
293
            [(names, read_bytes(None))
294
             for (names, read_bytes) in reader.iter_records()])
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
295
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
296
    def test_validate_empty_container(self):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
297
        """validate does not raise an error for a container with no records."""
6257.5.1 by Martin Pool
ContainerWriter: Avoid one possible large-string join
298
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
299
            b"Bazaar pack format 1 (introduced in 0.18)\nE")
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
300
        # No exception raised
301
        reader.validate()
302
303
    def test_validate_non_empty_valid_container(self):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
304
        """validate does not raise an error for a container with a valid record.
305
        """
2506.2.10 by Andrew Bennetts
Add '(introduced in 0.18)' to pack format string.
306
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
307
            b"Bazaar pack format 1 (introduced in 0.18)\nB3\nname\n\nabcE")
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
308
        # No exception raised
309
        reader.validate()
310
311
    def test_validate_bad_format(self):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
312
        """validate raises an error for unrecognised format strings.
313
314
        It may raise either UnexpectedEndOfContainerError or
315
        UnknownContainerFormatError, depending on exactly what the string is.
316
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
317
        inputs = [
318
            b"", b"x", b"Bazaar pack format 1 (introduced in 0.18)", b"bad\n"]
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
319
        for input in inputs:
320
            reader = self.get_reader_for(input)
321
            self.assertRaises(
322
                (errors.UnexpectedEndOfContainerError,
323
                 errors.UnknownContainerFormatError),
324
                reader.validate)
325
326
    def test_validate_bad_record_marker(self):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
327
        """validate raises UnknownRecordTypeError for unrecognised record
328
        types.
329
        """
2506.2.10 by Andrew Bennetts
Add '(introduced in 0.18)' to pack format string.
330
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
331
            b"Bazaar pack format 1 (introduced in 0.18)\nX")
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
332
        self.assertRaises(errors.UnknownRecordTypeError, reader.validate)
333
334
    def test_validate_data_after_end_marker(self):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
335
        """validate raises ContainerHasExcessDataError if there are any bytes
336
        after the end of the container.
337
        """
2506.2.10 by Andrew Bennetts
Add '(introduced in 0.18)' to pack format string.
338
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
339
            b"Bazaar pack format 1 (introduced in 0.18)\nEcrud")
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
340
        self.assertRaises(
341
            errors.ContainerHasExcessDataError, reader.validate)
342
343
    def test_validate_no_end_marker(self):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
344
        """validate raises UnexpectedEndOfContainerError if there's no end of
345
        container marker, even if the container up to this point has been valid.
346
        """
2506.2.10 by Andrew Bennetts
Add '(introduced in 0.18)' to pack format string.
347
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
348
            b"Bazaar pack format 1 (introduced in 0.18)\n")
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
349
        self.assertRaises(
350
            errors.UnexpectedEndOfContainerError, reader.validate)
351
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
352
    def test_validate_duplicate_name(self):
353
        """validate raises DuplicateRecordNameError if the same name occurs
354
        multiple times in the container.
355
        """
356
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
357
            b"Bazaar pack format 1 (introduced in 0.18)\n"
358
            b"B0\nname\n\n"
359
            b"B0\nname\n\n"
360
            b"E")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
361
        self.assertRaises(errors.DuplicateRecordNameError, reader.validate)
362
363
    def test_validate_undecodeable_name(self):
364
        """Names that aren't valid UTF-8 cause validate to fail."""
2506.2.10 by Andrew Bennetts
Add '(introduced in 0.18)' to pack format string.
365
        reader = self.get_reader_for(
6803.2.5 by Martin
Make test_pack pass on Python 3
366
            b"Bazaar pack format 1 (introduced in 0.18)\nB0\n\xcc\n\nE")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
367
        self.assertRaises(errors.InvalidRecordError, reader.validate)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
368
2506.3.1 by Andrew Bennetts
More progress:
369
2506.3.2 by Andrew Bennetts
Test docstring tweaks, inspired by looking over the output of jml's testdoc tool.
370
class TestBytesRecordReader(tests.TestCase):
2916.2.13 by Andrew Bennetts
Improve some docstrings.
371
    """Tests for reading and validating Bytes records with
372
    BytesRecordReader.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
373
2916.2.13 by Andrew Bennetts
Improve some docstrings.
374
    Like TestContainerReader, this explicitly tests the reading of format 1
375
    data.  If a new version of the format is added, then a separate set of
376
    tests for reading that format should be added.
377
    """
2506.3.1 by Andrew Bennetts
More progress:
378
6803.2.5 by Martin
Make test_pack pass on Python 3
379
    def get_reader_for(self, data):
380
        stream = BytesIO(data)
2535.3.26 by Andrew Bennetts
Revert merge of container-format changes rejected for bzr.dev (i.e. undo andrew.bennetts@canonical.com-20070717044423-cetp5spep142xsr4).
381
        reader = pack.BytesRecordReader(stream)
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
382
        return reader
383
2506.3.1 by Andrew Bennetts
More progress:
384
    def test_record_with_no_name(self):
385
        """Reading a Bytes record with no name returns an empty list of
386
        names.
387
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
388
        reader = self.get_reader_for(b"5\n\naaaaa")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
389
        names, get_bytes = reader.read()
2506.3.1 by Andrew Bennetts
More progress:
390
        self.assertEqual([], names)
6803.2.5 by Martin
Make test_pack pass on Python 3
391
        self.assertEqual(b'aaaaa', get_bytes(None))
2506.3.1 by Andrew Bennetts
More progress:
392
393
    def test_record_with_one_name(self):
394
        """Reading a Bytes record with one name returns a list of just that
395
        name.
396
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
397
        reader = self.get_reader_for(b"5\nname1\n\naaaaa")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
398
        names, get_bytes = reader.read()
6803.2.5 by Martin
Make test_pack pass on Python 3
399
        self.assertEqual([(b'name1', )], names)
400
        self.assertEqual(b'aaaaa', get_bytes(None))
2506.3.1 by Andrew Bennetts
More progress:
401
402
    def test_record_with_two_names(self):
403
        """Reading a Bytes record with two names returns a list of both names.
404
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
405
        reader = self.get_reader_for(b"5\nname1\nname2\n\naaaaa")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
406
        names, get_bytes = reader.read()
6803.2.5 by Martin
Make test_pack pass on Python 3
407
        self.assertEqual([(b'name1', ), (b'name2', )], names)
408
        self.assertEqual(b'aaaaa', get_bytes(None))
2682.1.1 by Robert Collins
* The ``bzrlib.pack`` interface has changed to use tuples of bytestrings
409
410
    def test_record_with_two_part_names(self):
411
        """Reading a Bytes record with a two_part name reads both."""
6803.2.5 by Martin
Make test_pack pass on Python 3
412
        reader = self.get_reader_for(b"5\nname1\x00name2\n\naaaaa")
2682.1.1 by Robert Collins
* The ``bzrlib.pack`` interface has changed to use tuples of bytestrings
413
        names, get_bytes = reader.read()
6803.2.5 by Martin
Make test_pack pass on Python 3
414
        self.assertEqual([(b'name1', b'name2', )], names)
415
        self.assertEqual(b'aaaaa', get_bytes(None))
2506.3.1 by Andrew Bennetts
More progress:
416
417
    def test_invalid_length(self):
418
        """If the length-prefix is not a number, parsing raises
419
        InvalidRecordError.
420
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
421
        reader = self.get_reader_for(b"not a number\n")
2506.3.1 by Andrew Bennetts
More progress:
422
        self.assertRaises(errors.InvalidRecordError, reader.read)
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
423
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
424
    def test_early_eof(self):
425
        """Tests for premature EOF occuring during parsing Bytes records with
426
        BytesRecordReader.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
427
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
428
        A incomplete container might be interrupted at any point.  The
429
        BytesRecordReader needs to cope with the input stream running out no
430
        matter where it is in the parsing process.
431
432
        In all cases, UnexpectedEndOfContainerError should be raised.
433
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
434
        complete_record = b"6\nname\n\nabcdef"
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
435
        for count in range(0, len(complete_record)):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
436
            incomplete_record = complete_record[:count]
437
            reader = self.get_reader_for(incomplete_record)
438
            # We don't use assertRaises to make diagnosing failures easier
439
            # (assertRaises doesn't allow a custom failure message).
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
440
            try:
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
441
                names, read_bytes = reader.read()
442
                read_bytes(None)
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
443
            except errors.UnexpectedEndOfContainerError:
444
                pass
445
            else:
446
                self.fail(
447
                    "UnexpectedEndOfContainerError not raised when parsing %r"
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
448
                    % (incomplete_record,))
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
449
2506.5.2 by Andrew Bennetts
Raise InvalidRecordError on invalid names.
450
    def test_initial_eof(self):
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
451
        """EOF before any bytes read at all."""
6803.2.5 by Martin
Make test_pack pass on Python 3
452
        reader = self.get_reader_for(b"")
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
453
        self.assertRaises(errors.UnexpectedEndOfContainerError, reader.read)
454
2506.5.2 by Andrew Bennetts
Raise InvalidRecordError on invalid names.
455
    def test_eof_after_length(self):
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
456
        """EOF after reading the length and before reading name(s)."""
6803.2.5 by Martin
Make test_pack pass on Python 3
457
        reader = self.get_reader_for(b"123\n")
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
458
        self.assertRaises(errors.UnexpectedEndOfContainerError, reader.read)
459
2506.5.2 by Andrew Bennetts
Raise InvalidRecordError on invalid names.
460
    def test_eof_during_name(self):
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
461
        """EOF during reading a name."""
6803.2.5 by Martin
Make test_pack pass on Python 3
462
        reader = self.get_reader_for(b"123\nname")
2506.3.3 by Andrew Bennetts
Deal with EOF in the middle of a bytes record.
463
        self.assertRaises(errors.UnexpectedEndOfContainerError, reader.read)
464
2535.3.26 by Andrew Bennetts
Revert merge of container-format changes rejected for bzr.dev (i.e. undo andrew.bennetts@canonical.com-20070717044423-cetp5spep142xsr4).
465
    def test_read_invalid_name_whitespace(self):
466
        """Names must have no whitespace."""
467
        # A name with a space.
6803.2.5 by Martin
Make test_pack pass on Python 3
468
        reader = self.get_reader_for(b"0\nbad name\n\n")
2535.3.26 by Andrew Bennetts
Revert merge of container-format changes rejected for bzr.dev (i.e. undo andrew.bennetts@canonical.com-20070717044423-cetp5spep142xsr4).
469
        self.assertRaises(errors.InvalidRecordError, reader.read)
470
471
        # A name with a tab.
6803.2.5 by Martin
Make test_pack pass on Python 3
472
        reader = self.get_reader_for(b"0\nbad\tname\n\n")
2535.3.26 by Andrew Bennetts
Revert merge of container-format changes rejected for bzr.dev (i.e. undo andrew.bennetts@canonical.com-20070717044423-cetp5spep142xsr4).
473
        self.assertRaises(errors.InvalidRecordError, reader.read)
474
475
        # A name with a vertical tab.
6803.2.5 by Martin
Make test_pack pass on Python 3
476
        reader = self.get_reader_for(b"0\nbad\vname\n\n")
2535.3.26 by Andrew Bennetts
Revert merge of container-format changes rejected for bzr.dev (i.e. undo andrew.bennetts@canonical.com-20070717044423-cetp5spep142xsr4).
477
        self.assertRaises(errors.InvalidRecordError, reader.read)
478
479
    def test_validate_whitespace_in_name(self):
480
        """Names must have no whitespace."""
6803.2.5 by Martin
Make test_pack pass on Python 3
481
        reader = self.get_reader_for(b"0\nbad name\n\n")
2535.3.26 by Andrew Bennetts
Revert merge of container-format changes rejected for bzr.dev (i.e. undo andrew.bennetts@canonical.com-20070717044423-cetp5spep142xsr4).
482
        self.assertRaises(errors.InvalidRecordError, reader.validate)
483
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
484
    def test_validate_interrupted_prelude(self):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
485
        """EOF during reading a record's prelude causes validate to fail."""
6803.2.5 by Martin
Make test_pack pass on Python 3
486
        reader = self.get_reader_for(b"")
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
487
        self.assertRaises(
488
            errors.UnexpectedEndOfContainerError, reader.validate)
489
490
    def test_validate_interrupted_body(self):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
491
        """EOF during reading a record's body causes validate to fail."""
6803.2.5 by Martin
Make test_pack pass on Python 3
492
        reader = self.get_reader_for(b"1\n\n")
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
493
        self.assertRaises(
494
            errors.UnexpectedEndOfContainerError, reader.validate)
495
496
    def test_validate_unparseable_length(self):
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
497
        """An unparseable record length causes validate to fail."""
6803.2.5 by Martin
Make test_pack pass on Python 3
498
        reader = self.get_reader_for(b"\n\n")
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
499
        self.assertRaises(
500
            errors.InvalidRecordError, reader.validate)
501
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
502
    def test_validate_undecodeable_name(self):
503
        """Names that aren't valid UTF-8 cause validate to fail."""
6803.2.5 by Martin
Make test_pack pass on Python 3
504
        reader = self.get_reader_for(b"0\n\xcc\n\n")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
505
        self.assertRaises(errors.InvalidRecordError, reader.validate)
506
507
    def test_read_max_length(self):
508
        """If the max_length passed to the callable returned by read is not
509
        None, then no more than that many bytes will be read.
510
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
511
        reader = self.get_reader_for(b"6\n\nabcdef")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
512
        names, get_bytes = reader.read()
6803.2.5 by Martin
Make test_pack pass on Python 3
513
        self.assertEqual(b'abc', get_bytes(3))
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
514
515
    def test_read_no_max_length(self):
516
        """If the max_length passed to the callable returned by read is None,
517
        then all the bytes in the record will be read.
518
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
519
        reader = self.get_reader_for(b"6\n\nabcdef")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
520
        names, get_bytes = reader.read()
6803.2.5 by Martin
Make test_pack pass on Python 3
521
        self.assertEqual(b'abcdef', get_bytes(None))
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
522
523
    def test_repeated_read_calls(self):
524
        """Repeated calls to the callable returned from BytesRecordReader.read
525
        will not read beyond the end of the record.
526
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
527
        reader = self.get_reader_for(b"6\n\nabcdefB3\nnext-record\nXXX")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
528
        names, get_bytes = reader.read()
6803.2.5 by Martin
Make test_pack pass on Python 3
529
        self.assertEqual(b'abcdef', get_bytes(None))
530
        self.assertEqual(b'', get_bytes(None))
531
        self.assertEqual(b'', get_bytes(99))
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
532
533
2661.2.3 by Robert Collins
Review feedback.
534
class TestMakeReadvReader(tests.TestCaseWithTransport):
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
535
536
    def test_read_skipping_records(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
537
        pack_data = BytesIO()
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
538
        writer = pack.ContainerWriter(pack_data.write)
539
        writer.begin()
540
        memos = []
6803.2.5 by Martin
Make test_pack pass on Python 3
541
        memos.append(writer.add_bytes_record(b'abc', names=[]))
542
        memos.append(writer.add_bytes_record(b'def', names=[(b'name1', )]))
543
        memos.append(writer.add_bytes_record(b'ghi', names=[(b'name2', )]))
544
        memos.append(writer.add_bytes_record(b'jkl', names=[]))
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
545
        writer.end()
546
        transport = self.get_transport()
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
547
        transport.put_bytes('mypack', pack_data.getvalue())
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
548
        requested_records = [memos[0], memos[2]]
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
549
        reader = pack.make_readv_reader(transport, 'mypack', requested_records)
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
550
        result = []
551
        for names, reader_func in reader.iter_records():
552
            result.append((names, reader_func(None)))
6803.2.5 by Martin
Make test_pack pass on Python 3
553
        self.assertEqual([([], b'abc'), ([(b'name2', )], b'ghi')], result)
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
554
555
556
class TestReadvFile(tests.TestCaseWithTransport):
2661.2.3 by Robert Collins
Review feedback.
557
    """Tests of the ReadVFile class.
558
559
    Error cases are deliberately undefined: this code adapts the underlying
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
560
    transport interface to a single 'streaming read' interface as
2661.2.3 by Robert Collins
Review feedback.
561
    ContainerReader needs.
562
    """
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
563
564
    def test_read_bytes(self):
2661.2.3 by Robert Collins
Review feedback.
565
        """Test reading of both single bytes and all bytes in a hunk."""
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
566
        transport = self.get_transport()
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
567
        transport.put_bytes('sample', b'0123456789')
568
        f = pack.ReadVFile(transport.readv('sample', [(0, 1), (1, 2), (4, 1), (6, 2)]))
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
569
        results = []
570
        results.append(f.read(1))
571
        results.append(f.read(2))
572
        results.append(f.read(1))
573
        results.append(f.read(1))
574
        results.append(f.read(1))
6803.2.5 by Martin
Make test_pack pass on Python 3
575
        self.assertEqual([b'0', b'12', b'4', b'6', b'7'], results)
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
576
577
    def test_readline(self):
2661.2.3 by Robert Collins
Review feedback.
578
        """Test using readline() as ContainerReader does.
579
580
        This is always within a readv hunk, never across it.
581
        """
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
582
        transport = self.get_transport()
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
583
        transport.put_bytes('sample', b'0\n2\n4\n')
584
        f = pack.ReadVFile(transport.readv('sample', [(0, 2), (2, 4)]))
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
585
        results = []
586
        results.append(f.readline())
587
        results.append(f.readline())
588
        results.append(f.readline())
6803.2.5 by Martin
Make test_pack pass on Python 3
589
        self.assertEqual([b'0\n', b'2\n', b'4\n'], results)
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
590
591
    def test_readline_and_read(self):
2661.2.3 by Robert Collins
Review feedback.
592
        """Test exercising one byte reads, readline, and then read again."""
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
593
        transport = self.get_transport()
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
594
        transport.put_bytes('sample', b'0\n2\n4\n')
595
        f = pack.ReadVFile(transport.readv('sample', [(0, 6)]))
2661.2.2 by Robert Collins
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
596
        results = []
597
        results.append(f.read(1))
598
        results.append(f.readline())
599
        results.append(f.read(4))
6803.2.5 by Martin
Make test_pack pass on Python 3
600
        self.assertEqual([b'0', b'\n', b'2\n4\n'], results)
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
601
602
603
class PushParserTestCase(tests.TestCase):
2916.2.13 by Andrew Bennetts
Improve some docstrings.
604
    """Base class for TestCases involving ContainerPushParser."""
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
605
606
    def make_parser_expecting_record_type(self):
607
        parser = pack.ContainerPushParser()
6803.2.5 by Martin
Make test_pack pass on Python 3
608
        parser.accept_bytes(b"Bazaar pack format 1 (introduced in 0.18)\n")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
609
        return parser
610
611
    def make_parser_expecting_bytes_record(self):
612
        parser = pack.ContainerPushParser()
6803.2.5 by Martin
Make test_pack pass on Python 3
613
        parser.accept_bytes(b"Bazaar pack format 1 (introduced in 0.18)\nB")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
614
        return parser
615
6803.2.5 by Martin
Make test_pack pass on Python 3
616
    def assertRecordParsing(self, expected_record, data):
2916.2.13 by Andrew Bennetts
Improve some docstrings.
617
        """Assert that 'bytes' is parsed as a given bytes record.
618
619
        :param expected_record: A tuple of (names, bytes).
620
        """
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
621
        parser = self.make_parser_expecting_bytes_record()
6803.2.5 by Martin
Make test_pack pass on Python 3
622
        parser.accept_bytes(data)
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
623
        parsed_records = parser.read_pending_records()
624
        self.assertEqual([expected_record], parsed_records)
625
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
626
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
627
class TestContainerPushParser(PushParserTestCase):
2916.2.13 by Andrew Bennetts
Improve some docstrings.
628
    """Tests for ContainerPushParser.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
629
2916.2.13 by Andrew Bennetts
Improve some docstrings.
630
    The ContainerPushParser reads format 1 containers, so these tests
631
    explicitly test how it reacts to format 1 data.  If a new version of the
632
    format is added, then separate tests for that format should be added.
633
    """
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
634
635
    def test_construct(self):
636
        """ContainerPushParser can be constructed."""
637
        pack.ContainerPushParser()
638
639
    def test_multiple_records_at_once(self):
2916.2.2 by Andrew Bennetts
Add a couple of docstrings to the tests.
640
        """If multiple records worth of data are fed to the parser in one
641
        string, the parser will correctly parse all the records.
642
643
        (A naive implementation might stop after parsing the first record.)
644
        """
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
645
        parser = self.make_parser_expecting_record_type()
6803.2.5 by Martin
Make test_pack pass on Python 3
646
        parser.accept_bytes(b"B5\nname1\n\nbody1B5\nname2\n\nbody2")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
647
        self.assertEqual(
6803.2.5 by Martin
Make test_pack pass on Python 3
648
            [([(b'name1',)], b'body1'), ([(b'name2',)], b'body2')],
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
649
            parser.read_pending_records())
650
4464.1.1 by Aaron Bentley
ContainerPushParser.accept_bytes handles zero-length records correctly.
651
    def test_multiple_empty_records_at_once(self):
652
        """If multiple empty records worth of data are fed to the parser in one
653
        string, the parser will correctly parse all the records.
654
655
        (A naive implementation might stop after parsing the first empty
656
        record, because the buffer size had not changed.)
657
        """
658
        parser = self.make_parser_expecting_record_type()
6803.2.5 by Martin
Make test_pack pass on Python 3
659
        parser.accept_bytes(b"B0\nname1\n\nB0\nname2\n\n")
4464.1.1 by Aaron Bentley
ContainerPushParser.accept_bytes handles zero-length records correctly.
660
        self.assertEqual(
6803.2.5 by Martin
Make test_pack pass on Python 3
661
            [([(b'name1',)], b''), ([(b'name2',)], b'')],
4464.1.1 by Aaron Bentley
ContainerPushParser.accept_bytes handles zero-length records correctly.
662
            parser.read_pending_records())
663
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
664
665
class TestContainerPushParserBytesParsing(PushParserTestCase):
2916.2.13 by Andrew Bennetts
Improve some docstrings.
666
    """Tests for reading Bytes records with ContainerPushParser.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
667
2916.2.13 by Andrew Bennetts
Improve some docstrings.
668
    The ContainerPushParser reads format 1 containers, so these tests
669
    explicitly test how it reacts to format 1 data.  If a new version of the
670
    format is added, then separate tests for that format should be added.
671
    """
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
672
673
    def test_record_with_no_name(self):
674
        """Reading a Bytes record with no name returns an empty list of
675
        names.
676
        """
6803.2.5 by Martin
Make test_pack pass on Python 3
677
        self.assertRecordParsing(([], b'aaaaa'), b"5\n\naaaaa")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
678
679
    def test_record_with_one_name(self):
680
        """Reading a Bytes record with one name returns a list of just that
681
        name.
682
        """
683
        self.assertRecordParsing(
6803.2.5 by Martin
Make test_pack pass on Python 3
684
            ([(b'name1', )], b'aaaaa'),
685
            b"5\nname1\n\naaaaa")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
686
687
    def test_record_with_two_names(self):
688
        """Reading a Bytes record with two names returns a list of both names.
689
        """
690
        self.assertRecordParsing(
6803.2.5 by Martin
Make test_pack pass on Python 3
691
            ([(b'name1', ), (b'name2', )], b'aaaaa'),
692
            b"5\nname1\nname2\n\naaaaa")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
693
694
    def test_record_with_two_part_names(self):
695
        """Reading a Bytes record with a two_part name reads both."""
696
        self.assertRecordParsing(
6803.2.5 by Martin
Make test_pack pass on Python 3
697
            ([(b'name1', b'name2')], b'aaaaa'),
698
            b"5\nname1\x00name2\n\naaaaa")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
699
700
    def test_invalid_length(self):
701
        """If the length-prefix is not a number, parsing raises
702
        InvalidRecordError.
703
        """
704
        parser = self.make_parser_expecting_bytes_record()
705
        self.assertRaises(
6803.2.5 by Martin
Make test_pack pass on Python 3
706
            errors.InvalidRecordError, parser.accept_bytes, b"not a number\n")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
707
708
    def test_incomplete_record(self):
709
        """If the bytes seen so far don't form a complete record, then there
710
        will be nothing returned by read_pending_records.
711
        """
712
        parser = self.make_parser_expecting_bytes_record()
6803.2.5 by Martin
Make test_pack pass on Python 3
713
        parser.accept_bytes(b"5\n\nabcd")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
714
        self.assertEqual([], parser.read_pending_records())
715
716
    def test_accept_nothing(self):
717
        """The edge case of parsing an empty string causes no error."""
718
        parser = self.make_parser_expecting_bytes_record()
6803.2.5 by Martin
Make test_pack pass on Python 3
719
        parser.accept_bytes(b"")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
720
6803.2.5 by Martin
Make test_pack pass on Python 3
721
    def assertInvalidRecord(self, data):
722
        """Assert that parsing the given bytes raises InvalidRecordError."""
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
723
        parser = self.make_parser_expecting_bytes_record()
724
        self.assertRaises(
6803.2.5 by Martin
Make test_pack pass on Python 3
725
            errors.InvalidRecordError, parser.accept_bytes, data)
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
726
727
    def test_read_invalid_name_whitespace(self):
728
        """Names must have no whitespace."""
729
        # A name with a space.
6803.2.5 by Martin
Make test_pack pass on Python 3
730
        self.assertInvalidRecord(b"0\nbad name\n\n")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
731
732
        # A name with a tab.
6803.2.5 by Martin
Make test_pack pass on Python 3
733
        self.assertInvalidRecord(b"0\nbad\tname\n\n")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
734
735
        # A name with a vertical tab.
6803.2.5 by Martin
Make test_pack pass on Python 3
736
        self.assertInvalidRecord(b"0\nbad\vname\n\n")
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
737
738
    def test_repeated_read_pending_records(self):
739
        """read_pending_records will not return the same record twice."""
740
        parser = self.make_parser_expecting_bytes_record()
6803.2.5 by Martin
Make test_pack pass on Python 3
741
        parser.accept_bytes(b"6\n\nabcdef")
742
        self.assertEqual([([], b'abcdef')], parser.read_pending_records())
2916.2.1 by Andrew Bennetts
Initial implementation of a 'push' parser for the container format.
743
        self.assertEqual([], parser.read_pending_records())