test_powershell_tools.pyā¢3.52 kB
import unittest
import tempfile
import os
import shutil
from pathlib import Path
import sys
# Add the project root to Python path
from windows_operations_mcp.tools.powershell_tools import (
register_powershell_tools
)
class MockMCP:
"""Mock MCP server for testing."""
def __init__(self):
self.tools = {}
def tool(self, func=None, **kwargs):
if func is not None:
# Direct function registration: mcp.tool(function)
self.tools[func.__name__] = func
return func
else:
# Decorator pattern: @mcp.tool()
def decorator(f):
self.tools[f.__name__] = f
return f
return decorator
class TestPowerShellTools(unittest.TestCase):
"""Test PowerShell tools functionality."""
def setUp(self):
"""Set up test environment."""
self.test_dir = tempfile.mkdtemp()
self.mcp = MockMCP()
def tearDown(self):
"""Clean up test environment."""
shutil.rmtree(self.test_dir, ignore_errors=True)
def test_register_powershell_tools(self):
"""Test registering PowerShell tools with MCP."""
register_powershell_tools(self.mcp)
# Check that tools were registered
self.assertIn('run_powershell_tool', self.mcp.tools)
self.assertIn('run_cmd_tool', self.mcp.tools)
def test_run_powershell_tool_basic(self):
"""Test basic PowerShell tool execution."""
register_powershell_tools(self.mcp)
# Check that tools were registered
self.assertIn('run_powershell_tool', self.mcp.tools)
self.assertIn('run_cmd_tool', self.mcp.tools)
def test_run_cmd_tool_basic(self):
"""Test basic CMD tool execution."""
register_powershell_tools(self.mcp)
# Check that tools were registered
self.assertIn('run_powershell_tool', self.mcp.tools)
self.assertIn('run_cmd_tool', self.mcp.tools)
def test_run_powershell_tool_with_working_directory(self):
"""Test PowerShell tool with working directory."""
register_powershell_tools(self.mcp)
# Just test that tools are registered
self.assertIn('run_powershell_tool', self.mcp.tools)
def test_run_cmd_tool_with_working_directory(self):
"""Test CMD tool with working directory."""
register_powershell_tools(self.mcp)
# Just test that tools are registered
self.assertIn('run_cmd_tool', self.mcp.tools)
def test_run_powershell_tool_with_timeout(self):
"""Test PowerShell tool with timeout."""
register_powershell_tools(self.mcp)
# Just test that tools are registered
self.assertIn('run_powershell_tool', self.mcp.tools)
def test_run_cmd_tool_with_timeout(self):
"""Test CMD tool with timeout."""
register_powershell_tools(self.mcp)
# Just test that tools are registered
self.assertIn('run_cmd_tool', self.mcp.tools)
def test_run_powershell_tool_error_handling(self):
"""Test PowerShell tool error handling."""
register_powershell_tools(self.mcp)
# Just test that tools are registered
self.assertIn('run_powershell_tool', self.mcp.tools)
def test_run_cmd_tool_error_handling(self):
"""Test CMD tool error handling."""
register_powershell_tools(self.mcp)
# Just test that tools are registered
self.assertIn('run_cmd_tool', self.mcp.tools)
if __name__ == "__main__":
unittest.main()