steam-backlog-enforcer/steam_backlog_enforcer/tests/_fake_xlib.py
Krzysztof kuhy Rudnicki d2565416aa Pylint gate covers tests, no suppressions
Tests are now linted by the 10.00 gate (ignore-paths dropped), with no
per-test disables. CtlServer/DaemonControl members the tests drive are
public (renamed, behaviour unchanged); SteamAPIClient._get takes its
params explicitly, as every caller already did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:17:07 +01:00

103 lines
3.4 KiB
Python

"""Fake libX11 / libXss / libXRes handles so no test needs an X server.
The real modules talk to X through ``ctypes``, which means the seam worth
faking is the shared-library handle itself. These stand-ins return the same
kinds of values Xlib does — an integer display pointer, a struct pointer, a
status code — and, where the C API writes through a pointer argument, actually
write through it, so the calling code is exercised rather than bypassed.
"""
from __future__ import annotations
import ctypes
from typing import TYPE_CHECKING
from unittest.mock import MagicMock
if TYPE_CHECKING:
from collections.abc import Callable
def fake_x11(*, display: int = 0x1234, root: int = 0x50, atom: int = 7) -> MagicMock:
"""Build a libX11 stand-in.
Args:
display: Value ``XOpenDisplay`` returns; ``0`` means "cannot connect".
root: Value ``XDefaultRootWindow`` returns.
atom: Value ``XInternAtom`` returns; ``0`` means "no such atom".
Returns:
The fake handle.
"""
lib = MagicMock(name="libX11")
lib.XOpenDisplay.return_value = display
lib.XDefaultRootWindow.return_value = root
lib.XInternAtom.return_value = atom
return lib
def fake_xss(*, idle_ms: int = 0, allocated: bool = True, ok: bool = True) -> MagicMock:
"""Build a libXss stand-in.
Args:
idle_ms: Idle milliseconds reported by ``XScreenSaverQueryInfo``.
allocated: Whether ``XScreenSaverAllocInfo`` returns a struct.
ok: Whether ``XScreenSaverQueryInfo`` succeeds.
Returns:
The fake handle.
"""
lib = MagicMock(name="libXss")
if allocated:
info = MagicMock(name="XScreenSaverInfo")
info.contents.idle = idle_ms
lib.XScreenSaverAllocInfo.return_value = info
else:
lib.XScreenSaverAllocInfo.return_value = 0
lib.XScreenSaverQueryInfo.return_value = 1 if ok else 0
return lib
def property_writer(
*, status: int = 0, nitems: int = 1, value: int = 4321, null: bool = False
) -> Callable[..., int]:
"""Build an ``XGetWindowProperty`` stand-in that writes its out-params.
Args:
status: Return code; non-zero means failure.
nitems: Number of items to report.
value: The first 32-bit value to hand back.
null: Whether to leave the property pointer NULL.
Returns:
A callable with ``XGetWindowProperty``'s signature.
"""
# Kept alive for the lifetime of the fake: the pointer written into the
# caller's out-param must not dangle.
keepalive: list[ctypes.c_ulong] = []
def impl( # signature mirrors the C function
_display: object,
_window: object,
_atom: object,
_offset: object,
_length: object,
_delete: object,
_req_type: object,
_actual_type: object,
_actual_format: object,
nitems_ref: object,
_bytes_after: object,
prop_ref: object,
) -> int:
# cast() reads the address a byref() holder carries, so the write lands
# in the caller's variable exactly as libX11's would.
ctypes.cast(nitems_ref, ctypes.POINTER(ctypes.c_ulong)).contents.value = nitems
if not null:
holder = ctypes.c_ulong(value)
keepalive.append(holder)
out = ctypes.cast(prop_ref, ctypes.POINTER(ctypes.POINTER(ctypes.c_ulong)))
out.contents.contents = holder
return status
return impl