From c99b2f176deca9ac087eb986a721e1b4ea712def Mon Sep 17 00:00:00 2001 From: Billie Cleek Date: Fri, 14 Feb 2020 11:43:11 -0800 Subject: [PATCH] lint: execute errcheck using ExecInDir 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 --- autoload/go/lint.vim | 7 +-- autoload/go/lint_test.vim | 49 ++++++++++++++++++- .../lint/src/errcheck/errcheck.go | 10 ++++ .../lint/src/errcheck/errcheck_test.go | 11 +++++ 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 autoload/go/test-fixtures/lint/src/errcheck/errcheck.go create mode 100644 autoload/go/test-fixtures/lint/src/errcheck/errcheck_test.go diff --git a/autoload/go/lint.vim b/autoload/go/lint.vim index 6b7a7fe0fe..a730cd1dfd 100644 --- a/autoload/go/lint.vim +++ b/autoload/go/lint.vim @@ -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 diff --git a/autoload/go/lint_test.vim b/autoload/go/lint_test.vim index b5702cf131..0a32a00c6e 100644 --- a/autoload/go/lint_test.vim +++ b/autoload/go/lint_test.vim @@ -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() @@ -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() @@ -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') diff --git a/autoload/go/test-fixtures/lint/src/errcheck/errcheck.go b/autoload/go/test-fixtures/lint/src/errcheck/errcheck.go new file mode 100644 index 0000000000..72d7fc32e4 --- /dev/null +++ b/autoload/go/test-fixtures/lint/src/errcheck/errcheck.go @@ -0,0 +1,10 @@ +package errcheck + +import ( + "io" + "os" +) + +func foo() { + io.Copy(os.Stdout, os.Stdin) +} diff --git a/autoload/go/test-fixtures/lint/src/errcheck/errcheck_test.go b/autoload/go/test-fixtures/lint/src/errcheck/errcheck_test.go new file mode 100644 index 0000000000..445d9e55bf --- /dev/null +++ b/autoload/go/test-fixtures/lint/src/errcheck/errcheck_test.go @@ -0,0 +1,11 @@ +package errcheck + +import ( + "io" + "os" + "testing" +) + +func TestFoo(t *testing.T) { + io.Copy(os.Stdout, os.Stdin) +}