Skip to content
Closed
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
38 changes: 38 additions & 0 deletions tests/tools/test_ssh_environment_decoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from types import SimpleNamespace

from tools.environments import ssh as ssh_env


def test_ensure_remote_dirs_decodes_subprocess_output_tolerantly(monkeypatch):
calls = []

def fake_run(cmd, **kwargs):
calls.append((cmd, kwargs))
return SimpleNamespace(returncode=0, stdout="", stderr="")

env = object.__new__(ssh_env.SSHEnvironment)
env._remote_home = "/home/testuser"
env._build_ssh_command = lambda: ["ssh", "example.com"]

monkeypatch.setattr(ssh_env.subprocess, "run", fake_run)

env._ensure_remote_dirs()

assert calls
_, kwargs = calls[0]
assert kwargs["text"] is True
assert kwargs["encoding"] == "utf-8"
assert kwargs["errors"] == "replace"


def test_ssh_subprocess_text_captures_all_use_tolerant_decoding():
source = ssh_env.Path(ssh_env.__file__).read_text(encoding="utf-8")
snippets = [
line for line in source.splitlines()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only recognizes one-line subprocess.run(..., capture_output=True, text=True) calls. Current main formats all seven relevant SSH calls across multiple lines, so this test would find no snippets after salvage even when the decoding kwargs are present. Please use a behavior-oriented test or a layout-independent inspection.

if "subprocess.run(" in line and "capture_output=True" in line and "text=True" in line
]

assert snippets
for line in snippets:
assert 'encoding="utf-8"' in line
assert 'errors="replace"' in line
14 changes: 7 additions & 7 deletions tools/environments/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _establish_connection(self):
cmd = self._build_ssh_command()
cmd.append("echo 'SSH connection established'")
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=15)
result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=15)
if result.returncode != 0:
error_msg = result.stderr.strip() or result.stdout.strip()
raise RuntimeError(f"SSH connection failed: {error_msg}")
Expand All @@ -113,7 +113,7 @@ def _detect_remote_home(self) -> str:
try:
cmd = self._build_ssh_command()
cmd.append("echo $HOME")
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10)
home = result.stdout.strip()
if home and result.returncode == 0:
logger.debug("SSH: remote home = %s", home)
Expand All @@ -134,7 +134,7 @@ def _ensure_remote_dirs(self) -> None:
dirs = [base, f"{base}/skills", f"{base}/credentials", f"{base}/cache"]
cmd = self._build_ssh_command()
cmd.append(quoted_mkdir_command(dirs))
subprocess.run(cmd, capture_output=True, text=True, timeout=10)
subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10)

# _get_sync_files provided via iter_sync_files in FileSyncManager init

Expand All @@ -143,15 +143,15 @@ def _scp_upload(self, host_path: str, remote_path: str) -> None:
parent = str(Path(remote_path).parent)
mkdir_cmd = self._build_ssh_command()
mkdir_cmd.append(f"mkdir -p {shlex.quote(parent)}")
subprocess.run(mkdir_cmd, capture_output=True, text=True, timeout=10)
subprocess.run(mkdir_cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10)

scp_cmd = ["scp", "-o", f"ControlPath={self.control_socket}"]
if self.port != 22:
scp_cmd.extend(["-P", str(self.port)])
if self.key_path:
scp_cmd.extend(["-i", self.key_path])
scp_cmd.extend([host_path, f"{self.user}@{self.host}:{remote_path}"])
result = subprocess.run(scp_cmd, capture_output=True, text=True, timeout=30)
result = subprocess.run(scp_cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=30)
if result.returncode != 0:
raise RuntimeError(f"scp failed: {result.stderr.strip()}")

Expand All @@ -174,7 +174,7 @@ def _ssh_bulk_upload(self, files: list[tuple[str, str]]) -> None:
if parents:
cmd = self._build_ssh_command()
cmd.append(quoted_mkdir_command(parents))
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=30)
if result.returncode != 0:
raise RuntimeError(f"remote mkdir failed: {result.stderr.strip()}")

Expand Down Expand Up @@ -266,7 +266,7 @@ def _ssh_delete(self, remote_paths: list[str]) -> None:
"""Batch-delete remote files in one SSH call."""
cmd = self._build_ssh_command()
cmd.append(quoted_rm_command(remote_paths))
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10)
if result.returncode != 0:
raise RuntimeError(f"remote rm failed: {result.stderr.strip()}")

Expand Down
Loading