-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #717 from alcarney/fix-validation
lsp: Clamp `Position` values to the valid range
- Loading branch information
Showing
6 changed files
with
102 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix ``ValueError`` thrown when the server is given invalid ``Position`` data. | ||
Invalid values will be clamped to the range permitted by the language server protocol (``[0, 2147483647]``) |
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
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,34 @@ | ||
import attrs | ||
import pytest | ||
from lsprotocol import types | ||
from pytest_lsp import LanguageClient | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_code_actions_invalid_params(client: LanguageClient): | ||
"""Ensure that the server can handle invalid code actions data.""" | ||
|
||
with attrs.validators.disabled(): | ||
params = types.CodeActionParams( | ||
text_document=types.TextDocumentIdentifier( | ||
uri=client.root_uri + "/test.rst" | ||
), | ||
range=types.Range( | ||
start=types.Position(line=1, character=0), | ||
end=types.Position(line=1, character=5), | ||
), | ||
context=types.CodeActionContext( | ||
diagnostics=[ | ||
types.Diagnostic( | ||
message="I am an invalid diagnostic", | ||
range=types.Range( | ||
start=types.Position(line=1, character=0), | ||
end=types.Position(line=1, character=int(1e100)), | ||
), | ||
) | ||
] | ||
), | ||
) | ||
|
||
results = await client.text_document_code_action_async(params) | ||
assert results == [] |
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,51 @@ | ||
import pytest | ||
from lsprotocol import types | ||
|
||
from esbonio.cli import esbonio_converter | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"data, expected", | ||
[ | ||
(dict(line=None, character=0), types.Position(line=0, character=0)), | ||
(dict(line=-1, character=0), types.Position(line=0, character=0)), | ||
( | ||
dict(line=int(1e100), character=0), | ||
types.Position(line=2147483647, character=0), | ||
), | ||
(dict(line=1, character=-2), types.Position(line=1, character=0)), | ||
(dict(line=1, character=None), types.Position(line=1, character=0)), | ||
( | ||
dict(line=1, character=int(1e100)), | ||
types.Position(line=1, character=2147483647), | ||
), | ||
( | ||
dict( | ||
diagnostics=[ | ||
dict( | ||
message="Example message", | ||
range=dict( | ||
start=dict(line=1, character=0), | ||
end=dict(line=1, character=int(1e100)), | ||
), | ||
) | ||
] | ||
), | ||
types.CodeActionContext( | ||
diagnostics=[ | ||
types.Diagnostic( | ||
message="Example message", | ||
range=types.Range( | ||
start=types.Position(line=1, character=0), | ||
end=types.Position(line=1, character=2147483647), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
], | ||
) | ||
def test_parse_invalid_data(data, expected): | ||
"""Ensure that we can handle invalid data as gracefully as possible.""" | ||
converter = esbonio_converter() | ||
assert converter.structure(data, type(expected)) == expected |