| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import pathlib |
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import trio |
| 10 |
from trio._file_io import AsyncIOWrapper |
| 11 |
|
| 12 |
if TYPE_CHECKING: |
| 13 |
from collections.abc import Awaitable, Callable |
| 14 |
|
| 15 |
|
| 16 |
@pytest.fixture |
| 17 |
def path(tmp_path: pathlib.Path) -> trio.Path: |
| 18 |
return trio.Path(tmp_path / "test") |
| 19 |
|
| 20 |
|
| 21 |
def method_pair( |
| 22 |
path: str, |
| 23 |
method_name: str, |
| 24 |
) -> tuple[Callable[[], object], Callable[[], Awaitable[object]]]: |
| 25 |
sync_path = pathlib.Path(path) |
| 26 |
async_path = trio.Path(path) |
| 27 |
return getattr(sync_path, method_name), getattr(async_path, method_name) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.skipif(os.name == "nt", reason="OS is not posix") |
| 31 |
def test_instantiate_posix() -> None: |
| 32 |
assert isinstance(trio.Path(), trio.PosixPath) |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.skipif(os.name != "nt", reason="OS is not Windows") |
| 36 |
def test_instantiate_windows() -> None: |
| 37 |
assert isinstance(trio.Path(), trio.WindowsPath) |
| 38 |
|
| 39 |
|
| 40 |
async def test_open_is_async_context_manager(path: trio.Path) -> None: |
| 41 |
async with await path.open("w") as f: |
| 42 |
assert isinstance(f, AsyncIOWrapper) |
| 43 |
|
| 44 |
assert f.closed |
| 45 |
|
| 46 |
|
| 47 |
def test_magic() -> None: |
| 48 |
path = trio.Path("test") |
| 49 |
|
| 50 |
assert str(path) == "test" |
| 51 |
assert bytes(path) == b"test" |
| 52 |
|
| 53 |
|
| 54 |
EitherPathType = type[trio.Path] | type[pathlib.Path] |
| 55 |
PathOrStrType = EitherPathType | type[str] |
| 56 |
cls_pairs: list[tuple[EitherPathType, EitherPathType]] = [ |
| 57 |
(trio.Path, pathlib.Path), |
| 58 |
(pathlib.Path, trio.Path), |
| 59 |
(trio.Path, trio.Path), |
| 60 |
] |
| 61 |
|
| 62 |
|
| 63 |
@pytest.mark.parametrize(("cls_a", "cls_b"), cls_pairs) |
| 64 |
def test_cmp_magic(cls_a: EitherPathType, cls_b: EitherPathType) -> None: |
| 65 |
a, b = cls_a(""), cls_b("") |
| 66 |
assert a == b |
| 67 |
assert not a != b # noqa: SIM202 # negate-not-equal-op |
| 68 |
|
| 69 |
a, b = cls_a("a"), cls_b("b") |
| 70 |
assert a < b |
| 71 |
assert b > a |
| 72 |
|
| 73 |
# this is intentionally testing equivalence with none, due to the |
| 74 |
# other=sentinel logic in _forward_magic |
| 75 |
assert not a == None # noqa |
| 76 |
assert not b == None # noqa |
| 77 |
|
| 78 |
|
| 79 |
# upstream python3.8 bug: we should also test (pathlib.Path, trio.Path), but |
| 80 |
# __*div__ does not properly raise NotImplementedError like the other comparison |
| 81 |
# magic, so trio.Path's implementation does not get dispatched |
| 82 |
cls_pairs_str: list[tuple[PathOrStrType, PathOrStrType]] = [ |
| 83 |
(trio.Path, pathlib.Path), |
| 84 |
(trio.Path, trio.Path), |
| 85 |
(trio.Path, str), |
| 86 |
(str, trio.Path), |
| 87 |
] |
| 88 |
|
| 89 |
|
| 90 |
@pytest.mark.parametrize(("cls_a", "cls_b"), cls_pairs_str) |
| 91 |
def test_div_magic(cls_a: PathOrStrType, cls_b: PathOrStrType) -> None: |
| 92 |
a, b = cls_a("a"), cls_b("b") |
| 93 |
|
| 94 |
result = a / b # type: ignore[operator] |
| 95 |
# Type checkers think str / str could happen. Check each combo manually in type_tests/. |
| 96 |
assert isinstance(result, trio.Path) |
| 97 |
assert str(result) == os.path.join("a", "b") |
| 98 |
|
| 99 |
|
| 100 |
@pytest.mark.parametrize( |
| 101 |
("cls_a", "cls_b"), |
| 102 |
[(trio.Path, pathlib.Path), (trio.Path, trio.Path)], |
| 103 |
) |
| 104 |
@pytest.mark.parametrize("path", ["foo", "foo/bar/baz", "./foo"]) |
| 105 |
def test_hash_magic( |
| 106 |
cls_a: EitherPathType, |
| 107 |
cls_b: EitherPathType, |
| 108 |
path: str, |
| 109 |
) -> None: |
| 110 |
a, b = cls_a(path), cls_b(path) |
| 111 |
assert hash(a) == hash(b) |
| 112 |
|
| 113 |
|
| 114 |
def test_forwarded_properties(path: trio.Path) -> None: |
| 115 |
# use `name` as a representative of forwarded properties |
| 116 |
|
| 117 |
assert "name" in dir(path) |
| 118 |
assert path.name == "test" |
| 119 |
|
| 120 |
|
| 121 |
def test_async_method_signature(path: trio.Path) -> None: |
| 122 |
# use `resolve` as a representative of wrapped methods |
| 123 |
|
| 124 |
assert path.resolve.__name__ == "resolve" |
| 125 |
assert path.resolve.__qualname__ == "Path.resolve" |
| 126 |
|
| 127 |
assert path.resolve.__doc__ is not None |
| 128 |
assert path.resolve.__qualname__ in path.resolve.__doc__ |
| 129 |
|
| 130 |
|
| 131 |
@pytest.mark.parametrize("method_name", ["is_dir", "is_file"]) |
| 132 |
async def test_compare_async_stat_methods(method_name: str) -> None: |
| 133 |
method, async_method = method_pair(".", method_name) |
| 134 |
|
| 135 |
result = method() |
| 136 |
async_result = await async_method() |
| 137 |
|
| 138 |
assert result == async_result |
| 139 |
|
| 140 |
|
| 141 |
def test_invalid_name_not_wrapped(path: trio.Path) -> None: |
| 142 |
with pytest.raises(AttributeError): |
| 143 |
getattr(path, "invalid_fake_attr") # noqa: B009 # "get-attr-with-constant" |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.parametrize("method_name", ["absolute", "resolve"]) |
| 147 |
async def test_async_methods_rewrap(method_name: str) -> None: |
| 148 |
method, async_method = method_pair(".", method_name) |
| 149 |
|
| 150 |
result = method() |
| 151 |
async_result = await async_method() |
| 152 |
|
| 153 |
assert isinstance(async_result, trio.Path) |
| 154 |
assert str(result) == str(async_result) |
| 155 |
|
| 156 |
|
| 157 |
def test_forward_methods_rewrap(path: trio.Path, tmp_path: pathlib.Path) -> None: |
| 158 |
with_name = path.with_name("foo") |
| 159 |
with_suffix = path.with_suffix(".py") |
| 160 |
|
| 161 |
assert isinstance(with_name, trio.Path) |
| 162 |
assert with_name == tmp_path / "foo" |
| 163 |
assert isinstance(with_suffix, trio.Path) |
| 164 |
assert with_suffix == tmp_path / "test.py" |
| 165 |
|
| 166 |
|
| 167 |
def test_forward_properties_rewrap(path: trio.Path) -> None: |
| 168 |
assert isinstance(path.parent, trio.Path) |
| 169 |
|
| 170 |
|
| 171 |
def test_forward_methods_without_rewrap(path: trio.Path) -> None: |
| 172 |
assert "totally-unique-path" in str(path.joinpath("totally-unique-path")) |
| 173 |
|
| 174 |
|
| 175 |
def test_repr() -> None: |
| 176 |
path = trio.Path(".") |
| 177 |
|
| 178 |
assert repr(path) == "trio.Path('.')" |
| 179 |
|
| 180 |
|
| 181 |
@pytest.mark.parametrize("meth", [trio.Path.__init__, trio.Path.joinpath]) |
| 182 |
async def test_path_wraps_path( |
| 183 |
path: trio.Path, |
| 184 |
meth: Callable[[trio.Path, trio.Path], object], |
| 185 |
) -> None: |
| 186 |
wrapped = await path.absolute() |
| 187 |
result = meth(path, wrapped) |
| 188 |
if result is None: |
| 189 |
result = path |
| 190 |
|
| 191 |
assert wrapped == result |
| 192 |
|
| 193 |
|
| 194 |
def test_path_nonpath() -> None: |
| 195 |
with pytest.raises(TypeError): |
| 196 |
trio.Path(1) # type: ignore |
| 197 |
|
| 198 |
|
| 199 |
async def test_open_file_can_open_path(path: trio.Path) -> None: |
| 200 |
async with await trio.open_file(path, "w") as f: |
| 201 |
assert f.name == os.fspath(path) |
| 202 |
|
| 203 |
|
| 204 |
async def test_globmethods(path: trio.Path) -> None: |
| 205 |
# Populate a directory tree |
| 206 |
await path.mkdir() |
| 207 |
await (path / "foo").mkdir() |
| 208 |
await (path / "foo" / "_bar.txt").write_bytes(b"") |
| 209 |
await (path / "bar.txt").write_bytes(b"") |
| 210 |
await (path / "bar.dat").write_bytes(b"") |
| 211 |
|
| 212 |
# Path.glob |
| 213 |
for pattern, results in { |
| 214 |
"*.txt": {"bar.txt"}, |
| 215 |
"**/*.txt": {"_bar.txt", "bar.txt"}, |
| 216 |
}.items(): |
| 217 |
entries = set() |
| 218 |
for entry in await path.glob(pattern): |
| 219 |
assert isinstance(entry, trio.Path) |
| 220 |
entries.add(entry.name) |
| 221 |
|
| 222 |
assert entries == results |
| 223 |
|
| 224 |
# Path.rglob |
| 225 |
entries = set() |
| 226 |
for entry in await path.rglob("*.txt"): |
| 227 |
assert isinstance(entry, trio.Path) |
| 228 |
entries.add(entry.name) |
| 229 |
|
| 230 |
assert entries == {"_bar.txt", "bar.txt"} |
| 231 |
|
| 232 |
|
| 233 |
async def test_as_uri(path: trio.Path) -> None: |
| 234 |
path = await path.parent.resolve() |
| 235 |
|
| 236 |
assert path.as_uri().startswith("file:///") |
| 237 |
|
| 238 |
|
| 239 |
async def test_iterdir(path: trio.Path) -> None: |
| 240 |
# Populate a directory |
| 241 |
await path.mkdir() |
| 242 |
await (path / "foo").mkdir() |
| 243 |
await (path / "bar.txt").write_bytes(b"") |
| 244 |
|
| 245 |
entries = set() |
| 246 |
for entry in await path.iterdir(): |
| 247 |
assert isinstance(entry, trio.Path) |
| 248 |
entries.add(entry.name) |
| 249 |
|
| 250 |
assert entries == {"bar.txt", "foo"} |
| 251 |
|
| 252 |
|
| 253 |
async def test_classmethods() -> None: |
| 254 |
assert isinstance(await trio.Path.home(), trio.Path) |
| 255 |
|
| 256 |
# pathlib.Path has only two classmethods |
| 257 |
assert str(await trio.Path.home()) == os.path.expanduser("~") # noqa: ASYNC240 |
| 258 |
assert str(await trio.Path.cwd()) == os.getcwd() |
| 259 |
|
| 260 |
# Wrapped method has docstring |
| 261 |
assert trio.Path.home.__doc__ |
| 262 |
|
| 263 |
|
| 264 |
@pytest.mark.parametrize( |
| 265 |
"wrapper", |
| 266 |
[ |
| 267 |
trio._path._wraps_async, |
| 268 |
trio._path._wrap_method, |
| 269 |
trio._path._wrap_method_path, |
| 270 |
trio._path._wrap_method_path_iterable, |
| 271 |
], |
| 272 |
) |
| 273 |
def test_wrapping_without_docstrings( |
| 274 |
wrapper: Callable[[Callable[[], None]], Callable[[], None]], |
| 275 |
) -> None: |
| 276 |
@wrapper |
| 277 |
def func_without_docstring() -> None: ... # pragma: no cover |
| 278 |
|
| 279 |
assert func_without_docstring.__doc__ is None |