Support $GO111MODULE with Go tooling

Allows the user to override $GO111MODULE environment variable through
ale options. This gives control over the default behavior of Go module
resolution.

Golang documentation:
https://github.com/golang/go/wiki/Modules#how-to-use-modules

Add `ale#Go#EnvString()` function to make it easy to add similar Go
environment variables in the future.

Use the new `EnvString` function in all available Go tools callbacks
& update tests

Also add test of linter command callback for `gofmt`
This commit is contained in:
Elias Martinez Cohen
2019-04-10 22:25:06 -04:00
parent 89f7292138
commit 49db8210f6
32 changed files with 269 additions and 15 deletions

View File

@@ -9,6 +9,7 @@ After:
endif
unlet! b:ale_completion_enabled
unlet! b:ale_go_go111module
call ale#assert#TearDownLinterTest()
@@ -34,6 +35,14 @@ Execute(should set bingo options):
AssertLinter 'bingo',
\ ale#Escape('bingo') . ' --mode stdio --trace'
Execute(should support Go environment variables):
call ale#test#SetFilename('go_paths/go1/prj1/file.go')
let b:ale_go_go111module = 'on'
AssertLinter 'bingo',
\ ale#Env('GO111MODULE', 'on') . ale#Escape('bingo') . ' --mode stdio'
Execute(Should return directory for 'go.mod' if found in parent directory):
call ale#test#SetFilename('../go_files/test.go')
@@ -44,3 +53,20 @@ Execute(Should return nearest directory with '.git' if found in parent directory
call mkdir(g:dir . '/.git')
AssertLSPProject g:dir
Execute(Should ignore 'go.mod' and return '.git' dir if modules off):
call ale#test#SetFilename('../go_files/test.go')
let b:ale_go_go111module = 'off'
let b:parent_dir = ale#path#Simplify(g:dir . '/..')
let b:git_dir = b:parent_dir . '/.git'
if !isdirectory(b:git_dir)
call mkdir(b:git_dir)
endif
AssertLSPProject b:parent_dir
call delete(b:git_dir, 'd')
unlet! b:parent_dir
unlet! b:git_dir

View File

@@ -14,6 +14,16 @@ Execute(The default commands should be correct):
\ ale#path#CdString(expand('%:p:h'))
\ . 'go test -c -o /dev/null ./'
Execute(Go environment variables should be supported):
let b:ale_go_go111module = 'on'
AssertLinter 'go',
\ ale#path#CdString(expand('%:p:h'))
\ . ale#Env('GO111MODULE', 'on')
\ . 'go test -c -o /dev/null ./'
unlet! b:ale_go_go111module
Execute(Extra options should be supported):
let g:ale_go_gobuild_options = '--foo-bar'

View File

@@ -0,0 +1,19 @@
Before:
call ale#assert#SetUpLinterTest('go', 'gofmt')
call ale#test#SetFilename('../go_files/testfile2.go')
After:
call ale#assert#TearDownLinterTest()
Execute(The default gofmt command should be correct):
AssertLinter 'gofmt',
\ ale#Escape('gofmt') . ' -e %t'
Execute(The gofmt command should support Go environment variables):
let b:ale_go_go111module = 'on'
AssertLinter 'gofmt',
\ ale#Env('GO111MODULE', 'on')
\ . ale#Escape('gofmt') . ' -e %t'
unlet! b:ale_go_go111module

View File

@@ -30,6 +30,18 @@ Execute(The golangci-lint callback should use configured options):
\ . ' run ' . ale#Escape(expand('%' . ':t'))
\ . ' --foobar'
Execute(The golangci-lint callback should support environment variables):
let b:ale_go_go111module = 'on'
AssertLinter 'golangci-lint',
\ ale#path#CdString(expand('%:p:h'))
\ . ale#Env('GO111MODULE', 'on')
\ . ale#Escape('golangci-lint')
\ . ' run ' . ale#Escape(expand('%' . ':t'))
\ . ' --enable-all'
unlet! b:ale_go_go111module
Execute(The golangci-lint `lint_package` option should use the correct command):
let b:ale_go_golangci_lint_package = 1

View File

@@ -14,6 +14,7 @@ After:
Restore
unlet! b:ale_completion_enabled
unlet! b:ale_go_go111module
unlet! g:sep
call ale#assert#TearDownLinterTest()
@@ -52,6 +53,12 @@ Execute(should ignore go-langserver -gocodecompletion option):
AssertLinter 'go-langserver', ale#Escape('go-langserver') . ' -trace'
Execute(should support Go environment variables):
let b:ale_go_go111module = 'on'
AssertLinter 'go-langserver',
\ ale#Env('GO111MODULE', 'on') . ale#Escape('go-langserver')
Execute(should set go-langserver for go app1):
call ale#test#SetFilename('go_paths/go1/prj1/file.go')

View File

@@ -16,3 +16,11 @@ Execute(The golint options should be configurable):
let b:ale_go_golint_options = '--foo'
AssertLinter 'golint', ale#Escape('golint') . ' --foo %t'
Execute(The golint command should support Go environment variables):
let b:ale_go_go111module = 'on'
AssertLinter 'golint',
\ ale#Env('GO111MODULE', 'on') . ale#Escape('golint') . ' %t'
unlet! b:ale_go_go111module

View File

@@ -30,6 +30,16 @@ Execute(The gometalinter callback should use configured options):
\ . ' --include=' . ale#Escape(ale#util#EscapePCRE(expand('%' . ':t')))
\ . ' --foobar' . ' .'
Execute(The gometalinter should use configured environment variables):
let b:ale_go_go111module = 'off'
AssertLinter 'gometalinter',
\ ale#path#CdString(expand('%:p:h'))
\ . ale#Env('GO111MODULE', 'off')
\ . ale#Escape('gometalinter')
\ . ' --include=' . ale#Escape(ale#util#EscapePCRE(expand('%' . ':t')))
\ . ' .'
unlet! b:ale_go_go111module
Execute(The gometalinter `lint_package` option should use the correct command):
let b:ale_go_gometalinter_lint_package = 1

View File

@@ -8,6 +8,7 @@ After:
call delete(g:dir . '/.git', 'd')
endif
unlet! b:ale_go_go111module
unlet! b:ale_completion_enabled
call ale#assert#TearDownLinterTest()
@@ -34,6 +35,12 @@ Execute(should set gopls options):
AssertLinter 'gopls',
\ ale#Escape('gopls') . ' --mode stdio --trace'
Execute(should support go environment variables):
let b:ale_go_go111module = 'off'
AssertLinter 'gopls',
\ ale#Env('GO111MODULE', 'off') . ale#Escape('gopls') . ' --mode stdio'
Execute(Should return directory for 'go.mod' if found in parent directory):
call ale#test#SetFilename('../go_files/test.go')
@@ -44,3 +51,20 @@ Execute(Should return nearest directory with '.git' if found in parent directory
call mkdir(g:dir . '/.git')
AssertLSPProject g:dir
Execute(Should ignore 'go.mod' and return '.git' dir if modules off):
call ale#test#SetFilename('../go_files/test.go')
let b:ale_go_go111module = 'off'
let b:parent_dir = ale#path#Simplify(g:dir . '/..')
let b:git_dir = b:parent_dir . '/.git'
if !isdirectory(b:git_dir)
call mkdir(b:git_dir)
endif
AssertLSPProject b:parent_dir
call delete(b:git_dir, 'd')
unlet! b:parent_dir
unlet! b:git_dir

View File

@@ -8,3 +8,12 @@ After:
Execute(The default gosimple command should be correct):
AssertLinter 'gosimple',
\ ale#path#CdString(expand('%:p:h')) . ' gosimple .'
Execute(The gosimple command should support Go environment variables):
let b:ale_go_go111module = 'on'
AssertLinter 'gosimple',
\ ale#path#CdString(expand('%:p:h')) . ' '
\ . ale#Env('GO111MODULE', 'on') . 'gosimple .'
unlet! b:ale_go_go111module

View File

@@ -13,3 +13,13 @@ Execute(The gotype callback should ignore test files):
call ale#test#SetFilename('bla_test.go')
AssertLinter 'gotype', ''
Execute(The gotype callback should support Go environment variables):
let b:ale_go_go111module = 'on'
AssertLinter 'gotype',
\ ale#path#CdString(expand('%:p:h')) . ' '
\ . ale#Env('GO111MODULE', 'on')
\ . 'gotype -e .'
unlet! b:ale_go_go111module

View File

@@ -17,3 +17,11 @@ Execute(Extra options should be supported):
Execute(The executable should be configurable):
let g:ale_go_go_executable = 'foobar'
AssertLinter 'foobar', ale#path#CdString(expand('%:p:h')) . ' foobar vet .'
Execute(Go environment variables should be supported):
let b:ale_go_go111module = 'on'
AssertLinter 'go',
\ ale#path#CdString(expand('%:p:h')) . ' '
\ . ale#Env('GO111MODULE', 'on')
\ . 'go vet .'
unlet! b:ale_go_go111module

View File

@@ -24,3 +24,20 @@ Execute(The staticcheck `lint_package` option should use the correct command):
AssertLinter 'staticcheck',
\ ale#path#CdString(expand('%:p:h')) . 'staticcheck .',
Execute(The staticcheck callback should use the `GO111MODULE` option if set):
let b:ale_go_go111module = 'off'
AssertLinter 'staticcheck',
\ ale#path#CdString(expand('%:p:h'))
\ . ale#Env('GO111MODULE', 'off')
\ . 'staticcheck '
\ . ale#Escape(expand('%' . ':t'))
" Test with lint_package option set
let b:ale_go_staticcheck_lint_package = 1
AssertLinter 'staticcheck',
\ ale#path#CdString(expand('%:p:h'))
\ . ale#Env('GO111MODULE', 'off')
\ . 'staticcheck .'
unlet! b:ale_go_go111module