Skip to content

Commit d4fb276

Browse files
committed
simplify test
1 parent b97c096 commit d4fb276

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

pylsp/plugins/rope_autoimport.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ def _sort_import(score: int) -> str:
234234
return "[z" + str(score).rjust(_score_pow, "0")
235235

236236

237+
def get_name_or_module(document, diagnostic) -> str:
238+
return (
239+
parso.parse(document.lines[diagnostic["range"]["start"]["line"]])
240+
.get_leaf_for_position((1, diagnostic["range"]["start"]["character"] + 1))
241+
.value
242+
)
243+
244+
237245
@hookimpl
238246
def pylsp_code_actions(
239247
config: Config,
@@ -267,10 +275,8 @@ def pylsp_code_actions(
267275
for diagnostic in context.get("diagnostics", []):
268276
if "undefined name" not in diagnostic.get("message", "").lower():
269277
continue
270-
expr = parso.parse(document.lines[diagnostic["range"]["start"]["line"]])
271-
word = expr.get_leaf_for_position(
272-
(1, diagnostic["range"]["start"]["character"] + 1)
273-
).value
278+
279+
word = get_name_or_module(document, diagnostic)
274280
log.debug(f"autoimport: searching for word: {word}")
275281
rope_config = config.settings(document_path=document.path).get("rope", {})
276282
autoimport = workspace._rope_autoimport(rope_config, feature="code_actions")

test/plugins/test_autoimport.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99

1010
from pylsp import lsp, uris
1111
from pylsp.config.config import Config
12-
from pylsp.plugins.rope_autoimport import _get_score, _should_insert, get_names
12+
from pylsp.plugins.rope_autoimport import (
13+
_get_score,
14+
_should_insert,
15+
get_name_or_module,
16+
get_names,
17+
)
1318
from pylsp.plugins.rope_autoimport import (
1419
pylsp_completions as pylsp_autoimport_completions,
15-
pylsp_code_actions as pylsp_autoimport_code_actions,
1620
)
1721
from pylsp.plugins.rope_autoimport import pylsp_initialize
1822
from pylsp.workspace import Workspace
@@ -227,26 +231,20 @@ class sfa:
227231
"message",
228232
["Undefined name `os`", "F821 undefined name 'numpy'", "undefined name 'numpy'"],
229233
)
230-
def test_autoimport_code_actions(config, autoimport_workspace, message):
231-
source = "os"
234+
def test_autoimport_code_actions_get_correct_module_name(autoimport_workspace, message):
235+
source = "os.path.join('a', 'b')"
232236
autoimport_workspace.put_document(DOC_URI, source=source)
233237
doc = autoimport_workspace.get_document(DOC_URI)
234-
context = {
235-
"diagnostics": [
236-
{
237-
"range": {
238-
"start": {"line": 0, "character": 0},
239-
"end": {"line": 0, "character": 2},
240-
},
241-
"message": message,
242-
}
243-
]
238+
diagnostic = {
239+
"range": {
240+
"start": {"line": 0, "character": 0},
241+
"end": {"line": 0, "character": 2},
242+
},
243+
"message": message,
244244
}
245-
actions = pylsp_autoimport_code_actions(
246-
config, autoimport_workspace, doc, None, context
247-
)
245+
module_name = get_name_or_module(doc, diagnostic)
248246
autoimport_workspace.rm_document(DOC_URI)
249-
assert any(action.get("title") == "import os" for action in actions)
247+
assert module_name == "os"
250248

251249

252250
# rope autoimport launches a sqlite database which checks from which thread it is called.

0 commit comments

Comments
 (0)