Skip to content

Commit 2d2b378

Browse files
committed
test with module aware mode enabled
Run tests with module aware mode enabled in preparation for the removal of GOPATH mode coming in Go 1.17.
1 parent a60ff48 commit 2d2b378

File tree

24 files changed

+99
-20
lines changed

24 files changed

+99
-20
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.15.1
1+
FROM golang:1.16.2
22

33
RUN apt-get update -y --allow-insecure-repositories && \
44
apt-get install -y build-essential curl git libncurses5-dev python3-pip && \

autoload/go/cmd_test.vim

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ func! Test_GoBuildErrors()
66
try
77
let l:filename = 'cmd/bad.go'
88
let l:tmp = gotest#load_fixture(l:filename)
9-
exe 'cd ' . l:tmp . '/src/cmd'
109

1110
" set the compiler type so that the errorformat option will be set
1211
" correctly.

autoload/go/debug.vim

+2-2
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,8 @@ function! go#debug#TestFunc(...) abort
734734
call call('go#debug#Start', extend(['test', '.', '-test.run', printf('%s$', l:test)], a:000))
735735
endfunction
736736

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

autoload/go/debug_test.vim

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function! Test_GoDebugStart_RelativePackage_NullModule() abort
1515
endfunction
1616

1717
function! Test_GoDebugStart_Package() abort
18-
call s:debug('debug/debugmain')
18+
call s:debug('vim-go.test/debug/debugmain')
1919
endfunction
2020

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

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

144144
if a:0 == 0
145-
call go#util#Chdir('debug/debugmain')
145+
call go#util#Chdir(printf('%s/src/debug/debugmain', l:tmp))
146146
let l:job = go#debug#Start('debug')
147147
else
148148
let l:job = go#debug#Start('debug', a:1)

autoload/go/job_test.vim

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ func! Test_JobDirWithSpaces()
1010
try
1111
let l:filename = 'job/dir has spaces/main.go'
1212
let l:tmp = gotest#load_fixture(l:filename)
13-
exe 'cd ' . fnameescape(l:tmp . '/src/job/dir has spaces')
13+
call go#util#Chdir(printf('%s/src/job/dir has spaces', l:tmp))
14+
call go#util#Exec(['go', 'mod', 'init', 'vim-go.test/job'])
1415

1516
" set the compiler type so that the errorformat option will be set
1617
" correctly.

autoload/go/lint_test.vim

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ endfunc
1212

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

1718
try
1819
let g:go_metalinter_command = a:metalinter
1920
let l:vim = s:vimdir()
2021
let expected = [
21-
\ {'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)'},
22+
\ {'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)'},
2223
\ ]
2324
if a:metalinter == 'golangci-lint'
2425
let expected = [
25-
\ {'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)'}
26+
\ {'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)'}
2627
\ ]
2728
endif
2829

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/errcheck
2+
3+
go 1.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/foo
2+
3+
go 1.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/lint
2+
3+
go 1.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/vet
2+
3+
go 1.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/compilerror
2+
3+
go 1.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/example
2+
3+
go 1.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/play
2+
3+
go 1.16

autoload/go/test-fixtures/test/src/play/play_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"sync"
55
"testing"
66

7-
"play/mock"
7+
"vim-go.test/play/mock"
88
)
99

1010
func TestTopSubHelper(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/showname
2+
3+
go 1.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/testcompilerror
2+
3+
go 1.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/timeout
2+
3+
go 1.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module vim-go.test/veterror
2+
3+
go 1.16

autoload/go/test_test.vim

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ endfunc
101101

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

106107
" clear the quickfix lists

autoload/go/tool_test.vim

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ set cpo&vim
55
func! Test_ExecuteInDir() abort
66
let l:tmp = gotest#write_file('a/a.go', ['package a'])
77
try
8+
let l:cwd = go#util#Exec(['pwd'])
89
let l:out = go#util#ExecInDir(['pwd'])
10+
call assert_notequal(l:cwd, l:out)
911
call assert_equal([l:tmp . "/src/a\n", 0], l:out)
1012
finally
1113
call delete(l:tmp, 'rf')

autoload/go/util.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ function! go#util#Chdir(dir) abort
715715
if !exists('*chdir')
716716
let l:olddir = getcwd()
717717
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
718-
execute cd . fnameescape(a:dir)
718+
execute printf('cd %s', fnameescape(a:dir))
719719
return l:olddir
720720
endif
721721
return chdir(a:dir)

autoload/gotest.vim

+47-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
let s:cpo_save = &cpo
33
set cpo&vim
44

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

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

2331
silent exe 'e! ' . a:path
2432

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

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

165+
" s:setupproject sets up a Go module in dir rooted at the first segment of
166+
" path and changes the current directory to the parent directory of the
167+
" project root.
168+
func! s:setupproject(dir, path) abort
169+
let l:projectdir = s:projectdir(a:path)
170+
let l:mod = printf('vim-go.test/%s', l:projectdir)
171+
let l:modroot = printf('%s/%s', a:dir, l:projectdir)
172+
call s:creategomod(l:mod, l:modroot)
173+
call go#util#Chdir(a:dir)
174+
endfunc
175+
176+
func! s:creategomod(modname, dir) abort
177+
call go#util#ExecInWorkDir(['go', 'mod', 'init', a:modname], a:dir)
178+
endfunc
179+
180+
" s:project dir returns the first element of path.
181+
func! s:projectdir(path) abort
182+
let l:path = a:path
183+
let l:next = fnamemodify(l:path, ':h')
184+
while l:next isnot '.'
185+
let l:path = l:next
186+
let l:next = fnamemodify(l:path, ':h')
187+
endwhile
188+
189+
return l:path
190+
endfunc
191+
150192
" restore Vi compatibility settings
151193
let &cpo = s:cpo_save
152194
unlet s:cpo_save

scripts/install-tools

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ installdir="/tmp/vim-go-test/$1-install"
1717
# Make sure all Go tools and other dependencies are installed.
1818
echo "Installing Go binaries"
1919
export GOPATH=$installdir
20-
export GO111MODULE=off
20+
export GO111MODULE=on
2121
export PATH=${GOPATH}/bin:$PATH
2222
"$vimgodir/scripts/run-vim" $vim +':silent :GoUpdateBinaries' +':qa'
2323

scripts/run-vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fi
2323

2424
dir="/tmp/vim-go-test/$1-install"
2525
export GOPATH=$dir
26-
export GO111MODULE=auto
26+
export GO111MODULE=on
2727
export PATH=${GOPATH}/bin:$PATH
2828
shift
2929

0 commit comments

Comments
 (0)