Skip to content

Commit a1142a0

Browse files
committed
Test for invalid initialization options
1 parent b408d68 commit a1142a0

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

tests/lsp_test_client/session.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717

1818
PUBLISH_DIAGNOSTICS = "textDocument/publishDiagnostics"
19+
WINDOW_LOG_MESSAGE = "window/logMessage"
20+
WINDOW_SHOW_MESSAGE = "window/showMessage"
1921

2022

2123
class LspSession(MethodDispatcher):
@@ -55,7 +57,10 @@ def __enter__(self):
5557
os.fdopen(self._sub.stdout.fileno(), "rb")
5658
)
5759

58-
dispatcher = {PUBLISH_DIAGNOSTICS: self._publish_diagnostics}
60+
dispatcher = {
61+
PUBLISH_DIAGNOSTICS: self._publish_diagnostics,
62+
WINDOW_SHOW_MESSAGE: self._window_show_message,
63+
}
5964
self._endpoint = Endpoint(dispatcher, self._writer.write)
6065
self._thread_pool.submit(self._reader.listen, self._endpoint.consume)
6166
return self
@@ -228,6 +233,18 @@ def _handler():
228233
self._thread_pool.submit(_handler)
229234
return fut
230235

236+
def _window_show_message(self, window_show_message_params):
237+
"""Internal handler for text document publish diagnostics."""
238+
fut = Future()
239+
240+
def _handler():
241+
callback = self.get_notification_callback(WINDOW_SHOW_MESSAGE)
242+
callback(window_show_message_params)
243+
fut.set_result(None)
244+
245+
self._thread_pool.submit(_handler)
246+
return fut
247+
231248
def _send_request(
232249
self, name, params=None, handle_response=lambda f: f.done()
233250
):

tests/lsp_tests/test_init_options.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Tests for initialization options over language server protocol."""
2+
3+
import copy
4+
from threading import Event
5+
6+
from hamcrest import assert_that, is_
7+
8+
from tests.lsp_test_client import defaults, session
9+
10+
11+
def test_invalid_initialization_options() -> None:
12+
"""Test what happens when invalid initialization is sent."""
13+
initialize_params = copy.deepcopy(defaults.VSCODE_DEFAULT_INITIALIZE)
14+
initialize_params["initializationOptions"]["diagnostics"] = 1
15+
16+
with session.LspSession() as ls_session:
17+
done = Event()
18+
actual = []
19+
20+
def _handler(params):
21+
actual.append(params)
22+
done.set()
23+
24+
ls_session.set_notification_callback(
25+
session.WINDOW_SHOW_MESSAGE, _handler
26+
)
27+
28+
ls_session.initialize(initialize_params)
29+
expected = [
30+
{
31+
"type": 1,
32+
"message": "Invalid InitializationOptions, using defaults: 1 validation error for InitializationOptions\ndiagnostics\n value is not a valid dict (type=type_error.dict)",
33+
}
34+
]
35+
36+
assert_that(actual, is_(expected))

0 commit comments

Comments
 (0)