From 0e5399f46ca1c627a177468a8d244775f8594bf8 Mon Sep 17 00:00:00 2001 From: runner Date: Wed, 25 Dec 2024 06:18:30 +0000 Subject: [PATCH] refactor: update diff messages to constants 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. --- aicmt/git_operations.py | 6 +++--- tests/test_git_operations.py | 41 ++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/aicmt/git_operations.py b/aicmt/git_operations.py index d36ddfc..861d58d 100644 --- a/aicmt/git_operations.py +++ b/aicmt/git_operations.py @@ -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 """ @@ -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 @@ -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: diff --git a/tests/test_git_operations.py b/tests/test_git_operations.py index c194ed3..346afb4 100644 --- a/tests/test_git_operations.py +++ b/tests/test_git_operations.py @@ -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 @@ -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([]) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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): @@ -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 @@ -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" @@ -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 @@ -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 @@ -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