| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING, Final, Literal, cast |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from filelock import AsyncFileLock, AsyncSoftFileLock, FileLock, SoftFileLock |
| 8 |
|
| 9 |
if TYPE_CHECKING: |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
|
| 13 |
pytestmark: Final[pytest.MarkDecorator] = pytest.mark.filterwarnings("ignore::filelock.SoftFileLockLifetimeWarning") |
| 14 |
|
| 15 |
|
| 16 |
@pytest.mark.parametrize( |
| 17 |
"lock_type", |
| 18 |
[ |
| 19 |
pytest.param(FileLock, id="native-sync"), |
| 20 |
pytest.param(SoftFileLock, id="soft-sync"), |
| 21 |
pytest.param(AsyncFileLock, id="native-async"), |
| 22 |
pytest.param(AsyncSoftFileLock, id="soft-async"), |
| 23 |
], |
| 24 |
) |
| 25 |
@pytest.mark.parametrize( |
| 26 |
"entry_point", |
| 27 |
[pytest.param("constructor", id="constructor"), pytest.param("setter", id="setter")], |
| 28 |
) |
| 29 |
@pytest.mark.parametrize( |
| 30 |
("bad_value", "error_type", "message"), |
| 31 |
[ |
| 32 |
pytest.param(-1, ValueError, "finite and non-negative", id="negative-int"), |
| 33 |
pytest.param(-0.5, ValueError, "finite and non-negative", id="negative-float"), |
| 34 |
pytest.param(float("nan"), ValueError, "finite and non-negative", id="nan"), |
| 35 |
pytest.param(float("inf"), ValueError, "finite and non-negative", id="positive-infinity"), |
| 36 |
pytest.param(float("-inf"), ValueError, "finite and non-negative", id="negative-infinity"), |
| 37 |
pytest.param(True, TypeError, "lifetime must be", id="true"), |
| 38 |
pytest.param(False, TypeError, "lifetime must be", id="false"), |
| 39 |
pytest.param("5", TypeError, "lifetime must be", id="string"), |
| 40 |
pytest.param(b"5", TypeError, "lifetime must be", id="bytes"), |
| 41 |
pytest.param([1], TypeError, "lifetime must be", id="list"), |
| 42 |
pytest.param({1: 2}, TypeError, "lifetime must be", id="dict"), |
| 43 |
pytest.param(complex(1, 0), TypeError, "lifetime must be", id="complex"), |
| 44 |
], |
| 45 |
) |
| 46 |
def test_lifetime_rejects_invalid_value( |
| 47 |
lock_type: type[FileLock | AsyncFileLock], |
| 48 |
entry_point: Literal["constructor", "setter"], |
| 49 |
bad_value: str | bytes | list[int] | dict[int, int] | complex, |
| 50 |
error_type: type[ValueError | TypeError], |
| 51 |
message: str, |
| 52 |
tmp_path: Path, |
| 53 |
) -> None: |
| 54 |
lock_path = tmp_path / "test.lock" |
| 55 |
lock = lock_type(lock_path) |
| 56 |
with pytest.raises(error_type, match=message): |
| 57 |
_set_lifetime(lock_type, entry_point, lock, cast("float", bad_value)) |
| 58 |
assert (lock.lifetime, lock_path.exists()) == (None, False) |
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.parametrize( |
| 62 |
"lock_type", |
| 63 |
[pytest.param(SoftFileLock, id="sync"), pytest.param(AsyncSoftFileLock, id="async")], |
| 64 |
) |
| 65 |
@pytest.mark.parametrize( |
| 66 |
"entry_point", |
| 67 |
[pytest.param("constructor", id="constructor"), pytest.param("setter", id="setter")], |
| 68 |
) |
| 69 |
@pytest.mark.parametrize( |
| 70 |
"value", |
| 71 |
[ |
| 72 |
pytest.param(None, id="none"), |
| 73 |
pytest.param(0, id="zero-int"), |
| 74 |
pytest.param(0.0, id="zero-float"), |
| 75 |
pytest.param(2.5, id="positive-float"), |
| 76 |
pytest.param(10**1000, id="large-int"), |
| 77 |
], |
| 78 |
) |
| 79 |
def test_lifetime_accepts_supported_value( |
| 80 |
lock_type: type[SoftFileLock | AsyncSoftFileLock], |
| 81 |
entry_point: Literal["constructor", "setter"], |
| 82 |
value: float | None, |
| 83 |
tmp_path: Path, |
| 84 |
) -> None: |
| 85 |
lock_path = tmp_path / "test.lock" |
| 86 |
if entry_point == "constructor": |
| 87 |
lock = lock_type(lock_path, lifetime=value) |
| 88 |
else: |
| 89 |
lock = lock_type(lock_path) |
| 90 |
lock.lifetime = value |
| 91 |
assert lock.lifetime == value |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.parametrize( |
| 95 |
"bad_value", |
| 96 |
[ |
| 97 |
pytest.param(-1, id="negative"), |
| 98 |
pytest.param(float("nan"), id="nan"), |
| 99 |
pytest.param(float("inf"), id="infinity"), |
| 100 |
], |
| 101 |
) |
| 102 |
def test_lifetime_singleton_rejection_preserves_instance(bad_value: float, tmp_path: Path) -> None: |
| 103 |
lock_path = tmp_path / "test.lock" |
| 104 |
lock = SoftFileLock(lock_path, is_singleton=True, lifetime=10) |
| 105 |
|
| 106 |
with pytest.raises(ValueError, match="finite and non-negative"): |
| 107 |
SoftFileLock(lock_path, is_singleton=True, lifetime=bad_value) |
| 108 |
|
| 109 |
assert SoftFileLock(lock_path, is_singleton=True, lifetime=10) is lock |
| 110 |
|
| 111 |
|
| 112 |
def _set_lifetime( |
| 113 |
lock_type: type[FileLock | AsyncFileLock], |
| 114 |
entry_point: Literal["constructor", "setter"], |
| 115 |
lock: FileLock | AsyncFileLock, |
| 116 |
value: float, |
| 117 |
) -> None: |
| 118 |
if entry_point == "constructor": |
| 119 |
lock_type(lock.lock_file, lifetime=value) |
| 120 |
else: |
| 121 |
lock.lifetime = value |