|
| 1 | +"""Test module for patch_text_file with end=None.""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from mcp_text_editor.text_editor import TextEditor |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.asyncio |
| 11 | +async def test_patch_text_file_end_none(tmp_path): |
| 12 | + """Test patching text file with end=None.""" |
| 13 | + # Create a test file |
| 14 | + file_path = os.path.join(tmp_path, "test.txt") |
| 15 | + editor = TextEditor() |
| 16 | + |
| 17 | + # Create initial content |
| 18 | + content = "line1\nline2\nline3\nline4\nline5\n" |
| 19 | + with open(file_path, "w", encoding="utf-8") as f: |
| 20 | + f.write(content) |
| 21 | + |
| 22 | + # Get file hash and range hash |
| 23 | + file_info = await editor.read_multiple_ranges( |
| 24 | + [ |
| 25 | + { |
| 26 | + "file_path": str(file_path), |
| 27 | + "ranges": [{"start": 2, "end": None}], # Test with end=None |
| 28 | + } |
| 29 | + ] |
| 30 | + ) |
| 31 | + |
| 32 | + # Extract file and range hashes |
| 33 | + file_content = file_info[str(file_path)] |
| 34 | + file_hash = file_content["file_hash"] |
| 35 | + range_hash = file_content["ranges"][0]["range_hash"] |
| 36 | + |
| 37 | + # Patch the file |
| 38 | + new_content = "new line2\nnew line3\nnew line4\nnew line5\n" |
| 39 | + patch = { |
| 40 | + "start": 2, |
| 41 | + "end": None, # Test with end=None |
| 42 | + "contents": new_content, |
| 43 | + "range_hash": range_hash, |
| 44 | + } |
| 45 | + result = await editor.edit_file_contents( |
| 46 | + str(file_path), |
| 47 | + file_hash, |
| 48 | + [patch], |
| 49 | + ) |
| 50 | + |
| 51 | + # Verify the patch was successful |
| 52 | + assert result["result"] == "ok" |
| 53 | + with open(file_path, "r", encoding="utf-8") as f: |
| 54 | + updated_content = f.read() |
| 55 | + assert updated_content == "line1\nnew line2\nnew line3\nnew line4\nnew line5\n" |
0 commit comments