Fix some escaping and make some tests set filenames consistently

This commit is contained in:
w0rp 2017-06-21 22:33:34 +01:00
parent ab534c2995
commit dab6f39eb0
7 changed files with 71 additions and 32 deletions

View file

@ -40,7 +40,7 @@ function! ale_linters#ruby#brakeman#GetCommand(buffer) abort
return 'brakeman -f json -q '
\ . ale#Var(a:buffer, 'ruby_brakeman_options')
\ . ' -p ' . l:rails_root
\ . ' -p ' . ale#Escape(l:rails_root)
endfunction
function! s:FindRailsRoot(buffer) abort

19
autoload/ale/test.vim Normal file
View file

@ -0,0 +1,19 @@
" Author: w0rp <devw0rp@gmail.com>
" Description: Functions for making testing ALE easier.
"
" This file should not typically be loaded during the normal execution of ALE.
" Change the filename for the current buffer using a relative path to
" the script without running autocmd commands.
"
" If a g:dir variable is set, it will be used as the path to the directory
" containing the test file.
function! ale#test#SetFilename(path) abort
let l:dir = get(g:, 'dir', '')
if empty(l:dir)
let l:dir = getcwd()
endif
silent noautocmd execute 'file ' . fnameescape(simplify(l:dir . '/' . a:path))
endfunction

View file

@ -1,26 +1,42 @@
Before:
Save g:ale_ruby_brakeman_options
runtime ale_linters/ruby/brakeman.vim
let g:ale_ruby_brakeman_options = ''
silent! cd /testplugin/test/command_callback
let g:dir = getcwd()
After:
Restore
silent execute 'cd ' . fnameescape(g:dir)
unlet! g:dir
call ale#linter#Reset()
Execute(The brakeman command callback should detect absence of a valid Rails app):
cd /testplugin/test/ruby_fixtures/not_a_rails_app/
call ale#test#SetFilename('../ruby_fixtures/not_a_rails_app/test.rb')
AssertEqual
\ '',
\ ale_linters#ruby#brakeman#GetCommand(bufnr(''))
Execute(The brakeman command callback should find a valid Rails app root):
cd /testplugin/test/ruby_fixtures/valid_rails_app/db/
call ale#test#SetFilename('../ruby_fixtures/valid_rails_app/db/test.rb')
AssertEqual
\ 'brakeman -f json -q -p /testplugin/test/ruby_fixtures/valid_rails_app',
\ 'brakeman -f json -q -p '
\ . ale#Escape(simplify(g:dir . '/../ruby_fixtures/valid_rails_app')),
\ ale_linters#ruby#brakeman#GetCommand(bufnr(''))
Execute(The brakeman command callback should include configured options):
cd /testplugin/test/ruby_fixtures/valid_rails_app/db/
call ale#test#SetFilename('../ruby_fixtures/valid_rails_app/db/test.rb')
let g:ale_ruby_brakeman_options = '--combobulate'
AssertEqual
\ 'brakeman -f json -q --combobulate -p /testplugin/test/ruby_fixtures/valid_rails_app',
\ 'brakeman -f json -q --combobulate -p '
\ . ale#Escape(simplify(g:dir . '/../ruby_fixtures/valid_rails_app')),
\ ale_linters#ruby#brakeman#GetCommand(bufnr(''))

View file

@ -5,36 +5,43 @@ Before:
After:
silent execute 'cd ' . fnameescape(b:dir)
unlet! b:dir
call ale#linter#Reset()
Execute(The default C cppcheck command should be correct):
runtime ale_linters/c/cppcheck.vim
call ale#test#SetFilename('cppcheck_paths/two/foo.cpp')
AssertEqual
\ 'cppcheck -q --language=c --enable=style %t',
\ ale_linters#c#cppcheck#GetCommand(bufnr(''))
Execute(cppcheck for C should detect compile_commands.json files):
runtime ale_linters/c/cppcheck.vim
cd cppcheck_paths/one
call ale#test#SetFilename('cppcheck_paths/one/foo.cpp')
AssertEqual
\ 'cd ' . shellescape(b:dir . '/cppcheck_paths/one') . ' && '
\ 'cd ' . ale#Escape(b:dir . '/cppcheck_paths/one') . ' && '
\ . 'cppcheck -q --language=c --project=compile_commands.json --enable=style %t',
\ ale_linters#c#cppcheck#GetCommand(bufnr(''))
Execute(The default C++ cppcheck command should be correct):
runtime ale_linters/cpp/cppcheck.vim
call ale#test#SetFilename('cppcheck_paths/two/foo.cpp')
AssertEqual
\ 'cppcheck -q --language=c++ --enable=style %t',
\ ale_linters#cpp#cppcheck#GetCommand(bufnr(''))
Execute(cppcheck for C++ should detect compile_commands.json files):
runtime ale_linters/cpp/cppcheck.vim
cd cppcheck_paths/one
call ale#test#SetFilename('cppcheck_paths/one/foo.cpp')
AssertEqual
\ 'cd ' . shellescape(b:dir . '/cppcheck_paths/one') . ' && '
\ 'cd ' . ale#Escape(b:dir . '/cppcheck_paths/one') . ' && '
\ . 'cppcheck -q --language=c++ --project=compile_commands.json --enable=style %t',
\ ale_linters#cpp#cppcheck#GetCommand(bufnr(''))

View file

@ -1,5 +1,9 @@
Before:
Save g:ale_php_phpcs_executable
Save g:ale_php_phpcs_use_global
let g:ale_php_phpcs_executable = 'phpcs_test'
let g:ale_php_phpcs_use_global = 0
silent! cd /testplugin/test
let g:dir = getcwd()
@ -7,39 +11,32 @@ Before:
runtime ale_linters/php/phpcs.vim
After:
let g:ale_php_phpcs_executable = 'phpcs'
let g:ale_php_phpcs_use_global = 0
Restore
silent execute 'cd ' . g:dir
silent execute 'cd ' . fnameescape(g:dir)
unlet! g:dir
call ale#linter#Reset()
Execute(project with phpcs should use local by default):
silent noautocmd new phpcs-test-files/project-with-phpcs/vendor/bin/phpcs
call ale#test#SetFilename('phpcs-test-files/project-with-phpcs/foo/test.php')
AssertEqual
\ g:dir . '/phpcs-test-files/project-with-phpcs/vendor/bin/phpcs',
\ ale_linters#php#phpcs#GetExecutable(bufnr(''))
:q
Execute(use-global should override local detection):
let g:ale_php_phpcs_use_global = 1
silent noautocmd new phpcs-test-files/project-with-phpcs/vendor/bin/phpcs
call ale#test#SetFilename('phpcs-test-files/project-with-phpcs/foo/test.php')
AssertEqual
\ 'phpcs_test',
\ ale_linters#php#phpcs#GetExecutable(bufnr(''))
:q
Execute(project without phpcs should use global):
silent noautocmd new phpcs-test-files/project-without-phpcs/vendor/bin/phpcs
call ale#test#SetFilename('phpcs-test-files/project-without-phpcs/foo/test.php')
AssertEqual
\ 'phpcs_test',
\ ale_linters#php#phpcs#GetExecutable(bufnr(''))
:q