Files
shelfmark/tests/download/test_fs_run_blocking_io.py
T
Alex 8d98e122ec Linter followup (#868)
Expanded Ruff rules and completed fixes
2026-04-11 16:18:52 +01:00

69 lines
1.7 KiB
Python

from __future__ import annotations
import subprocess
import sys
import textwrap
from pathlib import Path
import pytest
def test_run_blocking_io_handles_gevent_patched_subprocess_run() -> None:
pytest.importorskip("gevent")
repo_root = Path(__file__).resolve().parents[2]
script = textwrap.dedent(
"""
import os
import subprocess
import sys
import tempfile
from gevent import monkey
monkey.patch_all()
os.environ.setdefault("LOG_ROOT", tempfile.mkdtemp(prefix="shelfmark-log-root-"))
from shelfmark.download.fs import run_blocking_io
result = run_blocking_io(
subprocess.run,
[sys.executable, "-c", "print('ok')"],
check=True,
capture_output=True,
text=True,
)
assert result.stdout.strip() == "ok"
"""
)
completed = subprocess.run(
[sys.executable, "-c", script],
cwd=str(repo_root),
capture_output=True,
text=True,
)
assert completed.returncode == 0, (
f"Subprocess failed with exit code {completed.returncode}\n"
f"stdout:\n{completed.stdout}\n"
f"stderr:\n{completed.stderr}"
)
def test_run_blocking_io_reraises_captured_operational_errors(monkeypatch) -> None:
from shelfmark.download import fs
class _FakePool:
def apply(self, func, args):
return func(*args)
monkeypatch.setattr(fs, "_use_gevent_threadpool", lambda: True)
monkeypatch.setattr(fs, "_get_io_threadpool", lambda: _FakePool())
def _boom() -> None:
raise RuntimeError("boom")
with pytest.raises(RuntimeError, match="boom"):
fs.run_blocking_io(_boom)