-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pytest-lsp: Add support for
window/workDoneProgress/create
- Loading branch information
Showing
7 changed files
with
219 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest-lsp's ``LanguageClient`` is now able to handle ``window/workDoneProgress/create`` requests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
lib/pytest-lsp/tests/examples/window-create-progress/server.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from unittest.mock import Mock | ||
|
||
from lsprotocol import types | ||
from pygls.server import LanguageServer | ||
|
||
server = LanguageServer("window-create-progress", "v1") | ||
|
||
|
||
@server.command("do.progress") | ||
async def do_progress(ls: LanguageServer, *args): | ||
token = "a-token" | ||
|
||
await ls.progress.create_async(token) | ||
|
||
# Begin | ||
ls.progress.begin( | ||
token, | ||
types.WorkDoneProgressBegin(title="Indexing", percentage=0), | ||
) | ||
# Report | ||
for i in range(1, 4): | ||
ls.progress.report( | ||
token, | ||
types.WorkDoneProgressReport(message=f"{i * 25}%", percentage=i * 25), | ||
) | ||
# End | ||
ls.progress.end(token, types.WorkDoneProgressEnd(message="Finished")) | ||
|
||
return "a result" | ||
|
||
|
||
@server.command("duplicate.progress") | ||
async def duplicate_progress(ls: LanguageServer, *args): | ||
token = "duplicate-token" | ||
|
||
# Need to stop pygls preventing us from using the progress API wrong. | ||
ls.progress._check_token_registered = Mock() | ||
await ls.progress.create_async(token) | ||
|
||
# pytest-lsp should return an error here. | ||
await ls.progress.create_async(token) | ||
|
||
|
||
@server.command("no.progress") | ||
async def no_progress(ls: LanguageServer, *args): | ||
token = "undefined-token" | ||
|
||
# Begin | ||
ls.progress.begin( | ||
token, | ||
types.WorkDoneProgressBegin(title="Indexing", percentage=0, cancellable=False), | ||
) | ||
# Report | ||
for i in range(1, 4): | ||
ls.progress.report( | ||
token, | ||
types.WorkDoneProgressReport(message=f"{i * 25}%", percentage=i * 25), | ||
) | ||
# End | ||
ls.progress.end(token, types.WorkDoneProgressEnd(message="Finished")) | ||
|
||
|
||
if __name__ == "__main__": | ||
server.start_io() |
61 changes: 61 additions & 0 deletions
61
lib/pytest-lsp/tests/examples/window-create-progress/t_server.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import sys | ||
|
||
import pytest | ||
from lsprotocol import types | ||
|
||
import pytest_lsp | ||
from pytest_lsp import ClientServerConfig | ||
from pytest_lsp import LanguageClient | ||
from pytest_lsp import LspSpecificationWarning | ||
|
||
|
||
@pytest_lsp.fixture( | ||
config=ClientServerConfig(server_command=[sys.executable, "server.py"]), | ||
) | ||
async def client(lsp_client: LanguageClient): | ||
# Setup | ||
params = types.InitializeParams(capabilities=types.ClientCapabilities()) | ||
await lsp_client.initialize_session(params) | ||
|
||
yield | ||
|
||
# Teardown | ||
await lsp_client.shutdown_session() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_progress(client: LanguageClient): | ||
result = await client.workspace_execute_command_async( | ||
params=types.ExecuteCommandParams(command="do.progress") | ||
) | ||
|
||
assert result == "a result" | ||
|
||
progress = client.progress_reports["a-token"] | ||
assert progress == [ | ||
types.WorkDoneProgressBegin(title="Indexing", percentage=0), | ||
types.WorkDoneProgressReport(message="25%", percentage=25), | ||
types.WorkDoneProgressReport(message="50%", percentage=50), | ||
types.WorkDoneProgressReport(message="75%", percentage=75), | ||
types.WorkDoneProgressEnd(message="Finished"), | ||
] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_duplicate_progress(client: LanguageClient): | ||
with pytest.warns( | ||
LspSpecificationWarning, match="Duplicate progress token: 'duplicate-token'" | ||
): | ||
await client.workspace_execute_command_async( | ||
params=types.ExecuteCommandParams(command="duplicate.progress") | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_unknown_progress(client: LanguageClient): | ||
with pytest.warns( | ||
LspSpecificationWarning, match="Unknown progress token: 'undefined-token'" | ||
): | ||
await client.workspace_execute_command_async( | ||
params=types.ExecuteCommandParams(command="no.progress") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters