|
| 1 | +import logging |
1 | 2 | from pathlib import Path |
2 | 3 | from types import SimpleNamespace |
3 | 4 | from typing import List |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
| 8 | +from jupyter_lsp import LanguageServerManager |
| 9 | + |
7 | 10 | from ..virtual_documents_shadow import ( |
8 | 11 | EditableFile, |
9 | 12 | ShadowFilesystemError, |
@@ -93,9 +96,16 @@ def shadow_path(tmpdir): |
93 | 96 |
|
94 | 97 | @pytest.fixture |
95 | 98 | def manager(): |
96 | | - return SimpleNamespace( |
97 | | - language_servers={"python-lsp-server": {"requires_documents_on_disk": True}} |
98 | | - ) |
| 99 | + manager = LanguageServerManager() |
| 100 | + manager.language_servers = { |
| 101 | + "python-lsp-server": { |
| 102 | + "requires_documents_on_disk": True, |
| 103 | + "argv": [], |
| 104 | + "languages": ["python"], |
| 105 | + "version": 2, |
| 106 | + } |
| 107 | + } |
| 108 | + return manager |
99 | 109 |
|
100 | 110 |
|
101 | 111 | @pytest.mark.asyncio |
@@ -198,3 +208,44 @@ def run_shadow(message): |
198 | 208 | "params": {"textDocument": {"uri": ok_file_uri}}, |
199 | 209 | } |
200 | 210 | ) |
| 211 | + |
| 212 | + |
| 213 | +@pytest.fixture |
| 214 | +def forbidden_shadow_path(tmpdir): |
| 215 | + path = Path(tmpdir) / "no_permission_dir" |
| 216 | + path.mkdir() |
| 217 | + path.chmod(0o000) |
| 218 | + |
| 219 | + yield path |
| 220 | + |
| 221 | + # re-adjust the permissions, see https://github.com/pytest-dev/pytest/issues/7821 |
| 222 | + path.chmod(0o755) |
| 223 | + |
| 224 | + |
| 225 | +@pytest.mark.asyncio |
| 226 | +async def test_io_failure(forbidden_shadow_path, manager, caplog): |
| 227 | + file_uri = (forbidden_shadow_path / "test.py").as_uri() |
| 228 | + |
| 229 | + shadow = setup_shadow_filesystem(forbidden_shadow_path.as_uri()) |
| 230 | + |
| 231 | + def send_change(): |
| 232 | + message = did_open(file_uri, "content") |
| 233 | + return shadow("client", message, "python-lsp-server", manager) |
| 234 | + |
| 235 | + with caplog.at_level(logging.WARNING): |
| 236 | + assert await send_change() is None |
| 237 | + assert await send_change() is None |
| 238 | + # no message should be emitted during the first two attempts |
| 239 | + assert caplog.text == "" |
| 240 | + |
| 241 | + # a wargning should be emitted on third failure |
| 242 | + with caplog.at_level(logging.WARNING): |
| 243 | + assert await send_change() is None |
| 244 | + assert "initialization of shadow filesystem failed three times" in caplog.text |
| 245 | + assert "PermissionError" in caplog.text |
| 246 | + caplog.clear() |
| 247 | + |
| 248 | + # no message should be emitted in subsequent attempts |
| 249 | + with caplog.at_level(logging.WARNING): |
| 250 | + assert await send_change() is None |
| 251 | + assert caplog.text == "" |
0 commit comments