7
7
from mcp_shell_server .shell_executor import ShellExecutor
8
8
9
9
10
+ def clear_env (monkeypatch ):
11
+ monkeypatch .delenv ("ALLOW_COMMANDS" , raising = False )
12
+ monkeypatch .delenv ("ALLOWED_COMMANDS" , raising = False )
13
+
14
+
10
15
@pytest .fixture
11
16
def temp_test_dir ():
12
17
"""Create a temporary directory for testing"""
@@ -20,15 +25,15 @@ async def test_empty_command_validation():
20
25
"""Test validation of empty commands"""
21
26
executor = ShellExecutor ()
22
27
23
- # 空のコマンドのテスト
28
+ # Test empty command
24
29
with pytest .raises (ValueError , match = "Empty command" ):
25
30
executor ._validate_command ([])
26
31
27
32
28
33
@pytest .mark .asyncio
29
34
async def test_no_allowed_commands_validation (monkeypatch ):
30
35
"""Test validation when no commands are allowed"""
31
- # ALLOW_COMMANDSを削除
36
+ # Remove ALLOW_COMMANDS
32
37
monkeypatch .delenv ("ALLOW_COMMANDS" , raising = False )
33
38
monkeypatch .delenv ("ALLOWED_COMMANDS" , raising = False )
34
39
@@ -47,7 +52,7 @@ async def test_shell_operator_validation():
47
52
48
53
operators = [";" "&&" , "||" , "|" ]
49
54
for op in operators :
50
- # シェル操作子の検証
55
+ # Test shell operator validation
51
56
with pytest .raises (ValueError , match = f"Unexpected shell operator: { op } " ):
52
57
executor ._validate_no_shell_operators (op )
53
58
@@ -58,7 +63,7 @@ async def test_process_execution_timeout(monkeypatch, temp_test_dir):
58
63
monkeypatch .setenv ("ALLOW_COMMANDS" , "sleep" )
59
64
executor = ShellExecutor ()
60
65
61
- # プロセスのタイムアウトをテスト
66
+ # Test process timeout
62
67
command = ["sleep" , "5" ]
63
68
with pytest .raises (asyncio .TimeoutError ):
64
69
await asyncio .wait_for (executor .execute (command , temp_test_dir ), timeout = 0.1 )
@@ -70,6 +75,24 @@ async def test_process_failure(monkeypatch, temp_test_dir):
70
75
monkeypatch .setenv ("ALLOW_COMMANDS" , "false" )
71
76
executor = ShellExecutor ()
72
77
73
- # falseコマンドは常に終了コード1を返す
78
+ # false command always returns exit code 1
74
79
result = await executor .execute (["false" ], temp_test_dir )
75
80
assert result ["status" ] == 1
81
+
82
+
83
+ @pytest .mark .asyncio
84
+ async def test_directory_validation ():
85
+ """Test directory validation"""
86
+ executor = ShellExecutor ()
87
+
88
+ # Test missing directory
89
+ with pytest .raises (ValueError , match = "Directory is required" ):
90
+ executor ._validate_directory (None )
91
+
92
+ # Test relative path
93
+ with pytest .raises (ValueError , match = "Directory must be an absolute path" ):
94
+ executor ._validate_directory ("relative/path" )
95
+
96
+ # Test non-existent directory
97
+ with pytest .raises (ValueError , match = "Directory does not exist" ):
98
+ executor ._validate_directory ("/nonexistent/directory" )
0 commit comments