-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2529 from maxwell-k/terraform_linter
Add a terraform linter
- Loading branch information
Showing
6 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
" Author: Keith Maxwell <keith.maxwell@gmail.com> | ||
" Description: terraform fmt to check for errors | ||
|
||
call ale#Set('terraform_terraform_executable', 'terraform') | ||
|
||
function! ale_linters#terraform#terraform#GetExecutable(buffer) abort | ||
return ale#Var(a:buffer, 'terraform_terraform_executable') | ||
endfunction | ||
|
||
function! ale_linters#terraform#terraform#GetCommand(buffer) abort | ||
return ale#Escape(ale_linters#terraform#terraform#GetExecutable(a:buffer)) | ||
\ . ' fmt -no-color --check=true -' | ||
endfunction | ||
|
||
function! ale_linters#terraform#terraform#Handle(buffer, lines) abort | ||
let l:head = '^Error running fmt: In <standard input>: ' | ||
let l:output = [] | ||
let l:patterns = [ | ||
\ l:head.'At \(\d\+\):\(\d\+\): \(.*\)$', | ||
\ l:head.'\(.*\)$' | ||
\] | ||
|
||
for l:match in ale#util#GetMatches(a:lines, l:patterns) | ||
if len(l:match[2]) > 0 | ||
call add(l:output, { | ||
\ 'lnum': str2nr(l:match[1]), | ||
\ 'col': str2nr(l:match[2]), | ||
\ 'text': l:match[3], | ||
\ 'type': 'E', | ||
\}) | ||
else | ||
call add(l:output, { | ||
\ 'lnum': line('$'), | ||
\ 'text': l:match[1], | ||
\ 'type': 'E', | ||
\}) | ||
endif | ||
endfor | ||
|
||
return l:output | ||
endfunction | ||
|
||
call ale#linter#Define('terraform', { | ||
\ 'name': 'terraform', | ||
\ 'output_stream': 'stderr', | ||
\ 'executable': function('ale_linters#terraform#terraform#GetExecutable'), | ||
\ 'command': function('ale_linters#terraform#terraform#GetCommand'), | ||
\ 'callback': 'ale_linters#terraform#terraform#Handle', | ||
\}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
test/command_callback/test_terraform_terraform_command_callback.vader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
" Based upon :help ale-development | ||
Before: | ||
call ale#assert#SetUpLinterTest('terraform', 'terraform') | ||
|
||
After: | ||
call ale#assert#TearDownLinterTest() | ||
|
||
Execute(The default command should be correct): | ||
AssertLinter 'terraform', ale#Escape('terraform') . ' fmt -no-color --check=true -' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
Before: | ||
" Load the file which defines the linter. | ||
runtime ale_linters/terraform/terraform.vim | ||
|
||
After: | ||
" Unload all linters again. | ||
call ale#linter#Reset() | ||
|
||
Execute(The output should be correct): | ||
AssertEqual | ||
\ [ | ||
\ { | ||
\ 'lnum': 1, | ||
\ 'col': 20, | ||
\ 'type': 'E', | ||
\ 'text': 'illegal char', | ||
\ }, | ||
\ { | ||
\ 'lnum': 2, | ||
\ 'col': 14, | ||
\ 'type': 'E', | ||
\ 'text': 'literal not terminated', | ||
\ }, | ||
\ { | ||
\ 'lnum': 1, | ||
\ 'type': 'E', | ||
\ 'text': 'object expected closing RBRACE got: EOF', | ||
\ }, | ||
\ ], | ||
\ ale_linters#terraform#terraform#Handle(bufnr(''), [ | ||
\ 'Error running fmt: In <standard input>: At 1:20: illegal char', | ||
\ 'Error running fmt: In <standard input>: At 2:14: literal not terminated', | ||
\ 'Error running fmt: In <standard input>: object expected closing RBRACE got: EOF', | ||
\ ]) |