Fix issue #2524: Check if pyproject.toml exists before running uv sync

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-04-04 14:17:14 +00:00
parent 409892d65f
commit ffe68a3e66
2 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
import subprocess
from unittest import mock
import pytest
from click.testing import CliRunner
from pathlib import Path
from crewai.cli.install_crew import install_crew
@pytest.fixture
def mock_subprocess_run():
with mock.patch("subprocess.run") as mock_run:
yield mock_run
@pytest.fixture
def mock_path_exists():
with mock.patch("pathlib.Path.exists") as mock_exists:
yield mock_exists
def test_install_crew_pyproject_exists(mock_subprocess_run, mock_path_exists):
mock_path_exists.return_value = True
proxy_options = []
install_crew(proxy_options)
mock_subprocess_run.assert_called_once_with(
["uv", "sync"] + proxy_options, check=True, capture_output=False, text=True
)
def test_install_crew_pyproject_not_exists(mock_subprocess_run, mock_path_exists, capsys):
mock_path_exists.return_value = False
proxy_options = []
install_crew(proxy_options)
mock_subprocess_run.assert_not_called()
captured = capsys.readouterr()
assert "Error: No pyproject.toml found in current directory." in captured.err
assert "This command must be run from the root of a crew project." in captured.err
def test_install_crew_subprocess_error(mock_subprocess_run, mock_path_exists, capsys):
mock_path_exists.return_value = True
mock_subprocess_run.side_effect = subprocess.CalledProcessError(
cmd=["uv", "sync"], returncode=1
)
proxy_options = []
install_crew(proxy_options)
captured = capsys.readouterr()
assert "An error occurred while running the crew" in captured.err
def test_install_crew_general_exception(mock_subprocess_run, mock_path_exists, capsys):
mock_path_exists.return_value = True
mock_subprocess_run.side_effect = Exception("Test exception")
proxy_options = []
install_crew(proxy_options)
captured = capsys.readouterr()
assert "An unexpected error occurred: Test exception" in captured.err