#1151 - Overhaul the foodcritic linter for checking files on disk
This commit is contained in:
parent
ce2986cfa5
commit
ac7f69063d
4 changed files with 111 additions and 35 deletions
|
@ -1,24 +1,37 @@
|
|||
" Author: Edward Larkey <edwlarkey@mac.com>
|
||||
" Author: Jose Junior <jose.junior@gmail.com>
|
||||
" Author: w0rp <devw0rp@gmail.com>
|
||||
" Description: This file adds the foodcritic linter for Chef files.
|
||||
|
||||
" Support options!
|
||||
let g:ale_chef_foodcritic_options = get(g:, 'ale_chef_foodcritic_options', '')
|
||||
let g:ale_chef_foodcritic_executable = get(g:, 'ale_chef_foodcritic_executable', 'foodcritic')
|
||||
call ale#Set('chef_foodcritic_executable', 'foodcritic')
|
||||
call ale#Set('chef_foodcritic_options', '')
|
||||
|
||||
function! ale_linters#chef#foodcritic#GetExecutable(buffer) abort
|
||||
return ale#Var(a:buffer, 'chef_foodcritic_executable')
|
||||
endfunction
|
||||
|
||||
function! ale_linters#chef#foodcritic#GetCommand(buffer) abort
|
||||
let l:executable = ale_linters#chef#foodcritic#GetExecutable(a:buffer)
|
||||
let l:options = ale#Var(a:buffer, 'chef_foodcritic_options')
|
||||
|
||||
return ale#Escape(l:executable)
|
||||
\ . (!empty(l:options) ? ' ' . escape(l:options, '~') : '')
|
||||
\ . ' %s'
|
||||
endfunction
|
||||
|
||||
function! ale_linters#chef#foodcritic#Handle(buffer, lines) abort
|
||||
" Matches patterns line the following:
|
||||
"
|
||||
" FC002: Avoid string interpolation where not required: httpd.rb:13
|
||||
let l:pattern = '^\(.\+:\s.\+\):\s\(.\+\):\(\d\+\)$'
|
||||
let l:pattern = '\v([^:]+): (.+): ([a-zA-Z]?:?[^:]+):(\d+)$'
|
||||
let l:output = []
|
||||
|
||||
for l:match in ale#util#GetMatches(a:lines, l:pattern)
|
||||
let l:text = l:match[1]
|
||||
|
||||
call add(l:output, {
|
||||
\ 'lnum': l:match[3] + 0,
|
||||
\ 'text': l:text,
|
||||
\ 'code': l:match[1],
|
||||
\ 'text': l:match[2],
|
||||
\ 'filename': l:match[3],
|
||||
\ 'lnum': l:match[4] + 0,
|
||||
\ 'type': 'W',
|
||||
\})
|
||||
endfor
|
||||
|
@ -26,17 +39,10 @@ function! ale_linters#chef#foodcritic#Handle(buffer, lines) abort
|
|||
return l:output
|
||||
endfunction
|
||||
|
||||
function! ale_linters#chef#foodcritic#GetCommand(buffer) abort
|
||||
return printf('%s %s %%t',
|
||||
\ ale#Var(a:buffer, 'chef_foodcritic_executable'),
|
||||
\ escape(ale#Var(a:buffer, 'chef_foodcritic_options'), '~')
|
||||
\)
|
||||
endfunction
|
||||
|
||||
|
||||
call ale#linter#Define('chef', {
|
||||
\ 'name': 'foodcritic',
|
||||
\ 'executable': 'foodcritic',
|
||||
\ 'executable_callback': 'ale_linters#chef#foodcritic#GetExecutable',
|
||||
\ 'command_callback': 'ale_linters#chef#foodcritic#GetCommand',
|
||||
\ 'callback': 'ale_linters#chef#foodcritic#Handle',
|
||||
\ 'lint_file': 1,
|
||||
\})
|
||||
|
|
44
test/command_callback/test_foodcritic_command_callback.vader
Normal file
44
test/command_callback/test_foodcritic_command_callback.vader
Normal file
|
@ -0,0 +1,44 @@
|
|||
Before:
|
||||
Save g:ale_chef_foodcritic_executable
|
||||
Save g:ale_chef_foodcritic_options
|
||||
|
||||
unlet! g:ale_chef_foodcritic_executable
|
||||
unlet! g:ale_chef_foodcritic_options
|
||||
|
||||
call ale#test#SetDirectory('/testplugin/test')
|
||||
|
||||
runtime ale_linters/chef/foodcritic.vim
|
||||
|
||||
After:
|
||||
Restore
|
||||
|
||||
unlet! b:ale_chef_foodcritic_executable
|
||||
unlet! b:ale_chef_foodcritic_options
|
||||
|
||||
call ale#test#RestoreDirectory()
|
||||
call ale#linter#Reset()
|
||||
|
||||
Execute(The default command should be correct):
|
||||
AssertEqual
|
||||
\ 'foodcritic',
|
||||
\ ale_linters#chef#foodcritic#GetExecutable(bufnr(''))
|
||||
AssertEqual
|
||||
\ ale#Escape('foodcritic') . ' %s',
|
||||
\ ale_linters#chef#foodcritic#GetCommand(bufnr(''))
|
||||
|
||||
Execute(Extra options should be included with escapeed tildes (~)):
|
||||
let b:ale_chef_foodcritic_options = '-t ~F011'
|
||||
|
||||
AssertEqual
|
||||
\ ale#Escape('foodcritic') . ' -t \~F011 %s',
|
||||
\ ale_linters#chef#foodcritic#GetCommand(bufnr(''))
|
||||
|
||||
Execute(The executable should be configurable):
|
||||
let b:ale_chef_foodcritic_executable = 'foobar'
|
||||
|
||||
AssertEqual
|
||||
\ 'foobar',
|
||||
\ ale_linters#chef#foodcritic#GetExecutable(bufnr(''))
|
||||
AssertEqual
|
||||
\ ale#Escape('foobar') . ' %s',
|
||||
\ ale_linters#chef#foodcritic#GetCommand(bufnr(''))
|
44
test/handler/test_foodcritic_handler.vader
Normal file
44
test/handler/test_foodcritic_handler.vader
Normal file
|
@ -0,0 +1,44 @@
|
|||
Before:
|
||||
runtime ale_linters/chef/foodcritic.vim
|
||||
|
||||
After:
|
||||
call ale#linter#Reset()
|
||||
|
||||
Execute(Basic warnings should be handled):
|
||||
AssertEqual
|
||||
\ [
|
||||
\ {
|
||||
\ 'lnum': 1,
|
||||
\ 'code': 'CINK001',
|
||||
\ 'type': 'W',
|
||||
\ 'text': 'Missing CHANGELOG in markdown format',
|
||||
\ 'filename': '/foo/bar/CHANGELOG.md',
|
||||
\ },
|
||||
\ {
|
||||
\ 'lnum': 1,
|
||||
\ 'code': 'FC011',
|
||||
\ 'type': 'W',
|
||||
\ 'text': 'Missing README in markdown format',
|
||||
\ 'filename': '/foo/bar/README.md',
|
||||
\ },
|
||||
\ {
|
||||
\ 'lnum': 1,
|
||||
\ 'code': 'FC031',
|
||||
\ 'type': 'W',
|
||||
\ 'text': 'Cookbook without metadata.rb file',
|
||||
\ 'filename': '/foo/bar/metadata.rb',
|
||||
\ },
|
||||
\ {
|
||||
\ 'lnum': 1,
|
||||
\ 'code': 'FC071',
|
||||
\ 'type': 'W',
|
||||
\ 'text': 'Missing LICENSE file',
|
||||
\ 'filename': '/foo/bar/LICENSE',
|
||||
\ },
|
||||
\ ],
|
||||
\ ale_linters#chef#foodcritic#Handle(bufnr(''), [
|
||||
\ 'CINK001: Missing CHANGELOG in markdown format: /foo/bar/CHANGELOG.md:1',
|
||||
\ 'FC011: Missing README in markdown format: /foo/bar/README.md:1',
|
||||
\ 'FC031: Cookbook without metadata.rb file: /foo/bar/metadata.rb:1',
|
||||
\ 'FC071: Missing LICENSE file: /foo/bar/LICENSE:1',
|
||||
\ ])
|
|
@ -1,18 +0,0 @@
|
|||
Before:
|
||||
let g:ale_chef_foodcritic_options = '-t ~F011'
|
||||
let g:ale_chef_foodcritic_executable = 'foodcritic'
|
||||
|
||||
call ale#test#SetDirectory('/testplugin/test')
|
||||
runtime ale_linters/chef/foodcritic.vim
|
||||
|
||||
After:
|
||||
let g:ale_chef_foodcritic_options = ''
|
||||
let g:ale_chef_foodcritic_executable = ''
|
||||
|
||||
call ale#test#RestoreDirectory()
|
||||
call ale#linter#Reset()
|
||||
|
||||
Execute(command line should be assembled correctly):
|
||||
AssertEqual
|
||||
\ 'foodcritic -t \~F011 %t',
|
||||
\ ale_linters#chef#foodcritic#GetCommand(bufnr(''))
|
Reference in a new issue