Skip to content

Commit 70e91b9

Browse files
committed
Add tests for when yaml is not importable
1 parent 27e5b12 commit 70e91b9

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/client/config/test_yaml_functionality.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# stdlib imports
22
from pathlib import Path
3+
from unittest.mock import patch
34

45
# third party imports
56
import pytest
@@ -109,3 +110,41 @@ def test_npx_filesystem_server(mcp_yaml_config_file: Path):
109110
"/Users/username/Desktop",
110111
"/path/to/other/allowed/dir",
111112
]
113+
114+
115+
def test_yaml_not_importable_error(mcp_yaml_config_file: Path):
116+
"""Test that trying to parse a YAML file when yaml module is not available raises ImportError."""
117+
118+
# Mock the yaml module to be None (simulating import failure)
119+
with patch("mcp.client.config.mcp_servers_config.yaml", None):
120+
with pytest.raises(ImportError, match="PyYAML is required to parse YAML files"):
121+
MCPServersConfig.from_file(mcp_yaml_config_file)
122+
123+
124+
def test_yaml_not_importable_error_with_use_pyyaml_true():
125+
"""Test that trying to use use_pyyaml=True when yaml module is not available raises ImportError."""
126+
127+
# Create a simple JSON file content but force YAML parsing
128+
json_file = Path(__file__).parent / "mcp.json"
129+
130+
# Mock the yaml module to be None (simulating import failure)
131+
with patch("mcp.client.config.mcp_servers_config.yaml", None):
132+
with pytest.raises(ImportError, match="PyYAML is required to parse YAML files"):
133+
MCPServersConfig.from_file(json_file, use_pyyaml=True)
134+
135+
136+
def test_yaml_not_importable_error_with_yml_extension(tmp_path: Path):
137+
"""Test that trying to parse a .yml file when yaml module is not available raises ImportError."""
138+
139+
# Create a temporary .yml file
140+
yml_file = tmp_path / "test_config.yml"
141+
yml_file.write_text("""
142+
mcpServers:
143+
test_server:
144+
command: python -m test_server
145+
""")
146+
147+
# Mock the yaml module to be None (simulating import failure)
148+
with patch("mcp.client.config.mcp_servers_config.yaml", None):
149+
with pytest.raises(ImportError, match="PyYAML is required to parse YAML files"):
150+
MCPServersConfig.from_file(yml_file)

0 commit comments

Comments
 (0)