| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
from stat import filemode |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from filelock import FileLock, SoftFileLock |
| 11 |
from filelock._api import _UNSET_FILE_MODE, BaseFileLock |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from pathlib import Path |
| 15 |
from unittest.mock import MagicMock |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock]) |
| 19 |
@pytest.mark.parametrize( |
| 20 |
("mode", "expected"), |
| 21 |
[ |
| 22 |
pytest.param(_UNSET_FILE_MODE, 0o644, id="default"), |
| 23 |
pytest.param(0o600, 0o600, id="explicit"), |
| 24 |
], |
| 25 |
) |
| 26 |
def test_mode_property(lock_type: type[BaseFileLock], mode: int, expected: int, tmp_path: Path) -> None: |
| 27 |
assert lock_type(tmp_path / "a.lock", mode=mode).mode == expected |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock]) |
| 31 |
@pytest.mark.parametrize( |
| 32 |
("mode", "expected"), |
| 33 |
[ |
| 34 |
pytest.param(_UNSET_FILE_MODE, False, id="default"), |
| 35 |
pytest.param(0o644, True, id="explicit"), |
| 36 |
], |
| 37 |
) |
| 38 |
def test_has_explicit_mode(lock_type: type[BaseFileLock], mode: int, expected: bool, tmp_path: Path) -> None: |
| 39 |
assert lock_type(tmp_path / "a.lock", mode=mode).has_explicit_mode is expected |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock]) |
| 43 |
@pytest.mark.parametrize( |
| 44 |
("mode", "expected"), |
| 45 |
[ |
| 46 |
pytest.param(_UNSET_FILE_MODE, 0o666, id="default"), |
| 47 |
pytest.param(0o600, 0o600, id="explicit"), |
| 48 |
], |
| 49 |
) |
| 50 |
def test_open_mode(lock_type: type[BaseFileLock], mode: int, expected: int, tmp_path: Path) -> None: |
| 51 |
assert lock_type(tmp_path / "a.lock", mode=mode)._open_mode() == expected |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.skipif(sys.platform == "win32", reason="Windows does not apply umask to file permissions") |
| 55 |
@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock]) |
| 56 |
def test_default_mode_respects_umask(lock_type: type[BaseFileLock], tmp_path: Path) -> None: # pragma: win32 no cover |
| 57 |
lock_path = tmp_path / "a.lock" |
| 58 |
lock = lock_type(str(lock_path)) |
| 59 |
|
| 60 |
initial_umask = os.umask(0o022) |
| 61 |
try: # pragma: win32 no cover |
| 62 |
lock.acquire() |
| 63 |
assert lock.is_locked |
| 64 |
assert filemode(lock_path.stat().st_mode) == "-rw-r--r--" |
| 65 |
finally: |
| 66 |
os.umask(initial_umask) |
| 67 |
|
| 68 |
lock.release() |
| 69 |
|
| 70 |
|
| 71 |
@pytest.mark.skipif(sys.platform == "win32", reason="fchmod only on Unix") |
| 72 |
def test_default_mode_skips_fchmod(tmp_path: Path, mocker: MagicMock) -> None: # pragma: win32 no cover |
| 73 |
lock = FileLock(str(tmp_path / "a.lock")) |
| 74 |
|
| 75 |
fchmod_spy = mocker.patch("os.fchmod") |
| 76 |
lock.acquire() |
| 77 |
assert lock.is_locked |
| 78 |
fchmod_spy.assert_not_called() |
| 79 |
lock.release() |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.skipif(sys.platform == "win32", reason="fchmod only on Unix") |
| 83 |
def test_explicit_mode_calls_fchmod(tmp_path: Path, mocker: MagicMock) -> None: # pragma: win32 no cover |
| 84 |
lock = FileLock(str(tmp_path / "a.lock"), mode=0o644) |
| 85 |
|
| 86 |
fchmod_spy = mocker.spy(os, "fchmod") |
| 87 |
lock.acquire() |
| 88 |
assert lock.is_locked |
| 89 |
fchmod_spy.assert_called_once() |
| 90 |
assert fchmod_spy.call_args[0][1] == 0o644 |
| 91 |
lock.release() |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.skipif(sys.platform == "win32", reason="fchmod only on Unix") |
| 95 |
def test_explicit_mode_overrides_umask(tmp_path: Path) -> None: # pragma: win32 no cover |
| 96 |
lock_path = tmp_path / "a.lock" |
| 97 |
lock = FileLock(str(lock_path), mode=0o666) |
| 98 |
|
| 99 |
initial_umask = os.umask(0o022) |
| 100 |
try: # pragma: win32 no cover |
| 101 |
lock.acquire() |
| 102 |
assert lock.is_locked |
| 103 |
assert filemode(lock_path.stat().st_mode) == "-rw-rw-rw-" |
| 104 |
finally: |
| 105 |
os.umask(initial_umask) |
| 106 |
|
| 107 |
lock.release() |
| 108 |
|
| 109 |
|
| 110 |
@pytest.mark.parametrize( |
| 111 |
"mode", |
| 112 |
[ |
| 113 |
pytest.param(_UNSET_FILE_MODE, id="default"), |
| 114 |
pytest.param(0o600, id="explicit"), |
| 115 |
], |
| 116 |
) |
| 117 |
def test_singleton_same_mode_matches(mode: int, tmp_path: Path) -> None: |
| 118 |
lock_path = str(tmp_path / "a.lock") |
| 119 |
assert FileLock(lock_path, mode=mode, is_singleton=True) is FileLock(lock_path, mode=mode, is_singleton=True) |
| 120 |
|
| 121 |
|
| 122 |
def test_singleton_default_vs_explicit_mode_differ(tmp_path: Path) -> None: |
| 123 |
lock_path = str(tmp_path / "a.lock") |
| 124 |
lock = FileLock(lock_path, is_singleton=True) |
| 125 |
with pytest.raises(ValueError, match="mode"): |
| 126 |
FileLock(lock_path, mode=0o644, is_singleton=True) |
| 127 |
del lock |
| 128 |
|
| 129 |
|
| 130 |
def test_unset_file_mode_sentinel_value() -> None: |
| 131 |
assert _UNSET_FILE_MODE == -1 |