|
| 1 | +from pathlib import Path |
| 2 | +from types import SimpleNamespace |
| 3 | +from unittest.mock import Mock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from aider.tools import insert_block |
| 8 | + |
| 9 | + |
| 10 | +class DummyIO: |
| 11 | + def __init__(self): |
| 12 | + self.tool_error = Mock() |
| 13 | + self.tool_warning = Mock() |
| 14 | + self.tool_output = Mock() |
| 15 | + |
| 16 | + def read_text(self, path): |
| 17 | + return Path(path).read_text() |
| 18 | + |
| 19 | + def write_text(self, path, content): |
| 20 | + Path(path).write_text(content) |
| 21 | + |
| 22 | + |
| 23 | +class DummyChangeTracker: |
| 24 | + def __init__(self): |
| 25 | + self.calls = [] |
| 26 | + |
| 27 | + def track_change( |
| 28 | + self, file_path, change_type, original_content, new_content, metadata, change_id=None |
| 29 | + ): |
| 30 | + self.calls.append( |
| 31 | + { |
| 32 | + "file_path": file_path, |
| 33 | + "change_type": change_type, |
| 34 | + "original_content": original_content, |
| 35 | + "new_content": new_content, |
| 36 | + "metadata": metadata, |
| 37 | + "change_id": change_id, |
| 38 | + } |
| 39 | + ) |
| 40 | + return f"change-{len(self.calls)}" |
| 41 | + |
| 42 | + |
| 43 | +class DummyCoder: |
| 44 | + def __init__(self, root): |
| 45 | + self.root = str(root) |
| 46 | + self.repo = SimpleNamespace(root=str(root)) |
| 47 | + self.io = DummyIO() |
| 48 | + self.change_tracker = DummyChangeTracker() |
| 49 | + self.aider_edited_files = set() |
| 50 | + self.files_edited_by_tools = set() |
| 51 | + self.abs_read_only_fnames = set() |
| 52 | + self.abs_fnames = set() |
| 53 | + |
| 54 | + def abs_root_path(self, file_path): |
| 55 | + path = Path(file_path) |
| 56 | + if path.is_absolute(): |
| 57 | + return str(path) |
| 58 | + return str((Path(self.root) / path).resolve()) |
| 59 | + |
| 60 | + def get_rel_fname(self, abs_path): |
| 61 | + return str(Path(abs_path).resolve().relative_to(self.root)) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def coder_with_file(tmp_path): |
| 66 | + file_path = tmp_path / "example.txt" |
| 67 | + file_path.write_text("first line\nsecond line\n") |
| 68 | + coder = DummyCoder(tmp_path) |
| 69 | + coder.abs_fnames.add(str(file_path.resolve())) |
| 70 | + return coder, file_path |
| 71 | + |
| 72 | + |
| 73 | +def test_position_top_succeeds_with_no_patterns(coder_with_file): |
| 74 | + coder, file_path = coder_with_file |
| 75 | + |
| 76 | + result = insert_block.Tool.execute( |
| 77 | + coder, |
| 78 | + file_path="example.txt", |
| 79 | + content="inserted line", |
| 80 | + position="top", |
| 81 | + ) |
| 82 | + |
| 83 | + assert result.startswith("Successfully executed InsertBlock.") |
| 84 | + assert file_path.read_text().splitlines()[0] == "inserted line" |
| 85 | + coder.io.tool_error.assert_not_called() |
| 86 | + |
| 87 | + |
| 88 | +def test_position_top_ignores_blank_patterns(coder_with_file): |
| 89 | + coder, file_path = coder_with_file |
| 90 | + |
| 91 | + result = insert_block.Tool.execute( |
| 92 | + coder, |
| 93 | + file_path="example.txt", |
| 94 | + content="inserted line", |
| 95 | + position="top", |
| 96 | + after_pattern="", |
| 97 | + ) |
| 98 | + |
| 99 | + assert result.startswith("Successfully executed InsertBlock.") |
| 100 | + assert file_path.read_text().splitlines()[0] == "inserted line" |
| 101 | + coder.io.tool_error.assert_not_called() |
| 102 | + |
| 103 | + |
| 104 | +def test_mutually_exclusive_parameters_raise(coder_with_file): |
| 105 | + coder, file_path = coder_with_file |
| 106 | + |
| 107 | + result = insert_block.Tool.execute( |
| 108 | + coder, |
| 109 | + file_path="example.txt", |
| 110 | + content="new line", |
| 111 | + position="top", |
| 112 | + after_pattern="first line", |
| 113 | + ) |
| 114 | + |
| 115 | + assert result.startswith("Error: Must specify exactly one of") |
| 116 | + assert file_path.read_text().startswith("first line") |
| 117 | + coder.io.tool_error.assert_called() |
0 commit comments