Skip to content

Commit 21376e6

Browse files
committed
test.vim: more efficient compiling of test files
This change improves the way we compile a Go test. Previously we would build a test binary to catch any build errors. A callback was then responsible to delete the test binary after showing build errors (if there were any). This could be racy though, because if the tests lasts long and we quit early, the callback to clean the binary file would never run. So we would end up with artifacts in our working directory. To fix the issue we're going to tell to the `go` tool to run a specific, unique function. We're passing a unique identifier as a function name (which is a randomly generated), i.e: ``` go test -run "F97CC94D-4414-41CF-A185-A077F645DF24" testing: warning: no tests to run PASS ok demo 0.006s ``` This will cause the go tool to build the test file and then try to run the test function. Because there is no such test function, it'll silently quit with zero exit status. As a side effect it'll compile the test file, so we're able to catch any build errors (if any) fixes fatih#907
1 parent 7fafcb1 commit 21376e6

File tree

1 file changed

+6
-37
lines changed

1 file changed

+6
-37
lines changed

autoload/go/test.vim

+6-37
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ function! go#test#Test(bang, compile, ...) abort
66

77
" don't run the test, only compile it. Useful to capture and fix errors.
88
if a:compile
9-
let compile_file = "vim-go-test-compile"
10-
call extend(args, ["-c", "-o", compile_file])
9+
" we're going to tell to run a test function that doesn't exist. This
10+
" triggers a build of the test file itself but no tests will run.
11+
call extend(args, ["-run", "499EE4A2-5C85-4D35-98FC-7377CD87F263"])
1112
endif
1213

1314
if a:0
@@ -45,13 +46,10 @@ function! go#test#Test(bang, compile, ...) abort
4546
\ 'bang': a:bang,
4647
\ 'winnr': winnr(),
4748
\ 'dir': getcwd(),
49+
\ 'compile_test': a:compile,
4850
\ 'jobdir': fnameescape(expand("%:p:h")),
4951
\ }
5052

51-
if a:compile
52-
let job_args['compile_cb'] = function('s:test_compile', [compile_file])
53-
endif
54-
5553
call s:test_job(job_args)
5654
return
5755
elseif has('nvim')
@@ -62,10 +60,6 @@ function! go#test#Test(bang, compile, ...) abort
6260
let id = go#jobcontrol#Spawn(a:bang, "test", args)
6361
endif
6462

65-
if a:compile
66-
call go#jobcontrol#AddHandler(function('s:test_compile_handler'))
67-
let s:test_compile_handlers[id] = compile_file
68-
endif
6963
return id
7064
endif
7165

@@ -81,10 +75,6 @@ function! go#test#Test(bang, compile, ...) abort
8175
let dir = getcwd()
8276
execute cd fnameescape(expand("%:p:h"))
8377

84-
if a:compile
85-
call delete(compile_file)
86-
endif
87-
8878
if go#util#ShellError() != 0
8979
let errors = s:parse_errors(split(out, '\n'))
9080
let errors = go#tool#FilterValids(errors)
@@ -150,7 +140,7 @@ function s:test_job(args) abort
150140
\ 'state': "started",
151141
\ }
152142

153-
if has_key(a:args, 'compile_cb')
143+
if a:args.compile_test
154144
let status.state = "compiling"
155145
endif
156146

@@ -171,8 +161,7 @@ function s:test_job(args) abort
171161
\ 'state': "pass",
172162
\ }
173163

174-
if has_key(a:args, 'compile_cb')
175-
call a:args.compile_cb(a:job, a:exitval, messages)
164+
if a:args.compile_test
176165
let status.state = "success"
177166
endif
178167

@@ -282,25 +271,5 @@ function! s:parse_errors(lines) abort
282271
return errors
283272
endfunction
284273

285-
" test_compile is called when a GoTestCompile call is finished
286-
function! s:test_compile(test_file, job, exit_status, data) abort
287-
call delete(a:test_file)
288-
endfunction
289-
290-
" -----------------------
291-
" | Neovim job handlers |
292-
" -----------------------
293-
let s:test_compile_handlers = {}
294-
295-
function! s:test_compile_handler(job, exit_status, data) abort
296-
if !has_key(s:test_compile_handlers, a:job.id)
297-
return
298-
endif
299-
let l:compile_file = s:test_compile_handlers[a:job.id]
300-
call delete(l:compile_file)
301-
unlet s:test_compile_handlers[a:job.id]
302-
endfunction
303-
304-
305274
" vim: sw=2 ts=2 et
306275
"

0 commit comments

Comments
 (0)