1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
import sys
from pathlib import Path
from stat import S_IRWXO
from typing import TYPE_CHECKING
import pytest
from fsspec.implementations.local import LocalFileSystem
from litestar.exceptions import InternalServerException, NotAuthorizedException
from litestar.file_system import BaseLocalFileSystem, FileSystemAdapter
if TYPE_CHECKING:
from litestar.types import FileSystemProtocol
@pytest.mark.parametrize("file_system", (BaseLocalFileSystem(), LocalFileSystem()))
async def test_file_adapter_open(tmpdir: Path, file_system: "FileSystemProtocol") -> None:
file = Path(tmpdir / "test.txt")
file.write_bytes(b"test")
adapter = FileSystemAdapter(file_system=file_system)
async with await adapter.open(file=file) as opened_file:
assert await opened_file.read() == b"test"
@pytest.mark.parametrize("file_system", (BaseLocalFileSystem(), LocalFileSystem()))
@pytest.mark.xfail(sys.platform == "win32", reason="permissions equivalent missing on windows")
async def test_file_adapter_open_handles_permission_exception(tmpdir: Path, file_system: "FileSystemProtocol") -> None:
file = Path(tmpdir / "test.txt")
file.write_bytes(b"test")
owner_permissions = file.stat().st_mode
file.chmod(S_IRWXO)
adapter = FileSystemAdapter(file_system=file_system)
with pytest.raises(NotAuthorizedException):
async with await adapter.open(file=file):
pass
Path(tmpdir).chmod(owner_permissions)
@pytest.mark.parametrize("file_system", (BaseLocalFileSystem(), LocalFileSystem()))
async def test_file_adapter_open_handles_file_not_found_exception(file_system: "FileSystemProtocol") -> None:
adapter = FileSystemAdapter(file_system=file_system)
with pytest.raises(InternalServerException):
async with await adapter.open(file="non_existing_file.txt"):
pass
@pytest.mark.parametrize("file_system", (BaseLocalFileSystem(), LocalFileSystem()))
@pytest.mark.xfail(sys.platform == "win32", reason="Suspected fsspec issue", strict=False)
async def test_file_adapter_info(tmpdir: Path, file_system: "FileSystemProtocol") -> None:
file = Path(tmpdir / "test.txt")
file.write_bytes(b"test")
adapter = FileSystemAdapter(file_system=file_system)
result = file.stat()
assert await adapter.info(file) == {
"gid": result.st_gid,
"ino": result.st_ino,
"islink": False,
"mode": result.st_mode,
"mtime": result.st_mtime,
"name": str(file),
"nlink": 1,
"created": result.st_ctime,
"size": result.st_size,
"type": "file",
"uid": result.st_uid,
}
@pytest.mark.parametrize("file_system", (BaseLocalFileSystem(), LocalFileSystem()))
async def test_file_adapter_info_handles_file_not_found_exception(file_system: "FileSystemProtocol") -> None:
adapter = FileSystemAdapter(file_system=file_system)
with pytest.raises(FileNotFoundError):
await adapter.info(path="non_existing_file.txt")
@pytest.mark.parametrize("file_system", (BaseLocalFileSystem(), LocalFileSystem()))
@pytest.mark.xfail(sys.platform == "win32", reason="permissions equivalent missing on windows")
async def test_file_adapter_info_handles_permission_exception(tmpdir: Path, file_system: "FileSystemProtocol") -> None:
file = Path(tmpdir / "test.txt")
file.write_bytes(b"test")
owner_permissions = file.stat().st_mode
Path(tmpdir).chmod(S_IRWXO)
adapter = FileSystemAdapter(file_system=file_system)
with pytest.raises(NotAuthorizedException):
await adapter.info(path=file)
Path(tmpdir).chmod(owner_permissions)
|