Skip to content

Instantly share code, notes, and snippets.

@franciscoj
Created April 17, 2022 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franciscoj/b6741a01816246726438c9ca2f7ccc7b to your computer and use it in GitHub Desktop.
Save franciscoj/b6741a01816246726438c9ca2f7ccc7b to your computer and use it in GitHub Desktop.
Run go test, using grc for colorizing, make and vim-test
regexp==== RUN .*
colour=blue
-
regexp=--- PASS: .*
colour=green
-
regexp=^PASS$
colour=green
-
regexp=^(ok|\?) .*
colour=default
-
regexp=^\s*panic: .*
colour=red
-
regexp=--- FAIL: .*
colour=bold red
-
regexp=[^\s]+\.go(:\d+)?
colour=cyan
" This is the config for vim-test. I use this plugin
" https://github.com/embear/vim-localvimrc for local project config.
" In addition to make I like using grc so that go tests are colorized.
let test#go#gotest#executable = 'grc make test'
function! MakeTransform(cmd) abort
let l:args = substitute(a:cmd, g:test#go#gotest#executable." ", "", "")
" echo "args: ".l:args
" This is fun:
" Running the nearest test in go uses the `-run='Test_Name$'` format.
"
" We want those to run from make, But since the dollar sign has a special
" meaning in make, we need to scape it. To do that we need to use $$ instead
" of $.
"
" Since the $ sign also has a meaning in the shell, we need those to be \$\$
" instead.
"
" And then, since the substitution is done with Vim... I don't even know how
" this worked, made it through test and error and it ended up working like
" this.
let l:escaped = substitute(l:args, "\\\$", "\\\\\\$\\\\\\$", "g")
" echo "escaped: ".l:escaped
" There's an example Makefile on the gist explaining this :D
let l:final = 'TEST_ARGS="'.l:escaped.'"'
" echo "final: ".l:final
return g:test#go#gotest#executable." ".l:final
endfunction
let g:test#custom_transformations = {'make': function('MakeTransform')}
let g:test#transformation = 'make'
# https://github.com/garabik/grc
# Go test
\bgo.* test\b
conf.gotest
# Go test through make
\bmake test.*\b
conf.gotest
# Build the test command allowing to overwrite some of the options.
TEST_FLAGS?=-v -race -count 1 -timeout 3s
TEST_ARGS?=./...
GO_TEST=go test $(TEST_FLAGS)
.PHONY: test
test:
$(GO_TEST) $(TEST_ARGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment