| 1 |
"""The :class:`~filelock._soft_rw._storage.OsFiles` implementation of the protocol's filesystem operations.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
import time |
| 8 |
from errno import EIO |
| 9 |
from pathlib import Path |
| 10 |
from typing import TYPE_CHECKING |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from filelock import SoftFileLockProtocolError |
| 15 |
from filelock._soft_rw import _storage as storage_mod |
| 16 |
from filelock._soft_rw._storage import OsFiles |
| 17 |
|
| 18 |
if TYPE_CHECKING: |
| 19 |
from collections.abc import Callable |
| 20 |
|
| 21 |
from pytest_mock import MockerFixture |
| 22 |
|
| 23 |
pytestmark = pytest.mark.requires_hard_links |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.parametrize( |
| 27 |
("call", "expected"), |
| 28 |
[ |
| 29 |
pytest.param(lambda files, path: files.read(path), None, id="read"), |
| 30 |
pytest.param(lambda files, path: files.overwrite(path, b"data"), False, id="overwrite"), |
| 31 |
pytest.param(lambda files, path: files.listdir(path), [], id="listdir"), |
| 32 |
], |
| 33 |
) |
| 34 |
def test_missing_path_returns_default(tmp_path: Path, call: Callable[[OsFiles, str], object], expected: object) -> None: |
| 35 |
assert call(OsFiles(str(tmp_path / "x.lock")), str(tmp_path / "absent")) == expected |
| 36 |
|
| 37 |
|
| 38 |
@pytest.mark.parametrize( |
| 39 |
"call", |
| 40 |
[ |
| 41 |
pytest.param(lambda files, path: files.read(path), id="read"), |
| 42 |
pytest.param(lambda files, path: files.overwrite(path, b"data"), id="overwrite"), |
| 43 |
], |
| 44 |
) |
| 45 |
def test_open_other_errors_propagate( |
| 46 |
tmp_path: Path, mocker: MockerFixture, call: Callable[[OsFiles, str], object] |
| 47 |
) -> None: |
| 48 |
mocker.patch.object(storage_mod.os, "open", side_effect=OSError(EIO, "Input/output error")) |
| 49 |
with pytest.raises(OSError, match="Input/output error"): |
| 50 |
call(OsFiles(str(tmp_path / "x.lock")), str(tmp_path / "absent")) |
| 51 |
|
| 52 |
|
| 53 |
def test_read_non_regular_file_is_empty(tmp_path: Path) -> None: |
| 54 |
# The null device is a character device on every platform, so it stands in for anything planted at a record path |
| 55 |
# that is not a regular file. |
| 56 |
assert OsFiles(str(tmp_path / "x.lock")).read(os.devnull) == b"" |
| 57 |
|
| 58 |
|
| 59 |
def test_create_rolls_back_a_failed_write(tmp_path: Path, mocker: MockerFixture) -> None: |
| 60 |
mocker.patch.object(storage_mod, "write_all", side_effect=OSError("write boom")) |
| 61 |
with pytest.raises(OSError, match="write boom"): |
| 62 |
OsFiles(str(tmp_path / "x.lock")).create(str(tmp_path / "record"), b"data") |
| 63 |
assert not (tmp_path / "record").exists() |
| 64 |
|
| 65 |
|
| 66 |
def test_link_trusts_identity_over_an_error(tmp_path: Path, mocker: MockerFixture) -> None: |
| 67 |
# An NFS retransmit can report a link that landed as failed; the target naming the source's file is what counts. |
| 68 |
real_link = storage_mod._link_no_follow |
| 69 |
|
| 70 |
def link_then_fail(source: str, target: str) -> None: |
| 71 |
real_link(source, target) |
| 72 |
raise OSError(EIO, "Input/output error") |
| 73 |
|
| 74 |
mocker.patch.object(storage_mod, "_link_no_follow", side_effect=link_then_fail) |
| 75 |
source = tmp_path / "source" |
| 76 |
source.write_bytes(b"data") |
| 77 |
assert OsFiles(str(tmp_path / "x.lock")).link(str(source), str(tmp_path / "target")) |
| 78 |
|
| 79 |
|
| 80 |
def test_link_reports_an_unsupported_filesystem(tmp_path: Path, mocker: MockerFixture) -> None: |
| 81 |
mocker.patch.object(storage_mod, "_link_no_follow", side_effect=NotImplementedError("no os.link")) |
| 82 |
source = tmp_path / "source" |
| 83 |
source.write_bytes(b"data") |
| 84 |
with pytest.raises(SoftFileLockProtocolError, match="hard-link"): |
| 85 |
OsFiles(str(tmp_path / "x.lock")).link(str(source), str(tmp_path / "target")) |
| 86 |
|
| 87 |
|
| 88 |
def test_link_from_a_missing_source_raises(tmp_path: Path) -> None: |
| 89 |
with pytest.raises(FileNotFoundError): |
| 90 |
OsFiles(str(tmp_path / "x.lock")).link(str(tmp_path / "absent"), str(tmp_path / "target")) |
| 91 |
|
| 92 |
|
| 93 |
def test_link_raises_a_fault_that_did_not_land(tmp_path: Path, mocker: MockerFixture) -> None: |
| 94 |
# A persistent EPERM or EIO is not a peer winning the race; reporting it as one would spin forever. |
| 95 |
mocker.patch.object(storage_mod, "_link_no_follow", side_effect=OSError(EIO, "Input/output error")) |
| 96 |
source = tmp_path / "source" |
| 97 |
source.write_bytes(b"data") |
| 98 |
with pytest.raises(OSError, match="Input/output error"): |
| 99 |
OsFiles(str(tmp_path / "x.lock")).link(str(source), str(tmp_path / "target")) |
| 100 |
|
| 101 |
|
| 102 |
def test_unlink_tolerates_a_refused_removal(tmp_path: Path, mocker: MockerFixture) -> None: |
| 103 |
mocker.patch.object(Path, "unlink", side_effect=PermissionError("held open")) |
| 104 |
OsFiles(str(tmp_path / "x.lock")).unlink(str(tmp_path / "record")) |
| 105 |
|
| 106 |
|
| 107 |
def test_unlink_other_errors_propagate(tmp_path: Path, mocker: MockerFixture) -> None: |
| 108 |
mocker.patch.object(Path, "unlink", side_effect=OSError(EIO, "Input/output error")) |
| 109 |
with pytest.raises(OSError, match="Input/output error"): |
| 110 |
OsFiles(str(tmp_path / "x.lock")).unlink(str(tmp_path / "record")) |
| 111 |
|
| 112 |
|
| 113 |
@pytest.mark.skipif(sys.platform == "win32", reason="Windows retries a refused open as a sharing race") |
| 114 |
def test_read_refused_on_posix_raises(tmp_path: Path, mocker: MockerFixture) -> None: # pragma: win32 no cover |
| 115 |
# Reading a peer's record as missing when it is unreadable would age it as a constant and evict a holder. |
| 116 |
mocker.patch.object(storage_mod.os, "open", side_effect=PermissionError("denied")) |
| 117 |
with pytest.raises(PermissionError): |
| 118 |
OsFiles(str(tmp_path / "x.lock")).read(str(tmp_path / "record")) |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.skipif(sys.platform != "win32", reason="only Windows retries a refused open as a sharing race") |
| 122 |
def test_read_refused_on_windows_is_missing_after_the_grace( |
| 123 |
tmp_path: Path, mocker: MockerFixture |
| 124 |
) -> None: # pragma: win32 cover |
| 125 |
# A file still refusing after the grace has been delete-pending longer than any deletion takes: on its way out. |
| 126 |
mocker.patch.object(storage_mod.os, "open", side_effect=PermissionError("sharing violation")) |
| 127 |
started = time.monotonic() |
| 128 |
assert OsFiles(str(tmp_path / "x.lock")).read(str(tmp_path / "record")) is None |
| 129 |
assert time.monotonic() - started >= storage_mod._WINDOWS_OPEN_GRACE - 0.05 |
| 130 |
|
| 131 |
|
| 132 |
def test_prepare_creates_missing_parents(tmp_path: Path) -> None: |
| 133 |
root = str(tmp_path / "nested" / "deeper" / "x.lock.rw") |
| 134 |
OsFiles(str(tmp_path / "nested" / "deeper" / "x.lock")).prepare(root) |
| 135 |
assert Path(root, "gen").is_dir() |