#2132 - lint and fix with ale#command#Run

A new function is added here which will later be modified for public use
in linter and fixer callbacks. All linting and fixing now goes through
this new function, to prove that it works in all cases.
This commit is contained in:
w0rp 2019-02-06 22:00:11 +00:00
parent 3e11cbd18d
commit 81c73da3b9
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
25 changed files with 561 additions and 434 deletions

View File

@ -3,33 +3,37 @@
" managing files during linting and fixing cycles.
" This dictionary holds lists of files and directories to remove later.
if !exists('s:managed_data')
let s:managed_data = {}
if !exists('s:buffer_data')
let s:buffer_data = {}
endif
" Used to get the data in tests.
function! ale#command#GetData() abort
return deepcopy(s:managed_data)
return deepcopy(s:buffer_data)
endfunction
function! ale#command#ClearData() abort
let s:managed_data = {}
let s:buffer_data = {}
endfunction
function! ale#command#InitData(buffer) abort
if !has_key(s:buffer_data, a:buffer)
let s:buffer_data[a:buffer] = {
\ 'jobs': {},
\ 'file_list': [],
\ 'directory_list': [],
\}
endif
endfunction
function! ale#command#ManageFile(buffer, file) abort
if !has_key(s:managed_data, a:buffer)
let s:managed_data[a:buffer] = {'file_list': [], 'directory_list': []}
endif
call add(s:managed_data[a:buffer].file_list, a:file)
call ale#command#InitData(a:buffer)
call add(s:buffer_data[a:buffer].file_list, a:file)
endfunction
function! ale#command#ManageDirectory(buffer, directory) abort
if !has_key(s:managed_data, a:buffer)
let s:managed_data[a:buffer] = {'file_list': [], 'directory_list': []}
endif
call add(s:managed_data[a:buffer].directory_list, a:directory)
call ale#command#InitData(a:buffer)
call add(s:buffer_data[a:buffer].directory_list, a:directory)
endfunction
function! ale#command#CreateFile(buffer) abort
@ -61,17 +65,9 @@ function! ale#command#CreateDirectory(buffer) abort
endfunction
function! ale#command#RemoveManagedFiles(buffer) abort
let l:info = get(s:managed_data, a:buffer, {})
let l:info = get(s:buffer_data, a:buffer, {})
if !empty(l:info)
\&& (
\ !exists('*ale#engine#IsCheckingBuffer')
\ || !ale#engine#IsCheckingBuffer(a:buffer)
\)
\&& (
\ !has_key(g:ale_fix_buffer_data, a:buffer)
\ || g:ale_fix_buffer_data[a:buffer].done
\)
if !empty(l:info) && empty(l:info.jobs)
" We can't delete anything in a sandbox, so wait until we escape from
" it to delete temporary files and directories.
if ale#util#InSandbox()
@ -91,7 +87,7 @@ function! ale#command#RemoveManagedFiles(buffer) abort
call delete(l:directory, 'rf')
endfor
call remove(s:managed_data, a:buffer)
call remove(s:buffer_data, a:buffer)
endif
endfunction
@ -187,3 +183,148 @@ function! ale#command#FormatCommand(buffer, executable, command, pipe_file_if_ne
return [l:temporary_file, l:command, l:file_created]
endfunction
function! ale#command#StopJobs(buffer, job_type) abort
let l:info = get(s:buffer_data, a:buffer, {})
if !empty(l:info)
let l:new_map = {}
for [l:job_id, l:job_type] in items(l:info.jobs)
let l:job_id = str2nr(l:job_id)
if a:job_type is# 'all' || a:job_type is# l:job_type
call ale#job#Stop(l:job_id)
else
let l:new_map[l:job_id] = l:job_type
endif
endfor
let l:info.jobs = l:new_map
endif
endfunction
function! s:GatherOutput(line_list, job_id, line) abort
call add(a:line_list, a:line)
endfunction
function! s:ExitCallback(buffer, line_list, Callback, data) abort
if !has_key(s:buffer_data, a:buffer)
return
endif
let l:jobs = s:buffer_data[a:buffer].jobs
if !has_key(l:jobs, a:data.job_id)
return
endif
let l:job_type = remove(l:jobs, a:data.job_id)
if g:ale_history_enabled
call ale#history#SetExitCode(a:buffer, a:data.job_id, a:data.exit_code)
" Log the output of the command for ALEInfo if we should.
if g:ale_history_log_output && a:data.log_output is 1
call ale#history#RememberOutput(
\ a:buffer,
\ a:data.job_id,
\ a:line_list[:]
\)
endif
endif
" If the callback starts any new jobs, use the same job type for them.
call setbufvar(a:buffer, 'ale_job_type', l:job_type)
call a:Callback(a:buffer, a:line_list, a:data)
endfunction
function! ale#command#Run(buffer, command, options) abort
let l:Callback = a:options.callback
let l:output_stream = get(a:options, 'output_stream', 'stdout')
let l:line_list = []
let [l:temporary_file, l:command, l:file_created] = ale#command#FormatCommand(
\ a:buffer,
\ get(a:options, 'executable', ''),
\ a:command,
\ get(a:options, 'read_buffer', 0),
\ get(a:options, 'input', v:null),
\)
let l:command = ale#job#PrepareCommand(a:buffer, l:command)
let l:job_options = {
\ 'exit_cb': {job_id, exit_code -> s:ExitCallback(
\ a:buffer,
\ l:line_list,
\ l:Callback,
\ {
\ 'job_id': job_id,
\ 'exit_code': exit_code,
\ 'temporary_file': l:temporary_file,
\ 'log_output': get(a:options, 'log_output', 1),
\ }
\ )},
\ 'mode': 'nl',
\}
if l:output_stream is# 'stdout'
let l:job_options.out_cb = function('s:GatherOutput', [l:line_list])
elseif l:output_stream is# 'stderr'
let l:job_options.err_cb = function('s:GatherOutput', [l:line_list])
elseif l:output_stream is# 'both'
let l:job_options.out_cb = function('s:GatherOutput', [l:line_list])
let l:job_options.err_cb = function('s:GatherOutput', [l:line_list])
endif
let l:status = 'failed'
if get(g:, 'ale_run_synchronously') == 1
if get(g:, 'ale_emulate_job_failure') == 1
let l:job_id = 0
else
" Generate a fake job ID for tests.
let s:fake_job_id = get(s:, 'fake_job_id', 0) + 1
let l:job_id = s:fake_job_id
endif
else
let l:job_id = ale#job#Start(l:command, l:job_options)
endif
if l:job_id
let l:status = 'started'
let l:job_type = getbufvar(a:buffer, 'ale_job_type', 'all')
call ale#command#InitData(a:buffer)
let s:buffer_data[a:buffer].jobs[l:job_id] = l:job_type
endif
if g:ale_history_enabled
call ale#history#Add(a:buffer, l:status, l:job_id, l:command)
endif
if get(g:, 'ale_run_synchronously') == 1 && l:job_id
" Run a command synchronously if this test option is set.
call extend(l:line_list, systemlist(
\ type(l:command) is v:t_list
\ ? join(l:command[0:1]) . ' ' . ale#Escape(l:command[2])
\ : l:command
\))
" Don't capture output when the callbacks aren't set.
if !has_key(l:job_options, 'out_cb')
\&& !has_key(l:job_options, 'err_cb')
let l:line_list = []
endif
if !exists('g:ale_run_synchronously_callbacks')
let g:ale_run_synchronously_callbacks = []
endif
call add(
\ g:ale_run_synchronously_callbacks,
\ {-> l:job_options.exit_cb(l:job_id, v:shell_error)}
\)
endif
return l:job_id ? v:true : v:false
endfunction

View File

@ -5,20 +5,10 @@
" Remapping of linter problems.
let g:ale_type_map = get(g:, 'ale_type_map', {})
" Stores information for each job including:
"
" linter: The linter dictionary for the job.
" buffer: The buffer number for the job.
" output: The array of lines for the output of the job.
if !has_key(s:, 'job_info_map')
let s:job_info_map = {}
endif
if !has_key(s:, 'executable_cache_map')
let s:executable_cache_map = {}
endif
function! ale#engine#CleanupEveryBuffer() abort
for l:key in keys(g:ale_buffer_info)
" The key could be a filename or a buffer number, so try and
@ -71,11 +61,9 @@ endfunction
function! ale#engine#InitBufferInfo(buffer) abort
if !has_key(g:ale_buffer_info, a:buffer)
" job_list will hold the list of job IDs
" active_linter_list will hold the list of active linter names
" loclist holds the loclist items after all jobs have completed.
let g:ale_buffer_info[a:buffer] = {
\ 'job_list': [],
\ 'active_linter_list': [],
\ 'active_other_sources_list': [],
\ 'loclist': [],
@ -121,12 +109,6 @@ function! ale#engine#CreateDirectory(buffer) abort
return ale#command#CreateDirectory(a:buffer)
endfunction
function! s:GatherOutput(job_id, line) abort
if has_key(s:job_info_map, a:job_id)
call add(s:job_info_map[a:job_id].output, a:line)
endif
endfunction
function! ale#engine#HandleLoclist(linter_name, buffer, loclist, from_other_source) abort
let l:info = get(g:ale_buffer_info, a:buffer, {})
@ -137,7 +119,7 @@ function! ale#engine#HandleLoclist(linter_name, buffer, loclist, from_other_sour
if !a:from_other_source
" Remove this linter from the list of active linters.
" This may have already been done when the job exits.
call filter(l:info.active_linter_list, 'v:val isnot# a:linter_name')
call filter(l:info.active_linter_list, 'v:val.name isnot# a:linter_name')
endif
" Make some adjustments to the loclists to fix common problems, and also
@ -170,27 +152,19 @@ function! ale#engine#HandleLoclist(linter_name, buffer, loclist, from_other_sour
call ale#engine#SetResults(a:buffer, l:info.loclist)
endfunction
function! s:HandleExit(job_id, exit_code) abort
if !has_key(s:job_info_map, a:job_id)
function! s:HandleExit(job_info, buffer, output, data) abort
let l:buffer_info = get(g:ale_buffer_info, a:buffer)
if empty(l:buffer_info)
return
endif
let l:job_info = s:job_info_map[a:job_id]
let l:linter = l:job_info.linter
let l:output = l:job_info.output
let l:buffer = l:job_info.buffer
let l:executable = l:job_info.executable
let l:next_chain_index = l:job_info.next_chain_index
if g:ale_history_enabled
call ale#history#SetExitCode(l:buffer, a:job_id, a:exit_code)
endif
let l:linter = a:job_info.linter
let l:executable = a:job_info.executable
let l:next_chain_index = a:job_info.next_chain_index
" Remove this job from the list.
call ale#job#Stop(a:job_id)
call remove(s:job_info_map, a:job_id)
call filter(g:ale_buffer_info[l:buffer].job_list, 'v:val isnot# a:job_id')
call filter(g:ale_buffer_info[l:buffer].active_linter_list, 'v:val isnot# l:linter.name')
call filter(l:buffer_info.active_linter_list, 'v:val.name isnot# l:linter.name')
" Stop here if we land in the handle for a job completing if we're in
" a sandbox.
@ -198,29 +172,24 @@ function! s:HandleExit(job_id, exit_code) abort
return
endif
if has('nvim') && !empty(l:output) && empty(l:output[-1])
call remove(l:output, -1)
if has('nvim') && !empty(a:output) && empty(a:output[-1])
call remove(a:output, -1)
endif
if l:next_chain_index < len(get(l:linter, 'command_chain', []))
call s:InvokeChain(l:buffer, l:executable, l:linter, l:next_chain_index, l:output)
call s:InvokeChain(a:buffer, l:executable, l:linter, l:next_chain_index, a:output)
return
endif
" Log the output of the command for ALEInfo if we should.
if g:ale_history_enabled && g:ale_history_log_output
call ale#history#RememberOutput(l:buffer, a:job_id, l:output[:])
endif
try
let l:loclist = ale#util#GetFunction(l:linter.callback)(l:buffer, l:output)
let l:loclist = ale#util#GetFunction(l:linter.callback)(a:buffer, a:output)
" Handle the function being unknown, or being deleted.
catch /E700/
let l:loclist = []
endtry
call ale#engine#HandleLoclist(l:linter.name, l:buffer, l:loclist, 0)
call ale#engine#HandleLoclist(l:linter.name, a:buffer, l:loclist, 0)
endfunction
function! ale#engine#SetResults(buffer, loclist) abort
@ -431,7 +400,7 @@ function! s:RunJob(options) abort
let l:command = a:options.command
if empty(l:command)
return 0
return v:false
endif
let l:executable = a:options.executable
@ -442,87 +411,37 @@ function! s:RunJob(options) abort
let l:read_buffer = a:options.read_buffer
let l:info = g:ale_buffer_info[l:buffer]
let [l:temporary_file, l:command, l:file_created] = ale#command#FormatCommand(
\ l:buffer,
\ l:executable,
\ l:command,
\ l:read_buffer,
\ v:null,
\)
if l:file_created
" If a temporary filename has been formatted in to the command, then
" we do not need to send the Vim buffer to the command.
let l:read_buffer = 0
endif
let l:command = ale#job#PrepareCommand(l:buffer, l:command)
let l:job_options = {
\ 'mode': 'nl',
\ 'exit_cb': function('s:HandleExit'),
\}
if l:output_stream is# 'stderr'
let l:job_options.err_cb = function('s:GatherOutput')
elseif l:output_stream is# 'both'
let l:job_options.out_cb = function('s:GatherOutput')
let l:job_options.err_cb = function('s:GatherOutput')
else
let l:job_options.out_cb = function('s:GatherOutput')
endif
if get(g:, 'ale_run_synchronously') == 1
" Find a unique Job value to use, which will be the same as the ID for
" running commands synchronously. This is only for test code.
let l:job_id = len(s:job_info_map) + 1
while has_key(s:job_info_map, l:job_id)
let l:job_id += 1
endwhile
else
let l:job_id = ale#job#Start(l:command, l:job_options)
endif
let l:status = 'failed'
let l:run = ale#command#Run(l:buffer, l:command, {
\ 'output_stream': l:output_stream,
\ 'executable': l:executable,
\ 'read_buffer': l:read_buffer,
\ 'log_output': l:next_chain_index >= len(get(l:linter, 'command_chain', [])),
\ 'callback': function('s:HandleExit', [{
\ 'linter': l:linter,
\ 'executable': l:executable,
\ 'next_chain_index': l:next_chain_index,
\ }]),
\})
" Only proceed if the job is being run.
if l:job_id
" Add the job to the list of jobs, so we can track them.
call add(l:info.job_list, l:job_id)
if l:run
let l:found = 0
if index(l:info.active_linter_list, l:linter.name) < 0
call add(l:info.active_linter_list, l:linter.name)
for l:other_linter in l:info.active_linter_list
if l:other_linter.name is# l:linter.name
let l:found = 1
break
endif
endfor
if !l:found
call add(l:info.active_linter_list, l:linter)
endif
let l:status = 'started'
" Store the ID for the job in the map to read back again.
let s:job_info_map[l:job_id] = {
\ 'linter': l:linter,
\ 'buffer': l:buffer,
\ 'executable': l:executable,
\ 'output': [],
\ 'next_chain_index': l:next_chain_index,
\}
silent doautocmd <nomodeline> User ALEJobStarted
endif
if g:ale_history_enabled
call ale#history#Add(l:buffer, l:status, l:job_id, l:command)
endif
if get(g:, 'ale_run_synchronously') == 1
" Run a command synchronously if this test option is set.
let s:job_info_map[l:job_id].output = systemlist(
\ type(l:command) is v:t_list
\ ? join(l:command[0:1]) . ' ' . ale#Escape(l:command[2])
\ : l:command
\)
call l:job_options.exit_cb(l:job_id, v:shell_error)
endif
return l:job_id != 0
return l:run
endfunction
" Determine which commands to run for a link in a command chain, or
@ -599,35 +518,26 @@ function! s:InvokeChain(buffer, executable, linter, chain_index, input) abort
endfunction
function! s:StopCurrentJobs(buffer, include_lint_file_jobs) abort
call ale#command#StopJobs(a:buffer, 'linter')
if a:include_lint_file_jobs
call ale#command#StopJobs(a:buffer, 'file_linter')
endif
let l:info = get(g:ale_buffer_info, a:buffer, {})
let l:new_job_list = []
let l:new_active_linter_list = []
for l:job_id in get(l:info, 'job_list', [])
let l:job_info = get(s:job_info_map, l:job_id, {})
if !empty(l:job_info)
if a:include_lint_file_jobs || !l:job_info.linter.lint_file
call ale#job#Stop(l:job_id)
call remove(s:job_info_map, l:job_id)
else
call add(l:new_job_list, l:job_id)
" Linters with jobs still running are still active.
call add(l:new_active_linter_list, l:job_info.linter.name)
endif
for l:linter in get(l:info, 'active_linter_list', [])
" Keep jobs for linting files when we're only linting buffers.
if !a:include_lint_file_jobs && get(l:linter, 'lint_file')
call add(l:new_active_linter_list, l:linter)
endif
endfor
" Remove duplicates from the active linter list.
call uniq(sort(l:new_active_linter_list))
" Update the List, so it includes only the jobs we still need.
let l:info.job_list = l:new_job_list
" Update the active linter list, clearing out anything not running.
let l:info.active_linter_list = l:new_active_linter_list
endfunction
function! s:RemoveProblemsForDisabledLinters(buffer, linters) abort
" Figure out which linters are still enabled, and remove
" problems for linters which are no longer enabled.
@ -687,6 +597,10 @@ function! s:RunLinter(buffer, linter) abort
else
let l:executable = ale#linter#GetExecutable(a:buffer, a:linter)
" Use different job types for file or linter jobs.
let l:job_type = a:linter.lint_file ? 'file_linter' : 'linter'
call setbufvar(a:buffer, 'ale_job_type', l:job_type)
if ale#engine#IsExecutable(a:buffer, l:executable)
return s:InvokeChain(a:buffer, l:executable, a:linter, 0, [])
endif
@ -757,90 +671,3 @@ function! ale#engine#GetLoclist(buffer) abort
return g:ale_buffer_info[a:buffer].loclist
endfunction
" This function can be called with a timeout to wait for all jobs to finish.
" If the jobs to not finish in the given number of milliseconds,
" an exception will be thrown.
"
" The time taken will be a very rough approximation, and more time may be
" permitted than is specified.
function! ale#engine#WaitForJobs(deadline) abort
let l:start_time = ale#events#ClockMilliseconds()
if l:start_time == 0
throw 'Failed to read milliseconds from the clock!'
endif
let l:job_list = []
" Gather all of the jobs from every buffer.
for l:info in values(g:ale_buffer_info)
call extend(l:job_list, get(l:info, 'job_list', []))
endfor
" NeoVim has a built-in API for this, so use that.
if has('nvim')
let l:nvim_code_list = jobwait(l:job_list, a:deadline)
if index(l:nvim_code_list, -1) >= 0
throw 'Jobs did not complete on time!'
endif
return
endif
let l:should_wait_more = 1
while l:should_wait_more
let l:should_wait_more = 0
for l:job_id in l:job_list
if ale#job#IsRunning(l:job_id)
let l:now = ale#events#ClockMilliseconds()
if l:now - l:start_time > a:deadline
" Stop waiting after a timeout, so we don't wait forever.
throw 'Jobs did not complete on time!'
endif
" Wait another 10 milliseconds
let l:should_wait_more = 1
sleep 10ms
break
endif
endfor
endwhile
" Sleep for a small amount of time after all jobs finish.
" This seems to be enough to let handlers after jobs end run, and
" prevents the occasional failure where this function exits after jobs
" end, but before handlers are run.
sleep 10ms
" We must check the buffer data again to see if new jobs started
" for command_chain linters.
let l:has_new_jobs = 0
" Check again to see if any jobs are running.
for l:info in values(g:ale_buffer_info)
for l:job_id in get(l:info, 'job_list', [])
if ale#job#IsRunning(l:job_id)
let l:has_new_jobs = 1
break
endif
endfor
endfor
if l:has_new_jobs
" We have to wait more. Offset the timeout by the time taken so far.
let l:now = ale#events#ClockMilliseconds()
let l:new_deadline = a:deadline - (l:now - l:start_time)
if l:new_deadline <= 0
" Enough time passed already, so stop immediately.
throw 'Jobs did not complete on time!'
endif
call ale#engine#WaitForJobs(l:new_deadline)
endif
endfunction

View File

@ -1,13 +1,3 @@
if !has_key(s:, 'job_info_map')
let s:job_info_map = {}
endif
function! s:GatherOutput(job_id, line) abort
if has_key(s:job_info_map, a:job_id)
call add(s:job_info_map[a:job_id].output, a:line)
endif
endfunction
" Apply fixes queued up for buffers which may be hidden.
" Vim doesn't let you modify hidden buffers.
function! ale#fix#ApplyQueuedFixes() abort
@ -94,52 +84,48 @@ function! ale#fix#ApplyFixes(buffer, output) abort
call ale#fix#ApplyQueuedFixes()
endfunction
function! s:HandleExit(job_id, exit_code) abort
if !has_key(s:job_info_map, a:job_id)
function! s:HandleExit(job_info, buffer, job_output, data) abort
let l:buffer_info = get(g:ale_fix_buffer_data, a:buffer, {})
if empty(l:buffer_info)
return
endif
let l:job_info = remove(s:job_info_map, a:job_id)
let l:buffer = l:job_info.buffer
if g:ale_history_enabled
call ale#history#SetExitCode(l:buffer, a:job_id, a:exit_code)
if a:job_info.read_temporary_file
let l:output = !empty(a:data.temporary_file)
\ ? readfile(a:data.temporary_file)
\ : []
else
let l:output = a:job_output
endif
if has_key(l:job_info, 'file_to_read')
let l:job_info.output = readfile(l:job_info.file_to_read)
endif
let l:ChainCallback = get(l:job_info, 'chain_with', v:null)
let l:ProcessWith = get(l:job_info, 'process_with', v:null)
let l:ChainCallback = get(a:job_info, 'chain_with', v:null)
let l:ProcessWith = get(a:job_info, 'process_with', v:null)
" Post-process the output with a function if we have one.
if l:ProcessWith isnot v:null
let l:job_info.output = call(
\ ale#util#GetFunction(l:ProcessWith),
\ [l:buffer, l:job_info.output]
\)
let l:output = call(l:ProcessWith, [a:buffer, l:output])
endif
" Use the output of the job for changing the file if it isn't empty,
" otherwise skip this job and use the input from before.
"
" We'll use the input from before for chained commands.
if l:ChainCallback is v:null && !empty(split(join(l:job_info.output)))
let l:input = l:job_info.output
if l:ChainCallback is v:null && !empty(split(join(l:output)))
let l:input = l:output
else
let l:input = l:job_info.input
let l:input = a:job_info.input
endif
let l:next_index = l:ChainCallback is v:null
\ ? l:job_info.callback_index + 1
\ : l:job_info.callback_index
\ ? a:job_info.callback_index + 1
\ : a:job_info.callback_index
call s:RunFixer({
\ 'buffer': l:buffer,
\ 'buffer': a:buffer,
\ 'input': l:input,
\ 'output': l:job_info.output,
\ 'callback_list': l:job_info.callback_list,
\ 'output': l:output,
\ 'callback_list': a:job_info.callback_list,
\ 'callback_index': l:next_index,
\ 'chain_callback': l:ChainCallback,
\})
@ -149,15 +135,13 @@ function! s:RunJob(options) abort
let l:buffer = a:options.buffer
let l:command = a:options.command
let l:input = a:options.input
let l:output_stream = a:options.output_stream
let l:read_temporary_file = a:options.read_temporary_file
let l:ChainWith = a:options.chain_with
let l:read_buffer = a:options.read_buffer
if empty(l:command)
" If there's nothing further to chain the command with, stop here.
if l:ChainWith is v:null
return 0
return v:false
endif
" If there's another chained callback to run, then run that.
@ -170,87 +154,30 @@ function! s:RunJob(options) abort
\ 'output': [],
\})
return 1
return v:true
endif
let [l:temporary_file, l:command, l:file_created] = ale#command#FormatCommand(
\ l:buffer,
\ '',
\ l:command,
\ l:read_buffer,
\ l:input,
\)
let l:output_stream = a:options.output_stream
let l:command = ale#job#PrepareCommand(l:buffer, l:command)
let l:job_options = {
\ 'mode': 'nl',
\ 'exit_cb': function('s:HandleExit'),
\}
if a:options.read_temporary_file
let l:output_stream = 'none'
endif
let l:job_info = {
\ 'buffer': l:buffer,
return ale#command#Run(l:buffer, l:command, {
\ 'output_stream': l:output_stream,
\ 'executable': '',
\ 'read_buffer': l:read_buffer,
\ 'input': l:input,
\ 'output': [],
\ 'chain_with': l:ChainWith,
\ 'callback_index': a:options.callback_index,
\ 'callback_list': a:options.callback_list,
\ 'process_with': a:options.process_with,
\}
if l:read_temporary_file
" TODO: Check that a temporary file is set here.
let l:job_info.file_to_read = l:temporary_file
elseif l:output_stream is# 'stderr'
let l:job_options.err_cb = function('s:GatherOutput')
elseif l:output_stream is# 'both'
let l:job_options.out_cb = function('s:GatherOutput')
let l:job_options.err_cb = function('s:GatherOutput')
else
let l:job_options.out_cb = function('s:GatherOutput')
endif
if get(g:, 'ale_emulate_job_failure') == 1
let l:job_id = 0
elseif get(g:, 'ale_run_synchronously') == 1
" Find a unique Job value to use, which will be the same as the ID for
" running commands synchronously. This is only for test code.
let l:job_id = len(s:job_info_map) + 1
while has_key(s:job_info_map, l:job_id)
let l:job_id += 1
endwhile
else
let l:job_id = ale#job#Start(l:command, l:job_options)
endif
let l:status = l:job_id ? 'started' : 'failed'
if g:ale_history_enabled
call ale#history#Add(l:buffer, l:status, l:job_id, l:command)
endif
if l:job_id == 0
return 0
endif
let s:job_info_map[l:job_id] = l:job_info
if get(g:, 'ale_run_synchronously') == 1
" Run a command synchronously if this test option is set.
let l:output = systemlist(
\ type(l:command) is v:t_list
\ ? join(l:command[0:1]) . ' ' . ale#Escape(l:command[2])
\ : l:command
\)
if !l:read_temporary_file
let s:job_info_map[l:job_id].output = l:output
endif
call l:job_options.exit_cb(l:job_id, v:shell_error)
endif
return 1
\ 'log_output': 0,
\ 'callback': function('s:HandleExit', [{
\ 'input': l:input,
\ 'chain_with': l:ChainWith,
\ 'callback_index': a:options.callback_index,
\ 'callback_list': a:options.callback_list,
\ 'process_with': a:options.process_with,
\ 'read_temporary_file': a:options.read_temporary_file,
\ }]),
\})
endfunction
function! s:RunFixer(options) abort
@ -259,6 +186,9 @@ function! s:RunFixer(options) abort
let l:index = a:options.callback_index
let l:ChainCallback = get(a:options, 'chain_callback', v:null)
" Record new jobs started as fixer jobs.
call setbufvar(l:buffer, 'ale_job_type', 'fixer')
while len(a:options.callback_list) > l:index
let l:Function = l:ChainCallback isnot v:null
\ ? ale#util#GetFunction(l:ChainCallback)
@ -419,16 +349,7 @@ function! ale#fix#Fix(buffer, fixing_flag, ...) abort
return 0
endif
for l:job_id in keys(s:job_info_map)
call remove(s:job_info_map, l:job_id)
call ale#job#Stop(l:job_id)
endfor
" Mark the buffer as `done` so files can be removed.
if has_key(g:ale_fix_buffer_data, a:buffer)
let g:ale_fix_buffer_data[a:buffer].done = 1
endif
call ale#command#StopJobs(a:buffer, 'fixer')
" Clean up any files we might have left behind from a previous run.
call ale#command#RemoveManagedFiles(a:buffer)
call ale#fix#InitBufferData(a:buffer, a:fixing_flag)

View File

@ -99,7 +99,8 @@ function! s:VimCloseCallback(channel) abort
if job_status(l:job) is# 'dead'
try
if !empty(l:info) && has_key(l:info, 'exit_cb')
call ale#util#GetFunction(l:info.exit_cb)(l:job_id, get(l:info, 'exit_code', 1))
" We have to remove the callback, so we don't call it twice.
call ale#util#GetFunction(remove(l:info, 'exit_cb'))(l:job_id, get(l:info, 'exit_code', 1))
endif
finally
" Automatically forget about the job after it's done.
@ -124,7 +125,8 @@ function! s:VimExitCallback(job, exit_code) abort
if ch_status(job_getchannel(a:job)) is# 'closed'
try
if !empty(l:info) && has_key(l:info, 'exit_cb')
call ale#util#GetFunction(l:info.exit_cb)(l:job_id, a:exit_code)
" We have to remove the callback, so we don't call it twice.
call ale#util#GetFunction(remove(l:info, 'exit_cb'))(l:job_id, a:exit_code)
endif
finally
" Automatically forget about the job after it's done.

View File

@ -292,8 +292,17 @@ function! ale#lsp_linter#CheckWithLSP(buffer, linter) abort
endif
if l:notified
if index(l:info.active_linter_list, a:linter.name) < 0
call add(l:info.active_linter_list, a:linter.name)
let l:found = 0
for l:other_linter in l:info.active_linter_list
if l:other_linter.name is# a:linter.name
let l:found = 1
break
endif
endfor
if !l:found
call add(l:info.active_linter_list, a:linter)
endif
endif

View File

@ -85,3 +85,103 @@ function! ale#test#GetPreviewWindowText() abort
endif
endfor
endfunction
" This function can be called with a timeout to wait for all jobs to finish.
" If the jobs to not finish in the given number of milliseconds,
" an exception will be thrown.
"
" The time taken will be a very rough approximation, and more time may be
" permitted than is specified.
function! ale#test#WaitForJobs(deadline) abort
let l:start_time = ale#events#ClockMilliseconds()
if l:start_time == 0
throw 'Failed to read milliseconds from the clock!'
endif
let l:job_list = []
" Gather all of the jobs from every buffer.
for [l:buffer, l:data] in items(ale#command#GetData())
call extend(l:job_list, map(keys(l:data.jobs), 'str2nr(v:val)'))
endfor
" NeoVim has a built-in API for this, so use that.
if has('nvim')
let l:nvim_code_list = jobwait(l:job_list, a:deadline)
if index(l:nvim_code_list, -1) >= 0
throw 'Jobs did not complete on time!'
endif
return
endif
let l:should_wait_more = 1
while l:should_wait_more
let l:should_wait_more = 0
for l:job_id in l:job_list
if ale#job#IsRunning(l:job_id)
let l:now = ale#events#ClockMilliseconds()
if l:now - l:start_time > a:deadline
" Stop waiting after a timeout, so we don't wait forever.
throw 'Jobs did not complete on time!'
endif
" Wait another 10 milliseconds
let l:should_wait_more = 1
sleep 10ms
break
endif
endfor
endwhile
" Sleep for a small amount of time after all jobs finish.
" This seems to be enough to let handlers after jobs end run, and
" prevents the occasional failure where this function exits after jobs
" end, but before handlers are run.
sleep 10ms
" We must check the buffer data again to see if new jobs started
" for command_chain linters.
let l:has_new_jobs = 0
" Check again to see if any jobs are running.
for l:info in values(g:ale_buffer_info)
for [l:job_id, l:linter] in get(l:info, 'job_list', [])
if ale#job#IsRunning(l:job_id)
let l:has_new_jobs = 1
break
endif
endfor
endfor
if l:has_new_jobs
" We have to wait more. Offset the timeout by the time taken so far.
let l:now = ale#events#ClockMilliseconds()
let l:new_deadline = a:deadline - (l:now - l:start_time)
if l:new_deadline <= 0
" Enough time passed already, so stop immediately.
throw 'Jobs did not complete on time!'
endif
call ale#test#WaitForJobs(l:new_deadline)
endif
endfunction
function! ale#test#FlushJobs() abort
" The variable is checked for in a loop, as calling one series of
" callbacks can trigger a further series of callbacks.
while exists('g:ale_run_synchronously_callbacks')
let l:callbacks = g:ale_run_synchronously_callbacks
unlet g:ale_run_synchronously_callbacks
for l:Callback in l:callbacks
call l:Callback()
endfor
endwhile
endfunction

View File

@ -2669,9 +2669,9 @@ ale#command#ManageDirectory(buffer, directory) *ale#command#ManageDirectory()*
ale#command#ManageFile(buffer, filename) *ale#command#ManageFile()*
Given a buffer number for a buffer currently running some linting tasks
and a filename, register a filename with ALE for automatic deletion after
linting is complete, or when Vim exits.
Given a buffer number for a buffer currently running some linting or fixing
tasks and a filename, register a filename with ALE for automatic deletion
after linting or fixing is complete, or when Vim exits.
If Vim exits suddenly, ALE will try its best to remove temporary files, but
ALE cannot guarantee with absolute certainty that the files will be removed.
@ -2682,7 +2682,7 @@ ale#command#ManageFile(buffer, filename) *ale#command#ManageFile()*
files and symlinks given to this function. This is to prevent entire
directories from being accidentally deleted, say in cases of writing
`dir . '/' . filename` where `filename` is actually `''`, etc. ALE instead
manages directories separetly with the |ale#command#ManageDirectory| function.
manages directories separately with the |ale#command#ManageDirectory| function.
ale#engine#GetLoclist(buffer) *ale#engine#GetLoclist()*

View File

@ -11,6 +11,7 @@ Before:
let g:ale_enabled = 0
let g:ale_echo_cursor = 0
let g:ale_run_synchronously = 1
unlet! g:ale_run_synchronously_callbacks
let g:ale_set_lists_synchronously = 1
let g:ale_fix_buffer_data = {}
let g:ale_fixers = {
@ -59,6 +60,10 @@ Before:
return {'command': 'echo x > %t', 'read_temporary_file': 1}
endfunction
function CatWithTempFile(buffer, done, lines) abort
return {'command': 'cat %t <(echo d)'}
endfunction
function RemoveLastLine(buffer, done, lines) abort
return ['a', 'b']
endfunction
@ -177,6 +182,7 @@ After:
Restore
unlet! g:ale_run_synchronously
unlet! g:ale_set_lists_synchronously
unlet! g:ale_run_synchronously_callbacks
unlet! g:ale_emulate_job_failure
unlet! b:ale_fixers
unlet! b:ale_fix_on_save
@ -187,6 +193,7 @@ After:
delfunction CatLine
delfunction CatLineOneArg
delfunction ReplaceWithTempFile
delfunction CatWithTempFile
delfunction RemoveLastLine
delfunction RemoveLastLineOneArg
delfunction TestCallback
@ -235,11 +242,13 @@ Given testft (A file with three lines):
Execute(ALEFix should complain when there are no functions to call):
ALEFix
call ale#test#FlushJobs()
AssertEqual 'No fixers have been defined. Try :ALEFixSuggest', GetLastMessage()
Execute(ALEFix should apply simple functions):
let g:ale_fixers.testft = ['AddCarets']
ALEFix
call ale#test#FlushJobs()
Expect(The first function should be used):
^a
@ -249,6 +258,7 @@ Expect(The first function should be used):
Execute(ALEFix should apply simple functions in a chain):
let g:ale_fixers.testft = ['AddCarets', 'AddDollars']
ALEFix
call ale#test#FlushJobs()
Expect(Both functions should be used):
$^a
@ -258,6 +268,7 @@ Expect(Both functions should be used):
Execute(ALEFix should allow 0 to be returned to skip functions):
let g:ale_fixers.testft = ['DoNothing', 'AddDollars']
ALEFix
call ale#test#FlushJobs()
Expect(Only the second function should be applied):
$a
@ -268,6 +279,7 @@ Execute(The * fixers shouldn't be used if an empty list is set for fixers):
let g:ale_fixers.testft = []
let g:ale_fixers['*'] = ['AddDollars']
ALEFix
call ale#test#FlushJobs()
Expect(Nothing should be changed):
a
@ -277,6 +289,7 @@ Expect(Nothing should be changed):
Execute(* fixers should be used if no filetype is matched):
let g:ale_fixers = {'*': ['AddDollars']}
ALEFix
call ale#test#FlushJobs()
Expect(The file should be changed):
$a
@ -290,6 +303,7 @@ Execute(ALEFix should allow commands to be run):
else
let g:ale_fixers.testft = ['CatLine']
ALEFix
call ale#test#FlushJobs()
endif
Expect(An extra line should be added):
@ -301,6 +315,7 @@ Expect(An extra line should be added):
Execute(ALEFix should use fixers passed in commandline when provided):
let g:ale_fixers.testft = ['RemoveLastLine']
ALEFix AddCarets AddDollars
call ale#test#FlushJobs()
Expect(Only fixers passed via command line should be run):
$^a
@ -315,11 +330,28 @@ Execute(ALEFix should allow temporary files to be read):
else
let g:ale_fixers.testft = ['ReplaceWithTempFile']
ALEFix
call ale#test#FlushJobs()
endif
Expect(The line we wrote to the temporary file should be used here):
x
Execute(ALEFix should not read the temporary file when the option is not set):
if has('win32')
" Just skip this test on Windows, we can't run it.
call setline(1, ['a', 'b', 'c', 'd'])
else
let g:ale_fixers.testft = ['CatWithTempFile']
ALEFix
call ale#test#FlushJobs()
endif
Expect(An extra line should be added):
a
b
c
d
Execute(ALEFix should allow jobs and simple functions to be combined):
if has('win32')
" Just skip this test on Windows, we can't run it.
@ -328,6 +360,7 @@ Execute(ALEFix should allow jobs and simple functions to be combined):
else
let g:ale_fixers.testft = ['ReplaceWithTempFile', 'AddDollars']
ALEFix
call ale#test#FlushJobs()
endif
Expect(The lines from the temporary file should be modified):
@ -340,6 +373,7 @@ Execute(ALEFix should send lines modified by functions to jobs):
else
let g:ale_fixers.testft = ['AddDollars', 'CatLine']
ALEFix
call ale#test#FlushJobs()
endif
Expect(The lines should first be modified by the function, then the job):
@ -352,6 +386,7 @@ Execute(ALEFix should skip commands when jobs fail to run):
let g:ale_emulate_job_failure = 1
let g:ale_fixers.testft = ['CatLine', 'AddDollars']
ALEFix
call ale#test#FlushJobs()
Expect(Only the second function should be applied):
$a
@ -361,6 +396,7 @@ Expect(Only the second function should be applied):
Execute(ALEFix should handle strings for selecting a single function):
let g:ale_fixers.testft = 'AddCarets'
ALEFix
call ale#test#FlushJobs()
Expect(The first function should be used):
^a
@ -371,6 +407,7 @@ Execute(ALEFix should use functions from the registry):
call ale#fix#registry#Add('add_carets', 'AddCarets', [], 'Add some carets')
let g:ale_fixers.testft = ['add_carets']
ALEFix
call ale#test#FlushJobs()
Expect(The registry function should be used):
^a
@ -380,6 +417,7 @@ Expect(The registry function should be used):
Execute(ALEFix should be able to remove the last line for files):
let g:ale_fixers.testft = ['RemoveLastLine']
ALEFix
call ale#test#FlushJobs()
Expect(There should be only two lines):
a
@ -388,6 +426,7 @@ Expect(There should be only two lines):
Execute(ALEFix should accept funcrefs):
let g:ale_fixers.testft = [function('RemoveLastLine')]
ALEFix
call ale#test#FlushJobs()
Expect(There should be only two lines):
a
@ -401,6 +440,7 @@ Execute(ALEFix should accept lambdas):
else
let g:ale_fixers.testft = [{buffer, done, lines -> lines + ['d']}]
ALEFix
call ale#test#FlushJobs()
endif
Expect(There should be an extra line):
@ -413,6 +453,7 @@ Execute(ALEFix should user buffer-local fixer settings):
let g:ale_fixers.testft = ['AddCarets', 'AddDollars']
let b:ale_fixers = {'testft': ['RemoveLastLine']}
ALEFix
call ale#test#FlushJobs()
Expect(There should be only two lines):
a
@ -422,6 +463,7 @@ Execute(ALEFix should allow Lists to be used for buffer-local fixer settings):
let g:ale_fixers.testft = ['AddCarets', 'AddDollars']
let b:ale_fixers = ['RemoveLastLine']
ALEFix
call ale#test#FlushJobs()
Expect(There should be only two lines):
a
@ -447,6 +489,7 @@ Execute(ALEFix should fix files on the save event):
call SetUpLinters()
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
" We should save the file.
AssertEqual ['$a', '$b', '$c'], readfile('fix_test_file')
@ -518,6 +561,7 @@ Execute(ALEFix should still lint with no linters to be applied):
call SetUpLinters()
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
Assert !filereadable('fix_test_file'), 'The file should not have been saved'
@ -552,6 +596,7 @@ Execute(ALEFix should still lint when nothing was fixed on save):
call SetUpLinters()
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
Assert !filereadable('fix_test_file'), 'The file should not have been saved'
@ -597,6 +642,7 @@ Execute(ale#fix#InitBufferData() should set up the correct data):
Execute(ALEFix simple functions should be able to accept one argument, the buffer):
let g:ale_fixers.testft = ['RemoveLastLineOneArg']
ALEFix
call ale#test#FlushJobs()
Expect(There should be only two lines):
a
@ -632,6 +678,7 @@ Execute(ALEFix functions returning jobs should be able to accept one argument):
else
let g:ale_fixers.testft = ['CatLine']
ALEFix
call ale#test#FlushJobs()
endif
Expect(An extra line should be added):
@ -643,22 +690,26 @@ Expect(An extra line should be added):
Execute(ALE should print a message telling you something isn't a valid fixer when you type some nonsense):
let g:ale_fixers.testft = ['CatLine', 'invalidname']
ALEFix
call ale#test#FlushJobs()
AssertEqual 'There is no fixer named `invalidname`. Check :ALEFixSuggest', GetLastMessage()
Execute(ALE should complain about invalid fixers with minuses in the name):
let g:ale_fixers.testft = ['foo-bar']
ALEFix
call ale#test#FlushJobs()
AssertEqual 'There is no fixer named `foo-bar`. Check :ALEFixSuggest', GetLastMessage()
Execute(ALE should tolerate valid fixers with minuses in the name):
let g:ale_fixers.testft = ['prettier-standard']
ALEFix
call ale#test#FlushJobs()
Execute(Test fixing with chained callbacks):
let g:ale_fixers.testft = ['FirstChainCallback']
ALEFix
call ale#test#FlushJobs()
" The buffer shouldn't be piped in for earlier commands in the chain.
AssertEqual
@ -677,6 +728,7 @@ Expect(The echoed line should be added):
Execute(Test fixing with chained callback where the first command is skipped):
let g:ale_fixers.testft = ['FirstChainCallbackSkipped']
ALEFix
call ale#test#FlushJobs()
Expect(The default line should be added):
a
@ -687,6 +739,7 @@ Expect(The default line should be added):
Execute(Test fixing with chained callback where the second command is skipped):
let g:ale_fixers.testft = ['FirstChainCallbackSecondSkipped']
ALEFix
call ale#test#FlushJobs()
Expect(The default line should be added):
a
@ -697,6 +750,7 @@ Expect(The default line should be added):
Execute(Test fixing with chained callback where the final callback is skipped):
let g:ale_fixers.testft = ['ChainWhereLastIsSkipped']
ALEFix
call ale#test#FlushJobs()
Expect(The lines should be the same):
a
@ -706,6 +760,7 @@ Expect(The lines should be the same):
Execute(Empty output should be ignored):
let g:ale_fixers.testft = ['IgnoredEmptyOutput']
ALEFix
call ale#test#FlushJobs()
Expect(The lines should be the same):
a
@ -715,6 +770,7 @@ Expect(The lines should be the same):
Execute(A temporary file shouldn't be piped into the command when disabled):
let g:ale_fixers.testft = ['EchoLineNoPipe']
ALEFix
call ale#test#FlushJobs()
AssertEqual
\ string(ale#job#PrepareCommand(bufnr(''), 'echo new line')),
@ -731,6 +787,7 @@ Expect(The new line should be used):
Execute(Post-processing should work):
let g:ale_fixers.testft = ['FixWithJSONPostProcessing']
ALEFix
call ale#test#FlushJobs()
Expect(The lines in the JSON should be used):
x
@ -740,5 +797,7 @@ Expect(The lines in the JSON should be used):
Execute(ALEFix should apply autocmds):
let g:ale_fixers.testft = ['AddCarets']
ALEFix
call ale#test#FlushJobs()
AssertEqual g:pre_success, 1
AssertEqual g:post_success, 1

View File

@ -18,6 +18,9 @@ Before:
endfunction
call ale#engine#InitBufferInfo(bufnr(''))
" Call this function first, so we can be sure the module is loaded before we
" check if it exists.
call ale#lsp_linter#ClearLSPData()
call ale#linter#Define('testft', {
\ 'name': 'lsplinter',
@ -68,7 +71,10 @@ Execute(ALEStopAllLSPs should clear the loclist):
\ 'linter_name': 'otherlinter',
\ },
\]
let g:ale_buffer_info[bufnr('')].active_linter_list = ['lsplinter', 'otherlinter']
let g:ale_buffer_info[bufnr('')].active_linter_list = [
\ {'name': 'lsplinter'},
\ {'name': 'otherlinter'},
\]
ALEStopAllLSPs
@ -87,4 +93,6 @@ Execute(ALEStopAllLSPs should clear the loclist):
\]
" The LSP linter should be removed from the active linter list.
AssertEqual g:ale_buffer_info[bufnr('')].active_linter_list, ['otherlinter']
AssertEqual
\ ['otherlinter'],
\ map(copy(g:ale_buffer_info[bufnr('')].active_linter_list), 'v:val.name')

View File

@ -63,5 +63,6 @@ After:
Execute(The signs should be updated after linting is done):
ALELint
call ale#test#FlushJobs()
AssertEqual [['1', 'ALEWarningSign'], ['2', 'ALEErrorSign']], CollectSigns()

View File

@ -134,6 +134,7 @@ Given testft(A file with warnings/errors):
Execute(The current signs should be set for running a job):
ALELint
call ale#test#FlushJobs()
AssertEqual
\ [

View File

@ -66,7 +66,7 @@ Execute(Linters should run with the default options):
" where tests fail randomly.
for g:i in range(has('nvim-0.3') || has('win32') ? 5 : 1)
call ale#Queue(0, '')
call ale#engine#WaitForJobs(2000)
call ale#test#WaitForJobs(2000)
let g:results = ale#test#GetLoclistWithoutModule()
@ -110,7 +110,7 @@ Execute(Linters should run in PowerShell too):
\})
call ale#Queue(0, '')
call ale#engine#WaitForJobs(4000)
call ale#test#WaitForJobs(4000)
AssertEqual [
\ {
@ -140,7 +140,7 @@ Execute(Linters should run in PowerShell too):
Execute(Previous errors should be removed when linters change):
call ale#Queue(0, '')
call ale#engine#WaitForJobs(2000)
call ale#test#WaitForJobs(2000)
call ale#linter#Reset()
@ -167,7 +167,7 @@ Execute(Previous errors should be removed when linters change):
" where tests fail randomly.
for g:i in range(has('nvim-0.3') || has('win32') ? 5 : 1)
call ale#Queue(0, '')
call ale#engine#WaitForJobs(2000)
call ale#test#WaitForJobs(2000)
let g:results = ale#test#GetLoclistWithoutModule()

View File

@ -1,7 +1,9 @@
Before:
Save g:ale_buffer_info
Save g:ale_enabled
let g:ale_buffer_info = {}
let g:ale_enabled = 1
let g:expected_loclist = [{
\ 'bufnr': bufnr('%'),
@ -58,7 +60,7 @@ Execute(ALELint should run the linters):
" Try to run the linter a few times, as it fails randomly in NeoVim.
for b:i in range(5)
ALELint
call ale#engine#WaitForJobs(2000)
call ale#test#WaitForJobs(2000)
if !has('nvim')
" Sleep so the delayed list function can run.

View File

@ -10,6 +10,7 @@ Before:
let g:ale_set_signs = 1
let g:ale_set_lists_synchronously = 1
let g:ale_run_synchronously = 1
unlet! g:ale_run_synchronously_callbacks
let g:ale_pattern_options = {}
let g:ale_pattern_options_enabled = 1
let g:ale_set_balloons =
@ -85,6 +86,7 @@ Before:
After:
Restore
unlet! g:ale_run_synchronously_callbacks
unlet! g:expected_loclist
unlet! g:expected_groups
unlet! b:ale_enabled
@ -113,6 +115,7 @@ Execute(ALEToggle should reset everything and then run again):
AssertEqual 'foobar', &filetype
ALELint
call ale#test#FlushJobs()
" First check that everything is there...
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
@ -135,6 +138,7 @@ Execute(ALEToggle should reset everything and then run again):
" Toggle ALE on, everything should be set up and run again.
ALEToggle
call ale#test#FlushJobs()
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
@ -157,6 +161,7 @@ Execute(ALEToggle should skip filename keys and preserve them):
\}
ALELint
call ale#test#FlushJobs()
" Now Toggle ALE off.
ALEToggle
@ -174,6 +179,7 @@ Execute(ALEToggle should skip filename keys and preserve them):
" Toggle ALE on again.
ALEToggle
call ale#test#FlushJobs()
AssertEqual
\ {
@ -188,15 +194,18 @@ Execute(ALEToggle should skip filename keys and preserve them):
Execute(ALEDisable should reset everything and stay disabled):
ALELint
call ale#test#FlushJobs()
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
ALEDisable
call ale#test#FlushJobs()
AssertEqual [], ale#test#GetLoclistWithoutModule()
AssertEqual 0, g:ale_enabled
ALEDisable
call ale#test#FlushJobs()
AssertEqual [], ale#test#GetLoclistWithoutModule()
AssertEqual 0, g:ale_enabled
@ -205,6 +214,7 @@ Execute(ALEEnable should enable ALE and lint again):
let g:ale_enabled = 0
ALEEnable
call ale#test#FlushJobs()
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
AssertEqual 1, g:ale_enabled
@ -213,6 +223,7 @@ Execute(ALEReset should reset everything for a buffer):
AssertEqual 'foobar', &filetype
ALELint
call ale#test#FlushJobs()
" First check that everything is there...
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
@ -224,6 +235,7 @@ Execute(ALEReset should reset everything for a buffer):
" Now Toggle ALE off.
ALEReset
call ale#test#FlushJobs()
" Everything should be cleared.
Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'
@ -237,6 +249,7 @@ Execute(ALEToggleBuffer should reset everything and then run again):
AssertEqual 'foobar', &filetype
ALELint
call ale#test#FlushJobs()
" First check that everything is there...
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
@ -257,6 +270,7 @@ Execute(ALEToggleBuffer should reset everything and then run again):
" Toggle ALE on, everything should be set up and run again.
ALEToggleBuffer
call ale#test#FlushJobs()
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
@ -268,10 +282,12 @@ Execute(ALEToggleBuffer should reset everything and then run again):
Execute(ALEDisableBuffer should reset everything and stay disabled):
ALELint
call ale#test#FlushJobs()
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
ALEDisableBuffer
call ale#test#FlushJobs()
AssertEqual [], ale#test#GetLoclistWithoutModule()
AssertEqual 0, b:ale_enabled
@ -280,6 +296,7 @@ Execute(ALEEnableBuffer should enable ALE and lint again):
let b:ale_enabled = 0
ALEEnableBuffer
call ale#test#FlushJobs()
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
AssertEqual 1, b:ale_enabled
@ -303,6 +320,7 @@ Execute(ALEResetBuffer should reset everything for a buffer):
AssertEqual 'foobar', &filetype
ALELint
call ale#test#FlushJobs()
" First check that everything is there...
AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutModule()
@ -314,6 +332,7 @@ Execute(ALEResetBuffer should reset everything for a buffer):
" Now Toggle ALE off.
ALEResetBuffer
call ale#test#FlushJobs()
" Everything should be cleared.
Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'

View File

@ -1,6 +1,7 @@
Before:
Save &shell, g:ale_run_synchronously
let g:ale_run_synchronously = 1
unlet! g:ale_run_synchronously_callbacks
if !has('win32')
set shell=/bin/sh
@ -47,6 +48,7 @@ Before:
After:
Restore
unlet! g:ale_run_synchronously_callbacks
unlet! g:first_echo_called
unlet! g:second_echo_called
unlet! g:final_callback_called
@ -63,6 +65,7 @@ Given foobar (Some imaginary filetype):
Execute(Check the results of running the chain):
AssertEqual 'foobar', &filetype
call ale#Queue(0)
call ale#test#FlushJobs()
Assert g:first_echo_called, 'The first chain item was not called'
Assert g:second_echo_called, 'The second chain item was not called'

View File

@ -3,7 +3,6 @@ Before:
Save g:ale_buffer_info
Save g:ale_echo_cursor
Save g:ale_run_synchronously
Save g:ale_run_synchronously
Save g:ale_set_highlights
Save g:ale_set_loclist
Save g:ale_set_quickfix
@ -17,6 +16,7 @@ Before:
let g:ale_echo_cursor = 0
let g:ale_run_synchronously = 1
unlet! g:ale_run_synchronously_callbacks
call setloclist(0, [])
noautocmd let &filetype = 'foobar'
@ -44,6 +44,8 @@ Before:
After:
Restore
unlet! g:ale_run_synchronously_callbacks
delfunction TestCallback
call ale#linter#Reset()
@ -51,6 +53,7 @@ After:
Execute(Error should be removed when the filetype changes to something else we cannot check):
call ale#Queue(0)
call ale#test#FlushJobs()
sleep 1ms
AssertEqual 1, len(ale#test#GetLoclistWithoutModule())
@ -58,6 +61,7 @@ Execute(Error should be removed when the filetype changes to something else we c
noautocmd let &filetype = 'foobar2'
call ale#Queue(0)
call ale#test#FlushJobs()
sleep 1ms
" We should get some items from the second filetype.
@ -66,6 +70,7 @@ Execute(Error should be removed when the filetype changes to something else we c
noautocmd let &filetype = 'xxx'
call ale#Queue(0)
call ale#test#FlushJobs()
sleep 1ms
AssertEqual 0, len(ale#test#GetLoclistWithoutModule())

View File

@ -13,6 +13,7 @@ Before:
let g:ale_echo_cursor = 0
let g:ale_enabled = 1
let g:ale_run_synchronously = 1
unlet! g:ale_run_synchronously_callbacks
let g:ale_set_highlights = 0
let g:ale_set_loclist = 0
let g:ale_set_quickfix = 0
@ -41,6 +42,7 @@ Before:
After:
Restore
unlet! g:ale_run_synchronously_callbacks
unlet! g:output
delfunction TestCallback
@ -56,5 +58,6 @@ Execute(ALE should be able to read the %t file):
AssertEqual 'foobar', &filetype
ALELint
call ale#test#FlushJobs()
AssertEqual ['foo', 'bar', 'baz'], g:output

View File

@ -9,6 +9,7 @@ Before:
Save g:ale_set_signs
let g:ale_run_synchronously = 1
unlet! g:ale_run_synchronously_callbacks
let g:ale_set_highlights = 1
let g:ale_set_signs = 1
let g:ale_buffer_info = {}
@ -64,6 +65,7 @@ Before:
After:
Restore
unlet! g:ale_run_synchronously_callbacks
unlet! g:items
unlet! b:ale_enabled
@ -81,6 +83,7 @@ Given testft(A Javscript file with warnings/errors):
Execute(Highlights should be set when a linter runs):
ALELint
call ale#test#FlushJobs()
AssertEqual
\ [

View File

@ -2,6 +2,10 @@ Before:
Save g:ale_max_buffer_history_size
Save g:ale_history_log_output
Save g:ale_run_synchronously
Save g:ale_enabled
let g:ale_enabled = 1
let g:ale_run_synchronously = 1
unlet! b:ale_fixers
unlet! b:ale_enabled
@ -68,27 +72,19 @@ Given foobar (Some imaginary filetype):
Execute(History should be set when commands are run):
AssertEqual 'foobar', &filetype
let g:expected_results = ['command', 'exit_code', 'job_id', 'status']
let b:ale_history = []
ALELint
call ale#test#FlushJobs()
" Retry this test until it works. This one can randomly fail.
for g:i in range(has('nvim-0.3') || has('win32') ? 5 : 1)
let b:ale_history = []
call ale#Queue(0)
call ale#engine#WaitForJobs(2000)
let g:history = filter(
\ copy(ale#history#Get(bufnr(''))),
\ 'v:val.job_id isnot# ''executable''',
\)
let g:history = filter(
\ copy(ale#history#Get(bufnr(''))),
\ 'v:val.job_id isnot# ''executable''',
\)
AssertEqual 1, len(g:history)
if sort(keys(g:history[0])) == g:expected_results
break
endif
endfor
AssertEqual g:expected_results, sort(keys(g:history[0]))
AssertEqual 1, len(g:history)
AssertEqual
\ ['command', 'exit_code', 'job_id', 'status'],
\ sort(keys(g:history[0]))
if has('win32')
AssertEqual 'cmd /s/c "echo command history test"', g:history[0].command
@ -106,8 +102,8 @@ Execute(History should be not set when disabled):
let g:ale_history_enabled = 0
call ale#Queue(0)
call ale#engine#WaitForJobs(2000)
ALELint
call ale#test#FlushJobs()
AssertEqual [], ale#history#Get(bufnr(''))
@ -115,24 +111,21 @@ Execute(History should include command output if logging is enabled):
AssertEqual 'foobar', &filetype
let g:ale_history_log_output = 1
let g:expected_results = ['command history test']
" Retry this test until it works. This one can randomly fail.
for g:i in range(has('nvim-0.3') || has('win32') ? 5 : 1)
let b:ale_history = []
call ale#Queue(0)
call ale#engine#WaitForJobs(2000)
let b:ale_history = []
ALELint
call ale#test#FlushJobs()
let g:history = ale#history#Get(bufnr(''))
let g:history = ale#history#Get(bufnr(''))
AssertEqual 1, len(g:history)
if get(g:history[0], 'output', []) == g:expected_results
break
endif
endfor
AssertEqual g:expected_results, get(g:history[0], 'output', [])
AssertEqual 1, len(g:history)
AssertEqual
\ ['command history test'],
\ map(
\ copy(get(g:history[0], 'output', [])),
\ 'substitute(v:val, ''[\r ]*$'', '''', ''g'')'
\ )
Execute(History items should be popped after going over the max):
let b:ale_history = map(range(20), '{''status'': ''started'', ''job_id'': v:val, ''command'': ''foobar''}')
@ -169,10 +162,13 @@ Execute(The history should be updated when fixers are run):
let b:ale_fixers = {'foobar': ['TestFixer']}
let b:ale_enabled = 0
let g:ale_run_synchronously = 1
ALEFix
AssertEqual ['started'], map(copy(b:ale_history), 'v:val.status')
call ale#test#FlushJobs()
AssertEqual ['finished'], map(copy(b:ale_history), 'v:val.status')
if has('win32')

View File

@ -8,6 +8,7 @@ Before:
let g:ale_buffer_info = {}
let g:ale_run_synchronously = 1
unlet! g:ale_run_synchronously_callbacks
let g:ale_set_lists_synchronously = 1
let b:ale_save_event_fired = 0
@ -89,6 +90,7 @@ After:
Restore
unlet! g:ale_run_synchronously_callbacks
unlet! b:ale_save_event_fired
unlet! b:ale_enabled
unlet g:buffer_result
@ -111,6 +113,7 @@ Given foobar (Some imaginary filetype):
Execute(Running linters without 'lint_file' should run only buffer linters):
call ale#Queue(0)
call ale#test#FlushJobs()
AssertEqual [
\ {
@ -131,6 +134,7 @@ Execute(Running linters with 'lint_file' should run all linters):
Assert filereadable(expand('%:p')), 'The file was not readable'
call ale#Queue(0, 'lint_file')
call ale#test#FlushJobs()
AssertEqual [
\ {
@ -163,6 +167,7 @@ Execute(Linter errors from files should be kept):
Assert filereadable(expand('%:p')), 'The file was not readable'
call ale#Queue(0, 'lint_file')
call ale#test#FlushJobs()
" Change the results for the buffer callback.
let g:buffer_result = [
@ -175,6 +180,7 @@ Execute(Linter errors from files should be kept):
\]
call ale#Queue(0)
call ale#test#FlushJobs()
AssertEqual [
\ {
@ -202,6 +208,7 @@ Execute(Linter errors from files should be kept when no other linters are run):
Assert filereadable(expand('%:p')), 'The file was not readable'
call ale#Queue(0, 'lint_file')
call ale#test#FlushJobs()
AssertEqual [
\ {
@ -240,11 +247,13 @@ Execute(The Save event should respect the buffer number):
Assert filereadable(expand('%:p')), 'The file was not readable'
call ale#events#SaveEvent(bufnr('') + 1)
call ale#test#FlushJobs()
" We shouldn't get any prblems yet.
AssertEqual [], GetSimplerLoclist()
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
" We should get them now we used the right buffer number.
AssertEqual [
@ -268,6 +277,7 @@ Execute(The Save event should set b:ale_save_event_fired to 1):
call ale#linter#Reset()
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
" This flag needs to be set so windows can be opened, etc.
AssertEqual 1, b:ale_save_event_fired
@ -276,6 +286,7 @@ Execute(b:ale_save_event_fired should be set to 0 when results are set):
let b:ale_save_event_fired = 1
call ale#engine#SetResults(bufnr(''), [])
call ale#test#FlushJobs()
AssertEqual 0, b:ale_save_event_fired
@ -289,15 +300,18 @@ Execute(lint_file linters should stay running after checking without them):
" The lint_file linter should still be running.
AssertEqual
\ ['lint_file_linter', 'buffer_linter'],
\ g:ale_buffer_info[bufnr('')].active_linter_list
\ map(copy(g:ale_buffer_info[bufnr('')].active_linter_list), 'v:val.name')
" We should have 1 job for each linter.
AssertEqual 2, len(g:ale_buffer_info[bufnr('')].job_list)
AssertEqual
\ 2,
\ len(keys(get(get(ale#command#GetData(), bufnr(''), {}), 'jobs', {})))
call ale#engine#WaitForJobs(2000)
call ale#test#WaitForJobs(2000)
Execute(The save event should not lint the buffer when ALE is disabled):
let g:ale_enabled = 0
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
AssertEqual [], GetSimplerLoclist()
AssertEqual 0, b:ale_save_event_fired

View File

@ -50,6 +50,7 @@ Execute(The file changed event function should set b:ale_file_changed):
Execute(The file changed event function should lint the current buffer when it has changed):
set filetype=foobar
call ale#events#FileChangedEvent(bufnr(''))
call ale#test#FlushJobs()
AssertEqual [{
\ 'bufnr': bufnr(''),
@ -68,6 +69,7 @@ Execute(The buffer should be checked after entering it after the file has change
set filetype=foobar
call ale#events#ReadOrEnterEvent(bufnr(''))
call ale#test#FlushJobs()
AssertEqual [{
\ 'bufnr': bufnr(''),

View File

@ -11,6 +11,6 @@ Given unite (A Unite.vim file):
Execute(Running ALE on a blacklisted file shouldn't change anything):
call ale#Queue(0)
call ale#engine#WaitForJobs(2000)
call ale#test#WaitForJobs(2000)
AssertEqual {}, g:ale_buffer_info

View File

@ -64,6 +64,7 @@ Given foobar (Some JavaScript with problems):
Execute(The loclist should be updated after linting is done):
ALELint
call ale#test#FlushJobs()
AssertEqual
\ [

View File

@ -58,6 +58,7 @@ Execute(No linting should be done on :wq or :x):
" First try just the SaveEvent, to be sure that we set errors in the test.
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
AssertEqual 1, len(ale#test#GetLoclistWithoutModule())
@ -65,6 +66,7 @@ Execute(No linting should be done on :wq or :x):
call setloclist(0, [])
call ale#events#QuitEvent(bufnr(''))
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
AssertEqual [], ale#test#GetLoclistWithoutModule()
@ -73,11 +75,13 @@ Execute(No linting should be for :w after :q fails):
let g:ale_fix_on_save = 0
call ale#events#QuitEvent(bufnr(''))
call ale#test#FlushJobs()
" Simulate 2 seconds passing.
let b:ale_quitting -= 1000
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
AssertEqual 1, len(ale#test#GetLoclistWithoutModule())
@ -86,6 +90,7 @@ Execute(No linting should be done on :wq or :x after fixing files):
let g:ale_fix_on_save = 1
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
AssertEqual 1, len(ale#test#GetLoclistWithoutModule())
@ -93,6 +98,7 @@ Execute(No linting should be done on :wq or :x after fixing files):
call setloclist(0, [])
call ale#events#QuitEvent(bufnr(''))
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
AssertEqual [], ale#test#GetLoclistWithoutModule()
@ -101,10 +107,12 @@ Execute(Linting should be done after :q fails and fixing files):
let g:ale_fix_on_save = 1
call ale#events#QuitEvent(bufnr(''))
call ale#test#FlushJobs()
" Simulate 2 seconds passing.
let b:ale_quitting -= 1000
call ale#events#SaveEvent(bufnr(''))
call ale#test#FlushJobs()
AssertEqual 1, len(ale#test#GetLoclistWithoutModule())

View File

@ -70,7 +70,7 @@ Execute(ALE should delete managed files/directories appropriately after linting)
AssertEqual 'foobar', &filetype
call ale#Queue(0)
call ale#engine#WaitForJobs(2000)
call ale#test#FlushJobs()
Assert !filereadable(g:filename), 'The temporary file was not deleted'
Assert !isdirectory(g:directory), 'The temporary directory was not deleted'
@ -82,7 +82,7 @@ Execute(ALE should delete managed files even if no command is run):
let g:command = ''
call ale#Queue(0)
call ale#engine#WaitForJobs(2000)
call ale#test#WaitForJobs(2000)
Assert !filereadable(g:filename), 'The temporary file was not deleted'
Assert !isdirectory(g:directory), 'The temporary directory was not deleted'
@ -119,24 +119,26 @@ Execute(ALE should create and delete directories for ale#command#CreateDirectory
Assert !isdirectory(b:dir), 'The directory was not deleted'
Assert !isdirectory(b:dir2), 'The second directory was not deleted'
Execute(ale#command#ManageFile should add the file even if the buffer info hasn't be set yet):
Execute(ale#command#ManageFile should add the file even if the buffer info hasn't been set yet):
call ale#command#ManageFile(bufnr(''), '/foo/bar')
AssertEqual
\ {
\ bufnr(''): {
\ 'jobs': {},
\ 'file_list': ['/foo/bar'],
\ 'directory_list': [],
\ },
\ },
\ ale#command#GetData()
Execute(ale#command#ManageDirectory should add the directory even if the buffer info hasn't be set yet):
Execute(ale#command#ManageDirectory should add the directory even if the buffer info hasn't been set yet):
call ale#command#ManageDirectory(bufnr(''), '/foo/bar')
AssertEqual
\ {
\ bufnr(''): {
\ 'jobs': {},
\ 'file_list': [],
\ 'directory_list': ['/foo/bar'],
\ },