From dab6f39eb0b5d0dd77a8dc3fe58d0648d82696f7 Mon Sep 17 00:00:00 2001 From: w0rp Date: Wed, 21 Jun 2017 22:33:34 +0100 Subject: [PATCH] Fix some escaping and make some tests set filenames consistently --- ale_linters/ruby/brakeman.vim | 2 +- autoload/ale/test.vim | 19 ++++++++ .../test_brakeman_command_callback.vader | 46 +++++++++++++------ .../test_cppcheck_command_callbacks.vader | 15 ++++-- .../project-with-phpcs/foo/test.php | 0 .../project-without-phpcs/foo/test.php | 0 test/test_phpcs_executable_detection.vader | 21 ++++----- 7 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 autoload/ale/test.vim create mode 100644 test/phpcs-test-files/project-with-phpcs/foo/test.php create mode 100644 test/phpcs-test-files/project-without-phpcs/foo/test.php diff --git a/ale_linters/ruby/brakeman.vim b/ale_linters/ruby/brakeman.vim index 3cc5b77..fa5617d 100644 --- a/ale_linters/ruby/brakeman.vim +++ b/ale_linters/ruby/brakeman.vim @@ -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 diff --git a/autoload/ale/test.vim b/autoload/ale/test.vim new file mode 100644 index 0000000..f63fc3a --- /dev/null +++ b/autoload/ale/test.vim @@ -0,0 +1,19 @@ +" Author: w0rp +" 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 diff --git a/test/command_callback/test_brakeman_command_callback.vader b/test/command_callback/test_brakeman_command_callback.vader index 262f865..607aec6 100644 --- a/test/command_callback/test_brakeman_command_callback.vader +++ b/test/command_callback/test_brakeman_command_callback.vader @@ -1,26 +1,42 @@ Before: - runtime ale_linters/ruby/brakeman.vim + 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: - call ale#linter#Reset() + 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/ - AssertEqual - \ '', - \ ale_linters#ruby#brakeman#GetCommand(bufnr('')) + 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/ - AssertEqual - \ 'brakeman -f json -q -p /testplugin/test/ruby_fixtures/valid_rails_app', - \ ale_linters#ruby#brakeman#GetCommand(bufnr('')) + call ale#test#SetFilename('../ruby_fixtures/valid_rails_app/db/test.rb') + + AssertEqual + \ '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/ - let g:ale_ruby_brakeman_options = '--combobulate' + 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', - \ ale_linters#ruby#brakeman#GetCommand(bufnr('')) + AssertEqual + \ 'brakeman -f json -q --combobulate -p ' + \ . ale#Escape(simplify(g:dir . '/../ruby_fixtures/valid_rails_app')), + \ ale_linters#ruby#brakeman#GetCommand(bufnr('')) diff --git a/test/command_callback/test_cppcheck_command_callbacks.vader b/test/command_callback/test_cppcheck_command_callbacks.vader index 6395864..69bb214 100644 --- a/test/command_callback/test_cppcheck_command_callbacks.vader +++ b/test/command_callback/test_cppcheck_command_callbacks.vader @@ -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('')) diff --git a/test/phpcs-test-files/project-with-phpcs/foo/test.php b/test/phpcs-test-files/project-with-phpcs/foo/test.php new file mode 100644 index 0000000..e69de29 diff --git a/test/phpcs-test-files/project-without-phpcs/foo/test.php b/test/phpcs-test-files/project-without-phpcs/foo/test.php new file mode 100644 index 0000000..e69de29 diff --git a/test/test_phpcs_executable_detection.vader b/test/test_phpcs_executable_detection.vader index 678606f..72b7af0 100644 --- a/test/test_phpcs_executable_detection.vader +++ b/test/test_phpcs_executable_detection.vader @@ -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