Skip to content

Commit

Permalink
Merge pull request #3183 from bhcleek/test-with-modules-only
Browse files Browse the repository at this point in the history
test with module aware mode enabled
  • Loading branch information
bhcleek authored Mar 22, 2021
2 parents a60ff48 + 2d2b378 commit 00c5f2d
Show file tree
Hide file tree
Showing 24 changed files with 99 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.15.1
FROM golang:1.16.2

RUN apt-get update -y --allow-insecure-repositories && \
apt-get install -y build-essential curl git libncurses5-dev python3-pip && \
Expand Down
1 change: 0 additions & 1 deletion autoload/go/cmd_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ func! Test_GoBuildErrors()
try
let l:filename = 'cmd/bad.go'
let l:tmp = gotest#load_fixture(l:filename)
exe 'cd ' . l:tmp . '/src/cmd'

" set the compiler type so that the errorformat option will be set
" correctly.
Expand Down
4 changes: 2 additions & 2 deletions autoload/go/debug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,8 @@ function! go#debug#TestFunc(...) abort
call call('go#debug#Start', extend(['test', '.', '-test.run', printf('%s$', l:test)], a:000))
endfunction

" Start the debug mode. The first argument is the package name to compile and
" debug, anything else will be passed to the running program.
" Start the debug mode. The first variadic argument is the package name to
" compile and debug, anything else will be passed to the running program.
function! go#debug#Start(mode, ...) abort
call go#cmd#autowrite()

Expand Down
6 changes: 3 additions & 3 deletions autoload/go/debug_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function! Test_GoDebugStart_RelativePackage_NullModule() abort
endfunction

function! Test_GoDebugStart_Package() abort
call s:debug('debug/debugmain')
call s:debug('vim-go.test/debug/debugmain')
endfunction

function! Test_GoDebugStart_Errors() abort
Expand All @@ -27,7 +27,7 @@ function! Test_GoDebugStart_Errors() abort
let l:tmp = gotest#load_fixture('debug/compilerror/main.go')

let l:expected = [
\ {'lnum': 0, 'bufnr': 0, 'col': 0, 'valid': 0, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': '# debug/compilerror'},
\ {'lnum': 0, 'bufnr': 0, 'col': 0, 'valid': 0, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': '# vim-go.test/debug/compilerror'},
\ {'lnum': 6, 'bufnr': bufnr('%'), 'col': 22, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': ' syntax error: unexpected newline, expecting comma or )'},
\ {'lnum': 0, 'bufnr': 0, 'col': 0, 'valid': 0, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exit status 2'}
\]
Expand Down Expand Up @@ -142,7 +142,7 @@ function! s:debug(...) abort
call assert_false(exists(':GoDebugStop'))

if a:0 == 0
call go#util#Chdir('debug/debugmain')
call go#util#Chdir(printf('%s/src/debug/debugmain', l:tmp))
let l:job = go#debug#Start('debug')
else
let l:job = go#debug#Start('debug', a:1)
Expand Down
3 changes: 2 additions & 1 deletion autoload/go/job_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ func! Test_JobDirWithSpaces()
try
let l:filename = 'job/dir has spaces/main.go'
let l:tmp = gotest#load_fixture(l:filename)
exe 'cd ' . fnameescape(l:tmp . '/src/job/dir has spaces')
call go#util#Chdir(printf('%s/src/job/dir has spaces', l:tmp))
call go#util#Exec(['go', 'mod', 'init', 'vim-go.test/job'])

" set the compiler type so that the errorformat option will be set
" correctly.
Expand Down
7 changes: 4 additions & 3 deletions autoload/go/lint_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ endfunc

func! s:gometa(metalinter) abort
let RestoreGOPATH = go#util#SetEnv('GOPATH', fnamemodify(getcwd(), ':p') . 'test-fixtures/lint')
silent exe 'e! ' . $GOPATH . '/src/lint/lint.go'
call go#util#Chdir('test-fixtures/lint/src/foo')
silent exe 'e! ' . $GOPATH . '/src/foo/foo.go'

try
let g:go_metalinter_command = a:metalinter
let l:vim = s:vimdir()
let expected = [
\ {'lnum': 1, 'bufnr': bufnr('%')+9, 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'at least one file in a package should have a package comment (ST1000)'},
\ {'lnum': 1, 'bufnr': bufnr('%'), 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'at least one file in a package should have a package comment (ST1000)'},
\ ]
if a:metalinter == 'golangci-lint'
let expected = [
\ {'lnum': 5, 'bufnr': bufnr('%')+9, 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function `MissingFooDoc` should have comment or be unexported (golint)'}
\ {'lnum': 5, 'bufnr': bufnr('%'), 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function `MissingFooDoc` should have comment or be unexported (golint)'}
\ ]
endif

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

go 1.16
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/lint/src/foo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/foo

go 1.16
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/lint/src/lint/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/lint

go 1.16
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/lint/src/vet/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/vet

go 1.16
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/test/src/compilerror/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/compilerror

go 1.16
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/test/src/example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/example

go 1.16
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/test/src/play/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/play

go 1.16
2 changes: 1 addition & 1 deletion autoload/go/test-fixtures/test/src/play/play_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sync"
"testing"

"play/mock"
"vim-go.test/play/mock"
)

func TestTopSubHelper(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/test/src/showname/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/showname

go 1.16
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/test/src/testcompilerror/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/testcompilerror

go 1.16
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/test/src/timeout/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/timeout

go 1.16
3 changes: 3 additions & 0 deletions autoload/go/test-fixtures/test/src/veterror/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module vim-go.test/veterror

go 1.16
1 change: 1 addition & 0 deletions autoload/go/test_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ endfunc

func! s:test(file, expected, ...) abort
let $GOPATH = fnameescape(fnamemodify(getcwd(), ':p')) . 'test-fixtures/test'
call go#util#Chdir(printf('%s/src/%s', $GOPATH, fnamemodify(a:file, ':h')))
silent exe 'e ' . $GOPATH . '/src/' . a:file

" clear the quickfix lists
Expand Down
2 changes: 2 additions & 0 deletions autoload/go/tool_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ set cpo&vim
func! Test_ExecuteInDir() abort
let l:tmp = gotest#write_file('a/a.go', ['package a'])
try
let l:cwd = go#util#Exec(['pwd'])
let l:out = go#util#ExecInDir(['pwd'])
call assert_notequal(l:cwd, l:out)
call assert_equal([l:tmp . "/src/a\n", 0], l:out)
finally
call delete(l:tmp, 'rf')
Expand Down
2 changes: 1 addition & 1 deletion autoload/go/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ function! go#util#Chdir(dir) abort
if !exists('*chdir')
let l:olddir = getcwd()
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
execute cd . fnameescape(a:dir)
execute printf('cd %s', fnameescape(a:dir))
return l:olddir
endif
return chdir(a:dir)
Expand Down
52 changes: 47 additions & 5 deletions autoload/gotest.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
let s:cpo_save = &cpo
set cpo&vim

" Write a Go file to a temporary directory and append this directory to $GOPATH.
" Write a Go file to a temporary directory and append this directory to
" $GOPATH.
"
" The file will written to a:path, which is relative to the temporary directory,
" and this file will be loaded as the current buffer.
" The file will written to a:path, which is relative to the temporary
" directory, and this file will be loaded as the current buffer.
"
" A Go module will be configured in the first segment of a:path within the
" temporary directory. The module's name will be prefixed with vim-go.test/
" followed by the first segment in a:path.
"
" The current directory will be changed to the parent directory of module
" root.
"
" The cursor will be placed on the character before any 0x1f byte.
"
Expand All @@ -18,7 +26,7 @@ fun! gotest#write_file(path, contents) abort

call mkdir(fnamemodify(l:full_path, ':h'), 'p')
call writefile(a:contents, l:full_path)
call go#util#Chdir(l:dir . '/src')
call s:setupproject(printf('%s/src', l:dir), a:path)

silent exe 'e! ' . a:path

Expand Down Expand Up @@ -48,6 +56,13 @@ endfun
"
" The file will be copied to a new GOPATH-compliant temporary directory and
" loaded as the current buffer.
"
" A Go module will be configured in the first segment of a:path within the
" temporary directory. The module's name will be prefixed with vim-go.test/
" followed by the first segment in a:path.
"
" The current directory will be changed to the parent directory of module
" root.
fun! gotest#load_fixture(path) abort
if go#util#has_job()
call go#lsp#CleanWorkspaces()
Expand All @@ -57,7 +72,7 @@ fun! gotest#load_fixture(path) abort
let l:full_path = l:dir . '/src/' . a:path

call mkdir(fnamemodify(l:full_path, ':h'), 'p')
call go#util#Chdir(l:dir . '/src')
call s:setupproject(printf('%s/src', l:dir), a:path)
silent exe 'noautocmd e! ' . a:path
silent exe printf('read %s/test-fixtures/%s', g:vim_go_root, a:path)
silent noautocmd w!
Expand Down Expand Up @@ -147,6 +162,33 @@ func! gotest#assert_quickfix(got, want) abort
return l:retval
endfunc

" s:setupproject sets up a Go module in dir rooted at the first segment of
" path and changes the current directory to the parent directory of the
" project root.
func! s:setupproject(dir, path) abort
let l:projectdir = s:projectdir(a:path)
let l:mod = printf('vim-go.test/%s', l:projectdir)
let l:modroot = printf('%s/%s', a:dir, l:projectdir)
call s:creategomod(l:mod, l:modroot)
call go#util#Chdir(a:dir)
endfunc

func! s:creategomod(modname, dir) abort
call go#util#ExecInWorkDir(['go', 'mod', 'init', a:modname], a:dir)
endfunc

" s:project dir returns the first element of path.
func! s:projectdir(path) abort
let l:path = a:path
let l:next = fnamemodify(l:path, ':h')
while l:next isnot '.'
let l:path = l:next
let l:next = fnamemodify(l:path, ':h')
endwhile

return l:path
endfunc

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
Expand Down
2 changes: 1 addition & 1 deletion scripts/install-tools
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ installdir="/tmp/vim-go-test/$1-install"
# Make sure all Go tools and other dependencies are installed.
echo "Installing Go binaries"
export GOPATH=$installdir
export GO111MODULE=off
export GO111MODULE=on
export PATH=${GOPATH}/bin:$PATH
"$vimgodir/scripts/run-vim" $vim +':silent :GoUpdateBinaries' +':qa'

Expand Down
2 changes: 1 addition & 1 deletion scripts/run-vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fi

dir="/tmp/vim-go-test/$1-install"
export GOPATH=$dir
export GO111MODULE=auto
export GO111MODULE=on
export PATH=${GOPATH}/bin:$PATH
shift

Expand Down

0 comments on commit 00c5f2d

Please sign in to comment.