|
1 | 1 | # stdlib imports |
2 | 2 | from pathlib import Path |
| 3 | +from unittest.mock import patch |
3 | 4 |
|
4 | 5 | # third party imports |
5 | 6 | import pytest |
@@ -109,3 +110,41 @@ def test_npx_filesystem_server(mcp_yaml_config_file: Path): |
109 | 110 | "/Users/username/Desktop", |
110 | 111 | "/path/to/other/allowed/dir", |
111 | 112 | ] |
| 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