64
64
output = StringIO()
65
65
writer = pack.ContainerWriter(output.write)
67
offset, length = writer.add_bytes_record('abc', names=['name1'])
67
offset, length = writer.add_bytes_record('abc', names=[('name1', )])
68
68
self.assertEqual((42, 13), (offset, length))
70
70
'Bazaar pack format 1 (introduced in 0.18)\n'
76
76
output = StringIO()
77
77
writer = pack.ContainerWriter(output.write)
79
offset, length = writer.add_bytes_record('abc', names=['name1', 'name2'])
80
self.assertEqual((42, 19), (offset, length))
82
'Bazaar pack format 1 (introduced in 0.18)\n'
83
'B3\nname1\nname2\n\nabc',
79
offset, length = writer.add_bytes_record('abc', names=[('name1', ), ('name2', )])
80
self.assertEqual((42, 19), (offset, length))
82
'Bazaar pack format 1 (introduced in 0.18)\n'
83
'B3\nname1\nname2\n\nabc',
86
def test_add_bytes_record_two_names(self):
87
"""Add a bytes record with two names."""
89
writer = pack.ContainerWriter(output.write)
91
offset, length = writer.add_bytes_record('abc', names=[('name1', ), ('name2', )])
92
self.assertEqual((42, 19), (offset, length))
94
'Bazaar pack format 1 (introduced in 0.18)\n'
95
'B3\nname1\nname2\n\nabc',
98
def test_add_bytes_record_two_element_name(self):
99
"""Add a bytes record with a two-element name."""
101
writer = pack.ContainerWriter(output.write)
103
offset, length = writer.add_bytes_record('abc', names=[('name1', 'name2')])
104
self.assertEqual((42, 19), (offset, length))
106
'Bazaar pack format 1 (introduced in 0.18)\n'
107
'B3\nname1\x00name2\n\nabc',
84
108
output.getvalue())
86
110
def test_add_second_bytes_record_gets_higher_offset(self):
106
130
self.assertRaises(
107
131
errors.InvalidRecordError,
108
writer.add_bytes_record, 'abc', names=['bad name'])
132
writer.add_bytes_record, 'abc', names=[('bad name', )])
111
135
class TestContainerReader(tests.TestCase):
264
288
reader = self.get_reader_for("5\nname1\n\naaaaa")
265
289
names, get_bytes = reader.read()
266
self.assertEqual(['name1'], names)
290
self.assertEqual([('name1', )], names)
267
291
self.assertEqual('aaaaa', get_bytes(None))
269
293
def test_record_with_two_names(self):
272
296
reader = self.get_reader_for("5\nname1\nname2\n\naaaaa")
273
297
names, get_bytes = reader.read()
274
self.assertEqual(['name1', 'name2'], names)
298
self.assertEqual([('name1', ), ('name2', )], names)
299
self.assertEqual('aaaaa', get_bytes(None))
301
def test_record_with_two_part_names(self):
302
"""Reading a Bytes record with a two_part name reads both."""
303
reader = self.get_reader_for("5\nname1\x00name2\n\naaaaa")
304
names, get_bytes = reader.read()
305
self.assertEqual([('name1', 'name2', )], names)
275
306
self.assertEqual('aaaaa', get_bytes(None))
277
308
def test_invalid_length(self):
401
432
memos.append(writer.add_bytes_record('abc', names=[]))
402
memos.append(writer.add_bytes_record('def', names=['name1']))
403
memos.append(writer.add_bytes_record('ghi', names=['name2']))
433
memos.append(writer.add_bytes_record('def', names=[('name1', )]))
434
memos.append(writer.add_bytes_record('ghi', names=[('name2', )]))
404
435
memos.append(writer.add_bytes_record('jkl', names=[]))
406
437
transport = self.get_transport()
411
442
for names, reader_func in reader.iter_records():
412
443
result.append((names, reader_func(None)))
413
self.assertEqual([([], 'abc'), (['name2'], 'ghi')], result)
444
self.assertEqual([([], 'abc'), ([('name2', )], 'ghi')], result)
416
447
class TestReadvFile(tests.TestCaseWithTransport):