Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/panopticon/sessionservice/local_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ def _subprocess_run(args: Sequence[str], *, check: bool = True, interactive: boo
if interactive or verbose: # inherit streams: TTY attachment (interactive) or visible build output (verbose)
subprocess.run(list(args), check=check)
return ""
return subprocess.run(list(args), check=check, capture_output=True, text=True).stdout
try:
return subprocess.run(list(args), check=check, capture_output=True, text=True).stdout
except subprocess.CalledProcessError as exc:
stderr = (exc.stderr or "").strip()
msg = f"Command '{exc.cmd[0]}' exited {exc.returncode}"
if stderr:
msg += f": {stderr}"
raise RuntimeError(msg) from exc



Expand Down
29 changes: 28 additions & 1 deletion tests/test_local_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import pytest

from panopticon.core.models import LifecyclePhase
from panopticon.sessionservice.local_runner import LocalRunner
from panopticon.sessionservice.local_runner import LocalRunner, _subprocess_run
from panopticon.sessionservice.runner import Runner


Expand Down Expand Up @@ -235,6 +235,33 @@ def test_tmux_socket_can_be_overridden() -> None:
assert rec.calls[3][0][:4] == ["tmux", "-L", "panopt", "new-session"] # kill-session, rm, run, tmux


# -- _subprocess_run error surfacing -------------------------------------------------


def test_subprocess_run_includes_stderr_in_error(monkeypatch: pytest.MonkeyPatch) -> None:
"""CalledProcessError.stderr is surfaced in the RuntimeError message."""
err = subprocess.CalledProcessError(125, ["docker", "run"], stderr="no such image: panopticon-foo")

def _failing_run(*args: object, **kwargs: object) -> subprocess.CompletedProcess[str]:
raise err

monkeypatch.setattr(subprocess, "run", _failing_run)
with pytest.raises(RuntimeError, match="no such image: panopticon-foo"):
_subprocess_run(["docker", "run", "panopticon-foo"])


def test_subprocess_run_error_without_stderr(monkeypatch: pytest.MonkeyPatch) -> None:
"""CalledProcessError with no stderr still raises RuntimeError with exit code."""
err = subprocess.CalledProcessError(1, ["docker", "build"], stderr="")

def _failing_run(*args: object, **kwargs: object) -> subprocess.CompletedProcess[str]:
raise err

monkeypatch.setattr(subprocess, "run", _failing_run)
with pytest.raises(RuntimeError, match=r"Command 'docker' exited 1$"):
_subprocess_run(["docker", "build", "."])


# -- integration: real docker + tmux ------------------------------------------------

_HAVE_DOCKER_TMUX = bool(shutil.which("docker") and shutil.which("tmux"))
Expand Down
Loading