| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import gc |
| 4 |
import os |
| 5 |
from pathlib import Path |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
from capabilities import CAPABILITIES |
| 10 |
|
| 11 |
if TYPE_CHECKING: |
| 12 |
from collections.abc import Callable, Iterator |
| 13 |
|
| 14 |
from pytest_mock import MockerFixture |
| 15 |
|
| 16 |
# Coverage restarts in every patched subprocess and re-imports the plugin there; pytest's pythonpath reaches only |
| 17 |
# this process, so export it. |
| 18 |
os.environ["PYTHONPATH"] = os.pathsep.join( |
| 19 |
part for part in (str(Path(__file__).parent.parent / "tasks"), os.environ.get("PYTHONPATH")) if part |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None: |
| 24 |
# StrictSoftFileLock publishes claims with hard links, so tests marked requires_hard_links cannot run where |
| 25 |
# os.link is missing (Termux/Android CPython ships without it). The no-hard-link degradation itself is covered by |
| 26 |
# the os.link tests in test_filelock.py, which carry no marker and run everywhere. |
| 27 |
if not hasattr(os, "link"): # pragma: no cover # the body runs only on Termux/Android, absent from the CI matrix |
| 28 |
skip = pytest.mark.skip(reason="StrictSoftFileLock requires os.link (hard links), absent on Termux/Android") |
| 29 |
for item in items: |
| 30 |
if item.get_closest_marker("requires_hard_links") is not None: |
| 31 |
item.add_marker(skip) |
| 32 |
|
| 33 |
|
| 34 |
@pytest.fixture(autouse=True, scope="session") |
| 35 |
def _tracemalloc_lookup_without_support() -> Iterator[None]: |
| 36 |
# pytest's thread and unraisable exception hooks ask tracemalloc where the offending object was allocated. Where |
| 37 |
# that lookup raises (GraalPy), the hook reports "Failed to process thread exception" in place of the thread's real |
| 38 |
# error, so hand it the answer tracemalloc gives while it is not tracing. |
| 39 |
with pytest.MonkeyPatch.context() as patch: |
| 40 |
if not CAPABILITIES["tracemalloc-object-traceback"]: # pragma: lacks tracemalloc-object-traceback |
| 41 |
patch.setattr("tracemalloc.get_object_traceback", lambda _source: None) |
| 42 |
yield |
| 43 |
|
| 44 |
|
| 45 |
@pytest.fixture(autouse=True) |
| 46 |
def _collect_garbage() -> Iterator[None]: |
| 47 |
# Multiprocessing objects register finalizers that close pipes, unlink semaphores and write to the resource |
| 48 |
# tracker; when cyclic garbage from one test is collected during a later one, those run against whatever that |
| 49 |
# later test has patched onto os.close or os.write. Collect after every test so finalizers fire in the test that |
| 50 |
# created the objects, before any such patch is in place. This runs after mocker's undo, since autouse fixtures |
| 51 |
# set up first and tear down last. |
| 52 |
yield |
| 53 |
gc.collect() |
| 54 |
|
| 55 |
|
| 56 |
@pytest.fixture |
| 57 |
def close_failure( |
| 58 |
mocker: MockerFixture, |
| 59 |
) -> Iterator[tuple[Callable[[int], None], OSError, RuntimeError]]: |
| 60 |
locked_fd: int | None = None |
| 61 |
release_error = OSError("release failed") |
| 62 |
release_cause = RuntimeError("release cause") |
| 63 |
release_error.__cause__ = release_cause |
| 64 |
release_error.__suppress_context__ = True |
| 65 |
real_close = os.close |
| 66 |
|
| 67 |
def capture(fd: int) -> None: |
| 68 |
nonlocal locked_fd |
| 69 |
locked_fd = fd |
| 70 |
mocker.patch("filelock._api.os.close", side_effect=close) |
| 71 |
|
| 72 |
def close(fd: int) -> None: |
| 73 |
# filelock._api.os is the os module, so this patches os.close process-wide. Raise only for the descriptor |
| 74 |
# under test. An unrelated close inside a GC finalizer would escape as an unraisable exception. |
| 75 |
if fd == locked_fd: |
| 76 |
raise release_error |
| 77 |
real_close(fd) # pragma: no cover # only an unrelated close (e.g. a GC finalizer) reaches here |
| 78 |
|
| 79 |
yield capture, release_error, release_cause |
| 80 |
if locked_fd is not None: # pragma: no branch # every consumer calls capture, so this is always set |
| 81 |
real_close(locked_fd) |