Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_simple_peek(mode, tmpdir):
"""Test flushing to a file."""
filename = 'file.bin'
full_file = tmpdir.join(filename)
full_file.write_binary(b'0123456789')
async with aioopen(str(full_file), mode=mode) as file:
if 'a' in mode:
await file.seek(0) # Rewind for append modes.
peeked = await file.peek(1)
# Technically it's OK for the peek to return less bytes than requested.
if peeked:
assert peeked.startswith(b'0')
read = await file.read(1)
assert peeked.startswith(read)
async def test_simple_iteration(mode):
"""Test iterating over lines from a file."""
filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')
async with aioopen(filename, mode=mode) as file:
# Append mode needs us to seek.
await file.seek(0)
counter = 1
# The old iteration pattern:
while True:
line = await file.readline()
if not line:
break
assert line.strip() == 'line ' + str(counter)
counter += 1
await file.seek(0)
counter = 1
async def test_simple_close_ctx_mgr(mode, buffering, tmpdir):
"""Open a file, read a byte, and close it."""
filename = 'bigfile.bin'
content = b'0' * 4 * io.DEFAULT_BUFFER_SIZE
full_file = tmpdir.join(filename)
full_file.write_binary(content)
async with aioopen(str(full_file), mode=mode, buffering=buffering) as file:
assert not file.closed
assert not file._file.closed
assert file.closed
assert file._file.closed
async def test_simple_read(mode, buffering):
"""Just read some bytes from a test file."""
filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')
async with aioopen(filename, mode=mode, buffering=buffering) as file:
await file.seek(0) # Needed for the append mode.
actual = await file.read()
assert b'' == (await file.read())
assert actual == open(filename, mode='rb').read()
async def test_simple_close(mode, tmpdir):
"""Open a file, read a byte, and close it."""
filename = 'bigfile.bin'
content = '0' * 4 * io.DEFAULT_BUFFER_SIZE
full_file = tmpdir.join(filename)
full_file.write(content)
async with aioopen(str(full_file), mode=mode) as file:
assert not file.closed
assert not file._file.closed
assert file.closed
assert file._file.closed