74
76
output = StringIO()
75
77
writer = pack.ContainerWriter(output.write)
77
writer.add_bytes_record('abc', names=['name1', 'name2'])
79
offset, length = writer.add_bytes_record('abc', names=['name1', 'name2'])
80
self.assertEqual((42, 19), (offset, length))
79
82
'Bazaar pack format 1 (introduced in 0.18)\n'
80
83
'B3\nname1\nname2\n\nabc',
86
def test_add_second_bytes_record_gets_higher_offset(self):
88
writer = pack.ContainerWriter(output.write)
90
writer.add_bytes_record('abc', names=[])
91
offset, length = writer.add_bytes_record('abc', names=[])
92
self.assertEqual((49, 7), (offset, length))
94
'Bazaar pack format 1 (introduced in 0.18)\n'
83
99
def test_add_bytes_record_invalid_name(self):
84
100
"""Adding a Bytes record with a name with whitespace in it raises
85
101
InvalidRecordError.
375
391
self.assertEqual('', get_bytes(99))
394
class TestMakeReadvReader(tests.TestCaseWithTransport):
396
def test_read_skipping_records(self):
397
pack_data = StringIO()
398
writer = pack.ContainerWriter(pack_data.write)
401
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']))
404
memos.append(writer.add_bytes_record('jkl', names=[]))
406
transport = self.get_transport()
407
transport.put_bytes('mypack', pack_data.getvalue())
408
requested_records = [memos[0], memos[2]]
409
reader = pack.make_readv_reader(transport, 'mypack', requested_records)
411
for names, reader_func in reader.iter_records():
412
result.append((names, reader_func(None)))
413
self.assertEqual([([], 'abc'), (['name2'], 'ghi')], result)
416
class TestReadvFile(tests.TestCaseWithTransport):
417
"""Tests of the ReadVFile class.
419
Error cases are deliberately undefined: this code adapts the underlying
420
transport interface to a single 'streaming read' interface as
421
ContainerReader needs.
424
def test_read_bytes(self):
425
"""Test reading of both single bytes and all bytes in a hunk."""
426
transport = self.get_transport()
427
transport.put_bytes('sample', '0123456789')
428
f = pack.ReadVFile(transport.readv('sample', [(0,1), (1,2), (4,1), (6,2)]))
430
results.append(f.read(1))
431
results.append(f.read(2))
432
results.append(f.read(1))
433
results.append(f.read(1))
434
results.append(f.read(1))
435
self.assertEqual(['0', '12', '4', '6', '7'], results)
437
def test_readline(self):
438
"""Test using readline() as ContainerReader does.
440
This is always within a readv hunk, never across it.
442
transport = self.get_transport()
443
transport.put_bytes('sample', '0\n2\n4\n')
444
f = pack.ReadVFile(transport.readv('sample', [(0,2), (2,4)]))
446
results.append(f.readline())
447
results.append(f.readline())
448
results.append(f.readline())
449
self.assertEqual(['0\n', '2\n', '4\n'], results)
451
def test_readline_and_read(self):
452
"""Test exercising one byte reads, readline, and then read again."""
453
transport = self.get_transport()
454
transport.put_bytes('sample', '0\n2\n4\n')
455
f = pack.ReadVFile(transport.readv('sample', [(0,6)]))
457
results.append(f.read(1))
458
results.append(f.readline())
459
results.append(f.read(4))
460
self.assertEqual(['0', '\n', '2\n4\n'], results)