Files
langchain-python/libs/langchain/tests/unit_tests/tools/shell/test_shell.py
T
Nuno Campos 8329f81072 Use pytest asyncio auto mode (#13643)
<!-- Thank you for contributing to LangChain!

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes (if applicable),
  - **Dependencies:** any dependencies required for this change,
- **Tag maintainer:** for a quicker response, tag the relevant
maintainer (see below),
- **Twitter handle:** we announce bigger features on Twitter. If your PR
gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` to check this
locally.

See contribution guidelines for more information on how to write/run
tests, lint, etc:

https://github.com/langchain-ai/langchain/blob/master/.github/CONTRIBUTING.md

If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in `docs/extras`
directory.

If no one reviews your PR within a few days, please @-mention one of
@baskaryan, @eyurtsev, @hwchase17.
 -->
2023-11-21 15:00:13 +00:00

68 lines
2.0 KiB
Python

import warnings
from typing import List
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."
)
class PlaceholderProcess:
def __init__(self, output: str = "") -> None:
self._commands: List[str] = []
self.output = output
def _run(self, commands: List[str]) -> str:
self._commands = commands
return self.output
def run(self, commands: List[str]) -> str:
return self._run(commands)
async def arun(self, commands: List[str]) -> str:
return self._run(commands)
def test_shell_tool_init() -> None:
placeholder = PlaceholderProcess()
shell_tool = ShellTool(process=placeholder)
assert shell_tool.name == "terminal"
assert isinstance(shell_tool.description, str)
assert shell_tool.args_schema == ShellInput
assert shell_tool.process is not None
def test_shell_tool_run() -> None:
placeholder = PlaceholderProcess(output="hello")
shell_tool = ShellTool(process=placeholder)
result = shell_tool._run(commands=test_commands)
assert result.strip() == "hello"
async def test_shell_tool_arun() -> None:
placeholder = PlaceholderProcess(output="hello")
shell_tool = ShellTool(process=placeholder)
result = await shell_tool._arun(commands=test_commands)
assert result.strip() == "hello"
def test_shell_tool_run_str() -> None:
placeholder = PlaceholderProcess(output="hello")
shell_tool = ShellTool(process=placeholder)
result = shell_tool._run(commands="echo 'Hello, World!'")
assert result.strip() == "hello"