Skip to content

Commit

Permalink
fix: clean up whitespace and improve code readability
Browse files Browse the repository at this point in the history
These changes involve removing unnecessary whitespace and adjusting code formatting for better readability in both the main functionality and the tests. This enhances the overall clarity of the code without altering its functionality.
  • Loading branch information
versun committed Dec 25, 2024
1 parent 5fb2438 commit acafd3d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
4 changes: 2 additions & 2 deletions aicmt/git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def safe_file_operation(file_path: Union[str, Path]) -> Any:
try:
yield
except UnicodeDecodeError:
return FileStatus.NEW_BINARY, BINARY_MESSAGE
#yield (FileStatus.NEW_BINARY, BINARY_MESSAGE)
return FileStatus.NEW_BINARY, BINARY_MESSAGE
# yield (FileStatus.NEW_BINARY, BINARY_MESSAGE)
except IOError as e:
CLIInterface.display_error(f"Error reading file {file_path}: {str(e)}")
raise
Expand Down
61 changes: 30 additions & 31 deletions tests/test_git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def test_get_unstaged_changes(temp_git_repo):
# Create a new file and add it to git
with open("test.txt", "w") as f:
f.write("test content")

# Add file to git and modify it
git_ops.repo.index.add(["test.txt"])
git_ops.repo.index.commit("Initial commit")

with open("test.txt", "w") as f:
f.write("modified content")

Expand All @@ -72,8 +72,8 @@ def mock_handle_modified_file(*args):
raise Exception("Test error")

# Patch the _handle_modified_file method to raise an exception
with patch.object(GitOperations, '_handle_modified_file', side_effect=mock_handle_modified_file):
with patch('aicmt.cli_interface.CLIInterface.display_warning') as mock_warning:
with patch.object(GitOperations, "_handle_modified_file", side_effect=mock_handle_modified_file):
with patch("aicmt.cli_interface.CLIInterface.display_warning") as mock_warning:
changes = git_ops.get_unstaged_changes()
mock_warning.assert_called_once_with("Warning: Could not process test.txt: Test error")

Expand Down Expand Up @@ -623,25 +623,24 @@ def test_file_reading(temp_git_repo):
def test_safe_file_operation(temp_git_repo):
"""Test the safe_file_operation context manager"""
test_file = Path(temp_git_repo) / "test.txt"

# Test normal operation
with safe_file_operation(test_file):
with open(test_file, "w") as f:
f.write("test content")

assert test_file.exists()
assert test_file.read_text() == "test content"

# Test UnicodeDecodeError handling
binary_file = Path(temp_git_repo) / "binary.bin"
binary_file.write_bytes(b'\x80\x81')
binary_file.write_bytes(b"\x80\x81")

with safe_file_operation(binary_file) as result:
with open(binary_file, "r") as f:
content = f.read()
f.read()
assert result == (FileStatus.NEW_BINARY, BINARY_MESSAGE)



# Test IOError handling
non_existent_file = Path(temp_git_repo) / "non_existent.txt"
with pytest.raises(IOError):
Expand Down Expand Up @@ -678,21 +677,21 @@ def test_is_binary_file(temp_git_repo):

def test_get_file_content(tmp_path):
git_ops = GitOperations()

# Test text file
text_file = tmp_path / "test.txt"
text_file.write_text("Hello, World!", encoding="utf-8")
status, content = git_ops._get_file_content(text_file)
assert status == FileStatus.NEW_FILE
assert content == "Hello, World!"

# Test binary file
binary_file = tmp_path / "test.bin"
binary_file.write_bytes(b'\x00\x01\x02\x03')
binary_file.write_bytes(b"\x00\x01\x02\x03")
status, content = git_ops._get_file_content(binary_file)
assert status == FileStatus.NEW_BINARY
assert content == BINARY_MESSAGE

# Test non-existent file
non_existent = tmp_path / "non_existent.txt"
with pytest.raises(FileNotFoundError):
Expand All @@ -711,11 +710,11 @@ def test_file_processing_warning(temp_git_repo):
# Create a new file and add it to git
with open("test.txt", "w") as f:
f.write("test content")

# Add file to git and modify it
git_ops.repo.index.add(["test.txt"])
git_ops.repo.index.commit("Initial commit")

with open("test.txt", "w") as f:
f.write("modified content")

Expand All @@ -724,9 +723,9 @@ def mock_handle_modified_file(*args):
raise Exception("Test error")

# Patch the _handle_modified_file method to raise an exception
with patch.object(GitOperations, '_handle_modified_file', side_effect=mock_handle_modified_file):
with patch('aicmt.cli_interface.CLIInterface.display_warning') as mock_warning:
changes = git_ops.get_unstaged_changes()
with patch.object(GitOperations, "_handle_modified_file", side_effect=mock_handle_modified_file):
with patch("aicmt.cli_interface.CLIInterface.display_warning") as mock_warning:
git_ops.get_unstaged_changes()
mock_warning.assert_called_once_with("Warning: Could not process test.txt: Test error")

finally:
Expand All @@ -752,9 +751,9 @@ def mock_handle_untracked_file(*args):
raise Exception("Test error")

# Patch the _handle_untracked_file method to raise an exception
with patch.object(GitOperations, '_handle_untracked_file', side_effect=mock_handle_untracked_file):
with patch('aicmt.cli_interface.CLIInterface.display_warning') as mock_warning:
changes = git_ops.get_unstaged_changes()
with patch.object(GitOperations, "_handle_untracked_file", side_effect=mock_handle_untracked_file):
with patch("aicmt.cli_interface.CLIInterface.display_warning") as mock_warning:
git_ops.get_unstaged_changes()
mock_warning.assert_called_once_with("Warning: Could not process test.txt: Test error")

finally:
Expand All @@ -765,7 +764,7 @@ def mock_handle_untracked_file(*args):
def test_process_file_diff_deleted_exception(temp_git_repo):
"""Test _process_file_diff method when file deletion raises an exception"""
git_ops = GitOperations(temp_git_repo)

# Create a mock diff object
class MockDiff:
def __init__(self):
Expand All @@ -774,7 +773,7 @@ def __init__(self):
self.renamed_file = False
self.new_file = False
self.a_blob = None

# Test when an exception occurs during file deletion handling
diff = MockDiff()
status, content, insertions, deletions = git_ops._process_file_diff(diff)
Expand All @@ -787,21 +786,21 @@ def __init__(self):
def test_process_file_diff_git_error(temp_git_repo):
"""Test _process_file_diff method when git command raises an error"""
git_ops = GitOperations(temp_git_repo)

# Create a mock diff object
class MockDiff:
def __init__(self):
self.a_path = "test.txt"
self.deleted_file = False
self.new_file = False
self.renamed_file = False

# Create a mock git object
mock_git = MagicMock()
mock_git.diff.side_effect = GitCommandError('git diff', 128)
mock_git.diff.side_effect = GitCommandError("git diff", 128)

# Test when git command raises an error
with patch.object(git_ops.repo, 'git', mock_git):
with patch.object(git_ops.repo, "git", mock_git):
status, content, insertions, deletions = git_ops._process_file_diff(MockDiff())
assert status == FileStatus.ERROR
assert "[Error getting diff: " in content
Expand Down

0 comments on commit acafd3d

Please sign in to comment.