test_simple_file_ops.pyโข6.31 kB
#!/usr/bin/env python3
"""
Simple test for unified edit_file functionality
Tests the underlying functions directly without FastMCP decoration.
"""
import tempfile
from pathlib import Path
# Add the project root to the path
import sys
sys.path.append(str(Path(__file__).parent.parent))
def test_unified_file_operations():
"""Test basic file operations without using the FastMCP tool directly."""
print("๐งช Testing Unified File Operations\n")
# Create a temporary directory for testing
with tempfile.TemporaryDirectory() as temp_dir:
test_dir = Path(temp_dir)
test_file = test_dir / "test.txt"
print(f"๐ Test directory: {test_dir}\n")
# Test 1: Write file
print("1๏ธโฃ Testing write operation...")
content = "Hello, this is a test file!\nLine 2\nLine 3"
try:
with open(test_file, 'w', encoding='utf-8') as f:
f.write(content)
print(f"โ
File written successfully: {test_file}")
print(f"๐ Content length: {len(content)} characters")
except Exception as e:
print(f"โ Write failed: {e}")
print()
# Test 2: Read file
print("2๏ธโฃ Testing read operation...")
try:
with open(test_file, 'r', encoding='utf-8') as f:
read_content = f.read()
print(f"โ
File read successfully:")
print(f"๐ Content:\n{read_content}")
except Exception as e:
print(f"โ Read failed: {e}")
print()
# Test 3: Append to file
print("3๏ธโฃ Testing append operation...")
append_content = "\nAppended line 4\nAppended line 5"
try:
with open(test_file, 'a', encoding='utf-8') as f:
f.write(append_content)
file_size = test_file.stat().st_size
print(f"โ
Content appended successfully")
print(f"๐ Added: {len(append_content)} characters")
print(f"๐ Total file size: {file_size} bytes")
except Exception as e:
print(f"โ Append failed: {e}")
print()
# Test 4: File info
print("4๏ธโฃ Testing file info...")
try:
stat_info = test_file.stat()
size_bytes = stat_info.st_size
print(f"โ
File info retrieved:")
print(f"๐ Path: {test_file}")
print(f"๐ Type: File")
print(f"๐ Size: {size_bytes} bytes")
print(f"๐ Permissions: {oct(stat_info.st_mode)[-3:]}")
except Exception as e:
print(f"โ File info failed: {e}")
print()
# Test 5: Copy file
print("5๏ธโฃ Testing copy operation...")
test_copy = test_dir / "test_copy.txt"
try:
import shutil
shutil.copy2(test_file, test_copy)
source_size = test_file.stat().st_size
dest_size = test_copy.stat().st_size
print(f"โ
File copied successfully:")
print(f"๐ From: {test_file}")
print(f"๐ To: {test_copy}")
print(f"๐ Size: {source_size} bytes")
print(f"๐ Verified: Source and destination match ({dest_size} bytes)")
except Exception as e:
print(f"โ Copy failed: {e}")
print()
# Test 6: Move file
print("6๏ธโฃ Testing move operation...")
test_move = test_dir / "test_moved.txt"
try:
test_file.rename(test_move)
print(f"โ
File moved successfully:")
print(f"๐ From: {test_file}")
print(f"๐ To: {test_move}")
except Exception as e:
print(f"โ Move failed: {e}")
print()
# Test 7: Create directory
print("7๏ธโฃ Testing directory creation...")
test_subdir = test_dir / "subdir"
try:
test_subdir.mkdir(parents=True, exist_ok=False)
if test_subdir.exists() and test_subdir.is_dir():
print(f"โ
Directory created successfully:")
print(f"๐ Path: {test_subdir}")
else:
print(f"โ Directory creation failed")
except Exception as e:
print(f"โ Directory creation failed: {e}")
print()
# Test 8: List directory
print("8๏ธโฃ Testing directory listing...")
try:
items = []
for item in sorted(test_dir.iterdir()):
item_type = "๐" if item.is_dir() else "๐"
size_info = f" ({item.stat().st_size} bytes)" if item.is_file() else ""
items.append(f"{item_type} {item.name}{size_info}")
if not items:
print(f"โ
Directory is empty")
else:
print(f"โ
Directory listing:")
print(f"๐ Path: {test_dir}")
print(f"๐ Items ({len(items)}):")
for item in items:
print(f" {item}")
except Exception as e:
print(f"โ Directory listing failed: {e}")
print()
# Test 9: Delete file
print("9๏ธโฃ Testing file deletion...")
try:
if test_copy.exists():
test_copy.unlink()
print(f"โ
File deleted successfully:")
print(f"๐ Path: {test_copy}")
else:
print(f"โ File not found: {test_copy}")
except Exception as e:
print(f"โ File deletion failed: {e}")
print()
# Test 10: Remove directory
print("๐ Testing directory removal...")
try:
if test_subdir.exists() and test_subdir.is_dir():
test_subdir.rmdir()
print(f"โ
Directory deleted successfully:")
print(f"๐ Path: {test_subdir}")
else:
print(f"โ Directory not found: {test_subdir}")
except Exception as e:
print(f"โ Directory deletion failed: {e}")
print()
print("โ
All basic file operation tests completed!")
if __name__ == "__main__":
test_unified_file_operations()