|
21 | 21 |
|
22 | 22 | @pytest.fixture() |
23 | 23 | 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 |
28 | 29 |
|
29 | 30 |
|
30 | 31 | codeaction_str = dedent( |
31 | | - """ |
| 32 | + """ |
32 | 33 | import os |
33 | 34 | def f(): |
34 | 35 | a = 2 |
35 | 36 | """ |
36 | 37 | ) |
37 | 38 |
|
38 | 39 | import_str = dedent( |
39 | | - """ |
| 40 | + """ |
40 | 41 | import pathlib |
41 | 42 | import os |
42 | 43 | """ |
43 | 44 | ) |
44 | 45 |
|
45 | 46 | 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)", |
51 | 52 | ] |
52 | 53 |
|
53 | 54 | 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)", |
59 | 60 | ] |
60 | 61 |
|
61 | 62 | 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", |
65 | 66 | ] |
66 | 67 |
|
67 | 68 |
|
68 | 69 | 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 |
76 | 77 |
|
77 | 78 |
|
78 | 79 | 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) |
94 | 95 |
|
95 | 96 |
|
96 | 97 | 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) |
112 | 117 |
|
113 | 118 |
|
114 | 119 | 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) |
137 | 143 |
|
138 | 144 |
|
139 | 145 | def test_fix_all(workspace): |
140 | | - expected_str = dedent( |
141 | | - """ |
| 146 | + expected_str = dedent( |
| 147 | + """ |
142 | 148 | def f(): |
143 | 149 | pass |
144 | 150 | """ |
145 | | - ) |
146 | | - expected_str_safe = dedent( |
147 | | - """ |
| 151 | + ) |
| 152 | + expected_str_safe = dedent( |
| 153 | + """ |
148 | 154 | def f(): |
149 | 155 | a = 2 |
150 | 156 | """ |
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