From ffe68a3e6604259c62b2dfb763b906f13b811173 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 4 Apr 2025 14:17:14 +0000 Subject: [PATCH] Fix issue #2524: Check if pyproject.toml exists before running uv sync Co-Authored-By: Joe Moura --- src/crewai/cli/install_crew.py | 6 +++ tests/cli/install_crew_test.py | 67 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tests/cli/install_crew_test.py diff --git a/src/crewai/cli/install_crew.py b/src/crewai/cli/install_crew.py index d1d0ab9da..fcfd51971 100644 --- a/src/crewai/cli/install_crew.py +++ b/src/crewai/cli/install_crew.py @@ -1,4 +1,5 @@ import subprocess +from pathlib import Path import click @@ -7,6 +8,11 @@ def install_crew(proxy_options: list[str]) -> None: """ Install the crew by running the UV command to lock and install. """ + if not Path("pyproject.toml").exists(): + click.echo("Error: No pyproject.toml found in current directory.", err=True) + click.echo("This command must be run from the root of a crew project.", err=True) + return + try: command = ["uv", "sync"] + proxy_options subprocess.run(command, check=True, capture_output=False, text=True) diff --git a/tests/cli/install_crew_test.py b/tests/cli/install_crew_test.py new file mode 100644 index 000000000..1dba58f35 --- /dev/null +++ b/tests/cli/install_crew_test.py @@ -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