summaryrefslogtreecommitdiff
path: root/test/fixers/test_eslint_fixer_callback.vader
blob: 4a1dc47c81e8e01d89fc0534bca16f3e2642ee69 (plain)
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
Before:
  call ale#assert#SetUpFixerTest('javascript', 'eslint')
  Save g:ale_command_wrapper

  runtime autoload/ale/handlers/eslint.vim

  let g:ale_command_wrapper = ''

After:
  call ale#assert#TearDownFixerTest()

Execute(The executable path should be correct):
  call ale#test#SetFilename('../test-files/eslint/react-app/subdir/testfile.js')

  " eslint_d output with an older eslint version is used here.
  GivenCommandOutput ['v4.4.1 (eslint_d v5.1.0)']
  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/react-app'),
  \   'command': (has('win32') ? 'node.exe ' : '')
  \     . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/node_modules/eslint/bin/eslint.js'))
  \     . ' -c ' . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/.eslintrc.js'))
  \     . ' --fix %t',
  \ }

Execute(The ESLint fixer shouldn't run if no configuration file can be found):
  call ale#test#SetFilename('../no-configuration')
  AssertFixerNotExecuted

Execute(The ESLint fixer should use a config file option if set for old versions):
  call ale#test#SetFilename('../no-configuration')
  let b:ale_javascript_eslint_options = '-c /foo.cfg'

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': '',
  \   'command': ale#Escape('eslint') . ' -c /foo.cfg --fix %t',
  \ }

  let b:ale_javascript_eslint_options = '--bar -c /foo.cfg'

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': '',
  \   'command': ale#Escape('eslint') . ' --bar -c /foo.cfg --fix %t',
  \ }

  let b:ale_javascript_eslint_options = '--config /foo.cfg'

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': '',
  \   'command': ale#Escape('eslint') . ' --config /foo.cfg --fix %t',
  \ }

  let b:ale_javascript_eslint_options = '--bar --config /foo.cfg'

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': '',
  \   'command': ale#Escape('eslint') . ' --bar --config /foo.cfg --fix %t',
  \ }

Execute(The ESLint fixer should use a -c file option if set for eslint_d):
  let b:ale_javascript_eslint_executable = '/bin/eslint_d'
  GivenCommandOutput ['v3.19.0 (eslint_d v4.2.0)']
  call ale#test#SetFilename('../no-configuration')
  let b:ale_javascript_eslint_options = '-c /foo.cfg'

  AssertFixer
  \ {
  \   'process_with': 'ale#fixers#eslint#ProcessEslintDOutput',
  \   'cwd': '',
  \   'command': ale#Escape('/bin/eslint_d')
  \     . ' -c /foo.cfg'
  \     . ' --stdin-filename %s --stdin --fix-to-stdout'
  \ }

  let b:ale_javascript_eslint_options = '--bar -c /foo.cfg'

  AssertFixer
  \ {
  \   'process_with': 'ale#fixers#eslint#ProcessEslintDOutput',
  \   'cwd': '',
  \   'command': ale#Escape('/bin/eslint_d')
  \     . ' --bar -c /foo.cfg'
  \     . ' --stdin-filename %s --stdin --fix-to-stdout'
  \ }

  let b:ale_javascript_eslint_options = '--config /foo.cfg'

  AssertFixer
  \ {
  \   'process_with': 'ale#fixers#eslint#ProcessEslintDOutput',
  \   'cwd': '',
  \   'command': ale#Escape('/bin/eslint_d')
  \     . ' --config /foo.cfg'
  \     . ' --stdin-filename %s --stdin --fix-to-stdout'
  \ }

  let b:ale_javascript_eslint_options = '--bar --config /foo.cfg'

  AssertFixer
  \ {
  \   'process_with': 'ale#fixers#eslint#ProcessEslintDOutput',
  \   'cwd': '',
  \   'command': ale#Escape('/bin/eslint_d')
  \     . ' --bar --config /foo.cfg'
  \     . ' --stdin-filename %s --stdin --fix-to-stdout'
  \ }

Execute(The ESLint fixer should use a config file option if set for new versions):
  GivenCommandOutput ['4.9.0']
  call ale#test#SetFilename('../no-configuration')
  let b:ale_javascript_eslint_options = '-c /foo.cfg'

  AssertFixer
  \ {
  \   'process_with': 'ale#fixers#eslint#ProcessFixDryRunOutput',
  \   'cwd': '',
  \   'command': ale#Escape('eslint')
  \     . ' -c /foo.cfg'
  \     . ' --stdin-filename %s --stdin --fix-dry-run --format=json'
  \ }

  let b:ale_javascript_eslint_options = '--bar -c /foo.cfg'

  AssertFixer
  \ {
  \   'process_with': 'ale#fixers#eslint#ProcessFixDryRunOutput',
  \   'cwd': '',
  \   'command': ale#Escape('eslint')
  \     . ' --bar -c /foo.cfg'
  \     . ' --stdin-filename %s --stdin --fix-dry-run --format=json'
  \ }

  let b:ale_javascript_eslint_options = '--config /foo.cfg'

  AssertFixer
  \ {
  \   'process_with': 'ale#fixers#eslint#ProcessFixDryRunOutput',
  \   'cwd': '',
  \   'command': ale#Escape('eslint')
  \     . ' --config /foo.cfg'
  \     . ' --stdin-filename %s --stdin --fix-dry-run --format=json'
  \ }

  let b:ale_javascript_eslint_options = '--bar --config /foo.cfg'

  AssertFixer
  \ {
  \   'process_with': 'ale#fixers#eslint#ProcessFixDryRunOutput',
  \   'cwd': '',
  \   'command': ale#Escape('eslint')
  \     . ' --bar --config /foo.cfg'
  \     . ' --stdin-filename %s --stdin --fix-dry-run --format=json'
  \ }

Execute(The lower priority configuration file in a nested directory should be preferred):
  call ale#test#SetFilename('../test-files/eslint/react-app/subdir-with-config/testfile.js')

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/react-app'),
  \   'command': (has('win32') ? 'node.exe ' : '')
  \     . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/node_modules/eslint/bin/eslint.js'))
  \     . ' -c ' . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/subdir-with-config/.eslintrc'))
  \     . ' --fix %t',
  \ }

Execute(--config in options should override configuration file detection for old versions):
  call ale#test#SetFilename('../test-files/eslint/react-app/subdir-with-config/testfile.js')

  let b:ale_javascript_eslint_options = '--config /foo.cfg'

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/react-app'),
  \   'command': (has('win32') ? 'node.exe ' : '')
  \     . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/node_modules/eslint/bin/eslint.js'))
  \     . ' --config /foo.cfg'
  \     . ' --fix %t',
  \ }

  let b:ale_javascript_eslint_options = '-c /foo.cfg'

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/react-app'),
  \   'command': (has('win32') ? 'node.exe ' : '')
  \     . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/node_modules/eslint/bin/eslint.js'))
  \     . ' -c /foo.cfg'
  \     . ' --fix %t',
  \ }

Execute(package.json should be used as a last resort):
  call ale#test#SetFilename('../test-files/eslint/react-app/subdir-with-package-json/testfile.js')

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/react-app'),
  \   'command': (has('win32') ? 'node.exe ' : '')
  \     . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/node_modules/eslint/bin/eslint.js'))
  \     . ' -c ' . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/.eslintrc.js'))
  \     . ' --fix %t',
  \ }

  call ale#test#SetFilename('../test-files/eslint/package.json')

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint'),
  \   'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/node_modules/.bin/eslint'))
  \     . ' -c ' . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/package.json'))
  \     . ' --fix %t',
  \ }

Execute(The version check should be correct):
  call ale#test#SetFilename('../test-files/eslint/react-app/subdir-with-config/testfile.js')

  " We should run the command to get the version the first time.
  GivenCommandOutput ['4.9.0']
  AssertFixer [
  \ (has('win32') ? 'node.exe ' : '')
  \   . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/node_modules/eslint/bin/eslint.js'))
  \   . ' --version',
  \ {
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/react-app'),
  \   'command': (has('win32') ? 'node.exe ' : '')
  \     . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/node_modules/eslint/bin/eslint.js'))
  \     . ' --stdin-filename %s --stdin --fix-dry-run --format=json',
  \   'process_with': 'ale#fixers#eslint#ProcessFixDryRunOutput',
  \ },
  \]

  AssertFixer [
  \ {
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/react-app'),
  \   'command': (has('win32') ? 'node.exe ' : '')
  \     . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/node_modules/eslint/bin/eslint.js'))
  \     . ' --stdin-filename %s --stdin --fix-dry-run --format=json',
  \   'process_with': 'ale#fixers#eslint#ProcessFixDryRunOutput',
  \ },
  \]

Execute(--fix-dry-run should be used for 4.9.0 and up):
  call ale#test#SetFilename('../test-files/eslint/react-app/subdir/testfile.js')

  GivenCommandOutput ['4.9.0']
  AssertFixer
  \ {
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/react-app'),
  \   'command': (has('win32') ? 'node.exe ' : '')
  \     . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/react-app/node_modules/eslint/bin/eslint.js'))
  \     . ' --stdin-filename %s --stdin --fix-dry-run --format=json',
  \   'process_with': 'ale#fixers#eslint#ProcessFixDryRunOutput',
  \ }

Execute(--fix-to-stdout should be used for eslint_d):
  call ale#test#SetFilename('../test-files/eslint/app-with-eslint-d/testfile.js')

  AssertFixer
  \ {
  \   'read_temporary_file': 1,
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/app-with-eslint-d'),
  \   'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/app-with-eslint-d/node_modules/.bin/eslint_d'))
  \     . ' -c ' . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/package.json'))
  \     . ' --fix %t',
  \ }

  " The option should be used when eslint_d is new enough.
  " We look at the ESLint version instead of the eslint_d version.
  GivenCommandOutput ['v3.19.0 (eslint_d v4.2.0)']
  AssertFixer
  \ {
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/app-with-eslint-d'),
  \   'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/app-with-eslint-d/node_modules/.bin/eslint_d'))
  \     . ' --stdin-filename %s --stdin --fix-to-stdout',
  \   'process_with': 'ale#fixers#eslint#ProcessEslintDOutput',
  \ }

  " The option should be used for new versions too.
  GivenCommandOutput ['4.9.0']
  AssertFixer
  \ {
  \   'cwd': ale#path#Simplify(g:dir . '/../test-files/eslint/app-with-eslint-d'),
  \   'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/eslint/app-with-eslint-d/node_modules/.bin/eslint_d'))
  \     . ' --stdin-filename %s --stdin --fix-to-stdout',
  \   'process_with': 'ale#fixers#eslint#ProcessEslintDOutput',
  \ }

Execute(The --fix-dry-run post-processor should handle JSON output correctly):
  AssertEqual
  \ [],
  \ ale#fixers#eslint#ProcessFixDryRunOutput(bufnr(''), [])
  AssertEqual
  \ [],
  \ ale#fixers#eslint#ProcessFixDryRunOutput(bufnr(''), [''])
  AssertEqual
  \ [],
  \ ale#fixers#eslint#ProcessFixDryRunOutput(bufnr(''), ['[{}]'])
  AssertEqual
  \ ['foo', 'bar'],
  \ ale#fixers#eslint#ProcessFixDryRunOutput(bufnr(''), ['[{"output": "foo\nbar"}]'])

Execute(The eslint_d post-processor should permit regular JavaScript content):
  AssertEqual
  \ [
  \   'const x = ''Error: foo''',
  \   'const y = 3',
  \ ],
  \ ale#fixers#eslint#ProcessEslintDOutput(bufnr(''), [
  \   'const x = ''Error: foo''',
  \   'const y = 3',
  \ ])

Execute(The eslint_d post-processor should handle error messages correctly):
  AssertEqual
  \ [],
  \ ale#fixers#eslint#ProcessEslintDOutput(bufnr(''), [
  \   'Error: No ESLint configuration found.',
  \ ])

Execute(The eslint_d post-processor should handle failing to connect properly):
  AssertEqual
  \ [],
  \ ale#fixers#eslint#ProcessEslintDOutput(bufnr(''), [
  \   'Could not connect',
  \ ])