Skip to content

Commit 3f1af32

Browse files
committed
fix: pass --isolated to ruff during testing
1 parent 9d64114 commit 3f1af32

5 files changed

Lines changed: 191 additions & 167 deletions

File tree

‎pylsp_ruff/plugin.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,9 @@ def build_check_arguments(
655655
continue
656656
args.append(f"--ignore={','.join(errors)}")
657657

658+
if settings.isolated:
659+
args.append("--isolated")
660+
658661
if extra_arguments:
659662
args.extend(extra_arguments)
660663

@@ -709,6 +712,9 @@ def build_format_arguments(
709712
if settings.target_version:
710713
args.append(f"--target-version={settings.target_version}")
711714

715+
if settings.isolated:
716+
args.append("--isolated")
717+
712718
if extra_arguments:
713719
args.extend(extra_arguments)
714720

‎pylsp_ruff/settings.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class PluginSettings:
3333

3434
target_version: Optional[str] = None
3535

36+
# For testing only
37+
isolated: bool = False
38+
3639

3740
def to_camel_case(snake_str: str) -> str:
3841
components = snake_str.split("_")

‎tests/test_code_actions.py‎

Lines changed: 119 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -21,158 +21,166 @@
2121

2222
@pytest.fixture()
2323
def workspace(tmp_path):
24-
"""Return a workspace."""
25-
ws = Workspace(tmp_path.absolute().as_uri(), Mock())
26-
ws._config = Config(ws.root_uri, {}, 0, {})
27-
return ws
24+
"""Return a workspace."""
25+
ws = Workspace(tmp_path.absolute().as_uri(), Mock())
26+
ws._config = Config(ws.root_uri, {}, 0, {})
27+
ws._config.update({"plugins": {"ruff": {"isolated": True}}})
28+
return ws
2829

2930

3031
codeaction_str = dedent(
31-
"""
32+
"""
3233
import os
3334
def f():
3435
a = 2
3536
"""
3637
)
3738

3839
import_str = dedent(
39-
"""
40+
"""
4041
import pathlib
4142
import os
4243
"""
4344
)
4445

4546
codeactions = [
46-
"Ruff (F401): Remove unused import: `os`",
47-
"Ruff (F401): Disable for this line",
48-
"Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
49-
"Ruff (F841): Disable for this line",
50-
"Ruff: Fix All (safe fixes)",
47+
"Ruff (F401): Remove unused import: `os`",
48+
"Ruff (F401): Disable for this line",
49+
"Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
50+
"Ruff (F841): Disable for this line",
51+
"Ruff: Fix All (safe fixes)",
5152
]
5253

5354
codeactions_unfixable = [
54-
"Ruff (F401): Remove unused import: `os`",
55-
"Ruff (F401): Disable for this line",
56-
# "Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
57-
"Ruff (F841): Disable for this line",
58-
"Ruff: Fix All (safe fixes)",
55+
"Ruff (F401): Remove unused import: `os`",
56+
"Ruff (F401): Disable for this line",
57+
# "Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
58+
"Ruff (F841): Disable for this line",
59+
"Ruff: Fix All (safe fixes)",
5960
]
6061

6162
codeactions_import = [
62-
"Ruff: Organize imports",
63-
"Ruff: Fix All (safe fixes)",
64-
"Ruff (I001): Disable for this line",
63+
"Ruff: Organize imports",
64+
"Ruff: Fix All (safe fixes)",
65+
"Ruff (I001): Disable for this line",
6566
]
6667

6768

6869
def temp_document(doc_text, workspace):
69-
with tempfile.NamedTemporaryFile(
70-
mode="w", dir=workspace.root_path, delete=False
71-
) as temp_file:
72-
name = temp_file.name
73-
temp_file.write(doc_text)
74-
doc = Document(uris.from_fs_path(name), workspace)
75-
return name, doc
70+
with tempfile.NamedTemporaryFile(
71+
mode="w", dir=workspace.root_path, delete=False
72+
) as temp_file:
73+
name = temp_file.name
74+
temp_file.write(doc_text)
75+
doc = Document(uris.from_fs_path(name), workspace)
76+
return name, doc
7677

7778

7879
def test_ruff_code_actions(workspace):
79-
_, doc = temp_document(codeaction_str, workspace)
80-
81-
workspace._config.update(
82-
{"plugins": {"ruff": {"select": ["F"], "unsafeFixes": True}}}
83-
)
84-
diags = ruff_lint.pylsp_lint(workspace, doc)
85-
range_ = cattrs.unstructure(
86-
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
87-
)
88-
actions = ruff_lint.pylsp_code_actions(
89-
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
90-
)
91-
actions = converter.structure(actions, List[CodeAction])
92-
action_titles = list(map(lambda action: action.title, actions))
93-
assert sorted(codeactions) == sorted(action_titles)
80+
_, doc = temp_document(codeaction_str, workspace)
81+
82+
workspace._config.update(
83+
{"plugins": {"ruff": {"isolated": True, "select": ["F"], "unsafeFixes": True}}}
84+
)
85+
diags = ruff_lint.pylsp_lint(workspace, doc)
86+
range_ = cattrs.unstructure(
87+
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
88+
)
89+
actions = ruff_lint.pylsp_code_actions(
90+
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
91+
)
92+
actions = converter.structure(actions, List[CodeAction])
93+
action_titles = list(map(lambda action: action.title, actions))
94+
assert sorted(codeactions) == sorted(action_titles)
9495

9596

9697
def test_ruff_code_actions_unfixable(workspace):
97-
_, doc = temp_document(codeaction_str, workspace)
98-
99-
workspace._config.update(
100-
{"plugins": {"ruff": {"select": ["F"], "unfixable": ["F841"]}}}
101-
)
102-
diags = ruff_lint.pylsp_lint(workspace, doc)
103-
range_ = cattrs.unstructure(
104-
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
105-
)
106-
actions = ruff_lint.pylsp_code_actions(
107-
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
108-
)
109-
actions = converter.structure(actions, List[CodeAction])
110-
action_titles = list(map(lambda action: action.title, actions))
111-
assert sorted(codeactions_unfixable) == sorted(action_titles)
98+
_, doc = temp_document(codeaction_str, workspace)
99+
100+
workspace._config.update(
101+
{
102+
"plugins": {
103+
"ruff": {"isolated": True, "select": ["F"], "unfixable": ["F841"]}
104+
}
105+
}
106+
)
107+
diags = ruff_lint.pylsp_lint(workspace, doc)
108+
range_ = cattrs.unstructure(
109+
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
110+
)
111+
actions = ruff_lint.pylsp_code_actions(
112+
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
113+
)
114+
actions = converter.structure(actions, List[CodeAction])
115+
action_titles = list(map(lambda action: action.title, actions))
116+
assert sorted(codeactions_unfixable) == sorted(action_titles)
112117

113118

114119
def test_import_action(workspace):
115-
workspace._config.update(
116-
{
117-
"plugins": {
118-
"ruff": {
119-
"extendSelect": ["I"],
120-
"extendIgnore": ["F"],
121-
}
122-
}
123-
}
124-
)
125-
_, doc = temp_document(import_str, workspace)
126-
127-
diags = ruff_lint.pylsp_lint(workspace, doc)
128-
range_ = cattrs.unstructure(
129-
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
130-
)
131-
actions = ruff_lint.pylsp_code_actions(
132-
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
133-
)
134-
actions = converter.structure(actions, List[CodeAction])
135-
action_titles = list(map(lambda action: action.title, actions))
136-
assert sorted(codeactions_import) == sorted(action_titles)
120+
workspace._config.update(
121+
{
122+
"plugins": {
123+
"ruff": {
124+
"isolated": True,
125+
"extendSelect": ["I"],
126+
"extendIgnore": ["F"],
127+
}
128+
}
129+
}
130+
)
131+
_, doc = temp_document(import_str, workspace)
132+
133+
diags = ruff_lint.pylsp_lint(workspace, doc)
134+
range_ = cattrs.unstructure(
135+
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
136+
)
137+
actions = ruff_lint.pylsp_code_actions(
138+
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
139+
)
140+
actions = converter.structure(actions, List[CodeAction])
141+
action_titles = list(map(lambda action: action.title, actions))
142+
assert sorted(codeactions_import) == sorted(action_titles)
137143

138144

139145
def test_fix_all(workspace):
140-
expected_str = dedent(
141-
"""
146+
expected_str = dedent(
147+
"""
142148
def f():
143149
pass
144150
"""
145-
)
146-
expected_str_safe = dedent(
147-
"""
151+
)
152+
expected_str_safe = dedent(
153+
"""
148154
def f():
149155
a = 2
150156
"""
151-
)
152-
workspace._config.update(
153-
{
154-
"plugins": {
155-
"ruff": {
156-
"unsafeFixes": True,
157-
}
158-
}
159-
}
160-
)
161-
_, doc = temp_document(codeaction_str, workspace)
162-
settings = ruff_lint.load_settings(workspace, doc.path)
163-
fixed_str = ruff_lint.run_ruff_fix(doc, settings)
164-
assert fixed_str == expected_str
165-
166-
workspace._config.update(
167-
{
168-
"plugins": {
169-
"ruff": {
170-
"unsafeFixes": False,
171-
}
172-
}
173-
}
174-
)
175-
_, doc = temp_document(codeaction_str, workspace)
176-
settings = ruff_lint.load_settings(workspace, doc.path)
177-
fixed_str = ruff_lint.run_ruff_fix(doc, settings)
178-
assert fixed_str == expected_str_safe
157+
)
158+
workspace._config.update(
159+
{
160+
"plugins": {
161+
"ruff": {
162+
"isolated": True,
163+
"unsafeFixes": True,
164+
}
165+
}
166+
}
167+
)
168+
_, doc = temp_document(codeaction_str, workspace)
169+
settings = ruff_lint.load_settings(workspace, doc.path)
170+
fixed_str = ruff_lint.run_ruff_fix(doc, settings)
171+
assert fixed_str == expected_str
172+
173+
workspace._config.update(
174+
{
175+
"plugins": {
176+
"ruff": {
177+
"isolated": True,
178+
"unsafeFixes": False,
179+
}
180+
}
181+
}
182+
)
183+
_, doc = temp_document(codeaction_str, workspace)
184+
settings = ruff_lint.load_settings(workspace, doc.path)
185+
fixed_str = ruff_lint.run_ruff_fix(doc, settings)
186+
assert fixed_str == expected_str_safe

0 commit comments

Comments
 (0)