-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_exec_tools_stdin_devnull.py
More file actions
97 lines (73 loc) · 2.39 KB
/
Copy pathtest_exec_tools_stdin_devnull.py
File metadata and controls
97 lines (73 loc) · 2.39 KB
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
"""Ensure exec tools do not inherit host stdin."""
from __future__ import annotations
import subprocess
def test_bash_exec_uses_devnull_stdin(monkeypatch):
import os
from uagent.tools import bash_exec_tool as mod
if os.name == "nt":
# Tool is disabled on Windows; still verify call site when forced available.
monkeypatch.setattr(mod, "_TOOL_AVAILABLE", True)
captured = {}
class _P:
returncode = 0
stdout = "ok\n"
stderr = ""
def fake_run(*a, **k):
captured.update(k)
return _P()
monkeypatch.setattr(mod.subprocess, "run", fake_run)
monkeypatch.setattr(
mod,
"decide_cmd_exec",
lambda *a, **k: type(
"D", (), {"allowed": True, "require_confirm": False, "reason": ""}
)(),
)
monkeypatch.setattr(mod, "confirm_if_needed", lambda d: None)
out = mod.run_tool({"command": "echo ok"})
assert captured.get("stdin") is subprocess.DEVNULL
assert "ok" in out
def test_cmd_exec_json_uses_devnull_stdin(monkeypatch):
from uagent.tools import cmd_exec_json_tool as mod
captured = {}
class _P:
returncode = 0
stdout = "ok\n"
stderr = ""
def fake_run(*a, **k):
captured.update(k)
return _P()
monkeypatch.setattr(mod.subprocess, "run", fake_run)
monkeypatch.setattr(
mod,
"decide_cmd_exec",
lambda *a, **k: type(
"D", (), {"allowed": True, "require_confirm": False, "reason": ""}
)(),
)
monkeypatch.setattr(mod, "confirm_if_needed", lambda d: None)
out = mod.run_tool({"command": "echo ok"})
assert captured.get("stdin") is subprocess.DEVNULL
assert "ok" in out
def test_python_exec_uses_devnull_stdin(monkeypatch):
from uagent.tools import python_exec_tool as mod
from uagent.tools.context import ToolCallbacks, init_callbacks
captured = {}
class _P:
returncode = 0
stdout = "ok\n"
stderr = ""
def fake_run(*a, **k):
captured.update(k)
return _P()
init_callbacks(
ToolCallbacks(
cmd_encoding="utf-8",
python_exec_timeout_ms=5000,
truncate_output=None,
)
)
monkeypatch.setattr(mod.subprocess, "run", fake_run)
out = mod.run_tool({"code": "print('ok')"})
assert captured.get("stdin") is subprocess.DEVNULL
assert "ok" in out