|
3 | 3 |
|
4 | 4 | import tempfile
|
5 | 5 | import os
|
| 6 | +from textwrap import dedent |
6 | 7 | from unittest.mock import patch
|
7 | 8 | from pylsp import lsp, uris
|
8 | 9 | from pylsp.plugins import flake8_lint
|
@@ -59,6 +60,63 @@ def test_flake8_lint(workspace):
|
59 | 60 | os.remove(name)
|
60 | 61 |
|
61 | 62 |
|
| 63 | +def test_flake8_respecting_configuration(workspace): |
| 64 | + docs = [ |
| 65 | + ("src/__init__.py", ""), |
| 66 | + ("src/a.py", DOC), |
| 67 | + ("src/b.py", "import os"), |
| 68 | + ("setup.cfg", dedent(""" |
| 69 | + [flake8] |
| 70 | + ignore = E302,W191 |
| 71 | + per-file-ignores = |
| 72 | + src/a.py:F401 |
| 73 | + src/b.py:W292 |
| 74 | + """)) |
| 75 | + ] |
| 76 | + |
| 77 | + made = {} |
| 78 | + for rel, contents in docs: |
| 79 | + location = os.path.join(workspace.root_path, rel) |
| 80 | + made[rel] = {"uri": uris.from_fs_path(location)} |
| 81 | + |
| 82 | + os.makedirs(os.path.dirname(location), exist_ok=True) |
| 83 | + with open(location, "w", encoding="utf-8") as fle: |
| 84 | + fle.write(contents) |
| 85 | + |
| 86 | + workspace.put_document(made[rel]["uri"], contents) |
| 87 | + made[rel]["document"] = workspace._docs[made[rel]["uri"]] |
| 88 | + |
| 89 | + diags = flake8_lint.pylsp_lint(workspace, made["src/a.py"]["document"]) |
| 90 | + assert diags == [ |
| 91 | + { |
| 92 | + "source": "flake8", |
| 93 | + "code": "F841", |
| 94 | + "range": { |
| 95 | + "start": {"line": 5, "character": 1}, |
| 96 | + "end": {"line": 5, "character": 11}, |
| 97 | + }, |
| 98 | + "message": "F841 local variable 'a' is assigned to but never used", |
| 99 | + "severity": 1, |
| 100 | + "tags": [1], |
| 101 | + }, |
| 102 | + ] |
| 103 | + |
| 104 | + diags = flake8_lint.pylsp_lint(workspace, made["src/b.py"]["document"]) |
| 105 | + assert diags == [ |
| 106 | + { |
| 107 | + "source": "flake8", |
| 108 | + "code": "F401", |
| 109 | + "range": { |
| 110 | + "start": {"line": 0, "character": 0}, |
| 111 | + "end": {"line": 0, "character": 9}, |
| 112 | + }, |
| 113 | + "message": "F401 'os' imported but unused", |
| 114 | + "severity": 1, |
| 115 | + "tags": [1], |
| 116 | + } |
| 117 | + ] |
| 118 | + |
| 119 | + |
62 | 120 | def test_flake8_config_param(workspace):
|
63 | 121 | with patch('pylsp.plugins.flake8_lint.Popen') as popen_mock:
|
64 | 122 | mock_instance = popen_mock.return_value
|
@@ -126,7 +184,9 @@ def test_flake8_multiline(workspace):
|
126 | 184 | flake8_lint.pylsp_lint(workspace, doc)
|
127 | 185 |
|
128 | 186 | call_args = popen_mock.call_args[0][0]
|
129 |
| - assert call_args == ["flake8", "-", "--exclude=blah/,file_2.py"] |
| 187 | + |
| 188 | + init_file = os.path.join("blah", "__init__.py") |
| 189 | + assert call_args == ["flake8", "-", "--exclude=blah/,file_2.py", "--stdin-display-name", init_file] |
130 | 190 |
|
131 | 191 | os.unlink(os.path.join(workspace.root_path, "setup.cfg"))
|
132 | 192 |
|
|
0 commit comments