| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pickle # ruff:ignore[suspicious-pickle-import] # round-trips Timeout to assert it pickles |
| 4 |
from typing import TYPE_CHECKING |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from filelock import Timeout |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from collections.abc import Callable |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.parametrize( |
| 15 |
("extract", "expected"), |
| 16 |
[ |
| 17 |
pytest.param(str, "The file lock '/path/to/lock' could not be acquired.", id="str"), |
| 18 |
pytest.param(repr, "Timeout('/path/to/lock')", id="repr"), |
| 19 |
pytest.param(lambda t: t.lock_file, "/path/to/lock", id="lock_file"), |
| 20 |
pytest.param(lambda t: t.__reduce__(), (Timeout, ("/path/to/lock",)), id="reduce"), |
| 21 |
], |
| 22 |
) |
| 23 |
def test_timeout_attribute(timeout: Timeout, extract: Callable[[Timeout], object], expected: object) -> None: |
| 24 |
assert extract(timeout) == expected |
| 25 |
|
| 26 |
|
| 27 |
def test_timeout_pickle(timeout: Timeout) -> None: |
| 28 |
reloaded = pickle.loads(pickle.dumps(timeout)) # ruff:ignore[suspicious-pickle-usage] # input is the Timeout built in this test |
| 29 |
assert (type(reloaded), str(reloaded), repr(reloaded), reloaded.lock_file) == ( |
| 30 |
type(timeout), |
| 31 |
str(timeout), |
| 32 |
repr(timeout), |
| 33 |
timeout.lock_file, |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def timeout() -> Timeout: |
| 39 |
return Timeout("/path/to/lock") |