Skip to content

Commit 6832719

Browse files
committed
ControlMode(test[fixtures]): Add control_sandbox contextmanager fixture and smoke test
why: Provide an isolated, typed control-mode server for debugging and future tests. what: - Add contextmanager fixture control_sandbox with per-test socket/HOME/TMUX_TMPDIR - Smoke test that exercises control-mode server creation and a simple command
1 parent 4e6878b commit 6832719

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

conftest.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
from __future__ import annotations
1212

13+
import contextlib
14+
import pathlib
1315
import shutil
1416
import typing as t
17+
import uuid
1518

1619
import pytest
1720
from _pytest.doctest import DoctestItem
@@ -75,6 +78,46 @@ def setup_session(
7578
request.getfixturevalue("zshrc")
7679

7780

81+
# ---------------------------------------------------------------------------
82+
# Control-mode sandbox helper
83+
# ---------------------------------------------------------------------------
84+
85+
86+
@pytest.fixture
87+
@contextlib.contextmanager
88+
def control_sandbox(
89+
monkeypatch: pytest.MonkeyPatch,
90+
tmp_path_factory: pytest.TempPathFactory,
91+
) -> t.Iterator[Server]:
92+
"""Provide an isolated control-mode server for a test.
93+
94+
- Creates a unique tmux socket name per invocation
95+
- Isolates HOME and TMUX_TMPDIR under a per-test temp directory
96+
- Clears TMUX env var to avoid inheriting user sessions
97+
- Uses ControlModeEngine; on exit, kills the server best-effort
98+
"""
99+
socket_name = f"libtmux_test_{uuid.uuid4().hex[:8]}"
100+
base = tmp_path_factory.mktemp("ctrl_sandbox")
101+
home = base / "home"
102+
tmux_tmpdir = base / "tmux"
103+
home.mkdir()
104+
tmux_tmpdir.mkdir()
105+
106+
monkeypatch.setenv("HOME", str(home))
107+
monkeypatch.setenv("TMUX_TMPDIR", str(tmux_tmpdir))
108+
monkeypatch.delenv("TMUX", raising=False)
109+
110+
from libtmux._internal.engines.control_mode import ControlModeEngine
111+
112+
server = Server(socket_name=socket_name, engine=ControlModeEngine())
113+
114+
try:
115+
yield server
116+
finally:
117+
with contextlib.suppress(Exception):
118+
server.kill()
119+
120+
78121
def pytest_addoption(parser: pytest.Parser) -> None:
79122
"""Add CLI options for selecting tmux engine."""
80123
parser.addoption(

tests/test_control_sandbox.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Sanity checks for the control_sandbox context-manager fixture."""
2+
3+
from __future__ import annotations
4+
5+
import typing as t
6+
7+
import pytest
8+
9+
from libtmux.server import Server
10+
11+
12+
@pytest.mark.engines(["control"])
13+
def test_control_sandbox_smoke(control_sandbox: t.ContextManager[Server]) -> None:
14+
"""Control sandbox should spin up an isolated server and run commands."""
15+
with control_sandbox as server:
16+
session = server.new_session(
17+
session_name="sandbox_session",
18+
attach=False,
19+
kill_session=True,
20+
)
21+
assert session.name == "sandbox_session"
22+
assert server.has_session("sandbox_session")
23+
24+
# Run a simple command to ensure control mode path works.
25+
out = server.cmd("display-message", "-p", "hi")
26+
assert out.stdout == ["hi"]

0 commit comments

Comments
 (0)