Skip to content

Commit 6c5a5ff

Browse files
committed
feat(text_editor.py): add validation for required parameters and file path checks in delete_text_file_contents method to enhance error handling
test(delete_text_file.py): add unit tests for missing parameters and invalid file paths in delete_text_file_contents method to ensure robustness
1 parent 51926df commit 6c5a5ff

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/mcp_text_editor/text_editor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,20 @@ async def delete_text_file_contents(
562562
- hash: New file hash if successful
563563
- reason: Error message if result is "error"
564564
"""
565+
# Check required parameters
566+
if not file_path:
567+
raise RuntimeError("Missing required argument: file_path")
568+
if not file_hash:
569+
raise RuntimeError("Missing required argument: file_hash")
570+
if not range_hash:
571+
raise RuntimeError("Missing required argument: range_hash")
572+
573+
# Validate file path
574+
if not os.path.isabs(file_path):
575+
raise RuntimeError("File path must be absolute")
576+
if not os.path.exists(file_path):
577+
raise RuntimeError("File does not exist")
578+
565579
try:
566580
# Read current content and verify hash
567581
content, _, _, current_hash, total_lines, _ = await self.read_file_contents(

tests/test_delete_text_file.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,72 @@ async def test_delete_with_out_of_range(editor, test_file):
155155
assert result["result"] == "error"
156156
assert "line number out of range" in result["reason"].lower()
157157
assert test_file.read_text() == "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n"
158+
159+
160+
@pytest.mark.asyncio
161+
async def test_delete_without_file_path(editor):
162+
"""Test deleting without specifying file_path"""
163+
with pytest.raises(
164+
TypeError, match="missing 1 required positional argument: 'file_path'"
165+
):
166+
await editor.delete_text_file_contents(
167+
file_hash="any_hash",
168+
range_hash="any_hash",
169+
start_line=1,
170+
end_line=1,
171+
)
172+
173+
174+
@pytest.mark.asyncio
175+
async def test_delete_without_file_hash(editor, test_file):
176+
"""Test deleting without specifying file_hash"""
177+
with pytest.raises(
178+
TypeError, match="missing 1 required positional argument: 'file_hash'"
179+
):
180+
await editor.delete_text_file_contents(
181+
file_path=str(test_file),
182+
range_hash="any_hash",
183+
start_line=1,
184+
end_line=1,
185+
)
186+
187+
188+
@pytest.mark.asyncio
189+
async def test_delete_without_range_hash(editor, test_file):
190+
"""Test deleting without specifying range_hash"""
191+
with pytest.raises(
192+
TypeError, match="missing 1 required positional argument: 'range_hash'"
193+
):
194+
await editor.delete_text_file_contents(
195+
file_path=str(test_file),
196+
file_hash="any_hash",
197+
start_line=1,
198+
end_line=1,
199+
)
200+
201+
202+
@pytest.mark.asyncio
203+
async def test_delete_relative_path(editor):
204+
"""Test deleting with relative path"""
205+
with pytest.raises(RuntimeError, match="File path must be absolute"):
206+
await editor.delete_text_file_contents(
207+
file_path="relative/path/file.txt",
208+
file_hash="any_hash",
209+
range_hash="any_hash",
210+
start_line=1,
211+
end_line=1,
212+
)
213+
214+
215+
@pytest.mark.asyncio
216+
async def test_delete_nonexistent_file(editor, tmp_path):
217+
"""Test deleting a file that doesn't exist"""
218+
nonexistent_file = tmp_path / "nonexistent.txt"
219+
with pytest.raises(RuntimeError, match="File does not exist"):
220+
await editor.delete_text_file_contents(
221+
file_path=str(nonexistent_file),
222+
file_hash="any_hash",
223+
range_hash="any_hash",
224+
start_line=1,
225+
end_line=1,
226+
)

0 commit comments

Comments
 (0)