Skip to content

Commit

Permalink
pytest-lsp: Align test cases to changes in upstream pygls
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed Nov 4, 2024
1 parent a37bd19 commit e6b1854
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
7 changes: 7 additions & 0 deletions lib/pytest-lsp/tests/examples/generic-rpc/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

from pygls.protocol import JsonRPCProtocol, default_converter
from pygls.server import JsonRPCServer

Expand Down Expand Up @@ -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()
3 changes: 2 additions & 1 deletion lib/pytest-lsp/tests/examples/generic-rpc/t_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/pytest-lsp/tests/servers/invalid_json.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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")]


Expand Down
2 changes: 1 addition & 1 deletion lib/pytest-lsp/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
14 changes: 5 additions & 9 deletions lib/pytest-lsp/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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.")

Expand All @@ -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")

Expand Down Expand Up @@ -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)

0 comments on commit e6b1854

Please sign in to comment.