Skip to content

Commit

Permalink
lint: execute errcheck using ExecInDir
Browse files Browse the repository at this point in the history
Execute errcheck using ExecInDir so that it will use the package of the
current buffer. It looks like it was inadvertently changed from
ExecInDir to Exec a long while ago when the exec functions were
refactored.

Add tests to verify more go#lint#Errcheck behvaviors.

Fixes #2725
  • Loading branch information
bhcleek committed Feb 14, 2020
1 parent 364a8cf commit c99b2f1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
7 changes: 4 additions & 3 deletions autoload/go/lint.vim
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,19 @@ endfunction
function! go#lint#Errcheck(bang, ...) abort
if a:0 == 0
let l:import_path = go#package#ImportPath()
if import_path == -1
if l:import_path == -1
call go#util#EchoError('package is not inside GOPATH src')
return
endif
let l:args = [l:import_path]
else
let l:import_path = join(a:000, ' ')
let l:args = a:000
endif

call go#util#EchoProgress('[errcheck] analysing ...')
redraw

let [l:out, l:err] = go#util#Exec([go#config#ErrcheckBin(), '-abspath', l:import_path])
let [l:out, l:err] = go#util#ExecInDir([go#config#ErrcheckBin(), '-abspath'] + l:args)

let l:listtype = go#list#Type("GoErrCheck")
if l:err != 0
Expand Down
49 changes: 47 additions & 2 deletions autoload/go/lint_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func! Test_Lint_GOPATH() abort

let expected = [
\ {'lnum': 5, 'bufnr': bufnr('%'), 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function MissingDoc should have comment or be unexported'},
\ {'lnum': 5, 'bufnr': bufnr('%')+6, 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function AlsoMissingDoc should have comment or be unexported'}
\ {'lnum': 5, 'bufnr': bufnr('%')+5, 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function AlsoMissingDoc should have comment or be unexported'}
\ ]

let winnr = winnr()
Expand Down Expand Up @@ -249,7 +249,7 @@ func! Test_Lint_NullModule() abort

let expected = [
\ {'lnum': 5, 'bufnr': bufnr('%'), 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function MissingDoc should have comment or be unexported'},
\ {'lnum': 5, 'bufnr': bufnr('%')+6, 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function AlsoMissingDoc should have comment or be unexported'}
\ {'lnum': 5, 'bufnr': bufnr('%')+5, 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function AlsoMissingDoc should have comment or be unexported'}
\ ]

let winnr = winnr()
Expand All @@ -269,6 +269,51 @@ func! Test_Lint_NullModule() abort
call gotest#assert_quickfix(actual, expected)
endfunc

func! Test_Errcheck() abort
let RestoreGOPATH = go#util#SetEnv('GOPATH', fnamemodify(getcwd(), ':p') . 'test-fixtures/lint')
silent exe 'e ' . $GOPATH . '/src/errcheck/errcheck.go'

try
let l:bufnr = bufnr('')
let expected = [
\ {'lnum': 9, 'bufnr': bufnr(''), 'col': 9, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'module': '', 'text': ":\tio.Copy(os.Stdout, os.Stdin)"},
\ {'lnum': 10, 'bufnr': bufnr('')+1, 'col': 9, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'module': '', 'text': ":\tio.Copy(os.Stdout, os.Stdin)"},
\ ]

" clear the location lists
call setqflist([], 'r')

call go#lint#Errcheck(1)

call gotest#assert_quickfix(getqflist(), expected)
call assert_equal(l:bufnr, bufnr(''))
finally
call call(RestoreGOPATH, [])
endtry
endfunc

func! Test_Errcheck_options() abort
let RestoreGOPATH = go#util#SetEnv('GOPATH', fnamemodify(getcwd(), ':p') . 'test-fixtures/lint')
silent exe 'e ' . $GOPATH . '/src/errcheck/errcheck.go'

try
let l:bufnr = bufnr('')
let expected = [
\ {'lnum': 9, 'bufnr': bufnr(''), 'col': 9, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'module': '', 'text': ":\tio.Copy(os.Stdout, os.Stdin)"},
\ ]

" clear the location lists
call setqflist([], 'r')

call go#lint#Errcheck(1, '-ignoretests')

call gotest#assert_quickfix(getqflist(), expected)
call assert_equal(l:bufnr, bufnr(''))
finally
call call(RestoreGOPATH, [])
endtry
endfunc

func! Test_Errcheck_compilererror() abort
let l:tmp = gotest#load_fixture('lint/src/errcheck/compilererror/compilererror.go')

Expand Down
10 changes: 10 additions & 0 deletions autoload/go/test-fixtures/lint/src/errcheck/errcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package errcheck

import (
"io"
"os"
)

func foo() {
io.Copy(os.Stdout, os.Stdin)
}
11 changes: 11 additions & 0 deletions autoload/go/test-fixtures/lint/src/errcheck/errcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package errcheck

import (
"io"
"os"
"testing"
)

func TestFoo(t *testing.T) {
io.Copy(os.Stdout, os.Stdin)
}

0 comments on commit c99b2f1

Please sign in to comment.