c2459772eb
CI / helm (pull_request) Successful in 33s
CI / build (pull_request) Successful in 34s
CI / lint (pull_request) Successful in 47s
CI / typecheck (pull_request) Successful in 1m8s
CI / quality (pull_request) Successful in 1m14s
CI / security (pull_request) Successful in 1m27s
CI / push-validation (pull_request) Successful in 28s
CI / integration_tests (pull_request) Successful in 3m0s
CI / unit_tests (pull_request) Successful in 4m40s
CI / docker (pull_request) Successful in 1m28s
CI / coverage (pull_request) Successful in 11m27s
CI / status-check (pull_request) Successful in 3s
The `else: proc.stderr = stderr` clause was dropped when the helper was extracted into `_ltcov_helpers.py`. `stdin` and `stdout` both have the corresponding else branches; this restores parity so that callers passing a non-"auto" stderr value have it honoured.
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
"""Shared helper functions for LSP transport BDD step definitions.
|
|
|
|
Extracted common mock-building utilities so individual step files stay
|
|
under the 500-line limit (see CONTRIBUTING.md file-size guideline).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import subprocess
|
|
from typing import Any
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
def make_mock_process(
|
|
*,
|
|
poll_return: int | None = None,
|
|
returncode: int = 0,
|
|
stdin: object | None = "auto",
|
|
stdout: object | None = "auto",
|
|
stderr: object | None = "auto",
|
|
pid: int = 12345,
|
|
) -> MagicMock:
|
|
"""Create a ``MagicMock`` resembling ``subprocess.Popen``.
|
|
|
|
Args:
|
|
poll_return: Value for :attr:`~MagicMock.poll` return value.
|
|
``None`` means the process is still running.
|
|
returncode: The subprocess exit code when ``poll()`` returns.
|
|
stdin: Stdin mock object (or ``"auto"`` for auto-generated).
|
|
stdout: Stdout mock object (or ``"auto"`` for auto-generated).
|
|
stderr: Stderr mock object (or ``"auto"`` for auto-generated).
|
|
pid: Process ID to report.
|
|
|
|
Returns:
|
|
A fully configured ``MagicMock`` with ``subprocess.Popen`` spec.
|
|
"""
|
|
proc = MagicMock(spec=subprocess.Popen)
|
|
proc.pid = pid
|
|
proc.poll.return_value = poll_return
|
|
proc.returncode = returncode
|
|
if stdin == "auto":
|
|
proc.stdin = MagicMock()
|
|
else:
|
|
proc.stdin = stdin
|
|
if stdout == "auto":
|
|
proc.stdout = MagicMock()
|
|
else:
|
|
proc.stdout = stdout
|
|
if stderr == "auto":
|
|
proc.stderr = MagicMock()
|
|
else:
|
|
proc.stderr = stderr
|
|
return proc
|
|
|
|
|
|
def build_lsp_frame(body_dict: dict[str, Any]) -> bytes:
|
|
"""Encode a dict as a Content-Length framed LSP message.
|
|
|
|
Args:
|
|
body_dict: The JSON-serialisable message body.
|
|
|
|
Returns:
|
|
Raw bytes including the ``Content-Length`` header and empty line prefix.
|
|
"""
|
|
payload = json.dumps(body_dict, separators=(",", ":")).encode("utf-8")
|
|
header = f"Content-Length: {len(payload)}\r\n\r\n".encode("ascii")
|
|
return header + payload
|