Skip to content

Commit

Permalink
feat: add error handling for staging files
Browse files Browse the repository at this point in the history
This commit introduces error handling in the GitOperations class to raise a ValueError when no files are provided to the stage_files method. Additionally, it adds corresponding tests to ensure that this behavior is correctly implemented and validated.
  • Loading branch information
runner committed Dec 24, 2024
1 parent c8f3ee4 commit 1355c8c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aicmt/git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ def stage_files(self, files: List[str]) -> None:
Raises:
git.GitCommandError: If there is an error staging the files
"""
if not files:
raise ValueError("No files to stage!")

try:
# Get current status of files
status = self.repo.git.status("--porcelain").splitlines()
Expand Down
5 changes: 5 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ def test_run_no_approved_groups(mock_confirm, assistant):
with patch.multiple(assistant.cli, display_commit_groups=lambda x: []):
assistant.run()

def test_create_new_commits(assistant, capsys):
commit_groups = [{"files": [], "commit_message": "test commit", "description": "test description"}]
assistant._create_new_commits(commit_groups)
captured = capsys.readouterr()
assert "No files to stage!" in captured.out

def test_commit_creation_failure(assistant, capsys):
changes = [Change(file="test.py", status="modified", diff="test diff", insertions=1, deletions=0)]
Expand Down
5 changes: 5 additions & 0 deletions tests/test_git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def test_stage_files(temp_git_repo):
assert "stage_test.txt" not in untracked
# Directly check if the file name is in the index
assert any("stage_test.txt" in str(entry) for entry in git_ops.repo.index.entries.keys())

with pytest.raises(ValueError) as excinfo:
git_ops.stage_files([])
assert str(excinfo.value) == "No files to stage!"

finally:
# Restore the original directory
os.chdir(current_dir)
Expand Down

0 comments on commit 1355c8c

Please sign in to comment.