|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import builtins |
5 | 6 | from contextlib import contextmanager |
6 | 7 | from pathlib import Path |
7 | 8 |
|
@@ -45,6 +46,36 @@ def test_exclusive_file_lock_rejects_unknown_platform(tmp_path: Path): |
45 | 46 | pass |
46 | 47 |
|
47 | 48 |
|
| 49 | +def test_posix_lock_reports_unavailable_when_fcntl_missing(monkeypatch, tmp_path: Path): |
| 50 | + real_import = builtins.__import__ |
| 51 | + |
| 52 | + def fake_import(name, *args, **kwargs): |
| 53 | + if name == "fcntl": |
| 54 | + raise ImportError("missing fcntl") |
| 55 | + return real_import(name, *args, **kwargs) |
| 56 | + |
| 57 | + monkeypatch.setattr(builtins, "__import__", fake_import) |
| 58 | + |
| 59 | + with pytest.raises(lockfile.SwarmLockUnavailableError, match="fcntl not available"): |
| 60 | + with file_lock._exclusive_posix_lock(tmp_path / "posix.lock"): |
| 61 | + pass |
| 62 | + |
| 63 | + |
| 64 | +def test_windows_lock_reports_unavailable_when_msvcrt_missing(monkeypatch, tmp_path: Path): |
| 65 | + real_import = builtins.__import__ |
| 66 | + |
| 67 | + def fake_import(name, *args, **kwargs): |
| 68 | + if name == "msvcrt": |
| 69 | + raise ImportError("missing msvcrt") |
| 70 | + return real_import(name, *args, **kwargs) |
| 71 | + |
| 72 | + monkeypatch.setattr(builtins, "__import__", fake_import) |
| 73 | + |
| 74 | + with pytest.raises(lockfile.SwarmLockUnavailableError, match="msvcrt not available"): |
| 75 | + with file_lock._exclusive_windows_lock(tmp_path / "windows.lock"): |
| 76 | + pass |
| 77 | + |
| 78 | + |
48 | 79 | def test_swarm_lockfile_shim_re_exports_public_api(): |
49 | 80 | """Existing callers importing from ``swarm.lockfile`` must keep working.""" |
50 | 81 | assert lockfile.exclusive_file_lock is file_lock.exclusive_file_lock |
|
0 commit comments