diff --git a/ale_linters/go/gobuild.vim b/ale_linters/go/gobuild.vim index 783b9e0..068877a 100644 --- a/ale_linters/go/gobuild.vim +++ b/ale_linters/go/gobuild.vim @@ -1,9 +1,14 @@ " Author: Joshua Rubin , Ben Reedy , " Jeff Willette " Description: go build for Go files - " inspired by work from dzhou121 +call ale#Set('go_gobuild_options', '') + +function! ale_linters#go#gobuild#ResetEnv() abort + unlet! s:go_env +endfunction + function! ale_linters#go#gobuild#GoEnv(buffer) abort if exists('s:go_env') return '' @@ -13,6 +18,8 @@ function! ale_linters#go#gobuild#GoEnv(buffer) abort endfunction function! ale_linters#go#gobuild#GetCommand(buffer, goenv_output) abort + let l:options = ale#Var(a:buffer, 'go_gobuild_options') + if !exists('s:go_env') let s:go_env = { \ 'GOPATH': a:goenv_output[0], @@ -20,10 +27,16 @@ function! ale_linters#go#gobuild#GetCommand(buffer, goenv_output) abort \} endif + let l:gopath_env_command = has('win32') + \ ? 'set GOPATH=' . ale#Escape(s:go_env.GOPATH) . ' && ' + \ : 'GOPATH=' . ale#Escape(s:go_env.GOPATH) . ' ' + " Run go test in local directory with relative path - return 'GOPATH=' . s:go_env.GOPATH - \ . ' cd ' . fnamemodify(bufname(a:buffer), ':.:h') - \ . ' && go test -c -o /dev/null ./' + return l:gopath_env_command + \ . ale#path#BufferCdString(a:buffer) + \ . 'go test' + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . ' -c -o /dev/null ./' endfunction function! ale_linters#go#gobuild#GetMatches(lines) abort diff --git a/doc/ale-go.txt b/doc/ale-go.txt index c5a6887..502f237 100644 --- a/doc/ale-go.txt +++ b/doc/ale-go.txt @@ -20,6 +20,18 @@ the benefit of running a number of linters, more than ALE would by default, while ensuring it doesn't run any linters known to be slow or resource intensive. +=============================================================================== +gobuild *ale-go-gobuild* + +g:ale_go_gobuild_options *g:ale_go_gobuild_options* + *b:ale_go_gobuild_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to the gobuild linter. + They are injected directly after "go test". + + =============================================================================== gofmt *ale-go-gofmt* diff --git a/doc/ale.txt b/doc/ale.txt index 2644ebc..6ace9d5 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -77,6 +77,7 @@ CONTENTS *ale-contents* glslang.............................|ale-glsl-glslang| glslls..............................|ale-glsl-glslls| go....................................|ale-go-options| + gobuild.............................|ale-go-gobuild| gofmt...............................|ale-go-gofmt| gometalinter........................|ale-go-gometalinter| graphql...............................|ale-graphql-options| diff --git a/test/command_callback/test_gobuild_command_callback.vader b/test/command_callback/test_gobuild_command_callback.vader new file mode 100644 index 0000000..240f29c --- /dev/null +++ b/test/command_callback/test_gobuild_command_callback.vader @@ -0,0 +1,52 @@ +Before: + Save g:ale_go_gobuild_options + + unlet! g:ale_go_gobuild_options + + let g:env_prefix = has('win32') + \ ? 'set GOPATH=' . ale#Escape('/foo/bar') . ' && ' + \ : 'GOPATH=' . ale#Escape('/foo/bar') . ' ' + + runtime ale_linters/go/gobuild.vim + + call ale#test#SetDirectory('/testplugin/test/command_callback') + + call ale_linters#go#gobuild#ResetEnv() + +After: + Restore + + unlet! g:env_prefix + + call ale#linter#Reset() + call ale#test#RestoreDirectory() + +Execute(The default gobuild command should be correct): + AssertEqual + \ ale_linters#go#gobuild#GetCommand(bufnr(''), ['/foo/bar', '/foo/baz']), + \ g:env_prefix . 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' + \ . 'go test -c -o /dev/null ./' + +Execute(The command for getting GOPATH should be correct): + AssertEqual ale_linters#go#gobuild#GoEnv(bufnr('')), 'go env GOPATH GOROOT' + + call ale_linters#go#gobuild#GetCommand(bufnr(''), ['/foo/bar', '/foo/baz']) + + " We shouldn't run `go env` many times after we've got it. + AssertEqual ale_linters#go#gobuild#GoEnv(bufnr('')), '' + +Execute(The GOPATH output should be used after it has been read once): + call ale_linters#go#gobuild#GetCommand(bufnr(''), ['/foo/bar', '/foo/baz']) + + AssertEqual + \ ale_linters#go#gobuild#GetCommand(bufnr(''), []), + \ g:env_prefix . 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' + \ . 'go test -c -o /dev/null ./' + +Execute(Extra options should be supported): + let g:ale_go_gobuild_options = '--foo-bar' + + AssertEqual + \ ale_linters#go#gobuild#GetCommand(bufnr(''), ['/foo/bar', '/foo/baz']), + \ g:env_prefix . 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' + \ . 'go test --foo-bar -c -o /dev/null ./'