Skip to content

Commit 519db08

Browse files
committed
test: improve test coverage by fixing pipeline timeout tests
1 parent 41db93d commit 519db08

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

tests/test_shell_executor.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,7 @@ async def test_command_cleanup_on_error(executor, temp_test_dir, monkeypatch):
610610

611611
async def execute_with_keyboard_interrupt():
612612
# Simulate keyboard interrupt during execution
613-
result = await executor.execute(
614-
["sleep", "5"],
615-
temp_test_dir,
616-
timeout=1
617-
)
613+
result = await executor.execute(["sleep", "5"], temp_test_dir, timeout=1)
618614
return result
619615

620616
result = await execute_with_keyboard_interrupt()

tests/test_shell_executor_pipeline.py

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
1+
"""Test pipeline execution and cleanup scenarios."""
2+
import os
3+
import tempfile
4+
15
import pytest
26

37
from mcp_shell_server.shell_executor import ShellExecutor
48

59

10+
def clear_env(monkeypatch):
11+
monkeypatch.delenv("ALLOW_COMMANDS", raising=False)
12+
monkeypatch.delenv("ALLOWED_COMMANDS", raising=False)
13+
14+
15+
@pytest.fixture
16+
def executor():
17+
return ShellExecutor()
18+
19+
20+
@pytest.fixture
21+
def temp_test_dir():
22+
"""Create a temporary directory for testing"""
23+
with tempfile.TemporaryDirectory() as tmpdirname:
24+
# Return the real path to handle macOS /private/tmp symlink
25+
yield os.path.realpath(tmpdirname)
26+
27+
628
@pytest.mark.asyncio
7-
async def test_pipeline_split():
29+
async def test_pipeline_split(executor):
830
"""Test pipeline command splitting functionality"""
9-
executor = ShellExecutor()
10-
1131
# Test basic pipe command
1232
commands = executor._split_pipe_commands(["echo", "hello", "|", "grep", "h"])
1333
assert len(commands) == 2
@@ -35,19 +55,45 @@ async def test_pipeline_split():
3555

3656

3757
@pytest.mark.asyncio
38-
async def test_pipeline_execution_success():
58+
async def test_pipeline_execution_success(executor, temp_test_dir, monkeypatch):
3959
"""Test successful pipeline execution with proper return value"""
40-
executor = ShellExecutor()
41-
import os
42-
43-
os.environ["ALLOWED_COMMANDS"] = "echo,grep"
60+
clear_env(monkeypatch)
61+
monkeypatch.setenv("ALLOWED_COMMANDS", "echo,grep")
4462

4563
result = await executor.execute(
46-
["echo", "hello world", "|", "grep", "world"], directory="/tmp", timeout=5
64+
["echo", "hello world", "|", "grep", "world"], directory=temp_test_dir, timeout=5
4765
)
4866

4967
assert result["error"] is None
5068
assert result["status"] == 0
5169
assert "world" in result["stdout"]
5270
assert "execution_time" in result
53-
assert result["directory"] == "/tmp"
71+
72+
73+
@pytest.mark.asyncio
74+
async def test_pipeline_cleanup_and_timeouts(executor, temp_test_dir, monkeypatch):
75+
"""Test cleanup of processes in pipelines and timeout handling"""
76+
clear_env(monkeypatch)
77+
monkeypatch.setenv("ALLOW_COMMANDS", "cat,tr,head,sleep")
78+
79+
# Test pipeline with early termination
80+
test_file = os.path.join(temp_test_dir, "test.txt")
81+
with open(test_file, "w") as f:
82+
f.write("test\n" * 1000)
83+
84+
result = await executor.execute(
85+
["cat", test_file, "|", "tr", "[:lower:]", "[:upper:]", "|", "head", "-n", "1"],
86+
temp_test_dir,
87+
timeout=2
88+
)
89+
assert result["status"] == 0
90+
assert result["stdout"].strip() == "TEST"
91+
92+
# Test timeout handling in pipeline
93+
result = await executor.execute(
94+
["sleep", "5"],
95+
temp_test_dir,
96+
timeout=1
97+
)
98+
assert result["status"] == -1
99+
assert "timed out" in result["error"].lower() # タイムアウトエラーの確認

0 commit comments

Comments
 (0)