Create crewai tool create <tool> command (#1379)

This commit creates a new CLI command for scaffolding tools.
This commit is contained in:
Vini Brasil
2024-10-01 18:58:27 -03:00
committed by GitHub
parent 2c74efc8f2
commit 8aba99a67d
11 changed files with 312 additions and 0 deletions

View File

@@ -1,11 +1,59 @@
from contextlib import contextmanager
import tempfile
import unittest
import unittest.mock
import os
from crewai.cli.tools.main import ToolCommand
from io import StringIO
from unittest.mock import patch, MagicMock
class TestToolCommand(unittest.TestCase):
@contextmanager
def in_temp_dir(self):
original_dir = os.getcwd()
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
try:
yield temp_dir
finally:
os.chdir(original_dir)
@patch("crewai.cli.tools.main.subprocess.run")
def test_create_success(self, mock_subprocess):
with self.in_temp_dir():
tool_command = ToolCommand()
with patch.object(tool_command, "login") as mock_login, patch(
"sys.stdout", new=StringIO()
) as fake_out:
tool_command.create("test-tool")
output = fake_out.getvalue()
self.assertTrue(os.path.isdir("test_tool"))
self.assertTrue(os.path.isfile(os.path.join("test_tool", "README.md")))
self.assertTrue(os.path.isfile(os.path.join("test_tool", "pyproject.toml")))
self.assertTrue(
os.path.isfile(
os.path.join("test_tool", "src", "test_tool", "__init__.py")
)
)
self.assertTrue(
os.path.isfile(os.path.join("test_tool", "src", "test_tool", "tool.py"))
)
with open(
os.path.join("test_tool", "src", "test_tool", "tool.py"), "r"
) as f:
content = f.read()
self.assertIn("class TestTool", content)
mock_login.assert_called_once()
mock_subprocess.assert_called_once_with(["git", "init"], check=True)
self.assertIn("Creating custom tool test_tool...", output)
@patch("crewai.cli.tools.main.subprocess.run")
@patch("crewai.cli.plus_api.PlusAPI.get_tool")
def test_install_success(self, mock_get, mock_subprocess_run):