Files
komorebi/nix/linux/tests/vm/lib/tests/conftest.py
T
LGUG2Z f01853ceca refactor(repo): move the KDE and macOS nix files under nix/ (pure move)
komorebi-for-kde/nix/ -> nix/linux/
komorebi-for-mac/nix/ -> nix/macos/

The nix expressions of the platforms (modules, option files, VM test suites, kwin effects,
the darwin module) sit next to the existing nix/workspace-src.nix. Renames only (252
files); the flakes that import them are replaced by one root flake later in this stack, so
nix does not evaluate at this revision.
2026-11-05 15:03:27 -08:00

60 lines
2.0 KiB
Python

"""A fake machine that records commands and answers from a script."""
from collections.abc import Callable
from pathlib import Path
import pytest
from PIL import Image
class FakeMachine:
"""Answers `execute` with the first matching reply, and records every command."""
def __init__(self, out_dir: Path) -> None:
self.out_dir = out_dir
self.commands: list[str] = []
self.keys: list[str] = []
self.copied: list[str] = []
self.replies: list[tuple[Callable[[str], bool], tuple[int, str]]] = []
def reply(self, contains: str, status: int = 0, output: str = "") -> None:
"""Makes any command containing `contains` answer with `status` and `output`."""
self.replies.append((lambda command: contains in command, (status, output)))
def execute(self, command: str, /) -> tuple[int, str]:
self.commands.append(command)
for matches, answer in self.replies:
if matches(command):
return answer
return 0, ""
def send_key(self, key: str, /) -> None:
self.keys.append(key)
def copy_from_machine(self, source: str, /) -> None:
self.copied.append(source)
Image.new("RGB", (4, 4), (10, 20, 30)).save(self.out_dir / Path(source).name, format="PPM")
def screenshot(self, filename: str, /) -> None:
Image.new("RGB", (4, 4), (10, 20, 30)).save(self.out_dir / f"{filename}.png")
def succeed(self, *commands: str) -> str:
output = ""
for command in commands:
status, output = self.execute(command)
assert status == 0, f"{command!r} exited with {status}: {output}"
return output
def fail(self, *commands: str) -> str:
output = ""
for command in commands:
status, output = self.execute(command)
assert status != 0, f"{command!r} succeeded: {output}"
return output
@pytest.fixture
def machine(tmp_path: Path) -> FakeMachine:
return FakeMachine(tmp_path)