langchain/tests/unit_tests/tools/shell/test_shell.py
Zander Chase 5042bd40d3
Add Shell Tool (#3335)
Create an official bash shell tool to replace the dynamically generated one
2023-04-28 11:10:43 -07:00

44 lines
1.2 KiB
Python

import warnings
import pytest
from langchain.tools.shell.tool import ShellInput, ShellTool
# Test data
test_commands = ["echo 'Hello, World!'", "echo 'Another command'"]
def test_shell_input_validation() -> None:
shell_input = ShellInput(commands=test_commands)
assert isinstance(shell_input.commands, list)
assert len(shell_input.commands) == 2
with warnings.catch_warnings(record=True) as w:
ShellInput(commands=test_commands)
assert len(w) == 1
assert (
str(w[-1].message)
== "The shell tool has no safeguards by default. Use at your own risk."
)
def test_shell_tool_init() -> None:
shell_tool = ShellTool()
assert shell_tool.name == "terminal"
assert isinstance(shell_tool.description, str)
assert shell_tool.args_schema == ShellInput
assert shell_tool.process is not None
@pytest.mark.asyncio
async def test_shell_tool_arun() -> None:
shell_tool = ShellTool()
result = await shell_tool._arun(commands=test_commands)
assert result.strip() == "Hello, World!\nAnother command"
def test_shell_tool_run() -> None:
shell_tool = ShellTool()
result = shell_tool._run(commands=test_commands)
assert result.strip() == "Hello, World!\nAnother command"