| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import subprocess # ruff:ignore[suspicious-subprocess-import] # each client needs an isolated filelock installation |
| 5 |
import sys |
| 6 |
from typing import TYPE_CHECKING, Final, TextIO, cast |
| 7 |
|
| 8 |
import pytest |
| 9 |
from capabilities import CAPABILITIES |
| 10 |
|
| 11 |
from filelock import StrictSoftFileLock, Timeout |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from pathlib import Path |
| 15 |
|
| 16 |
_OLDEST_LEGACY_VERSION: Final[str] = "3.20.0" |
| 17 |
_OLD_CLIENT_PATH_VARIABLE: Final[str] = "FILELOCK_OLD_CLIENT_PATH" |
| 18 |
_OLD_HOLDER: Final[str] = """ |
| 19 |
import sys |
| 20 |
|
| 21 |
from filelock import SoftFileLock |
| 22 |
|
| 23 |
lock = SoftFileLock(sys.argv[1]) |
| 24 |
lock.acquire(timeout=5) |
| 25 |
print("acquired", flush=True) |
| 26 |
sys.stdin.readline() |
| 27 |
lock.release() |
| 28 |
print("released", flush=True) |
| 29 |
""" |
| 30 |
_OLD_PROBE: Final[str] = """ |
| 31 |
import sys |
| 32 |
|
| 33 |
from filelock import SoftFileLock, Timeout |
| 34 |
|
| 35 |
lock = SoftFileLock(sys.argv[1]) |
| 36 |
try: |
| 37 |
lock.acquire(timeout=0) |
| 38 |
except Timeout: |
| 39 |
print("blocked") |
| 40 |
else: |
| 41 |
lock.release() |
| 42 |
print("acquired") |
| 43 |
""" |
| 44 |
|
| 45 |
|
| 46 |
pytestmark = [ |
| 47 |
pytest.mark.requires_hard_links, |
| 48 |
pytest.mark.skipif( |
| 49 |
not CAPABILITIES["old-client"], |
| 50 |
reason=f"{_OLD_CLIENT_PATH_VARIABLE} must name an installed filelock {_OLDEST_LEGACY_VERSION}", |
| 51 |
), |
| 52 |
] |
| 53 |
|
| 54 |
|
| 55 |
@pytest.fixture(scope="module") |
| 56 |
def old_client_env() -> dict[str, str]: # pragma: needs old-client |
| 57 |
old_client_path = os.environ[_OLD_CLIENT_PATH_VARIABLE] |
| 58 |
env = os.environ.copy() |
| 59 |
# Prepend: the inherited PYTHONPATH carries the coverage plugin this child restarts under. |
| 60 |
env["PYTHONPATH"] = os.pathsep.join(part for part in (old_client_path, env.get("PYTHONPATH")) if part) |
| 61 |
installed = subprocess.run( |
| 62 |
[sys.executable, "-c", "import filelock; print(filelock.__version__)"], |
| 63 |
check=True, |
| 64 |
capture_output=True, |
| 65 |
cwd=old_client_path, |
| 66 |
env=env, |
| 67 |
text=True, |
| 68 |
) |
| 69 |
assert installed.stdout.strip() == _OLDEST_LEGACY_VERSION |
| 70 |
return env |
| 71 |
|
| 72 |
|
| 73 |
def test_old_soft_holder_blocks_strict_client( |
| 74 |
tmp_path: Path, old_client_env: dict[str, str] |
| 75 |
) -> None: # pragma: needs old-client |
| 76 |
lock_path = tmp_path / "resource.lock" |
| 77 |
with subprocess.Popen( |
| 78 |
[sys.executable, "-c", _OLD_HOLDER, os.fspath(lock_path)], |
| 79 |
cwd=tmp_path, |
| 80 |
env=old_client_env, |
| 81 |
stdin=subprocess.PIPE, |
| 82 |
stdout=subprocess.PIPE, |
| 83 |
stderr=subprocess.PIPE, |
| 84 |
text=True, |
| 85 |
) as holder: |
| 86 |
assert cast("TextIO", holder.stdout).readline() == "acquired\n" |
| 87 |
with pytest.raises(Timeout): |
| 88 |
StrictSoftFileLock(lock_path, timeout=0).acquire() |
| 89 |
stdout, stderr = holder.communicate("\n", timeout=5) |
| 90 |
assert (holder.returncode, stdout, stderr) == (0, "released\n", "") |
| 91 |
|
| 92 |
with StrictSoftFileLock(lock_path, timeout=0) as strict: |
| 93 |
assert strict.is_locked |
| 94 |
|
| 95 |
|
| 96 |
def test_strict_activation_rejects_old_soft_client( |
| 97 |
tmp_path: Path, old_client_env: dict[str, str] |
| 98 |
) -> None: # pragma: needs old-client |
| 99 |
lock_path = tmp_path / "resource.lock" |
| 100 |
with StrictSoftFileLock(lock_path): |
| 101 |
blocked = subprocess.run( |
| 102 |
[sys.executable, "-c", _OLD_PROBE, os.fspath(lock_path)], |
| 103 |
check=True, |
| 104 |
capture_output=True, |
| 105 |
cwd=tmp_path, |
| 106 |
env=old_client_env, |
| 107 |
text=True, |
| 108 |
) |
| 109 |
after_release = subprocess.run( |
| 110 |
[sys.executable, "-c", _OLD_PROBE, os.fspath(lock_path)], |
| 111 |
check=True, |
| 112 |
capture_output=True, |
| 113 |
cwd=tmp_path, |
| 114 |
env=old_client_env, |
| 115 |
text=True, |
| 116 |
) |
| 117 |
|
| 118 |
assert (blocked.stdout, blocked.stderr, after_release.stdout, after_release.stderr) == ( |
| 119 |
"blocked\n", |
| 120 |
"", |
| 121 |
"blocked\n", |
| 122 |
"", |
| 123 |
) |