Spaces:
Running
Running
File size: 8,559 Bytes
f1a28b3 a64c042 f1a28b3 51c5f7d f1a28b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | import signal
import subprocess
from pathlib import Path
from unittest.mock import Mock, call, patch
from multi_agent_sdlc.system.process import (
_start_process,
_terminate_process_group,
execute_process,
normalise_process_output,
)
def test_normalise_process_output_none() -> None:
assert normalise_process_output(None) == ""
def test_normalise_process_output_string() -> None:
assert normalise_process_output(" output\n") == "output"
def test_normalise_process_output_bytes() -> None:
assert normalise_process_output(b" output\n") == "output"
def test_normalise_process_output_replaces_invalid_utf8() -> None:
assert normalise_process_output(b"a\xffb") == "a\ufffdb"
def test_start_process(
tmp_path: Path,
) -> None:
command = ["python", "-c", "print('hello')"]
environment = {"PATH": "/sandbox/bin"}
process = Mock()
with (
patch(
"multi_agent_sdlc.system.process.build_sandbox_environment",
return_value=environment,
) as build_environment,
patch(
"multi_agent_sdlc.system.process.subprocess.Popen",
return_value=process,
) as popen,
):
result = _start_process(
command=command,
project_directory=tmp_path,
stdin_text=None,
)
assert result is process
build_environment.assert_called_once_with()
popen.assert_called_once_with(
command,
cwd=tmp_path,
env=environment,
stdin=None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
shell=False,
start_new_session=True,
)
def test_start_process_enables_stdin_when_provided(
tmp_path: Path,
) -> None:
with (
patch(
"multi_agent_sdlc.system.process.build_sandbox_environment",
return_value={},
),
patch(
"multi_agent_sdlc.system.process.subprocess.Popen",
) as popen,
):
_start_process(
command=["python"],
project_directory=tmp_path,
stdin_text="input",
)
assert popen.call_args.kwargs["stdin"] == subprocess.PIPE
def test_terminate_process_group_gracefully() -> None:
process = Mock()
process.pid = 1234
process.communicate.return_value = ("stdout", "stderr")
with (
patch(
"multi_agent_sdlc.system.process.os.getpgid",
return_value=5678,
) as getpgid,
patch(
"multi_agent_sdlc.system.process.os.killpg",
) as killpg,
):
result = _terminate_process_group(
process,
grace_period_seconds=3,
)
assert result == ("stdout", "stderr")
getpgid.assert_called_once_with(1234)
killpg.assert_called_once_with(
5678,
signal.SIGTERM,
)
process.communicate.assert_called_once_with(
timeout=3,
)
def test_terminate_process_group_force_kills_after_grace_period() -> None:
process = Mock()
process.pid = 1234
process.communicate.side_effect = [
subprocess.TimeoutExpired(
cmd=["python"],
timeout=5,
),
("stdout", "stderr"),
]
with (
patch(
"multi_agent_sdlc.system.process.os.getpgid",
return_value=5678,
),
patch(
"multi_agent_sdlc.system.process.os.killpg",
) as killpg,
):
result = _terminate_process_group(
process,
grace_period_seconds=5,
)
assert result == ("stdout", "stderr")
assert killpg.call_args_list == [
call(5678, signal.SIGTERM),
call(5678, signal.SIGKILL),
]
assert process.communicate.call_count == 2
def test_execute_process_success(
tmp_path: Path,
) -> None:
process = Mock()
process.returncode = 0
process.communicate.return_value = (
" hello\n",
" warning\n",
)
with patch(
"multi_agent_sdlc.system.process._start_process",
return_value=process,
) as start_process:
result = execute_process(
command=["python", "script.py"],
project_directory=tmp_path,
timeout_seconds=10,
)
start_process.assert_called_once_with(
command=["python", "script.py"],
project_directory=tmp_path,
stdin_text=None,
)
process.communicate.assert_called_once_with(
input=None,
timeout=10,
)
assert result == {
"command": ["python", "script.py"],
"exit_code": 0,
"stdout": "hello",
"stderr": "warning",
"timed_out": False,
}
def test_execute_process_records_command_start_failure(
tmp_path: Path,
) -> None:
error = FileNotFoundError(2, "No such file or directory", "docker")
with patch(
"multi_agent_sdlc.system.process._start_process",
side_effect=error,
):
result = execute_process(
command=["docker", "compose", "ps"],
project_directory=tmp_path,
timeout_seconds=10,
)
assert result == {
"command": ["docker", "compose", "ps"],
"exit_code": None,
"stdout": "",
"stderr": str(error),
"timed_out": False,
"message": "Command could not be started.",
}
def test_execute_process_passes_stdin(
tmp_path: Path,
) -> None:
process = Mock()
process.returncode = 0
process.communicate.return_value = ("output", "")
with patch(
"multi_agent_sdlc.system.process._start_process",
return_value=process,
):
execute_process(
command=["python", "script.py"],
project_directory=tmp_path,
timeout_seconds=10,
stdin_text="hello",
)
process.communicate.assert_called_once_with(
input="hello",
timeout=10,
)
def test_execute_process_returns_nonzero_exit_code(
tmp_path: Path,
) -> None:
process = Mock()
process.returncode = 2
process.communicate.return_value = (
"",
"Invalid argument",
)
with patch(
"multi_agent_sdlc.system.process._start_process",
return_value=process,
):
result = execute_process(
command=["tool", "--invalid"],
project_directory=tmp_path,
timeout_seconds=10,
)
assert result["exit_code"] == 2
assert result["stderr"] == "Invalid argument"
assert result["timed_out"] is False
def test_execute_process_timeout(
tmp_path: Path,
) -> None:
process = Mock()
timeout_error = subprocess.TimeoutExpired(
cmd=["python", "script.py"],
timeout=10,
output="partial stdout",
stderr="partial stderr",
)
process.communicate.side_effect = timeout_error
with (
patch(
"multi_agent_sdlc.system.process._start_process",
return_value=process,
),
patch(
"multi_agent_sdlc.system.process._terminate_process_group",
return_value=("final stdout", "final stderr"),
) as terminate,
):
result = execute_process(
command=["python", "script.py"],
project_directory=tmp_path,
timeout_seconds=10,
)
terminate.assert_called_once_with(process)
assert result == {
"command": ["python", "script.py"],
"exit_code": None,
"stdout": "final stdout",
"stderr": "final stderr",
"timed_out": True,
"message": (
"Command exceeded the 10-second timeout. "
"The process group was terminated."
),
}
def test_execute_process_timeout_uses_partial_output(
tmp_path: Path,
) -> None:
process = Mock()
process.communicate.side_effect = subprocess.TimeoutExpired(
cmd=["python"],
timeout=10,
output="partial stdout",
stderr="partial stderr",
)
with (
patch(
"multi_agent_sdlc.system.process._start_process",
return_value=process,
),
patch(
"multi_agent_sdlc.system.process._terminate_process_group",
return_value=("", ""),
),
):
result = execute_process(
command=["python"],
project_directory=tmp_path,
timeout_seconds=10,
)
assert result["stdout"] == "partial stdout"
assert result["stderr"] == "partial stderr"
|