Debian Codegraph @

test/test_plugin.py

raw
1 import collections
2 import os
3 import re
4 import sys
5 from pathlib import Path
6 from unittest.mock import Mock, patch
7
8 import pytest
9 from mypy import api as mypy_api
10 from pylsp import _utils, uris
11 from pylsp.config.config import Config
12 from pylsp.workspace import Document, Workspace
13
14 from pylsp_mypy import plugin
15
16 # TODO using these file as a document is a bad idea as tests can break by adding new tests
17 DOC_URI = f"file:/{Path(__file__)}"
18 DOC_TYPE_ERR = """{}.append(3)
19 """
20
21 # Mypy 1.7 changed <nothing> into "Never", so make this a regex to be compatible
22 # with multiple versions of mypy
23 TYPE_ERR_MSG_REGEX = (
24 r'"dict\[(?:(?:<nothing>)|(?:Never)), (?:(?:<nothing>)|(?:Never))\]" has no attribute "append"'
25 )
26
27 TEST_LINE = 'test_plugin.py:279:8:279:16: error: "Request" has no attribute "id" [attr-defined]'
28 TEST_LINE_NOTE = (
29 'test_plugin.py:124:1:129:77: note: Use "-> None" if function does not return a value'
30 )
31
32
33 @pytest.fixture
34 def last_diagnostics_monkeypatch(monkeypatch):
35 # gets called before every test altering last_diagnostics in order to reset it
36 monkeypatch.setattr(plugin, "last_diagnostics", collections.defaultdict(list))
37 return monkeypatch
38
39
40 @pytest.fixture
41 def workspace(tmpdir):
42 """Return a workspace."""
43 ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
44 ws._config = Config(ws.root_uri, {}, 0, {})
45 return ws
46
47
48 class FakeConfig(object):
49 def __init__(self, path):
50 self._root_path = path
51
52 def plugin_settings(self, plugin, document_path=None):
53 return {}
54
55
56 def test_settings(tmpdir):
57 config = Config(uris.from_fs_path(str(tmpdir)), {}, 0, {})
58 settings = plugin.pylsp_settings(config)
59 assert settings == {"plugins": {"pylsp_mypy": {}}}
60
61
62 def test_plugin(workspace, last_diagnostics_monkeypatch):
63 doc = Document(DOC_URI, workspace, DOC_TYPE_ERR)
64 plugin.pylsp_settings(workspace._config)
65 diags = plugin.pylsp_lint(workspace._config, workspace, doc, is_saved=False)
66
67 assert len(diags) == 1
68 diag = diags[0]
69 assert re.fullmatch(TYPE_ERR_MSG_REGEX, diag["message"])
70 assert diag["range"]["start"] == {"line": 0, "character": 0}
71 # Running mypy in 3.7 produces wrong error ends this can be removed when 3.7 reaches EOL
72 if sys.version_info < (3, 8):
73 assert diag["range"]["end"] == {"line": 0, "character": 1}
74 else:
75 assert diag["range"]["end"] == {"line": 0, "character": 9}
76 assert diag["severity"] == 1
77 assert diag["code"] == "attr-defined"
78
79
80 @pytest.mark.parametrize("doc_source", ["a = 1\nb\n", "a = 1\r\nb\r\n", "a = 1\rb\r"])
81 def test_handling_of_line_endings(workspace, last_diagnostics_monkeypatch, doc_source):
82 # setup
83 doc = Document(DOC_URI, workspace, doc_source)
84 plugin.pylsp_settings(workspace._config)
85
86 # run
87 diags = plugin.pylsp_lint(workspace._config, workspace, doc, is_saved=False)
88
89 # assert
90 undefined_name_diags = list(filter(lambda diag: diag["code"] == "name-defined", diags))
91 assert len(undefined_name_diags) == 1
92 diag = undefined_name_diags[0]
93 assert diag["message"] == 'Name "b" is not defined'
94 assert diag["range"]["start"] == {"line": 1, "character": 0}
95 assert diag["range"]["end"] == {"line": 1, "character": 1}
96
97
98 def test_parse_full_line(workspace):
99 diag = plugin.parse_line(TEST_LINE) # TODO parse a document here
100 assert diag["message"] == '"Request" has no attribute "id"'
101 assert diag["range"]["start"] == {"line": 278, "character": 7}
102 assert diag["range"]["end"] == {"line": 278, "character": 16}
103 assert diag["severity"] == 1
104 assert diag["code"] == "attr-defined"
105
106
107 def test_parse_note_line(workspace):
108 diag = plugin.parse_line(TEST_LINE_NOTE)
109 assert diag["message"] == 'Use "-> None" if function does not return a value'
110 assert diag["range"]["start"] == {"line": 123, "character": 0}
111 assert diag["range"]["end"] == {"line": 128, "character": 77}
112 assert diag["severity"] == 3
113 assert "code" not in diag
114
115
116 def test_multiple_workspaces(tmpdir, last_diagnostics_monkeypatch):
117 DOC_SOURCE = """
118 def foo():
119 return
120 unreachable = 1
121 """
122 DOC_ERR_MSG = "Statement is unreachable"
123
124 # Initialize two workspace folders.
125 folder1 = tmpdir.mkdir("folder1")
126 folder2 = tmpdir.mkdir("folder2")
127
128 # Create configuration file for workspace folder 1.
129 mypy_config = folder1.join("mypy.ini")
130 mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")
131
132 ws1 = Workspace(uris.from_fs_path(str(folder1)), Mock())
133 ws1._config = Config(ws1.root_uri, {}, 0, {})
134 ws2 = Workspace(uris.from_fs_path(str(folder2)), Mock())
135 ws2._config = Config(ws2.root_uri, {}, 0, {})
136
137 # Initialize settings for both folders.
138 plugin.pylsp_settings(ws1._config)
139 plugin.pylsp_settings(ws2._config)
140
141 # Test document in workspace 1 (uses mypy.ini configuration).
142 doc1 = Document(DOC_URI, ws1, DOC_SOURCE)
143 diags = plugin.pylsp_lint(ws1._config, ws1, doc1, is_saved=False)
144 assert len(diags) == 1
145 diag = diags[0]
146 assert diag["message"] == DOC_ERR_MSG
147 assert diag["code"] == "unreachable"
148
149 # Test document in workspace 2 (without mypy.ini configuration)
150 doc2 = Document(DOC_URI, ws2, DOC_SOURCE)
151 diags = plugin.pylsp_lint(ws2._config, ws2, doc2, is_saved=False)
152 assert len(diags) == 0
153
154
155 def test_apply_overrides():
156 assert plugin.apply_overrides(["1", "2"], []) == []
157 assert plugin.apply_overrides(["1", "2"], ["a"]) == ["a"]
158 assert plugin.apply_overrides(["1", "2"], ["a", True]) == ["a", "1", "2"]
159 assert plugin.apply_overrides(["1", "2"], [True, "a"]) == ["1", "2", "a"]
160 assert plugin.apply_overrides(["1"], ["a", True, "b"]) == ["a", "1", "b"]
161
162
163 @pytest.mark.skipif(os.name == "nt", reason="Not working on Windows due to test design.")
164 def test_option_overrides(tmpdir, last_diagnostics_monkeypatch, workspace):
165 import sys
166 from stat import S_IRWXU
167 from textwrap import dedent
168
169 sentinel = tmpdir / "ran"
170
171 source = dedent("""\
172 #!{}
173 import os, sys, pathlib
174 pathlib.Path({!r}).touch()
175 os.execv({!r}, sys.argv)
176 """).format(sys.executable, str(sentinel), sys.executable)
177
178 wrapper = tmpdir / "bin/wrapper"
179 wrapper.write(source, ensure=True)
180 wrapper.chmod(S_IRWXU)
181
182 overrides = ["--python-executable", wrapper.strpath, True]
183 last_diagnostics_monkeypatch.setattr(
184 FakeConfig,
185 "plugin_settings",
186 lambda _, p: {"overrides": overrides} if p == "pylsp_mypy" else {},
187 )
188
189 config = FakeConfig(uris.to_fs_path(workspace.root_uri))
190 plugin.pylsp_settings(config)
191
192 assert not sentinel.exists()
193
194 diags = plugin.pylsp_lint(
195 config=config,
196 workspace=workspace,
197 document=Document(DOC_URI, workspace, DOC_TYPE_ERR),
198 is_saved=False,
199 )
200 assert len(diags) == 1
201 assert sentinel.exists()
202
203
204 def test_option_overrides_dmypy(last_diagnostics_monkeypatch, workspace):
205 overrides = ["--python-executable", "/tmp/fake", True]
206 last_diagnostics_monkeypatch.setattr(
207 FakeConfig,
208 "plugin_settings",
209 lambda _, p: (
210 {
211 "overrides": overrides,
212 "dmypy": True,
213 "live_mode": False,
214 }
215 if p == "pylsp_mypy"
216 else {}
217 ),
218 )
219
220 m = Mock(wraps=lambda a, **_: Mock(returncode=0, **{"stdout": ""}))
221 last_diagnostics_monkeypatch.setattr(plugin.subprocess, "run", m)
222
223 document = Document(DOC_URI, workspace, DOC_TYPE_ERR)
224
225 config = FakeConfig(uris.to_fs_path(workspace.root_uri))
226 plugin.pylsp_settings(config)
227
228 plugin.pylsp_lint(
229 config=config,
230 workspace=workspace,
231 document=document,
232 is_saved=False,
233 )
234 expected = [
235 "dmypy",
236 "--status-file",
237 ".dmypy.json",
238 "run",
239 "--",
240 "--python-executable",
241 "/tmp/fake",
242 "--show-error-end",
243 "--no-error-summary",
244 "--no-pretty",
245 document.path,
246 ]
247 m.assert_called_with(expected, capture_output=True, **plugin.windows_flag, encoding="utf-8")
248
249
250 def test_dmypy_status_file(tmpdir, last_diagnostics_monkeypatch, workspace):
251 statusFile = tmpdir / ".custom_dmypy_status_file.json"
252
253 last_diagnostics_monkeypatch.setattr(
254 FakeConfig,
255 "plugin_settings",
256 lambda _, p: (
257 {
258 "dmypy": True,
259 "live_mode": False,
260 "dmypy_status_file": str(statusFile),
261 }
262 if p == "pylsp_mypy"
263 else {}
264 ),
265 )
266
267 document = Document(DOC_URI, workspace, DOC_TYPE_ERR)
268
269 config = FakeConfig(uris.to_fs_path(workspace.root_uri))
270 plugin.pylsp_settings(config)
271
272 assert not statusFile.exists()
273
274 try:
275 plugin.pylsp_lint(
276 config=config,
277 workspace=workspace,
278 document=document,
279 is_saved=False,
280 )
281
282 assert statusFile.exists()
283 finally:
284 mypy_api.run_dmypy(["--status-file", str(statusFile), "stop"])
285
286
287 def test_config_sub_paths(tmpdir, last_diagnostics_monkeypatch):
288 DOC_SOURCE = """
289 def foo():
290 return
291 unreachable = 1
292 """
293 DOC_ERR_MSG = "Statement is unreachable"
294
295 config_sub_paths = [".config"]
296
297 # Create configuration file for workspace.
298 plugin_config = tmpdir.join("pyproject.toml")
299 plugin_config.write(f"[tool.pylsp-mypy]\nenabled = true\nconfig_sub_paths = {config_sub_paths}")
300 config_dir = tmpdir.mkdir(".config")
301 mypy_config = config_dir.join("mypy.ini")
302 mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")
303
304 # Initialize workspace.
305
306 ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
307 ws._config = Config(ws.root_uri, {}, 0, {})
308
309 # Update settings for workspace.
310 settings = plugin.pylsp_settings(ws._config)
311 ws._config._plugin_settings = _utils.merge_dicts(ws._config._plugin_settings, settings)
312
313 # Test document to make sure it uses .config/mypy.ini configuration.
314 doc = Document(DOC_URI, ws, DOC_SOURCE)
315 diags = plugin.pylsp_lint(ws._config, ws, doc, is_saved=False)
316 assert len(diags) == 1
317 diag = diags[0]
318 assert diag["message"] == DOC_ERR_MSG
319 assert diag["code"] == "unreachable"
320
321
322 def test_config_sub_paths_config_changed(tmpdir, last_diagnostics_monkeypatch):
323 DOC_SOURCE = """
324 def foo():
325 return
326 unreachable = 1
327 """
328 DOC_ERR_MSG = "Statement is unreachable"
329
330 # Create configuration file for workspace.
331 config_dir = tmpdir.mkdir(".config")
332 mypy_config = config_dir.join("mypy.ini")
333 mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")
334
335 config_sub_paths = [".config"]
336
337 # Initialize workspace.
338 ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
339 ws._config = Config(ws.root_uri, {}, 0, {})
340
341 # Update settings for workspace.
342 plugin.pylsp_settings(ws._config)
343 ws.update_config({"pylsp": {"plugins": {"pylsp_mypy": {"config_sub_paths": config_sub_paths}}}})
344
345 # Test document to make sure it uses .config/mypy.ini configuration.
346 doc = Document(DOC_URI, ws, DOC_SOURCE)
347 diags = plugin.pylsp_lint(ws._config, ws, doc, is_saved=False)
348 assert len(diags) == 1
349 diag = diags[0]
350 assert diag["message"] == DOC_ERR_MSG
351 assert diag["code"] == "unreachable"
352
353
354 @pytest.mark.parametrize(
355 "document_path,pattern,os_sep,pattern_matched",
356 (
357 ("/workspace/my-file.py", "/someting-else", "/", False),
358 ("/workspace/my-file.py", "^/workspace$", "/", False),
359 ("/workspace/my-file.py", "/workspace", "/", True),
360 ("/workspace/my-file.py", "^/workspace(.*)$", "/", True),
361 # This is a broken regex (missing ')'), but should not choke
362 ("/workspace/my-file.py", "/((workspace)", "/", False),
363 # Windows paths are tricky with all those \\ and unintended escape,
364 # characters but they should 'just' work
365 ("d:\\a\\my-file.py", "/a", "\\", True),
366 (
367 "d:\\a\\pylsp-mypy\\pylsp-mypy\\test\\test_plugin.py",
368 "/a/pylsp-mypy/pylsp-mypy/test/test_plugin.py",
369 "\\",
370 True,
371 ),
372 ),
373 )
374 def test_match_exclude_patterns(document_path, pattern, os_sep, pattern_matched):
375 with patch("os.sep", new=os_sep):
376 assert (
377 plugin.match_exclude_patterns(document_path=document_path, exclude_patterns=[pattern])
378 is pattern_matched
379 )
380
381
382 def test_config_exclude(tmpdir, workspace):
383 """When exclude is set in config then mypy should not run for that file."""
384 doc = Document(DOC_URI, workspace, DOC_TYPE_ERR)
385
386 plugin.pylsp_settings(workspace._config)
387 workspace.update_config({"pylsp": {"plugins": {"pylsp_mypy": {}}}})
388 diags = plugin.pylsp_lint(workspace._config, workspace, doc, is_saved=False)
389 assert re.search(TYPE_ERR_MSG_REGEX, diags[0]["message"])
390
391 # Add the path of our document to the exclude patterns
392 exclude_path = doc.path.replace(os.sep, "/")
393 workspace.update_config({"pylsp": {"plugins": {"pylsp_mypy": {"exclude": [exclude_path]}}}})
394 diags = plugin.pylsp_lint(workspace._config, workspace, doc, is_saved=False)
395 assert diags == []
396
397
398 @pytest.mark.parametrize(
399 ("command", "settings", "cmd_on_path", "environmentVariableSet", "expected"),
400 [
401 ("mypy", {}, ["/bin/mypy"], True, ["mypy"]),
402 ("mypy", {}, None, True, []),
403 ("mypy", {"mypy_command": ["/path/to/mypy"]}, "/bin/mypy", True, ["/path/to/mypy"]),
404 ("mypy", {"mypy_command": ["/path/to/mypy"]}, None, True, ["/path/to/mypy"]),
405 ("dmypy", {}, "/bin/dmypy", True, ["dmypy"]),
406 ("dmypy", {}, None, True, []),
407 ("dmypy", {"dmypy_command": ["/path/to/dmypy"]}, "/bin/dmypy", True, ["/path/to/dmypy"]),
408 ("dmypy", {"dmypy_command": ["/path/to/dmypy"]}, None, True, ["/path/to/dmypy"]),
409 ("mypy", {}, ["/bin/mypy"], False, ["mypy"]),
410 ("mypy", {}, None, False, []),
411 ("mypy", {"mypy_command": ["/path/to/mypy"]}, "/bin/mypy", False, ["mypy"]),
412 ("mypy", {"mypy_command": ["/path/to/mypy"]}, None, False, []),
413 ("dmypy", {}, "/bin/dmypy", False, ["dmypy"]),
414 ("dmypy", {}, None, False, []),
415 ("dmypy", {"dmypy_command": ["/path/to/dmypy"]}, "/bin/dmypy", False, ["dmypy"]),
416 ("dmypy", {"dmypy_command": ["/path/to/dmypy"]}, None, False, []),
417 ],
418 )
419 def test_get_cmd(command, settings, cmd_on_path, environmentVariableSet: bool, expected):
420 with patch("shutil.which", return_value=cmd_on_path):
421 if environmentVariableSet:
422 os.environ["PYLSP_MYPY_ALLOW_DANGEROUS_CODE_EXECUTION"] = "Does not matter at all"
423 else:
424 os.environ.pop("PYLSP_MYPY_ALLOW_DANGEROUS_CODE_EXECUTION", None)
425 assert plugin.get_cmd(settings, command) == expected
426
427
428 def test_config_overrides_mypy_command(last_diagnostics_monkeypatch, workspace):
429 last_diagnostics_monkeypatch.setattr(
430 FakeConfig,
431 "plugin_settings",
432 lambda _, p: (
433 {
434 "mypy_command": ["/path/to/mypy"],
435 }
436 if p == "pylsp_mypy"
437 else {}
438 ),
439 )
440
441 m = Mock(wraps=lambda a, **_: Mock(returncode=0, **{"stdout": ""}))
442 last_diagnostics_monkeypatch.setattr(plugin.subprocess, "run", m)
443
444 document = Document(DOC_URI, workspace, DOC_TYPE_ERR)
445
446 config = FakeConfig(uris.to_fs_path(workspace.root_uri))
447 os.environ["PYLSP_MYPY_ALLOW_DANGEROUS_CODE_EXECUTION"] = "Does not matter at all"
448 plugin.pylsp_settings(config)
449
450 plugin.pylsp_lint(
451 config=config,
452 workspace=workspace,
453 document=document,
454 is_saved=False,
455 )
456
457 called_argv = m.call_args.args[0]
458 called_cmd = called_argv[0]
459 assert called_cmd == "/path/to/mypy"
460
461
462 def test_config_overrides_dmypy_command(last_diagnostics_monkeypatch, workspace):
463 last_diagnostics_monkeypatch.setattr(
464 FakeConfig,
465 "plugin_settings",
466 lambda _, p: (
467 {
468 "dmypy": True,
469 "live_mode": False,
470 "dmypy_command": ["poetry", "run", "dmypy"],
471 }
472 if p == "pylsp_mypy"
473 else {}
474 ),
475 )
476
477 m = Mock(wraps=lambda a, **_: Mock(returncode=0, **{"stdout": ""}))
478 last_diagnostics_monkeypatch.setattr(plugin.subprocess, "run", m)
479
480 document = Document(DOC_URI, workspace, DOC_TYPE_ERR)
481
482 config = FakeConfig(uris.to_fs_path(workspace.root_uri))
483 os.environ["PYLSP_MYPY_ALLOW_DANGEROUS_CODE_EXECUTION"] = "Does not matter at all"
484 plugin.pylsp_settings(config)
485
486 plugin.pylsp_lint(
487 config=config,
488 workspace=workspace,
489 document=document,
490 is_saved=False,
491 )
492
493 called_argv = m.call_args.args[0]
494 called_cmd = called_argv[:3]
495 assert called_cmd == ["poetry", "run", "dmypy"]