-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_code_actions.py
More file actions
178 lines (152 loc) · 4.93 KB
/
test_code_actions.py
File metadata and controls
178 lines (152 loc) · 4.93 KB
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
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.
import tempfile
from textwrap import dedent
from typing import List
from unittest.mock import Mock
import cattrs
import pytest
from lsprotocol.converters import get_converter
from lsprotocol.types import CodeAction, Position, Range
from pylsp import uris
from pylsp.config.config import Config
from pylsp.workspace import Document, Workspace
import pylsp_ruff.plugin as ruff_lint
converter = get_converter()
@pytest.fixture()
def workspace(tmp_path):
"""Return a workspace."""
ws = Workspace(tmp_path.absolute().as_uri(), Mock())
ws._config = Config(ws.root_uri, {}, 0, {})
return ws
codeaction_str = dedent(
"""
import os
def f():
a = 2
"""
)
import_str = dedent(
"""
import pathlib
import os
"""
)
codeactions = [
"Ruff (F401): Remove unused import: `os`",
"Ruff (F401): Disable for this line",
"Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
"Ruff (F841): Disable for this line",
"Ruff: Fix All (safe fixes)",
]
codeactions_unfixable = [
"Ruff (F401): Remove unused import: `os`",
"Ruff (F401): Disable for this line",
# "Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
"Ruff (F841): Disable for this line",
"Ruff: Fix All (safe fixes)",
]
codeactions_import = [
"Ruff: Organize imports",
"Ruff: Fix All (safe fixes)",
"Ruff (I001): Disable for this line",
]
def temp_document(doc_text, workspace):
with tempfile.NamedTemporaryFile(
mode="w", dir=workspace.root_path, delete=False
) as temp_file:
name = temp_file.name
temp_file.write(doc_text)
doc = Document(uris.from_fs_path(name), workspace)
return name, doc
def test_ruff_code_actions(workspace):
_, doc = temp_document(codeaction_str, workspace)
workspace._config.update(
{"plugins": {"ruff": {"select": ["F"], "unsafeFixes": True}}}
)
diags = ruff_lint.pylsp_lint(workspace, doc)
range_ = cattrs.unstructure(
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
)
actions = ruff_lint.pylsp_code_actions(
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
)
actions = converter.structure(actions, List[CodeAction])
action_titles = list(map(lambda action: action.title, actions))
assert sorted(codeactions) == sorted(action_titles)
def test_ruff_code_actions_unfixable(workspace):
_, doc = temp_document(codeaction_str, workspace)
workspace._config.update(
{"plugins": {"ruff": {"select": ["F"], "unfixable": ["F841"]}}}
)
diags = ruff_lint.pylsp_lint(workspace, doc)
range_ = cattrs.unstructure(
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
)
actions = ruff_lint.pylsp_code_actions(
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
)
actions = converter.structure(actions, List[CodeAction])
action_titles = list(map(lambda action: action.title, actions))
assert sorted(codeactions_unfixable) == sorted(action_titles)
def test_import_action(workspace):
workspace._config.update(
{
"plugins": {
"ruff": {
"extendSelect": ["I"],
"extendIgnore": ["F"],
}
}
}
)
_, doc = temp_document(import_str, workspace)
diags = ruff_lint.pylsp_lint(workspace, doc)
range_ = cattrs.unstructure(
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
)
actions = ruff_lint.pylsp_code_actions(
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
)
actions = converter.structure(actions, List[CodeAction])
action_titles = list(map(lambda action: action.title, actions))
assert sorted(codeactions_import) == sorted(action_titles)
def test_fix_all(workspace):
expected_str = dedent(
"""
def f():
pass
"""
)
expected_str_safe = dedent(
"""
def f():
a = 2
"""
)
workspace._config.update(
{
"plugins": {
"ruff": {
"unsafeFixes": True,
}
}
}
)
_, doc = temp_document(codeaction_str, workspace)
settings = ruff_lint.load_settings(workspace, doc.path)
fixed_str = ruff_lint.run_ruff_fix(doc, settings)
assert fixed_str == expected_str
workspace._config.update(
{
"plugins": {
"ruff": {
"unsafeFixes": False,
}
}
}
)
_, doc = temp_document(codeaction_str, workspace)
settings = ruff_lint.load_settings(workspace, doc.path)
fixed_str = ruff_lint.run_ruff_fix(doc, settings)
assert fixed_str == expected_str_safe