Skip to content

Commit

Permalink
refactor: update diff messages to constants
Browse files Browse the repository at this point in the history
These changes replace hardcoded diff messages in the GitOperations class and its tests with constants (BINARY_MESSAGE and DELETED_MESSAGE) for better maintainability and readability.
  • Loading branch information
runner committed Dec 25, 2024
1 parent 7022934 commit 0e5399f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
6 changes: 3 additions & 3 deletions aicmt/git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Change(NamedTuple):
Attributes:
file: Path to the changed file
status: Current status of the file ('modified', 'deleted', 'new file', etc.)
diff: Actual changes or special messages ('[File deleted]', '[Binary file]')
diff: Actual changes or special messages ('[File deleted]', 'BINARY_MESSAGE')
insertions: Number of inserted lines
deletions: Number of deleted lines
"""
Expand Down Expand Up @@ -120,7 +120,7 @@ def get_unstaged_changes(self) -> List[Change]:
- status can be one of:
"modified", "deleted", "new file", "new file (binary)", "error"
- diff contains the actual changes or special messages:
"[File deleted]", "[Binary file]"
DELETED_MESSAGE, BINARY_MESSAGE
Raises:
IOError: If there is an error reading a file
Expand Down Expand Up @@ -176,7 +176,7 @@ def get_staged_changes(self) -> List[Change]:
- status can be one of:
"modified", "deleted", "new file", "new file (binary)", "error"
- diff contains the actual changes or special messages:
"[File deleted]", "[Binary file]"
DELETED_MESSAGE, BINARY_MESSAGE
- insertions and deletions contain the number of added/removed lines
Raises:
Expand Down
41 changes: 25 additions & 16 deletions tests/test_git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
from git import Repo, InvalidGitRepositoryError, GitCommandError, NoSuchPathError
from gitdb.exc import BadName
from aicmt.git_operations import GitOperations
from aicmt.git_operations import GitOperations, safe_file_operation, FileStatus, BINARY_MESSAGE, DELETED_MESSAGE
from pathlib import Path


Expand Down Expand Up @@ -89,7 +89,8 @@ def test_stage_files(temp_git_repo):
# Verify that the file is correctly staged
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())
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([])
Expand Down Expand Up @@ -176,7 +177,7 @@ def test_binary_file_changes(temp_git_repo):
assert len(changes) == 1
assert changes[0].file == "binary_file.bin"
assert changes[0].status == "new file (binary)"
assert "[Binary file]" in changes[0].diff
assert BINARY_MESSAGE in changes[0].diff
finally:
os.chdir(current_dir)

Expand Down Expand Up @@ -223,7 +224,7 @@ def test_binary_file_decode_error(temp_git_repo):
assert len(changes) == 1
assert changes[0].file == "test.bin"
assert changes[0].status == "new file (binary)"
assert changes[0].diff == "[Binary file]"
assert changes[0].diff == BINARY_MESSAGE
finally:
os.chdir(current_dir)

Expand Down Expand Up @@ -252,7 +253,7 @@ def test_file_not_found_error(temp_git_repo):
assert len(matching_changes) == 1
change = matching_changes[0]
assert change.status == "deleted"
assert change.diff == "[File deleted]"
assert change.diff == DELETED_MESSAGE
finally:
os.chdir(current_dir)

Expand Down Expand Up @@ -304,6 +305,7 @@ def test_handle_modified_file_diff_error(temp_git_repo):

# Create a Mock object for Git
class MockGit:

def diff(self, *args, **kwargs):
raise GitCommandError("git diff", 128)

Expand Down Expand Up @@ -333,7 +335,7 @@ def test_handle_untracked_file_deleted(temp_git_repo):

# Assert the results
assert status == "deleted"
assert content == "[File deleted]"
assert content == DELETED_MESSAGE


def test_stage_deleted_file(temp_git_repo):
Expand Down Expand Up @@ -389,7 +391,8 @@ def test_get_commit_changes(temp_git_repo):
with open("test4.txt", "w") as f:
pass # Empty file

git_ops.repo.index.add(["test1.txt", "test2.txt", "test3.bin", "test4.txt"])
git_ops.repo.index.add(
["test1.txt", "test2.txt", "test3.bin", "test4.txt"])
git_ops.repo.index.commit("Initial commit")

# Make various changes
Expand Down Expand Up @@ -419,10 +422,14 @@ def test_get_commit_changes(temp_git_repo):
assert len(changes) == 4

# Find changes by filename
test1_change = next(change for change in changes if change.file == "test1.txt")
test2_change = next(change for change in changes if change.file == "test2.txt")
test3_change = next(change for change in changes if change.file == "test3.bin")
test5_change = next(change for change in changes if change.file == "test5.txt")
test1_change = next(change for change in changes
if change.file == "test1.txt")
test2_change = next(change for change in changes
if change.file == "test2.txt")
test3_change = next(change for change in changes
if change.file == "test3.bin")
test5_change = next(change for change in changes
if change.file == "test5.txt")

# Test modified text file
assert test1_change.status == "modified"
Expand All @@ -432,7 +439,7 @@ def test_get_commit_changes(temp_git_repo):

# Test deleted file
assert test2_change.status == "deleted"
assert "[File deleted]" in test2_change.diff
assert DELETED_MESSAGE in test2_change.diff
assert test2_change.insertions == 0
assert test2_change.deletions > 0

Expand Down Expand Up @@ -508,12 +515,14 @@ def test_get_staged_changes(temp_git_repo):
assert new_file.deletions == 0

# Verify new binary file
binary_file = next(c for c in first_changes if c.file == "new_binary.bin")
binary_file = next(c for c in first_changes
if c.file == "new_binary.bin")
assert binary_file.status == "new file (binary)"
assert binary_file.diff == "[Binary file]"
assert binary_file.diff == BINARY_MESSAGE

# Verify modified file
modified_file = next(c for c in second_changes if c.file == "modify.txt")
modified_file = next(c for c in second_changes
if c.file == "modify.txt")
assert modified_file.status == "modified"
assert "-initial" in modified_file.diff
assert "+modified" in modified_file.diff
Expand All @@ -523,7 +532,7 @@ def test_get_staged_changes(temp_git_repo):
# Verify deleted file
deleted_file = next(c for c in third_changes if c.file == "delete.txt")
assert deleted_file.status == "deleted"
assert deleted_file.diff == "[File deleted]"
assert deleted_file.diff == DELETED_MESSAGE
assert deleted_file.insertions == 0
assert deleted_file.deletions > 0

Expand Down

0 comments on commit 0e5399f

Please sign in to comment.