-
-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathtest_ignore_fixers.py
More file actions
422 lines (401 loc) · 15 KB
/
test_ignore_fixers.py
File metadata and controls
422 lines (401 loc) · 15 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
from textwrap import dedent
import sublime
from unittesting import DeferrableTestCase
from SublimeLinter.tests.mockito import unstub
from SublimeLinter.tests.parameterized import parameterized as p
from SublimeLinter.lint.quick_fix import (
apply_edits,
fix_eslint_error,
eslint_ignore_block,
fix_codespell_error,
fix_flake8_error,
fix_mypy_error,
fix_mypy_unused_ignore,
fix_mypy_specific_unused_ignore,
fix_stylelint_error,
fix_shellcheck_error,
actions_for_errors,
ignore_rules_inline,
)
class TestActionReducer(DeferrableTestCase):
@p.expand([
(
"filter error without code",
[
dict(linter="flake", code="", msg="foo unused", line=1),
],
[]
),
(
"single error",
[
dict(linter="flake", code="201", msg="foo unused", line=1),
],
[
'flake: Disable 201 — foo unused'
]
),
(
"two distinct errors",
[
dict(linter="flake", code="201", msg="foo unused", line=1),
dict(linter="flake", code="202", msg="zoo unused", line=1),
],
[
'flake: Disable 201 — foo unused',
'flake: Disable 202 — zoo unused'
]
),
(
"two similar errors, same line",
[
dict(linter="flake", code="201", msg="foo unused", line=1),
dict(linter="flake", code="201", msg="zoo unused", line=1),
],
[
'flake: Disable 201 (2x) — e.g. foo unused',
]
),
(
"two similar errors, same line, same message",
[
dict(linter="flake", code="201", msg="too much", line=1),
dict(linter="flake", code="201", msg="too much", line=1),
],
[
'flake: Disable 201 (2x) — too much',
]
),
(
"two similar errors, different line",
[
dict(linter="flake", code="201", msg="foo unused", line=1),
dict(linter="flake", code="201", msg="zoo unused", line=2),
],
[
'flake: Disable 201 (2x) — e.g. foo unused',
]
),
])
def test_action_descriptions(self, _, ERRORS, RESULT):
@ignore_rules_inline("flake", except_for=lambda e: not e["code"])
def fixer(errors, view):
None
actions = actions_for_errors(ERRORS)
self.assertEquals(RESULT, [action.description for action in actions])
class TestIgnoreFixers(DeferrableTestCase):
@classmethod
def setUpClass(cls):
# make sure we have a window to work with
sublime.run_command("new_window")
cls.window = sublime.active_window()
s = sublime.load_settings("Preferences.sublime-settings")
s.set("close_windows_when_empty", False)
@classmethod
def tearDownClass(cls):
cls.window.run_command('close_window')
def tearDown(self):
unstub()
def create_view(self, window):
view = window.new_file()
self.addCleanup(self.close_view, view)
return view
def close_view(self, view):
view.set_scratch(True)
view.close()
@p.expand([
(
"clean line",
"view = window.new_file()",
"view = window.new_file() # noqa: E203"
),
(
"extend one given",
"view = window.new_file() # noqa: F402",
"view = window.new_file() # noqa: E203, F402",
),
(
"extend two given",
"view = window.new_file() # noqa: F402, E111",
"view = window.new_file() # noqa: E111, E203, F402",
),
(
"normalize joiner",
"view = window.new_file() # noqa: F402,E111,E203",
"view = window.new_file() # noqa: E111, E203, F402",
),
(
"handle surrounding whitespace",
" view = window.new_file() ",
" view = window.new_file() # noqa: E203",
),
(
"keep existing comment",
"view = window.new_file() # comment ",
"view = window.new_file() # comment # noqa: E203",
),
(
"keep existing comment with only one space preceding",
"view = window.new_file() # comment",
"view = window.new_file() # comment # noqa: E203",
),
(
"keep existing comment while extending",
"view = window.new_file() # comment # noqa: F403",
"view = window.new_file() # comment # noqa: E203, F403",
),
(
"keep python comment position while extending",
"view = window.new_file() # noqa: F403 # comment",
"view = window.new_file() # noqa: E203, F403 # comment",
),
(
"keep informal comment position while extending",
"view = window.new_file() # noqa: F403, comment",
"view = window.new_file() # noqa: E203, F403, comment",
),
])
def test_flake8(self, _description, BEFORE, AFTER):
view = self.create_view(self.window)
view.run_command("insert", {"characters": BEFORE})
error = dict(code="E203", region=sublime.Region(4))
edit = fix_flake8_error(error, view)
apply_edits(view, edit)
view_content = view.substr(sublime.Region(0, view.size()))
self.assertEquals(AFTER, view_content)
@p.expand([
(
"clean line",
"view = window.new_file()",
"view = window.new_file() # type: ignore[no-idea]"
),
(
"extend one given",
"view = window.new_file() # type: ignore[attr]",
"view = window.new_file() # type: ignore[attr, no-idea]"
),
(
"extend two given",
"view = window.new_file() # type: ignore[attr, import]",
"view = window.new_file() # type: ignore[attr, import, no-idea]",
),
(
"normalize joiner",
"view = window.new_file() # type: ignore[attr,import]",
"view = window.new_file() # type: ignore[attr, import, no-idea]",
),
(
"handle surrounding whitespace",
" view = window.new_file() ",
" view = window.new_file() # type: ignore[no-idea]"
),
(
"mypy comment must come before existing comment",
"view = window.new_file() # comment ",
"view = window.new_file() # type: ignore[no-idea] # comment ",
),
(
"keep existing comment while extending",
"view = window.new_file() # type: ignore[attr] # comment ",
"view = window.new_file() # type: ignore[attr, no-idea] # comment ",
),
(
"keep existing type comment in-place",
"view = window.new_file() # type: sublime.View",
"view = window.new_file() # type: sublime.View # type: ignore[no-idea]",
),
])
def test_mypy(self, _description, BEFORE, AFTER):
view = self.create_view(self.window)
view.run_command("insert", {"characters": BEFORE})
error = dict(code="no-idea", region=sublime.Region(4))
edit = fix_mypy_error(error, view)
apply_edits(view, edit)
view_content = view.substr(sublime.Region(0, view.size()))
self.assertEquals(AFTER, view_content)
@p.expand([
(
"remove ignore comment at EOL",
"partial(fixer, error), # type: ignore[arg-type]",
"partial(fixer, error),",
),
(
"remove bare ignore comment at EOL",
"partial(fixer, error), # type: ignore",
"partial(fixer, error),",
),
])
def test_mypy_unused_ignore(self, _description, BEFORE, AFTER):
view = self.create_view(self.window)
view.run_command("insert", {"characters": BEFORE})
error = dict(msg='Unused "type: ignore" comment', region=sublime.Region(4))
edit = fix_mypy_unused_ignore(error, view)
apply_edits(view, edit)
view_content = view.substr(sublime.Region(0, view.size()))
self.assertEquals(AFTER, view_content)
@p.expand([
(
"remove ignore comment at EOL 1",
"partial(fixer, error), # type: ignore[arg-type, import, other]",
"partial(fixer, error), # type: ignore[arg-type]",
),
(
"remove ignore comment at EOL 2",
"partial(fixer, error), # type: ignore[arg-type, other, import]",
"partial(fixer, error), # type: ignore[arg-type]",
),
(
"remove ignore comment at EOL 3",
"partial(fixer, error), # type: ignore[other, import, arg-type]",
"partial(fixer, error), # type: ignore[arg-type]",
),
])
def test_mypy_specific_unused_ignore(self, _description, BEFORE, AFTER):
view = self.create_view(self.window)
view.run_command("insert", {"characters": BEFORE})
error = dict(msg='Unused "type: ignore[import, other]" comment', region=sublime.Region(4))
edit = fix_mypy_specific_unused_ignore(error, view)
apply_edits(view, edit)
view_content = view.substr(sublime.Region(0, view.size()))
self.assertEquals(AFTER, view_content)
@p.expand([
(
"replace word",
"# test crate",
"# test create",
),
])
def test_codespell(self, _description, BEFORE, AFTER):
view = self.create_view(self.window)
view.run_command("insert", {"characters": BEFORE})
error = dict(msg='crate ==> create', region=sublime.Region(7, 12))
edit = fix_codespell_error(error, view)
apply_edits(view, edit)
view_content = view.substr(sublime.Region(0, view.size()))
self.assertEquals(AFTER, view_content)
@p.expand([
(
"clean line",
"let |document = node.ownerDocument",
"// eslint-disable-next-line semi\nlet document = node.ownerDocument"
),
(
"handle surrounding whitespace",
" let |document = node.ownerDocument",
" // eslint-disable-next-line semi\n let document = node.ownerDocument"
),
(
"extend one given",
"// eslint-disable-next-line quote\nlet |document = node.ownerDocument",
"// eslint-disable-next-line quote, semi\nlet document = node.ownerDocument"
),
(
"extend two given",
"// eslint-disable-next-line no-alert, quote\nlet |document = node.ownerDocument",
"// eslint-disable-next-line no-alert, quote, semi\nlet document = node.ownerDocument"
),
(
"normalize joiner",
"// eslint-disable-next-line no-alert,quote,semi\nlet |document = node.ownerDocument",
"// eslint-disable-next-line no-alert, quote, semi\nlet document = node.ownerDocument"
),
(
"keep existing comment while extending",
"// eslint-disable-next-line quote -- some comment\nlet |document = node.ownerDocument",
"// eslint-disable-next-line quote, semi -- some comment\nlet document = node.ownerDocument"
),
(
"recognize plugin rules",
"// eslint-disable-next-line plugin/quote\nlet |document = node.ownerDocument",
"// eslint-disable-next-line plugin/quote, semi\nlet document = node.ownerDocument"
),
])
def test_eslint(self, _description, BEFORE, AFTER):
view = self.create_view(self.window)
BEFORE, POS = "".join(BEFORE.split("|")), BEFORE.index("|")
view.run_command("insert", {"characters": BEFORE})
error = dict(code="semi", region=sublime.Region(POS))
edit = fix_eslint_error(error, view)
apply_edits(view, edit)
view_content = view.substr(sublime.Region(0, view.size()))
self.assertEquals(AFTER, view_content)
@p.expand([
(
"clean block",
dedent("""\
let document = node.ownerDocument
""".rstrip()),
dedent("""\
/* eslint-disable semi */
let document = node.ownerDocument
/* eslint-enable semi */
""".rstrip()),
sublime.Region(4, 8)
),
(
"extend existing",
dedent("""\
/* eslint-disable emi */
let document = node.ownerDocument
/* eslint-enable emi */
""".rstrip()),
dedent("""\
/* eslint-disable emi, semi */
let document = node.ownerDocument
/* eslint-enable emi, semi */
""".rstrip()),
sublime.Region(28, 32)
)
])
def test_eslint_block(self, _description, BEFORE, AFTER, REGION):
view = self.create_view(self.window)
view.run_command("insert", {"characters": BEFORE})
errors = [
dict(code="semi", region=sublime.Region(28)),
]
edit = eslint_ignore_block(errors, REGION, view)
apply_edits(view, edit)
view_content = view.substr(sublime.Region(0, view.size()))
self.assertEquals(AFTER, view_content)
@p.expand([
(
"clean line",
"#id| {",
"#id { /* stylelint-disable-line selector-no-id */"
),
(
"extend given comment",
"#id| { /* stylelint-disable-line some-rule */",
"#id { /* stylelint-disable-line selector-no-id, some-rule */",
),
])
def test_stylelint(self, _description, BEFORE, AFTER):
view = self.create_view(self.window)
BEFORE, POS = "".join(BEFORE.split("|")), BEFORE.index("|")
view.run_command("insert", {"characters": BEFORE})
error = dict(code="selector-no-id", region=sublime.Region(POS))
edit = fix_stylelint_error(error, view)
apply_edits(view, edit)
view_content = view.substr(sublime.Region(0, view.size()))
self.assertEquals(AFTER, view_content)
@p.expand([
(
"clean line",
"r|esult=$variable",
"# shellcheck disable=SC2154\nresult=$variable"
),
(
"add to existing rule",
"# shellcheck disable=SC2034\nr|esult=$variable",
"# shellcheck disable=SC2034,SC2154\nresult=$variable"
),
])
def test_shellcheck(self, _description, BEFORE, AFTER):
view = self.create_view(self.window)
BEFORE, POS = "".join(BEFORE.split("|")), BEFORE.index("|")
view.run_command("insert", {"characters": BEFORE})
error = dict(msg="variable is referenced but not assigned. [SC2154]", region=sublime.Region(POS))
edit = fix_shellcheck_error(error, view)
apply_edits(view, edit)
view_content = view.substr(sublime.Region(0, view.size()))
self.assertEquals(AFTER, view_content)