forked from python-lsp/python-lsp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_autoimport.py
349 lines (274 loc) · 11.4 KB
/
test_autoimport.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Copyright 2022- Python Language Server Contributors.
from typing import Any, Dict, List
from unittest.mock import Mock, patch
from test.test_notebook_document import wait_for_condition
from test.test_utils import send_initialize_request, send_notebook_did_open
import jedi
import parso
import pytest
from pylsp import IS_WIN, lsp, uris
from pylsp.config.config import Config
from pylsp.plugins.rope_autoimport import (
_get_score,
_should_insert,
cache,
get_name_or_module,
get_names,
)
from pylsp.plugins.rope_autoimport import (
pylsp_completions as pylsp_autoimport_completions,
)
from pylsp.workspace import Workspace
DOC_URI = uris.from_fs_path(__file__)
def contains_autoimport_completion(suggestion: Dict[str, Any], module: str) -> bool:
"""Checks if `suggestion` contains an autoimport completion for `module`."""
return suggestion.get("label", "") == module and "import" in suggestion.get(
"detail", ""
)
def contains_autoimport_quickfix(suggestion: Dict[str, Any], module: str) -> bool:
"""Checks if `suggestion` contains an autoimport quick fix for `module`."""
return suggestion.get("title", "") == f"import {module}"
@pytest.fixture(scope="session")
def autoimport_workspace(tmp_path_factory) -> Workspace:
"Special autoimport workspace. Persists across sessions to make in-memory sqlite3 database fast."
workspace = Workspace(
uris.from_fs_path(str(tmp_path_factory.mktemp("pylsp"))), Mock()
)
workspace._config = Config(workspace.root_uri, {}, 0, {})
workspace._config.update(
{
"rope_autoimport": {
"memory": True,
"enabled": True,
"completions": {"enabled": True},
"code_actions": {"enabled": True},
}
}
)
cache.reload_cache(workspace._config, workspace, single_thread=True)
yield workspace
workspace.close()
# pylint: disable=redefined-outer-name
@pytest.fixture
def completions(config: Config, autoimport_workspace: Workspace, request):
document, position = request.param
com_position = {"line": 0, "character": position}
autoimport_workspace.put_document(DOC_URI, source=document)
doc = autoimport_workspace.get_document(DOC_URI)
yield pylsp_autoimport_completions(
config, autoimport_workspace, doc, com_position, None
)
autoimport_workspace.rm_document(DOC_URI)
def should_insert(phrase: str, position: int):
expr = parso.parse(phrase)
word_node = expr.get_leaf_for_position((1, position))
return _should_insert(expr, word_node)
def check_dict(query: Dict, results: List[Dict]) -> bool:
for result in results:
if all(result[key] == query[key] for key in query.keys()):
return True
return False
@pytest.mark.parametrize("completions", [("""pathli """, 6)], indirect=True)
def test_autoimport_completion(completions):
assert completions
assert check_dict(
{"label": "pathlib", "kind": lsp.CompletionItemKind.Module}, completions
)
@pytest.mark.parametrize("completions", [("""import """, 7)], indirect=True)
def test_autoimport_import(completions):
assert len(completions) == 0
@pytest.mark.parametrize("completions", [("""pathlib""", 2)], indirect=True)
def test_autoimport_pathlib(completions):
assert completions[0]["label"] == "pathlib"
start = {"line": 0, "character": 0}
edit_range = {"start": start, "end": start}
assert completions[0]["additionalTextEdits"] == [
{"range": edit_range, "newText": "import pathlib\n"}
]
@pytest.mark.parametrize("completions", [("""import test\n""", 10)], indirect=True)
def test_autoimport_import_with_name(completions):
assert len(completions) == 0
@pytest.mark.parametrize("completions", [("""def func(s""", 10)], indirect=True)
def test_autoimport_function(completions):
assert len(completions) == 0
@pytest.mark.parametrize("completions", [("""class Test""", 10)], indirect=True)
def test_autoimport_class(completions):
assert len(completions) == 0
@pytest.mark.parametrize("completions", [("""\n""", 0)], indirect=True)
def test_autoimport_empty_line(completions):
assert len(completions) == 0
@pytest.mark.parametrize(
"completions", [("""class Test(NamedTupl):""", 20)], indirect=True
)
def test_autoimport_class_complete(completions):
assert len(completions) > 0
@pytest.mark.parametrize(
"completions", [("""class Test(NamedTupl""", 20)], indirect=True
)
def test_autoimport_class_incomplete(completions):
assert len(completions) > 0
@pytest.mark.parametrize("completions", [("""def func(s:Lis""", 12)], indirect=True)
def test_autoimport_function_typing(completions):
assert len(completions) > 0
assert check_dict({"label": "List"}, completions)
@pytest.mark.parametrize(
"completions", [("""def func(s : Lis ):""", 16)], indirect=True
)
def test_autoimport_function_typing_complete(completions):
assert len(completions) > 0
assert check_dict({"label": "List"}, completions)
@pytest.mark.parametrize(
"completions", [("""def func(s : Lis ) -> Generat:""", 29)], indirect=True
)
def test_autoimport_function_typing_return(completions):
assert len(completions) > 0
assert check_dict({"label": "Generator"}, completions)
def test_autoimport_defined_name(config, workspace):
document = """List = "hi"\nLis"""
com_position = {"line": 1, "character": 3}
workspace.put_document(DOC_URI, source=document)
doc = workspace.get_document(DOC_URI)
completions = pylsp_autoimport_completions(
config, workspace, doc, com_position, None
)
workspace.rm_document(DOC_URI)
assert not check_dict({"label": "List"}, completions)
class TestShouldInsert:
def test_dot(self):
assert not should_insert("""str.""", 4)
def test_dot_partial(self):
assert not should_insert("""str.metho\n""", 9)
def test_comment(self):
assert not should_insert("""#""", 1)
def test_comment_indent(self):
assert not should_insert(""" # """, 5)
def test_from(self):
assert not should_insert("""from """, 5)
assert should_insert("""from """, 4)
def test_sort_sources():
result1 = _get_score(1, "import pathlib", "pathlib", "pathli")
result2 = _get_score(2, "import pathlib", "pathlib", "pathli")
assert result1 < result2
def test_sort_statements():
result1 = _get_score(
2, "from importlib_metadata import pathlib", "pathlib", "pathli"
)
result2 = _get_score(2, "import pathlib", "pathlib", "pathli")
assert result1 > result2
def test_sort_both():
result1 = _get_score(
3, "from importlib_metadata import pathlib", "pathlib", "pathli"
)
result2 = _get_score(2, "import pathlib", "pathlib", "pathli")
assert result1 > result2
def test_get_names():
source = """
from a import s as e
import blah, bleh
hello = "str"
a, b = 1, 2
def someone():
soemthing
class sfa:
sfiosifo
"""
results = get_names(jedi.Script(code=source))
assert results == set(["blah", "bleh", "e", "hello", "someone", "sfa", "a", "b"])
# Tests ruff, flake8 and pyflakes messages
@pytest.mark.parametrize(
"message",
["Undefined name `os`", "F821 undefined name 'numpy'", "undefined name 'numpy'"],
)
def test_autoimport_code_actions_get_correct_module_name(autoimport_workspace, message):
source = "os.path.join('a', 'b')"
autoimport_workspace.put_document(DOC_URI, source=source)
doc = autoimport_workspace.get_document(DOC_URI)
diagnostic = {
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 0, "character": 2},
},
"message": message,
}
module_name = get_name_or_module(doc, diagnostic)
autoimport_workspace.rm_document(DOC_URI)
assert module_name == "os"
def make_context(module_name, line, character_start, character_end):
return {
"diagnostics": [
{
"message": f"undefined name '{module_name}'",
"range": {
"start": {"line": line, "character": character_start},
"end": {"line": line, "character": character_end},
},
}
]
}
def position(line, character):
return {"line": line, "character": character}
@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows")
def test_autoimport_code_actions_and_completions_for_notebook_document(
client_server_pair,
):
client, server = client_server_pair
send_initialize_request(
client,
{
"pylsp": {
"plugins": {
"rope_autoimport": {
"memory": True,
"enabled": True,
"completions": {"enabled": True},
},
}
}
},
)
with patch.object(server._endpoint, "notify") as mock_notify:
# Expectations:
# 1. We receive an autoimport suggestion for "os" in the first cell because
# os is imported after that.
# 2. We don't receive an autoimport suggestion for "os" in the second cell because it's
# already imported in the second cell.
# 3. We don't receive an autoimport suggestion for "os" in the third cell because it's
# already imported in the second cell.
# 4. We receive an autoimport suggestion for "sys" because it's not already imported.
# 5. If diagnostics doesn't contain "undefined name ...", we send empty quick fix suggestions.
send_notebook_did_open(client, ["os", "import os\nos", "os", "sys"])
wait_for_condition(lambda: mock_notify.call_count >= 4)
# We received diagnostics messages for every cell
assert all(
"textDocument/publishDiagnostics" in c.args
for c in mock_notify.call_args_list
)
rope_autoimport_settings = server.workspace._config.plugin_settings(
"rope_autoimport"
)
assert rope_autoimport_settings.get("completions", {}).get("enabled", False) is True
assert rope_autoimport_settings.get("memory", False) is True
wait_for_condition(lambda: not cache.thread.is_alive())
# 1.
quick_fixes = server.code_actions("cell_1_uri", {}, make_context("os", 0, 0, 2))
assert any(s for s in quick_fixes if contains_autoimport_quickfix(s, "os"))
completions = server.completions("cell_1_uri", position(0, 2)).get("items")
assert any(s for s in completions if contains_autoimport_completion(s, "os"))
# 2.
# We don't test code actions here as in this case, there would be no code actions sent bc
# there wouldn't be a diagnostics message.
completions = server.completions("cell_2_uri", position(1, 2)).get("items")
assert not any(s for s in completions if contains_autoimport_completion(s, "os"))
# 3.
# Same as in 2.
completions = server.completions("cell_3_uri", position(0, 2)).get("items")
assert not any(s for s in completions if contains_autoimport_completion(s, "os"))
# 4.
quick_fixes = server.code_actions("cell_4_uri", {}, make_context("sys", 0, 0, 3))
assert any(s for s in quick_fixes if contains_autoimport_quickfix(s, "sys"))
completions = server.completions("cell_4_uri", position(0, 3)).get("items")
assert any(s for s in completions if contains_autoimport_completion(s, "sys"))
# 5.
context = {"diagnostics": [{"message": "A random message"}]}
quick_fixes = server.code_actions("cell_4_uri", {}, context)
assert len(quick_fixes) == 0