Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tumf committed Dec 23, 2024
2 parents c3fcef1 + 37bee30 commit c77cb4f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/mcp_text_editor/handlers/patch_text_file_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_tool_description(self) -> Tool:
"description": "Starting line number (1-based).it should match the range hash.",
},
"end": {
"type": ["integer", "null"],
"type": "integer",
"description": "Ending line number (null for end of file).it should match the range hash.",
},
"contents": {
Expand All @@ -58,7 +58,7 @@ def get_tool_description(self) -> Tool:
"description": "Hash of the content being replaced. it should get from get_text_file_contents tool with the same start and end.",
},
},
"required": ["start", "contents", "range_hash"],
"required": ["start", "end", "contents", "range_hash"],
},
},
"encoding": {
Expand Down
12 changes: 7 additions & 5 deletions src/mcp_text_editor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ class EditPatch(BaseModel):
)

@model_validator(mode="after")
def validate_end_line(self) -> "EditPatch":
"""Validate that end line is present when not in append mode."""
def validate_range_hash(self) -> "EditPatch":
"""Validate that range_hash is set and handle end field validation."""
# range_hash must be explicitly set
if self.range_hash is None:
raise ValueError("range_hash is required")

# For modifications (non-empty range_hash), end is required
if self.range_hash != "" and self.end is None:
raise ValueError("end line is required when not in append mode")
# For safety, convert None to the special range hash value
if self.end is None and self.range_hash != "":
# Special case: patch with end=None is allowed
pass

return self


Expand Down
6 changes: 0 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ def test_edit_patch():
with pytest.raises(ValidationError):
EditPatch()

# Test validation error - missing end in modification mode
with pytest.raises(
ValueError, match="end line is required when not in append mode"
):
EditPatch(start=1, contents="content", range_hash="somehash")


def test_edit_file_operation():
"""Test EditFileOperation model."""
Expand Down
55 changes: 55 additions & 0 deletions tests/test_patch_text_file_end_none.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Test module for patch_text_file with end=None."""

import os

import pytest

from mcp_text_editor.text_editor import TextEditor


@pytest.mark.asyncio
async def test_patch_text_file_end_none(tmp_path):
"""Test patching text file with end=None."""
# Create a test file
file_path = os.path.join(tmp_path, "test.txt")
editor = TextEditor()

# Create initial content
content = "line1\nline2\nline3\nline4\nline5\n"
with open(file_path, "w", encoding="utf-8") as f:
f.write(content)

# Get file hash and range hash
file_info = await editor.read_multiple_ranges(
[
{
"file_path": str(file_path),
"ranges": [{"start": 2, "end": None}], # Test with end=None
}
]
)

# Extract file and range hashes
file_content = file_info[str(file_path)]
file_hash = file_content["file_hash"]
range_hash = file_content["ranges"][0]["range_hash"]

# Patch the file
new_content = "new line2\nnew line3\nnew line4\nnew line5\n"
patch = {
"start": 2,
"end": None, # Test with end=None
"contents": new_content,
"range_hash": range_hash,
}
result = await editor.edit_file_contents(
str(file_path),
file_hash,
[patch],
)

# Verify the patch was successful
assert result["result"] == "ok"
with open(file_path, "r", encoding="utf-8") as f:
updated_content = f.read()
assert updated_content == "line1\nnew line2\nnew line3\nnew line4\nnew line5\n"

0 comments on commit c77cb4f

Please sign in to comment.