Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/cli/src/crewai_cli/train_crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def train_crew(n_iterations: int, filename: str) -> None:
except subprocess.CalledProcessError as e:
click.echo(f"An error occurred while training the crew: {e}", err=True)
click.echo(e.output, err=True)
raise SystemExit(e.returncode) from e

except Exception as e:
click.echo(f"An unexpected error occurred: {e}", err=True)
raise SystemExit(1) from e
51 changes: 49 additions & 2 deletions lib/cli/tests/test_train_crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import click
import pytest
from click.testing import CliRunner

from crewai_cli.cli import train
from crewai_cli.train_crew import train_crew


Expand Down Expand Up @@ -66,8 +68,10 @@ def test_train_crew_called_process_error(mock_subprocess_run, click):
output="Error",
stderr="Some error occurred",
)
train_crew(n_iterations, "trained_agents_data.pkl")
with pytest.raises(SystemExit) as exc_info:
train_crew(n_iterations, "trained_agents_data.pkl")

assert exc_info.value.code == 1
mock_subprocess_run.assert_called_once_with(
["uv", "run", "train", str(n_iterations), "trained_agents_data.pkl"],
capture_output=False,
Expand All @@ -90,8 +94,10 @@ def test_train_crew_called_process_error(mock_subprocess_run, click):
def test_train_crew_unexpected_exception(mock_subprocess_run, click):
n_iterations = 5
mock_subprocess_run.side_effect = Exception("Unexpected error")
train_crew(n_iterations, "trained_agents_data.pkl")
with pytest.raises(SystemExit) as exc_info:
train_crew(n_iterations, "trained_agents_data.pkl")

assert exc_info.value.code == 1
mock_subprocess_run.assert_called_once_with(
["uv", "run", "train", str(n_iterations), "trained_agents_data.pkl"],
capture_output=False,
Expand All @@ -101,3 +107,44 @@ def test_train_crew_unexpected_exception(mock_subprocess_run, click):
click.echo.assert_called_once_with(
"An unexpected error occurred: Unexpected error", err=True
)


@mock.patch("crewai_cli.train_crew.subprocess.run")
def test_train_crew_exits_with_the_script_exit_code(mock_subprocess_run):
mock_subprocess_run.side_effect = subprocess.CalledProcessError(
returncode=42, cmd=["uv", "run", "train", "5"]
)

with pytest.raises(SystemExit) as exc_info:
train_crew(5, "trained_agents_data.pkl")

assert exc_info.value.code == 42


@mock.patch("crewai_cli.train_crew.subprocess.run")
def test_train_command_exits_with_the_script_exit_code(
mock_subprocess_run, tmp_path, monkeypatch
):
monkeypatch.chdir(tmp_path)
mock_subprocess_run.side_effect = subprocess.CalledProcessError(
returncode=42, cmd=["uv", "run", "train", "1"]
)

result = CliRunner().invoke(train, ["-n", "1"])

assert result.exit_code == 42
assert "An error occurred while training the crew" in result.output


@mock.patch("crewai_cli.train_crew.subprocess.run")
def test_train_command_exits_zero_when_the_script_succeeds(
mock_subprocess_run, tmp_path, monkeypatch
):
monkeypatch.chdir(tmp_path)
mock_subprocess_run.return_value = subprocess.CompletedProcess(
args=["uv", "run", "train", "1"], returncode=0, stdout="", stderr=""
)

result = CliRunner().invoke(train, ["-n", "1"])

assert result.exit_code == 0
Loading