def test_staggered_read(mode): """Read bytes repeatedly.""" filename = join(dirname(__file__), '..', 'resources', 'test_file1.txt') file = yield from aioopen(filename, mode=mode) yield from file.seek(0) # Needed for the append mode. actual = [] while True: char = yield from file.read(1) if char: actual.append(char) else: break assert '' == (yield from file.read()) expected = [] with open(filename, mode='r') as f: while True: char = f.read(1) if char: expected.append(char) else: break assert actual == expected yield from file.close()
async def test_staggered_read(mode, buffering): """Read bytes repeatedly.""" 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 = [] while True: byte = await file.read(1) if byte: actual.append(byte) else: break assert b'' == (await file.read()) expected = [] with open(filename, mode='rb') as f: while True: byte = f.read(1) if byte: expected.append(byte) else: break assert actual == expected
async def test_simple_iteration(mode, buffering): """Test iterating over lines from a file.""" filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt') async with aioopen(filename, mode=mode, buffering=buffering) 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() == b'line ' + str(counter).encode() counter += 1 counter = 1 await file.seek(0) # The new iteration pattern: async for line in file: assert line.strip() == b'line ' + str(counter).encode() counter += 1 assert file.closed
def test_simple_truncate(mode, buffering, tmpdir): """Test truncating files.""" filename = 'bigfile.bin' content = b'0123456789' * 4 * io.DEFAULT_BUFFER_SIZE full_file = tmpdir.join(filename) full_file.write_binary(content) file = yield from aioopen(str(full_file), mode=mode, buffering=buffering) # The append modes want us to seek first. yield from file.seek(0) if 'w' in mode: # We've just erased the entire file. yield from file.write(content) yield from file.flush() yield from file.seek(0) yield from file.truncate() yield from file.close() assert b'' == full_file.read_binary()
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_readinto(mode, buffering): """Test the readinto functionality.""" 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. array = bytearray(4) bytes_read = await file.readinto(array) assert bytes_read == 4 assert array == open(filename, mode='rb').read(4)
async def test_simple_seek(mode, buffering, tmpdir): """Test seeking and then reading.""" filename = 'bigfile.bin' content = b'0123456789' * 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: await file.seek(4) assert b'4' == (await file.read(1))
async def test_simple_read(mode): """Just read some bytes from a test file.""" filename = join(dirname(__file__), '..', 'resources', 'test_file1.txt') async with aioopen(filename, mode=mode) as file: await file.seek(0) # Needed for the append mode. actual = await file.read() assert '' == (await file.read()) assert actual == open(filename, mode='r').read() assert file.closed
def test_simple_read(mode): """Just read some bytes from a test file.""" filename = join(dirname(__file__), '..', 'resources', 'test_file1.txt') file = yield from aioopen(filename, mode=mode) yield from file.seek(0) # Needed for the append mode. actual = yield from file.read() assert '' == (yield from file.read()) assert actual == open(filename, mode='r').read() yield from file.close()
async def test_simple_read_fail(mode, tmpdir): """Try reading some bytes and fail.""" filename = 'bigfile.bin' content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE full_file = tmpdir.join(filename) full_file.write(content) with pytest.raises(ValueError): async with aioopen(str(full_file), mode=mode) as file: await file.seek(0) # Needed for the append mode. await file.read() assert file.closed
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_readlines(mode, buffering): """Test the readlines functionality.""" filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt') with open(filename, mode='rb') as f: expected = f.readlines() async with aioopen(str(filename), mode=mode) as file: # Append mode needs us to seek. await file.seek(0) actual = await file.readlines() assert actual == expected
def test_simple_seek(mode, tmpdir): """Test seeking and then reading.""" filename = 'bigfile.bin' content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE full_file = tmpdir.join(filename) full_file.write(content) file = yield from aioopen(str(full_file), mode=mode) yield from file.seek(4) assert '4' == (yield from file.read(1)) yield from file.close()
async def test_simple_iteration_ctx_mgr(mode): """Test iterating over lines from a file.""" filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt') async with aioopen(filename, mode=mode) as file: assert not file.closed await file.seek(0) counter = 1 async for line in file: assert line.strip() == 'line ' + str(counter) counter += 1 assert file.closed
def test_simple_seek(mode, tmpdir): """Test seeking and then reading.""" filename = 'bigfile.bin' content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE full_file = tmpdir.join(filename) full_file.write(content) file = yield from aioopen(str(full_file), mode=mode) yield from file.seek(4) assert '4' == (yield from file.read(1)) yield from file.close()
async def test_simple_write(mode, buffering, tmpdir): """Test writing into a file.""" filename = 'bigfile.bin' content = b'0' * 4 * io.DEFAULT_BUFFER_SIZE full_file = tmpdir.join(filename) if 'r' in mode: full_file.ensure() # Read modes want it to already exist. async with aioopen(str(full_file), mode=mode, buffering=buffering) as file: bytes_written = await file.write(content) assert bytes_written == len(content) assert content == full_file.read_binary()
def test_simple_write(mode, tmpdir): """Test writing into a file.""" filename = 'bigfile.bin' content = '0' * 4 * io.DEFAULT_BUFFER_SIZE full_file = tmpdir.join(filename) if 'r' in mode: full_file.ensure() # Read modes want it to already exist. file = yield from aioopen(str(full_file), mode=mode) bytes_written = yield from file.write(content) yield from file.close() assert bytes_written == len(content) assert content == full_file.read()
async def test_simple_readlines(mode): """Test the readlines functionality.""" filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt') with open(filename, mode='r') as f: expected = f.readlines() async with aioopen(filename, mode=mode) as file: # Append mode needs us to seek. await file.seek(0) actual = await file.readlines() assert file.closed assert actual == expected
def test_simple_readlines(mode): """Test the readlines functionality.""" filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt') with open(filename, mode='r') as f: expected = f.readlines() file = yield from aioopen(filename, mode=mode) # Append mode needs us to seek. yield from file.seek(0) actual = yield from file.readlines() yield from file.close() assert actual == expected
async def test_simple_detach(tmpdir): """Test detaching for buffered streams.""" filename = 'file.bin' full_file = tmpdir.join(filename) full_file.write_binary(b'0123456789') with pytest.raises(ValueError): async with aioopen(str(full_file), mode='rb') as file: raw_file = file.detach() assert raw_file with pytest.raises(ValueError): await file.read() assert b'0123456789' == raw_file.read(10)
def test_simple_detach(tmpdir): """Test detaching for buffered streams.""" filename = 'file.bin' full_file = tmpdir.join(filename) full_file.write('0123456789') file = yield from aioopen(str(full_file), mode='r') raw_file = file.detach() assert raw_file with pytest.raises(ValueError): yield from file.read() assert b'0123456789' == raw_file.read(10)
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) file = yield from aioopen(str(full_file), mode=mode) assert not file.closed assert not file._file.closed yield from file.close() assert file.closed assert file._file.closed
def test_simple_readall(tmpdir): """Test the readall function by reading a large file in. Only RawIOBase supports readall(). """ filename = 'bigfile.bin' content = b'0' * 4 * io.DEFAULT_BUFFER_SIZE # Hopefully several reads. sync_file = tmpdir.join(filename) sync_file.write_binary(content) file = yield from aioopen(str(sync_file), mode='rb', buffering=0) actual = yield from file.readall() assert actual == content yield from file.close()
async def test_simple_flush(mode, tmpdir): """Test flushing to a file.""" filename = 'file.bin' full_file = tmpdir.join(filename) if 'r' in mode: full_file.ensure() # Read modes want it to already exist. async with aioopen(str(full_file), mode=mode) as file: await file.write('0') # Shouldn't flush. assert '' == full_file.read_text(encoding='utf8') await file.flush() assert '0' == full_file.read_text(encoding='utf8') assert file.closed
async def test_simple_flush(mode, tmpdir): """Test flushing to a file.""" filename = 'file.bin' full_file = tmpdir.join(filename) if 'r' in mode: full_file.ensure() # Read modes want it to already exist. async with aioopen(str(full_file), mode=mode) as file: await file.write('0') # Shouldn't flush. assert '' == full_file.read_text(encoding='utf8') await file.flush() assert '0' == full_file.read_text(encoding='utf8') assert file.closed
def test_file_not_found(mode): filename = 'non_existent' try: open(filename, mode=mode) except Exception as e: expected = e assert expected try: yield from aioopen(filename, mode=mode) except Exception as e: actual = e assert actual assert actual.errno == expected.errno assert str(actual) == str(expected)
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_flush(mode, buffering, tmpdir): """Test flushing to a file.""" filename = 'file.bin' full_file = tmpdir.join(filename) if 'r' in mode: full_file.ensure() # Read modes want it to already exist. async with aioopen(str(full_file), mode=mode, buffering=buffering) as file: await file.write(b'0') # Shouldn't flush. if buffering == -1: assert b'' == full_file.read_binary() else: assert b'0' == full_file.read_binary() await file.flush() assert b'0' == full_file.read_binary()
def test_simple_iteration(mode): """Test iterating over lines from a file.""" filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt') file = yield from aioopen(filename, mode=mode) # Append mode needs us to seek. yield from file.seek(0) counter = 1 # The recommended iteration pattern: while True: line = yield from file.readline() if not line: break assert line.strip() == 'line ' + str(counter) counter += 1 yield from file.close()
async def test_simple_truncate(mode, buffering, tmpdir): """Test truncating files.""" filename = 'bigfile.bin' content = b'0123456789' * 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: # The append modes want us to seek first. await file.seek(0) if 'w' in mode: # We've just erased the entire file. await file.write(content) await file.flush() await file.seek(0) await file.truncate() assert b'' == full_file.read_binary()
async def test_simple_truncate(mode, tmpdir): """Test truncating files.""" filename = 'bigfile.bin' content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE full_file = tmpdir.join(filename) full_file.write(content) async with aioopen(str(full_file), mode=mode) as file: # The append modes want us to seek first. await file.seek(0) if 'w' in mode: # We've just erased the entire file. await file.write(content) await file.flush() await file.seek(0) await file.truncate() assert '' == full_file.read()
def test_simple_truncate(mode, tmpdir): """Test truncating files.""" filename = 'bigfile.bin' content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE full_file = tmpdir.join(filename) full_file.write(content) file = yield from aioopen(str(full_file), mode=mode) # The append modes want us to seek first. yield from file.seek(0) if 'w' in mode: # We've just erased the entire file. yield from file.write(content) yield from file.flush() yield from file.seek(0) yield from file.truncate() yield from file.close() assert '' == full_file.read()