|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | + |
| 4 | +from git import Repo |
| 5 | + |
| 6 | +from cycode.cli import consts |
| 7 | +from cycode.cli.files_collector.commit_range_documents import get_safe_head_reference_for_diff |
| 8 | + |
| 9 | + |
| 10 | +class TestGetSafeHeadReferenceForDiff: |
| 11 | + """Test the safe HEAD reference functionality for git diff operations.""" |
| 12 | + |
| 13 | + def test_returns_head_when_repository_has_commits(self) -> None: |
| 14 | + """Test that HEAD is returned when the repository has existing commits.""" |
| 15 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 16 | + repo = Repo.init(temp_dir) |
| 17 | + |
| 18 | + test_file = os.path.join(temp_dir, 'test.py') |
| 19 | + with open(test_file, 'w') as f: |
| 20 | + f.write("print('test')") |
| 21 | + |
| 22 | + repo.index.add(['test.py']) |
| 23 | + repo.index.commit('Initial commit') |
| 24 | + |
| 25 | + result = get_safe_head_reference_for_diff(repo) |
| 26 | + assert result == consts.GIT_HEAD_COMMIT_REV |
| 27 | + |
| 28 | + def test_returns_empty_tree_hash_when_repository_has_no_commits(self) -> None: |
| 29 | + """Test that an empty tree hash is returned when the repository has no commits.""" |
| 30 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 31 | + repo = Repo.init(temp_dir) |
| 32 | + |
| 33 | + result = get_safe_head_reference_for_diff(repo) |
| 34 | + expected_empty_tree_hash = consts.GIT_EMPTY_TREE_OBJECT |
| 35 | + assert result == expected_empty_tree_hash |
| 36 | + |
| 37 | + |
| 38 | +class TestIndexDiffWithSafeHeadReference: |
| 39 | + """Test that index.diff works correctly with the safe head reference.""" |
| 40 | + |
| 41 | + def test_index_diff_works_on_bare_repository(self) -> None: |
| 42 | + """Test that index.diff works on repositories with no commits.""" |
| 43 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 44 | + repo = Repo.init(temp_dir) |
| 45 | + |
| 46 | + test_file = os.path.join(temp_dir, 'staged_file.py') |
| 47 | + with open(test_file, 'w') as f: |
| 48 | + f.write("print('staged content')") |
| 49 | + |
| 50 | + repo.index.add(['staged_file.py']) |
| 51 | + |
| 52 | + head_ref = get_safe_head_reference_for_diff(repo) |
| 53 | + diff_index = repo.index.diff(head_ref, create_patch=True, R=True) |
| 54 | + |
| 55 | + assert len(diff_index) == 1 |
| 56 | + diff = diff_index[0] |
| 57 | + assert diff.b_path == 'staged_file.py' |
| 58 | + |
| 59 | + def test_index_diff_works_on_repository_with_commits(self) -> None: |
| 60 | + """Test that index.diff continues to work on repositories with existing commits.""" |
| 61 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 62 | + repo = Repo.init(temp_dir) |
| 63 | + |
| 64 | + initial_file = os.path.join(temp_dir, 'initial.py') |
| 65 | + with open(initial_file, 'w') as f: |
| 66 | + f.write("print('initial')") |
| 67 | + |
| 68 | + repo.index.add(['initial.py']) |
| 69 | + repo.index.commit('Initial commit') |
| 70 | + |
| 71 | + new_file = os.path.join(temp_dir, 'new_file.py') |
| 72 | + with open(new_file, 'w') as f: |
| 73 | + f.write("print('new file')") |
| 74 | + |
| 75 | + with open(initial_file, 'w') as f: |
| 76 | + f.write("print('modified initial')") |
| 77 | + |
| 78 | + repo.index.add(['new_file.py', 'initial.py']) |
| 79 | + |
| 80 | + head_ref = get_safe_head_reference_for_diff(repo) |
| 81 | + diff_index = repo.index.diff(head_ref, create_patch=True, R=True) |
| 82 | + |
| 83 | + assert len(diff_index) == 2 |
| 84 | + file_paths = {diff.b_path or diff.a_path for diff in diff_index} |
| 85 | + assert 'new_file.py' in file_paths |
| 86 | + assert 'initial.py' in file_paths |
| 87 | + assert head_ref == consts.GIT_HEAD_COMMIT_REV |
| 88 | + |
| 89 | + def test_sequential_operations_on_same_repository(self) -> None: |
| 90 | + """Test behavior when transitioning from bare to committed repository.""" |
| 91 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 92 | + repo = Repo.init(temp_dir) |
| 93 | + |
| 94 | + test_file = os.path.join(temp_dir, 'test.py') |
| 95 | + with open(test_file, 'w') as f: |
| 96 | + f.write("print('test')") |
| 97 | + |
| 98 | + repo.index.add(['test.py']) |
| 99 | + |
| 100 | + head_ref_before = get_safe_head_reference_for_diff(repo) |
| 101 | + diff_before = repo.index.diff(head_ref_before, create_patch=True, R=True) |
| 102 | + |
| 103 | + expected_empty_tree = consts.GIT_EMPTY_TREE_OBJECT |
| 104 | + assert head_ref_before == expected_empty_tree |
| 105 | + assert len(diff_before) == 1 |
| 106 | + |
| 107 | + repo.index.commit('First commit') |
| 108 | + |
| 109 | + new_file = os.path.join(temp_dir, 'new.py') |
| 110 | + with open(new_file, 'w') as f: |
| 111 | + f.write("print('new')") |
| 112 | + |
| 113 | + repo.index.add(['new.py']) |
| 114 | + |
| 115 | + head_ref_after = get_safe_head_reference_for_diff(repo) |
| 116 | + diff_after = repo.index.diff(head_ref_after, create_patch=True, R=True) |
| 117 | + |
| 118 | + assert head_ref_after == consts.GIT_HEAD_COMMIT_REV |
| 119 | + assert len(diff_after) == 1 |
| 120 | + assert diff_after[0].b_path == 'new.py' |
0 commit comments