From e6b1854b9c589e8411f9afa78d8450cc34748a5e Mon Sep 17 00:00:00 2001 From: Alex Carney Date: Mon, 4 Nov 2024 20:15:01 +0000 Subject: [PATCH] pytest-lsp: Align test cases to changes in upstream pygls --- .../tests/examples/generic-rpc/server.py | 7 +++++++ .../tests/examples/generic-rpc/t_server.py | 3 ++- lib/pytest-lsp/tests/servers/invalid_json.py | 6 +++++- lib/pytest-lsp/tests/test_examples.py | 2 +- lib/pytest-lsp/tests/test_plugin.py | 14 +++++--------- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/pytest-lsp/tests/examples/generic-rpc/server.py b/lib/pytest-lsp/tests/examples/generic-rpc/server.py index 20cc043..d946d2a 100644 --- a/lib/pytest-lsp/tests/examples/generic-rpc/server.py +++ b/lib/pytest-lsp/tests/examples/generic-rpc/server.py @@ -1,3 +1,5 @@ +import sys + from pygls.protocol import JsonRPCProtocol, default_converter from pygls.server import JsonRPCServer @@ -28,5 +30,10 @@ def subtraction(ls: JsonRPCServer, params): return dict(total=b - a) +@server.feature("server/exit") +def server_exit(ls: JsonRPCServer, params): + sys.exit(0) + + if __name__ == "__main__": server.start_io() diff --git a/lib/pytest-lsp/tests/examples/generic-rpc/t_server.py b/lib/pytest-lsp/tests/examples/generic-rpc/t_server.py index 38b6e3c..519c2c2 100644 --- a/lib/pytest-lsp/tests/examples/generic-rpc/t_server.py +++ b/lib/pytest-lsp/tests/examples/generic-rpc/t_server.py @@ -27,7 +27,8 @@ async def client(rpc_client: JsonRPCClient): yield - # Teardown code here (if any) + # Teardown code here + rpc_client.protocol.notify("server/exit", {}) @pytest.mark.asyncio diff --git a/lib/pytest-lsp/tests/servers/invalid_json.py b/lib/pytest-lsp/tests/servers/invalid_json.py index 149394c..857ee6c 100644 --- a/lib/pytest-lsp/tests/servers/invalid_json.py +++ b/lib/pytest-lsp/tests/servers/invalid_json.py @@ -1,7 +1,9 @@ # A server that returns a message that cannot be parsed as JSON. import json +import sys from lsprotocol import types +from pygls.io_ import StdoutWriter from pygls.lsp.server import LanguageServer server = LanguageServer(name="completion-exit-server", version="v1.0") @@ -20,12 +22,14 @@ def bad_send_data(data): f"Content-Type: {self.CONTENT_TYPE}; charset={self.CHARSET}\r\n\r\n" ).encode(self.CHARSET) - self.transport.write(header + body) + self.writer.write(header + body) @server.feature(types.TEXT_DOCUMENT_COMPLETION) def on_complete(server: LanguageServer, params: types.CompletionParams): server.protocol._send_data = bad_send_data + server.protocol.set_writer(StdoutWriter(sys.stdout.buffer)) + return [types.CompletionItem(label="item-one")] diff --git a/lib/pytest-lsp/tests/test_examples.py b/lib/pytest-lsp/tests/test_examples.py index 5e5055d..21ee1d1 100644 --- a/lib/pytest-lsp/tests/test_examples.py +++ b/lib/pytest-lsp/tests/test_examples.py @@ -116,7 +116,7 @@ def test_getting_started_fail(pytester: pytest.Pytester): results = pytester.runpytest() results.assert_outcomes(errors=1) - message = r"E\s+RuntimeError: Server process \d+ exited with code: 0" + message = r"E\s+RuntimeError: Server process \d+ exited with return code: 0" results.stdout.re_match_lines(message) diff --git a/lib/pytest-lsp/tests/test_plugin.py b/lib/pytest-lsp/tests/test_plugin.py index 69a1f8e..b3960ee 100644 --- a/lib/pytest-lsp/tests/test_plugin.py +++ b/lib/pytest-lsp/tests/test_plugin.py @@ -130,7 +130,7 @@ async def test_capabilities(client): results.assert_outcomes(errors=1) - message = r"E\s+RuntimeError: Server process \d+ exited with code: 0" + message = r"E\s+RuntimeError: Server process \d+ exited with return code: 0" results.stdout.re_match_lines(message) @@ -162,7 +162,7 @@ async def test_capabilities(client): results.assert_outcomes(failed=1, errors=1) - message = r"E\s+RuntimeError: Server process \d+ exited with code: 0" + message = r"E\s+RuntimeError: Server process \d+ exited with return code: 0" results.stdout.re_match_lines(message) results.stdout.fnmatch_lines("E*RuntimeError: Client has been stopped.") @@ -183,7 +183,7 @@ async def test_capabilities(client): results.assert_outcomes(errors=1) - message = r"E\s+RuntimeError: Server process \d+ exited with code: 1" + message = r"E\s+RuntimeError: Server process \d+ exited with return code: 1" results.stdout.re_match_lines(message) results.stdout.fnmatch_lines("ZeroDivisionError: division by zero") @@ -215,11 +215,7 @@ async def test_capabilities(client): setup_test(pytester, "invalid_json.py", test_code) results = pytester.runpytest("-vv") - results.assert_outcomes(errors=1, failed=1) - - if sys.version_info < (3, 9): - message = "E*CancelledError" - else: - message = "E*asyncio.exceptions.CancelledError: JsonRpcInternalError: *" + results.assert_outcomes(failed=1) + message = "E*json.decoder.JSONDecodeError: *" results.stdout.fnmatch_lines(message)