1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
from pylsp_rope import commands, plugin, typing
from pylsp_rope.text import Range
from test.conftest import create_document
from test.helpers import (
assert_text_edits,
assert_single_document_edit,
)
def test_organize_import(config, workspace, document, code_action_context):
document = create_document(workspace, "redundant_import.py")
line = 1
start_col = 0
end_col = 0
selection = Range((line, start_col), (line, end_col))
response = plugin.pylsp_code_actions(
config=config,
workspace=workspace,
document=document,
range=selection,
context=code_action_context,
)
expected: typing.CodeAction = {
"title": "Organize import",
"kind": "source.organizeImports",
"command": {
"title": "Organize import",
"command": commands.COMMAND_SOURCE_ORGANIZE_IMPORT,
"arguments": [
{
"document_uri": document.uri,
}
],
},
}
assert expected in response
assert expected["command"] is not None
command = expected["command"]["command"]
arguments = expected["command"]["arguments"]
response = plugin.pylsp_execute_command(
config=config,
workspace=workspace,
command=command,
arguments=arguments,
)
edit_request = workspace._endpoint.request.call_args
document_edits = assert_single_document_edit(edit_request, document)
new_text = assert_text_edits(document_edits, target="simple.py")
assert document.source.count("import sys") == 2
assert new_text.count("import sys") == 1
|