Fix #1298 - Escape commands for PowerShell

This commit is contained in:
w0rp 2018-01-17 18:08:17 +00:00
parent af1b881c8b
commit c7a9ffcb9c
4 changed files with 66 additions and 4 deletions

View file

@ -174,7 +174,7 @@ function! ale#job#PrepareCommand(command) abort
" but we'll do this explicitly, so we use thes same exact command for both " but we'll do this explicitly, so we use thes same exact command for both
" versions. " versions.
if ale#Has('win32') if ale#Has('win32')
return 'cmd /c ' . a:command return 'cmd /s/c "' . a:command . '"'
endif endif
if &shell =~? 'fish$' if &shell =~? 'fish$'

View file

@ -1,6 +1,7 @@
Before: Before:
Save g:ale_set_lists_synchronously Save g:ale_set_lists_synchronously
Save g:ale_buffer_info Save g:ale_buffer_info
Save &shell
let g:ale_buffer_info = {} let g:ale_buffer_info = {}
let g:ale_set_lists_synchronously = 1 let g:ale_set_lists_synchronously = 1
@ -59,6 +60,67 @@ Execute(Linters should run with the default options):
\ 'valid': 1, \ 'valid': 1,
\ }], getloclist(0) \ }], getloclist(0)
Execute(Linters should run in PowerShell too):
if has('win32')
set shell=powershell
AssertEqual 'foobar', &filetype
" Replace the callback to handle two lines.
function! TestCallback(buffer, output)
" Windows adds extra spaces to the text from echo.
return [
\ {
\ 'lnum': 1,
\ 'col': 3,
\ 'text': substitute(a:output[0], ' *$', '', ''),
\ },
\ {
\ 'lnum': 2,
\ 'col': 3,
\ 'text': substitute(a:output[1], ' *$', '', ''),
\ },
\]
endfunction
" Recreate the command string to use &&, which PowerShell does not support.
call ale#linter#Reset()
call ale#linter#Define('foobar', {
\ 'name': 'testlinter',
\ 'callback': 'TestCallback',
\ 'executable': 'cmd',
\ 'command': 'echo foo && echo bar',
\})
call ale#Lint()
call ale#engine#WaitForJobs(2000)
AssertEqual [
\ {
\ 'bufnr': bufnr('%'),
\ 'lnum': 1,
\ 'vcol': 0,
\ 'col': 3,
\ 'text': 'foo',
\ 'type': 'E',
\ 'nr': -1,
\ 'pattern': '',
\ 'valid': 1,
\ },
\ {
\ 'bufnr': bufnr('%'),
\ 'lnum': 2,
\ 'vcol': 0,
\ 'col': 3,
\ 'text': 'bar',
\ 'type': 'E',
\ 'nr': -1,
\ 'pattern': '',
\ 'valid': 1,
\ },
\], getloclist(0)
endif
Execute(Previous errors should be removed when linters change): Execute(Previous errors should be removed when linters change):
call ale#Lint() call ale#Lint()
call ale#engine#WaitForJobs(2000) call ale#engine#WaitForJobs(2000)

View file

@ -76,7 +76,7 @@ Execute(History should be set when commands are run):
AssertEqual sort(['status', 'exit_code', 'job_id', 'command']), sort(keys(g:history[0])) AssertEqual sort(['status', 'exit_code', 'job_id', 'command']), sort(keys(g:history[0]))
if has('win32') if has('win32')
AssertEqual 'cmd /c echo command history test', g:history[0].command AssertEqual 'cmd /s/c "echo command history test"', g:history[0].command
else else
AssertEqual ['/bin/sh', '-c', '/bin/sh -c ''echo command history test'''], g:history[0].command AssertEqual ['/bin/sh', '-c', '/bin/sh -c ''echo command history test'''], g:history[0].command
endif endif
@ -151,7 +151,7 @@ Execute(The history should be updated when fixers are run):
AssertEqual ['finished'], map(copy(b:ale_history), 'v:val.status') AssertEqual ['finished'], map(copy(b:ale_history), 'v:val.status')
if has('win32') if has('win32')
AssertEqual 'cmd /c echo foo ', split(b:ale_history[0].command, '<')[0] AssertEqual 'cmd /s/c "echo foo ', split(b:ale_history[0].command, '<')[0]
else else
AssertEqual '/bin/sh -c echo foo ', split(join(b:ale_history[0].command), '<')[0] AssertEqual '/bin/sh -c echo foo ', split(join(b:ale_history[0].command), '<')[0]
endif endif

View file

@ -35,4 +35,4 @@ Execute(cmd /c as a string should be used on Windows):
let &shellcmdflag = 'whatever' let &shellcmdflag = 'whatever'
let g:ale_has_override = {'win32': 1} let g:ale_has_override = {'win32': 1}
AssertEqual 'cmd /c foobar', ale#job#PrepareCommand('foobar') AssertEqual 'cmd /s/c "foobar"', ale#job#PrepareCommand('foobar')