| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
import pytest |
| 6 |
from _pytest.fixtures import SubRequest |
| 7 |
from _pytest.tmpdir import TempPathFactory |
| 8 |
|
| 9 |
from anyio import ClosedResourceError, EndOfStream |
| 10 |
from anyio.abc import ByteReceiveStream |
| 11 |
from anyio.streams.file import FileReadStream, FileStreamAttribute, FileWriteStream |
| 12 |
|
| 13 |
|
| 14 |
class TestFileReadStream: |
| 15 |
@pytest.fixture(scope="class") |
| 16 |
def file_path(self, tmp_path_factory: TempPathFactory) -> Path: |
| 17 |
path = tmp_path_factory.mktemp("filestream") / "data.txt" |
| 18 |
path.write_text("Hello") |
| 19 |
return path |
| 20 |
|
| 21 |
@pytest.fixture(params=[False, True], ids=["str", "path"]) |
| 22 |
def file_path_or_str(self, request: SubRequest, file_path: Path) -> Path | str: |
| 23 |
return file_path if request.param else str(file_path) |
| 24 |
|
| 25 |
async def _run_filestream_test(self, stream: ByteReceiveStream) -> None: |
| 26 |
assert await stream.receive(3) == b"Hel" |
| 27 |
assert await stream.receive(3) == b"lo" |
| 28 |
with pytest.raises(EndOfStream): |
| 29 |
await stream.receive(1) |
| 30 |
|
| 31 |
async def test_read_file_as_path(self, file_path_or_str: Path | str) -> None: |
| 32 |
async with await FileReadStream.from_path(file_path_or_str) as stream: |
| 33 |
await self._run_filestream_test(stream) |
| 34 |
|
| 35 |
async def test_read_file(self, file_path: Path) -> None: |
| 36 |
with file_path.open("rb") as file: |
| 37 |
async with FileReadStream(file) as stream: |
| 38 |
await self._run_filestream_test(stream) |
| 39 |
|
| 40 |
async def test_read_after_close(self, file_path: Path) -> None: |
| 41 |
async with await FileReadStream.from_path(file_path) as stream: |
| 42 |
pass |
| 43 |
|
| 44 |
with pytest.raises(ClosedResourceError): |
| 45 |
await stream.receive() |
| 46 |
|
| 47 |
async def test_seek(self, file_path: Path) -> None: |
| 48 |
with file_path.open("rb") as file: |
| 49 |
async with FileReadStream(file) as stream: |
| 50 |
await stream.seek(2) |
| 51 |
assert await stream.tell() == 2 |
| 52 |
data = await stream.receive() |
| 53 |
assert data == b"llo" |
| 54 |
assert await stream.tell() == 5 |
| 55 |
|
| 56 |
async def test_extra_attributes(self, file_path: Path) -> None: |
| 57 |
async with await FileReadStream.from_path(file_path) as stream: |
| 58 |
path = stream.extra(FileStreamAttribute.path) |
| 59 |
assert path == file_path |
| 60 |
|
| 61 |
fileno = stream.extra(FileStreamAttribute.fileno) |
| 62 |
assert fileno > 2 |
| 63 |
|
| 64 |
file = stream.extra(FileStreamAttribute.file) |
| 65 |
assert file.fileno() == fileno |
| 66 |
|
| 67 |
|
| 68 |
class TestFileWriteStream: |
| 69 |
@pytest.fixture |
| 70 |
def file_path(self, tmp_path: Path) -> Path: |
| 71 |
return tmp_path / "written_data.txt" |
| 72 |
|
| 73 |
async def test_write_file(self, file_path: Path) -> None: |
| 74 |
async with await FileWriteStream.from_path(file_path) as stream: |
| 75 |
await stream.send(b"Hel") |
| 76 |
await stream.send(b"lo") |
| 77 |
|
| 78 |
assert file_path.read_text() == "Hello" |
| 79 |
|
| 80 |
async def test_append_file(self, file_path: Path) -> None: |
| 81 |
file_path.write_text("Hello") |
| 82 |
async with await FileWriteStream.from_path(file_path, True) as stream: |
| 83 |
await stream.send(b", World!") |
| 84 |
|
| 85 |
assert file_path.read_text() == "Hello, World!" |
| 86 |
|
| 87 |
async def test_write_after_close(self, file_path: Path) -> None: |
| 88 |
async with await FileWriteStream.from_path(file_path, True) as stream: |
| 89 |
pass |
| 90 |
|
| 91 |
with pytest.raises(ClosedResourceError): |
| 92 |
await stream.send(b"foo") |
| 93 |
|
| 94 |
async def test_extra_attributes(self, file_path: Path) -> None: |
| 95 |
async with await FileWriteStream.from_path(file_path) as stream: |
| 96 |
path = stream.extra(FileStreamAttribute.path) |
| 97 |
assert path == file_path |
| 98 |
|
| 99 |
fileno = stream.extra(FileStreamAttribute.fileno) |
| 100 |
assert fileno > 2 |
| 101 |
|
| 102 |
file = stream.extra(FileStreamAttribute.file) |
| 103 |
assert file.fileno() == fileno |