@@ -21,67 +21,69 @@ def temp_test_dir():
21
21
22
22
23
23
@pytest .mark .asyncio
24
- async def test_basic_command_execution (executor , monkeypatch ):
24
+ async def test_basic_command_execution (executor , temp_test_dir , monkeypatch ):
25
25
monkeypatch .setenv ("ALLOW_COMMANDS" , "echo" )
26
- result = await executor .execute (["echo" , "hello" ])
26
+ result = await executor .execute (["echo" , "hello" ], temp_test_dir )
27
27
assert result ["stdout" ].strip () == "hello"
28
28
assert result ["status" ] == 0
29
29
30
30
31
31
@pytest .mark .asyncio
32
- async def test_stdin_input (executor , monkeypatch ):
32
+ async def test_stdin_input (executor , temp_test_dir , monkeypatch ):
33
33
monkeypatch .setenv ("ALLOW_COMMANDS" , "cat" )
34
- result = await executor .execute (["cat" ], stdin = "hello world" )
34
+ result = await executor .execute (["cat" ], temp_test_dir , stdin = "hello world" )
35
35
assert result ["stdout" ].strip () == "hello world"
36
36
assert result ["status" ] == 0
37
37
38
38
39
39
@pytest .mark .asyncio
40
- async def test_command_with_space_allowed (executor , monkeypatch ):
40
+ async def test_command_with_space_allowed (executor , temp_test_dir , monkeypatch ):
41
41
monkeypatch .setenv ("ALLOW_COMMANDS" , "cat" )
42
- result = await executor .execute (["cat " ], stdin = "hello world" )
42
+ result = await executor .execute (["cat " ], temp_test_dir , stdin = "hello world" )
43
43
assert result ["error" ] is None
44
44
assert result ["stdout" ].strip () == "hello world"
45
45
assert result ["status" ] == 0
46
46
47
47
48
48
@pytest .mark .asyncio
49
- async def test_command_not_allowed (executor , monkeypatch ):
49
+ async def test_command_not_allowed (executor , temp_test_dir , monkeypatch ):
50
50
monkeypatch .setenv ("ALLOW_COMMANDS" , "ls" )
51
- result = await executor .execute (["rm" , "-rf" , "/" ])
51
+ result = await executor .execute (["rm" , "-rf" , "/" ], temp_test_dir )
52
52
assert result ["error" ] == "Command not allowed: rm"
53
53
assert result ["status" ] == 1
54
54
55
55
56
56
@pytest .mark .asyncio
57
- async def test_empty_command (executor ):
58
- result = await executor .execute ([])
57
+ async def test_empty_command (executor , temp_test_dir ):
58
+ result = await executor .execute ([], temp_test_dir )
59
59
assert result ["error" ] == "Empty command"
60
60
assert result ["status" ] == 1
61
61
62
62
63
63
@pytest .mark .asyncio
64
- async def test_command_with_space_in_allow_commands (executor , monkeypatch ):
64
+ async def test_command_with_space_in_allow_commands (
65
+ executor , temp_test_dir , monkeypatch
66
+ ):
65
67
monkeypatch .setenv ("ALLOW_COMMANDS" , "ls, echo ,cat" )
66
- result = await executor .execute (["echo" , "test" ])
68
+ result = await executor .execute (["echo" , "test" ], temp_test_dir )
67
69
assert result ["stdout" ].strip () == "test"
68
70
assert result ["status" ] == 0
69
71
70
72
71
73
@pytest .mark .asyncio
72
- async def test_multiple_commands_with_operator (executor , monkeypatch ):
74
+ async def test_multiple_commands_with_operator (executor , temp_test_dir , monkeypatch ):
73
75
monkeypatch .setenv ("ALLOW_COMMANDS" , "echo,ls" )
74
- result = await executor .execute (["echo" , "hello" , ";" ])
76
+ result = await executor .execute (["echo" , "hello" , ";" ], temp_test_dir )
75
77
assert result ["error" ] == "Unexpected shell operator: ;"
76
78
assert result ["status" ] == 1
77
79
78
80
79
81
@pytest .mark .asyncio
80
- async def test_shell_operators_not_allowed (executor , monkeypatch ):
82
+ async def test_shell_operators_not_allowed (executor , temp_test_dir , monkeypatch ):
81
83
monkeypatch .setenv ("ALLOW_COMMANDS" , "echo,ls,true" )
82
84
operators = [";" , "&&" , "||" ]
83
85
for op in operators :
84
- result = await executor .execute (["echo" , "hello" , op , "true" ])
86
+ result = await executor .execute (["echo" , "hello" , op , "true" ], temp_test_dir )
85
87
assert result ["error" ] == f"Unexpected shell operator: { op } "
86
88
assert result ["status" ] == 1
87
89
@@ -140,17 +142,6 @@ async def test_execute_with_file_as_directory(executor, temp_test_dir, monkeypat
140
142
assert result ["status" ] == 1
141
143
142
144
143
- @pytest .mark .asyncio
144
- async def test_execute_with_no_directory_specified (executor , monkeypatch ):
145
- """Test command execution without specifying a directory"""
146
- monkeypatch .setenv ("ALLOW_COMMANDS" , "pwd" )
147
- result = await executor .execute (["pwd" ])
148
- assert result ["error" ] is None
149
- assert result ["status" ] == 0
150
- assert os .path .exists (result ["stdout" ].strip ())
151
-
152
-
153
- @pytest .mark .asyncio
154
145
async def test_execute_with_nested_directory (executor , temp_test_dir , monkeypatch ):
155
146
"""Test command execution in a nested directory"""
156
147
monkeypatch .setenv ("ALLOW_COMMANDS" , "pwd,mkdir,ls" )
@@ -167,54 +158,54 @@ async def test_execute_with_nested_directory(executor, temp_test_dir, monkeypatc
167
158
168
159
169
160
@pytest .mark .asyncio
170
- async def test_command_timeout (executor , monkeypatch ):
161
+ async def test_command_timeout (executor , temp_test_dir , monkeypatch ):
171
162
"""Test command timeout functionality"""
172
163
monkeypatch .setenv ("ALLOW_COMMANDS" , "sleep" )
173
- result = await executor .execute (["sleep" , "2" ], timeout = 1 )
164
+ result = await executor .execute (["sleep" , "2" ], temp_test_dir , timeout = 1 )
174
165
assert result ["error" ] == "Command timed out after 1 seconds"
175
166
assert result ["status" ] == - 1
176
167
assert result ["stdout" ] == ""
177
168
assert result ["stderr" ] == "Command timed out after 1 seconds"
178
169
179
170
180
171
@pytest .mark .asyncio
181
- async def test_command_completes_within_timeout (executor , monkeypatch ):
172
+ async def test_command_completes_within_timeout (executor , temp_test_dir , monkeypatch ):
182
173
"""Test command that completes within timeout period"""
183
174
monkeypatch .setenv ("ALLOW_COMMANDS" , "sleep" )
184
- result = await executor .execute (["sleep" , "1" ], timeout = 2 )
175
+ result = await executor .execute (["sleep" , "1" ], temp_test_dir , timeout = 2 )
185
176
assert result ["error" ] is None
186
177
assert result ["status" ] == 0
187
178
assert result ["stdout" ] == ""
188
179
189
180
190
181
@pytest .mark .asyncio
191
- async def test_allowed_commands_alias (executor , monkeypatch ):
182
+ async def test_allowed_commands_alias (executor , temp_test_dir , monkeypatch ):
192
183
"""Test ALLOWED_COMMANDS alias functionality"""
193
184
monkeypatch .setenv ("ALLOWED_COMMANDS" , "echo" )
194
- result = await executor .execute (["echo" , "hello" ])
185
+ result = await executor .execute (["echo" , "hello" ], temp_test_dir )
195
186
assert result ["stdout" ].strip () == "hello"
196
187
assert result ["status" ] == 0
197
188
198
189
199
190
@pytest .mark .asyncio
200
- async def test_both_allow_commands_vars (executor , monkeypatch ):
191
+ async def test_both_allow_commands_vars (executor , temp_test_dir , monkeypatch ):
201
192
"""Test both ALLOW_COMMANDS and ALLOWED_COMMANDS working together"""
202
193
monkeypatch .setenv ("ALLOW_COMMANDS" , "echo" )
203
194
monkeypatch .setenv ("ALLOWED_COMMANDS" , "cat" )
204
195
205
196
# Test command from ALLOW_COMMANDS
206
- result1 = await executor .execute (["echo" , "hello" ])
197
+ result1 = await executor .execute (["echo" , "hello" ], temp_test_dir )
207
198
assert result1 ["stdout" ].strip () == "hello"
208
199
assert result1 ["status" ] == 0
209
200
210
201
# Test command from ALLOWED_COMMANDS
211
- result2 = await executor .execute (["cat" ], stdin = "world" )
202
+ result2 = await executor .execute (["cat" ], temp_test_dir , stdin = "world" )
212
203
assert result2 ["stdout" ].strip () == "world"
213
204
assert result2 ["status" ] == 0
214
205
215
206
216
207
@pytest .mark .asyncio
217
- async def test_allow_commands_precedence (executor , monkeypatch ):
208
+ async def test_allow_commands_precedence (executor , temp_test_dir , monkeypatch ):
218
209
"""Test that commands are combined from both environment variables"""
219
210
monkeypatch .setenv ("ALLOW_COMMANDS" , "echo,ls" )
220
211
monkeypatch .setenv ("ALLOWED_COMMANDS" , "echo,cat" )
@@ -224,10 +215,12 @@ async def test_allow_commands_precedence(executor, monkeypatch):
224
215
225
216
226
217
@pytest .mark .asyncio
227
- async def test_pipe_operator (executor , monkeypatch ):
218
+ async def test_pipe_operator (executor , temp_test_dir , monkeypatch ):
228
219
"""Test that pipe operator works correctly"""
229
220
monkeypatch .setenv ("ALLOW_COMMANDS" , "echo,grep" )
230
- result = await executor .execute (["echo" , "hello\n world" , "|" , "grep" , "world" ])
221
+ result = await executor .execute (
222
+ ["echo" , "hello\n world" , "|" , "grep" , "world" ], temp_test_dir
223
+ )
231
224
assert result ["error" ] is None
232
225
assert result ["status" ] == 0
233
226
assert result ["stdout" ].strip () == "world"
@@ -237,12 +230,15 @@ async def test_pipe_operator(executor, monkeypatch):
237
230
async def test_pipe_commands (executor , temp_test_dir , monkeypatch ):
238
231
"""Test piping commands together"""
239
232
monkeypatch .setenv ("ALLOW_COMMANDS" , "echo,grep,cut,tr" )
240
- result = await executor .execute (["echo" , "hello world" , "|" , "grep" , "world" ])
233
+ result = await executor .execute (
234
+ ["echo" , "hello world" , "|" , "grep" , "world" ], temp_test_dir
235
+ )
241
236
assert result ["stdout" ].strip () == "hello world"
242
237
243
238
# Test multiple pipes
244
239
result = await executor .execute (
245
- ["echo" , "hello world" , "|" , "cut" , "-d" , " " , "-f2" , "|" , "tr" , "a-z" , "A-Z" ]
240
+ ["echo" , "hello world" , "|" , "cut" , "-d" , " " , "-f2" , "|" , "tr" , "a-z" , "A-Z" ],
241
+ temp_test_dir ,
246
242
)
247
243
assert result ["stdout" ].strip () == "WORLD"
248
244
0 commit comments