All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 let s:command_output = []
3 function! ale#assert#GivenCommandOutput(...) abort
4 let s:command_output = a:000
7 function! s:GetLinter() abort
8 let l:linters = ale#linter#GetLintersLoaded()
9 let l:filetype_linters = get(values(l:linters), 0, [])
11 if len(l:linters) is 0 || len(l:filetype_linters) is 0
12 throw 'No linters were loaded'
15 if len(l:linters) > 1 || len(l:filetype_linters) > 1
16 throw 'More than one linter was loaded'
19 return l:filetype_linters[0]
22 function! s:FormatExe(command, executable) abort
23 return substitute(a:command, '%e', '\=ale#Escape(a:executable)', 'g')
26 function! s:ProcessDeferredCommands(initial_result) abort
27 let l:result = a:initial_result
28 let l:command_index = 0
31 while ale#command#IsDeferred(l:result)
32 call add(l:command, s:FormatExe(l:result.command, l:result.executable))
34 if get(g:, 'ale_run_synchronously_emulate_commands')
35 " Don't run commands, but simulate the results.
36 let l:Callback = g:ale_run_synchronously_callbacks[0]
37 let l:output = get(s:command_output, l:command_index, [])
38 call l:Callback(0, l:output)
39 unlet g:ale_run_synchronously_callbacks
41 let l:command_index += 1
43 " Run the commands in the shell, synchronously.
44 call ale#test#FlushJobs()
47 let l:result = l:result.value
50 call add(l:command, l:result)
55 function! s:ProcessDeferredCwds(initial_command, initial_cwd) abort
56 let l:result = a:initial_command
57 let l:last_cwd = v:null
58 let l:command_index = 0
61 while ale#command#IsDeferred(l:result)
62 call add(l:cwd_list, l:result.cwd)
64 if get(g:, 'ale_run_synchronously_emulate_commands')
65 " Don't run commands, but simulate the results.
66 let l:Callback = g:ale_run_synchronously_callbacks[0]
67 let l:output = get(s:command_output, l:command_index, [])
68 call l:Callback(0, l:output)
69 unlet g:ale_run_synchronously_callbacks
71 let l:command_index += 1
73 " Run the commands in the shell, synchronously.
74 call ale#test#FlushJobs()
77 let l:result = l:result.value
80 call add(l:cwd_list, a:initial_cwd is v:null ? l:last_cwd : a:initial_cwd)
85 " Load the currently loaded linter for a test case, and check that the command
86 " matches the given string.
87 function! ale#assert#Linter(expected_executable, expected_command) abort
88 let l:buffer = bufnr('')
89 let l:linter = s:GetLinter()
90 let l:executable = ale#linter#GetExecutable(l:buffer, l:linter)
92 while ale#command#IsDeferred(l:executable)
93 call ale#test#FlushJobs()
94 let l:executable = l:executable.value
97 let l:command = s:ProcessDeferredCommands(
98 \ ale#linter#GetCommand(l:buffer, l:linter),
101 if type(a:expected_command) isnot v:t_list
102 let l:command = l:command[-1]
105 if type(l:command) is v:t_string
106 " Replace %e with the escaped executable, so tests keep passing after
107 " linters are changed to use %e.
108 let l:command = s:FormatExe(l:command, l:executable)
109 elseif type(l:command) is v:t_list
110 call map(l:command, 's:FormatExe(v:val, l:executable)')
114 \ [a:expected_executable, a:expected_command],
115 \ [l:executable, l:command]
118 function! ale#assert#LinterCwd(expected_cwd) abort
119 let l:buffer = bufnr('')
120 let l:linter = s:GetLinter()
122 let l:initial_cwd = ale#linter#GetCwd(l:buffer, l:linter)
123 call ale#command#SetCwd(l:buffer, l:initial_cwd)
125 let l:cwd = s:ProcessDeferredCwds(
126 \ ale#linter#GetCommand(l:buffer, l:linter),
130 call ale#command#ResetCwd(l:buffer)
132 if type(a:expected_cwd) isnot v:t_list
133 let l:cwd = l:cwd[-1]
136 AssertEqual a:expected_cwd, l:cwd
139 function! ale#assert#FixerCwd(expected_cwd) abort
140 let l:buffer = bufnr('')
141 let l:cwd = s:ProcessDeferredCwds(s:FixerFunction(l:buffer), v:null)
143 if type(a:expected_cwd) isnot v:t_list
144 let l:cwd = l:cwd[-1]
147 AssertEqual a:expected_cwd, l:cwd
150 function! ale#assert#Fixer(expected_result) abort
151 let l:buffer = bufnr('')
152 let l:result = s:ProcessDeferredCommands(s:FixerFunction(l:buffer))
154 if type(a:expected_result) isnot v:t_list
155 let l:result = l:result[-1]
158 AssertEqual a:expected_result, l:result
161 function! ale#assert#FixerNotExecuted() abort
162 let l:buffer = bufnr('')
163 let l:result = s:ProcessDeferredCommands(s:FixerFunction(l:buffer))[-1]
165 Assert empty(l:result), "The fixer will be executed when it shouldn't be"
168 function! ale#assert#LinterNotExecuted() abort
169 let l:buffer = bufnr('')
170 let l:linter = s:GetLinter()
171 let l:executable = ale#linter#GetExecutable(l:buffer, l:linter)
174 if !empty(l:executable)
175 let l:command = ale#linter#GetCommand(l:buffer, l:linter)
177 if type(l:command) is v:t_list
178 let l:command = l:command[-1]
181 let l:executed = !empty(l:command)
186 Assert !l:executed, "The linter will be executed when it shouldn't be"
189 function! ale#assert#LSPOptions(expected_options) abort
190 let l:buffer = bufnr('')
191 let l:linter = s:GetLinter()
192 let l:initialization_options = ale#lsp_linter#GetOptions(l:buffer, l:linter)
194 AssertEqual a:expected_options, l:initialization_options
197 function! ale#assert#LSPConfig(expected_config) abort
198 let l:buffer = bufnr('')
199 let l:linter = s:GetLinter()
200 let l:config = ale#lsp_linter#GetConfig(l:buffer, l:linter)
202 AssertEqual a:expected_config, l:config
205 function! ale#assert#LSPLanguage(expected_language) abort
206 let l:buffer = bufnr('')
207 let l:linter = s:GetLinter()
208 let l:Language = l:linter.language
209 let l:language = type(l:Language) is v:t_func
210 \ ? l:Language(l:buffer)
213 AssertEqual a:expected_language, l:language
216 function! ale#assert#LSPProject(expected_root) abort
217 let l:buffer = bufnr('')
218 let l:linter = s:GetLinter()
219 let l:root = ale#lsp_linter#FindProjectRoot(l:buffer, l:linter)
221 AssertEqual a:expected_root, l:root
224 function! ale#assert#LSPAddress(expected_address) abort
225 let l:buffer = bufnr('')
226 let l:linter = s:GetLinter()
227 let l:address = ale#linter#GetAddress(l:buffer, l:linter)
229 AssertEqual a:expected_address, l:address
232 function! ale#assert#SetUpLinterTestCommands() abort
233 command! -nargs=+ GivenCommandOutput :call ale#assert#GivenCommandOutput(<args>)
234 command! -nargs=+ AssertLinterCwd :call ale#assert#LinterCwd(<args>)
235 command! -nargs=+ AssertLinter :call ale#assert#Linter(<args>)
236 command! -nargs=0 AssertLinterNotExecuted :call ale#assert#LinterNotExecuted()
237 command! -nargs=+ AssertLSPOptions :call ale#assert#LSPOptions(<args>)
238 command! -nargs=+ AssertLSPConfig :call ale#assert#LSPConfig(<args>)
239 command! -nargs=+ AssertLSPLanguage :call ale#assert#LSPLanguage(<args>)
240 command! -nargs=+ AssertLSPProject :call ale#assert#LSPProject(<args>)
241 command! -nargs=+ AssertLSPAddress :call ale#assert#LSPAddress(<args>)
244 function! ale#assert#SetUpFixerTestCommands() abort
245 command! -nargs=+ GivenCommandOutput :call ale#assert#GivenCommandOutput(<args>)
246 command! -nargs=+ AssertFixerCwd :call ale#assert#FixerCwd(<args>)
247 command! -nargs=+ AssertFixer :call ale#assert#Fixer(<args>)
248 command! -nargs=0 AssertFixerNotExecuted :call ale#assert#FixerNotExecuted()
251 function! ale#assert#ResetVariables(filetype, name, ...) abort
252 " If the suffix of the option names format is different, an additional
253 " argument can be used for that instead.
255 throw 'Too many arguments'
258 let l:option_suffix = get(a:000, 0, a:name)
259 let l:prefix = 'ale_' . a:filetype . '_'
260 \ . substitute(l:option_suffix, '-', '_', 'g')
261 let l:filter_expr = 'v:val[: len(l:prefix) - 1] is# l:prefix'
263 " Save and clear linter variables.
264 " We'll load the runtime file to reset them to defaults.
265 for l:key in filter(keys(g:), l:filter_expr)
266 execute 'Save g:' . l:key
270 for l:key in filter(keys(b:), l:filter_expr)
275 " A dummy function for making sure this module is loaded.
276 function! ale#assert#SetUpLinterTest(filetype, name) abort
277 " Set up a marker so ALE doesn't create real random temporary filenames.
278 let g:ale_create_dummy_temporary_file = 1
280 " Remove current linters.
281 call ale#linter#Reset()
282 call ale#linter#PreventLoading(a:filetype)
290 call ale#assert#ResetVariables(a:filetype, a:name)
292 Save g:ale_c_build_dir
293 unlet! g:ale_c_build_dir
294 unlet! b:ale_c_build_dir
296 execute 'runtime ale_linters/' . a:filetype . '/' . a:name . '.vim'
299 call ale#test#SetDirectory('/testplugin/test/linter')
302 call ale#assert#SetUpLinterTestCommands()
304 let g:ale_run_synchronously = 1
305 let g:ale_run_synchronously_emulate_commands = 1
308 function! ale#assert#TearDownLinterTest() abort
309 unlet! g:ale_create_dummy_temporary_file
310 unlet! g:ale_run_synchronously
311 unlet! g:ale_run_synchronously_callbacks
312 unlet! g:ale_run_synchronously_emulate_commands
313 unlet! g:ale_run_synchronously_command_results
314 let s:command_output = []
316 if exists(':GivenCommandOutput')
317 delcommand GivenCommandOutput
320 if exists(':AssertLinterCwd')
321 delcommand AssertLinterCwd
324 if exists(':AssertLinter')
325 delcommand AssertLinter
328 if exists(':AssertLinterNotExecuted')
329 delcommand AssertLinterNotExecuted
332 if exists(':AssertLSPOptions')
333 delcommand AssertLSPOptions
336 if exists(':AssertLSPConfig')
337 delcommand AssertLSPConfig
340 if exists(':AssertLSPLanguage')
341 delcommand AssertLSPLanguage
344 if exists(':AssertLSPProject')
345 delcommand AssertLSPProject
348 if exists(':AssertLSPAddress')
349 delcommand AssertLSPAddress
353 call ale#test#RestoreDirectory()
358 call ale#linter#Reset()
360 if exists('*ale#semver#ResetVersionCache')
361 call ale#semver#ResetVersionCache()
365 function! ale#assert#SetUpFixerTest(filetype, name, ...) abort
366 " If the suffix of the option names format is different, an additional
367 " argument can be used for that instead.
369 throw 'Too many arguments'
372 " Set up a marker so ALE doesn't create real random temporary filenames.
373 let g:ale_create_dummy_temporary_file = 1
375 let l:function_name = ale#fix#registry#GetFunc(a:name)
376 let s:FixerFunction = function(l:function_name)
378 let l:option_suffix = get(a:000, 0, a:name)
379 call ale#assert#ResetVariables(a:filetype, a:name, l:option_suffix)
381 execute 'runtime autoload/ale/fixers/' . substitute(a:name, '-', '_', 'g') . '.vim'
384 call ale#test#SetDirectory('/testplugin/test/fixers')
387 call ale#assert#SetUpFixerTestCommands()
389 let g:ale_run_synchronously = 1
390 let g:ale_run_synchronously_emulate_commands = 1
393 function! ale#assert#TearDownFixerTest() abort
394 unlet! g:ale_create_dummy_temporary_file
395 unlet! g:ale_run_synchronously
396 unlet! g:ale_run_synchronously_callbacks
397 unlet! g:ale_run_synchronously_emulate_commands
398 unlet! g:ale_run_synchronously_command_results
399 let s:command_output = []
400 unlet! s:FixerFunction
403 call ale#test#RestoreDirectory()
408 if exists('*ale#semver#ResetVersionCache')
409 call ale#semver#ResetVersionCache()
412 if exists(':GivenCommandOutput')
413 delcommand GivenCommandOutput
416 if exists(':AssertFixerCwd')
417 delcommand AssertFixerCwd
420 if exists(':AssertFixer')
421 delcommand AssertFixer
424 if exists(':AssertFixerNotExecuted')
425 delcommand AssertFixerNotExecuted