@ -26,12 +26,13 @@ local long_opts = {
}
local sep = require ( ' go.utils ' ) . sep ( )
local short_opts = ' a:cC: t:bsFmpn: v'
local short_opts = ' a:cC: b:fFmn:pst:r v'
local bench_opts = { ' -benchmem ' , ' -cpuprofile ' , ' profile.out ' }
local os_name = vim.loop . os_uname ( ) . sysname
local is_windows = os_name == ' Windows ' or os_name == ' Windows_NT '
local is_git_shell = is_windows and ( vim.fn . exists ( ' $SHELL ' ) and vim.fn . expand ( ' $SHELL ' ) : find ( ' bash.exe ' ) ~= nil )
local is_git_shell = is_windows
and ( vim.fn . exists ( ' $SHELL ' ) and vim.fn . expand ( ' $SHELL ' ) : find ( ' bash.exe ' ) ~= nil )
M.efm = function ( )
local indent = [[%\\%( %\\)]]
local efm = [[%-G=== RUN %.%#]]
@ -72,11 +73,10 @@ end
-- return "-tags=tag1,tag2"
M.get_build_tags = function ( args , tbl )
-- local tags = "-tags"
args = args or { }
local tags = { }
if _GO_NVIM_CFG.build_tags ~= ' ' then
ta gs = { _GO_NVIM_CFG.build_tags }
ta ble.insert( tags , _GO_NVIM_CFG.build_tags )
end
local optarg , _ , reminder = getopt.get_opts ( args , short_opts , long_opts )
@ -90,13 +90,16 @@ M.get_build_tags = function(args, tbl)
vim.list_extend ( tags , rt )
end
local t = ' -tags '
if _GO_NVIM_CFG.test_runner == ' dlv ' then
t = ' --build-flags '
end
if # tags > 0 then
if tbl then
return { ' -tags ' , table.concat ( tags , ' , ' ) } , reminder , optarg
return { t , table.concat ( tags , ' , ' ) } , reminder , optarg
end
return ' -tags =' .. table.concat ( tags , ' , ' ) , reminder , optarg
return t .. ' =' .. table.concat ( tags , ' , ' ) , reminder , optarg
end
return
end
function M . get_test_path ( )
@ -137,21 +140,25 @@ local function get_test_filebufnr()
return bufnr
end
-- {-c: compile, -v: verbose, -t: tags, -b: bench, -s: select}
local function run_test ( path , args )
local function cmd_builder ( path , args )
log ( args )
local compile = false
local bench = false
local extra_args = ' '
for i , arg in ipairs ( args ) do
--check if it is bench test
if arg : find ( ' -bench ' ) then
bench = true
table.remove ( args , i )
break
end
end
local optarg , oid , reminder = getopt.get_opts ( args , short_opts , long_opts )
trace ( optarg , oid , reminder )
if optarg [ ' c ' ] then
path = utils.rel_path ( true ) -- vfn.expand("%:p:h") can not resolve releative path
compile = true
end
if optarg [ ' b ' ] then
bench = true
end
if reminder and # reminder > 0 then
--if % in args expand to current file
@ -162,10 +169,6 @@ local function run_test(path, args)
end
end
if optarg [ ' a ' ] then
extra_args = optarg [ ' a ' ]
end
if next ( reminder ) then
path = reminder [ 1 ]
table.remove ( reminder , 1 )
@ -174,49 +177,56 @@ local function run_test(path, args)
if _GO_NVIM_CFG.test_runner ~= test_runner then
test_runner = _GO_NVIM_CFG.test_runner
if not install ( test_runner ) then
vim.notify ( ' test runner not found ' , vim.log . levels.INFO )
test_runner = ' go '
end
end
local tags = M.get_build_tags ( args )
log ( tags )
local cmd = { }
log ( ' tags ' , tags )
local cmd = { ' go ' , ' test ' }
local run_in_floaterm = optarg [ ' F ' ] or _GO_NVIM_CFG.run_in_floaterm
if run_in_floaterm then
table.insert ( cmd , test_runner or ' go ' )
table.insert ( cmd , ' test ' )
cmd [ 1 ] = test_runner or ' go '
end
if not empty ( tags ) then
cmd = vim.list_extend ( cmd , { tags } )
end
if optarg [ ' C ' ] then
if run_in_floaterm then
table.insert ( cmd , ' -coverprofile= ' .. optarg [ ' C ' ] )
else
table.insert ( cmd , ' -C ' )
table.insert ( cmd , optarg [ ' C ' ] )
end
if optarg [ ' c ' ] then
compile = true
end
if optarg [ ' n ' ] then
if run_in_floaterm then
table.insert ( cmd , ' -count= ' .. optarg [ ' n ' ] )
else
table.insert ( cmd , ' -n ' )
table.insert ( cmd , optarg [ ' n ' ] )
end
table.insert ( cmd , ' -count= ' .. optarg [ ' n ' ] )
end
if ( optarg [ ' v ' ] or _GO_NVIM_CFG.verbose_tests ) and _GO_NVIM_CFG.test_runner == ' go ' then
table.insert ( cmd , ' -v ' )
end
if not empty ( reminder ) then
cmd = vim.list_extend ( cmd , reminder )
log ( ' **** ' , reminder , cmd )
if optarg [ ' f ' ] then
log ( ' fuzz test ' )
table.insert ( cmd , ' -fuzz ' )
end
if optarg [ ' P ' ] then
table.insert ( cmd , ' -parallel ' )
table.insert ( cmd , optarg [ ' P ' ] )
end
if optarg [ ' r ' ] then
log ( ' run test ' , optarg [ ' r ' ] )
table.insert ( cmd , ' -run ' )
table.insert ( cmd , optarg [ ' r ' ] )
end
if optarg [ ' b ' ] and optarg [ ' b ' ] ~= ' ' then
log ( ' build test flags ' , optarg [ ' b ' ] )
assert ( type ( optarg [ ' b ' ] ) == ' string ' , ' build flags must be string ' )
table.insert ( cmd , optarg [ ' b ' ] )
end
if compile == true then
@ -240,11 +250,27 @@ local function run_test(path, args)
end
end
if # extra_args > 0 then
table.insert ( cmd , ' -a ' )
table.insert ( cmd , extra_args )
if optarg [ ' C ' ] then
table.insert ( cmd , ' -coverprofile= ' .. optarg [ ' C ' ] )
end
if not empty ( reminder ) then
cmd = vim.list_extend ( cmd , reminder )
log ( ' **** ' , reminder , cmd )
end
utils.log ( cmd , args )
if optarg [ ' a ' ] then
table.insert ( cmd , ' -args ' )
table.insert ( cmd , optarg [ ' a ' ] )
end
return cmd , optarg , tags
end
-- {-c: compile, -v: verbose, -t: tags, -b: bench, -s: select}
local function run_test ( path , args )
log ( args )
local cmd , optarg = cmd_builder ( path , args )
log ( cmd , args )
local run_in_floaterm = _GO_NVIM_CFG.run_in_floaterm or optarg [ ' F ' ]
if run_in_floaterm then
install ( ' richgo ' )
local term = require ( ' go.term ' ) . run
@ -257,7 +283,8 @@ local function run_test(path, args)
vim.cmd ( [[setl makeprg=]] .. _GO_NVIM_CFG.go .. [[\ test]] )
utils.log ( ' test cmd ' , cmd )
return require ( ' go.asyncmake ' ) . make ( unpack ( cmd ) )
local asyncmake = require ( ' go.asyncmake ' )
return asyncmake.runjob ( cmd , ' go test ' , args )
end
M.test = function ( ... )
@ -278,8 +305,23 @@ M.test = function(...)
package = ' p ' ,
}
local parallel = 0
for i , arg in ipairs ( args ) do
--check if it is bench test
if arg : find ( ' -parallel ' ) then
parallel = args [ i + 1 ] : match ( ' %d+ ' )
table.remove ( args , i )
table.remove ( args , i )
break
end
end
local test_short_opts = ' a:vcC:t:bsfmnpF '
local optarg , _ , reminder = getopt.get_opts ( args , test_short_opts , test_opts )
if parallel ~= 0 then
optarg [ ' P ' ] = parallel
table.insert ( args , ' -P ' )
table.insert ( args , parallel )
end
-- if % in reminder expand to current file
for i , v in ipairs ( reminder ) do
@ -383,11 +425,8 @@ M.get_testcase_name = function()
end
local function run_tests_with_ts_node ( args , func_node , tblcase_ns )
local optarg , _ , reminder = getopt.get_opts ( args , short_opts , long_opts )
local tags = M.get_build_tags ( args )
utils.log ( ' args: ' , args )
utils.log ( ' tags: ' , tags )
utils.log ( ' parnode ' .. vim.inspect ( func_node ) )
local fpath = M.get_test_path ( )
local cmd , optarg , tags = cmd_builder ( fpath , args )
local test_runner = _GO_NVIM_CFG.test_runner or ' go '
@ -395,54 +434,19 @@ local function run_tests_with_ts_node(args, func_node, tblcase_ns)
if not install ( test_runner ) then
test_runner = ' go '
end
if test_runner == ' ginkgo ' then
ginkgo.test_func ( args )
end
end
local run_flags = ' -r '
local cmd = { }
local run_in_floaterm = optarg [ ' F ' ] or _GO_NVIM_CFG.run_in_floaterm
if run_in_floaterm then
table.insert ( cmd , test_runner )
table.insert ( cmd , ' test ' )
run_flags = ' -run '
if test_runner == ' ginkgo ' then
return ginkgo.test_func ( args )
end
if optarg [ ' s ' ] then
return M.select_tests ( )
end
if ( optarg [ ' v ' ] or _GO_NVIM_CFG.verbose_tests ) and _GO_NVIM_CFG.test_runner == ' go ' then
table.insert ( cmd , ' -v ' )
end
if tags and tags ~= ' ' then
table.insert ( cmd , tags )
end
if func_node == nil or func_node.name == nil then
return
end
if optarg [ ' n ' ] then
if run_in_floaterm then
table.insert ( cmd , ' -count ' )
table.insert ( cmd , ( optarg [ ' n ' ] or ' 1 ' ) )
else
table.insert ( cmd , ' -n ' )
table.insert ( cmd , ( optarg [ ' n ' ] or ' 1 ' ) )
end
end
if optarg [ ' C ' ] then
if run_in_floaterm then
table.insert ( cmd , ' -count ' )
table.insert ( cmd , optarg [ ' C ' ] )
else
table.insert ( cmd , ' -C ' )
table.insert ( cmd , optarg [ ' C ' ] )
end
end
local tbl_name = ' '
if tblcase_ns then
tbl_name = tblcase_ns : gsub ( ' / ' , ' // ' )
@ -452,50 +456,39 @@ local function run_tests_with_ts_node(args, func_node, tblcase_ns)
end
if func_node.name : find ( ' Bench ' ) then
if run_in_floaterm then
local bench = ' -bench= ' .. func_node.name .. tbl_name
table.insert ( cmd , bench )
else
table.insert ( cmd , ' -b ' )
table.insert ( cmd , func_node.name )
local bench = ' -bench= ' .. func_node.name .. tbl_name
for i , v in ipairs ( cmd ) do
if v : find ( ' -bench ' ) then
cmd [ i ] = bench
break
end
if i == # cmd then
table.insert ( cmd , bench )
end
end
vim.list_extend ( cmd , bench_opts )
elseif func_node.name : find ( ' Fuzz ' ) then
if run_in_floaterm then
table.insert ( cmd , ' -fuzz=func_node.name ' )
else
table.insert ( cmd , ' -f ' )
table.insert ( cmd , func_node.name )
end
table.insert ( cmd , ' -fuzz= ' .. func_node.name )
else
table.insert ( cmd , run_flags )
table.insert ( cmd , ' -run ' )
if is_windows then
table.insert ( cmd , [[ ^]] .. func_node.name .. [[$ ]] .. tbl_name )
table.insert ( cmd , [["^]] .. func_node.name .. [[$"]] .. tbl_name )
else
table.insert ( cmd , [['^]] .. func_node.name .. [[$']] .. tbl_name )
end
end
local fpath = M.get_test_path ( )
table.insert ( cmd , fpath )
if optarg [ ' a ' ] then
table.insert ( cmd , ' -a ' )
table.insert ( cmd , optarg [ ' a ' ] )
end
if test_runner == ' dlv ' then
local runflag = string.format ( " -test.run='^%s$'%s " , func_node.name , tbl_name )
if tags and # tags > 0 then
cmd = { ' dlv ' , ' test ' , fpath , ' --build-flags ' , tags , ' -- ' , runflag }
else
cmd = { ' dlv ' , ' test ' , fpath , ' -- ' , runflag }
end
table.insert ( cmd , 3 , fpath )
table.insert ( cmd , ' -- ' )
table.insert ( cmd , runflag )
log ( cmd )
local term = require ( ' go.term ' ) . run
term ( { cmd = cmd , autoclose = false } )
return
end
local run_in_floaterm = optarg [ ' F ' ] or _GO_NVIM_CFG.run_in_floaterm
if run_in_floaterm then
utils.log ( cmd )
@ -505,13 +498,12 @@ local function run_tests_with_ts_node(args, func_node, tblcase_ns)
term ( { cmd = cmd , autoclose = false } )
return
end
vim.list_extend ( cmd , reminder )
vim.cmd ( [[setl makeprg=]] .. test_runner .. [[\ test]] )
-- set_efm()
utils.log ( ' test cmd ' , cmd )
return require ( ' go.asyncmake ' ) . make( unpack ( cmd ) )
return require ( ' go.asyncmake ' ) . runjob( cmd , ' go test ' , args )
end
--options {s:select, F: floaterm}
@ -524,8 +516,7 @@ M.test_func = function(...)
return M.select_tests ( )
end
local parser_path = vim.api . nvim_get_runtime_file ( ' parser ' .. sep .. ' go.so ' , false ) [ 1 ]
if not parser_path then
if not vim.api . nvim_get_runtime_file ( ' parser ' .. sep .. ' go.so ' , false ) [ 1 ] then
-- require('nvim-treesitter.install').commands.TSInstallSync['run!']('go')
vim.notify (
' go treesitter parser not found, please Run `:TSInstallSync go` ' ,
@ -566,18 +557,18 @@ M.get_test_cases = function()
.. [[| sed -n 's/func\s\+\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\|/g']]
-- TODO maybe with treesitter or lsp list all functions in current file and regex with Test
if vfn.executable ( ' sed ' ) == 0 then
vim.notify ( ' sed not found ' , vim.log . levels.WARN )
return
end
local tests = vfn.systemlist ( cmd )
local tests _results = vfn.systemlist ( cmd )
if vim.v . shell_error ~= 0 then
utils.warn ( ' go test failed ' .. vim.inspect ( tests ) )
utils.warn ( ' go test failed ' .. vim.inspect ( tests _results ) )
return
end
return tests [ 1 ]
return tests _results [ 1 ]
end
local sep = ' | '
local testsstr = vim.fn . join ( tests , sep )
local testsstr = vim.fn . join ( tests , ' | ' )
utils.log ( tests , testsstr )
return testsstr , tests
end
@ -593,18 +584,12 @@ M.test_file = function(...)
return M.test_package ( ... )
end
local optarg , _ , reminder = getopt.get_opts ( args , short_opts , long_opts )
local run_in_floaterm = optarg [ ' F ' ] or _GO_NVIM_CFG.run_in_floaterm
if vfn.empty ( tests ) == 1 then
vim.notify ( ' no test found fallback to package test ' , vim.log . levels.DEBUG )
M.test_package ( ... )
return
end
local tags = M.get_build_tags ( args )
local test_runner = _GO_NVIM_CFG.go
if _GO_NVIM_CFG.test_runner ~= ' go ' then
test_runner = _GO_NVIM_CFG.test_runner
@ -618,79 +603,70 @@ M.test_file = function(...)
local relpath = utils.rel_path ( true )
log ( relpath )
--
-- local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts)
--
-- local run_in_floaterm = optarg['F'] or _GO_NVIM_CFG.run_in_floaterm
-- local tags = M.get_build_tags(args)
--
-- local cmd_args = { 'go', 'test' }
-- if run_in_floaterm then
-- cmd_args[1] = test_runner or 'go'
-- end
--
-- if (optarg['v'] or _GO_NVIM_CFG.verbose_tests) and _GO_NVIM_CFG.test_runner == 'go' then
-- table.insert(cmd_args, '-v')
-- end
--
-- if tags ~= nil then
-- table.insert(cmd_args, tags)
-- end
--
-- if next(reminder) then
-- vim.list_extend(cmd_args, reminder)
-- end
-- if optarg['n'] then
-- table.insert(cmd_args, '-count=' .. (optarg['n'] or '1'))
-- table.insert(cmd_args, optarg['n'] or '1')
-- end
--
-- if optarg['C'] then
-- table.insert(cmd_args, '-coverprofile=' .. optarg['C'])
-- end
--
local cmd_args , optarg = cmd_builder ( relpath , args )
local cmd_args = { }
if run_in_floaterm then
table.insert ( cmd_args , test_runner )
table.insert ( cmd_args , ' test ' )
end
if ( optarg [ ' v ' ] or _GO_NVIM_CFG.verbose_tests ) and _GO_NVIM_CFG.test_runner == ' go ' then
table.insert ( cmd_args , ' -v ' )
end
if tags ~= nil then
table.insert ( cmd_args , tags )
end
if next ( reminder ) then
vim.list_extend ( cmd_args , reminder )
end
if optarg [ ' n ' ] then
if run_in_floaterm then
table.insert ( cmd_args , ' -count= ' .. ( optarg [ ' n ' ] or ' 1 ' ) )
table.insert ( cmd_args , optarg [ ' n ' ] or ' 1 ' )
else
table.insert ( cmd_args , ' -n ' )
table.insert ( cmd_args , optarg [ ' n ' ] or ' 1 ' )
end
end
if optarg [ ' C ' ] then
if run_in_floaterm then
table.insert ( cmd_args , ' -coverprofile= ' .. optarg [ ' C ' ] )
else
table.insert ( cmd_args , ' -C ' )
table.insert ( cmd_args , optarg [ ' C ' ] )
end
end
if run_in_floaterm then
table.insert ( cmd_args , ' -run ' )
else
table.insert ( cmd_args , ' -r ' )
end
table.insert ( cmd_args , ' -run ' )
if is_windows then
tests = tests
tests = ' " ' .. tests .. ' " '
else
tests = " ' " .. tests .. " ' "
end
table.insert ( cmd_args , tests ) -- shell script | is a pipe
table.insert ( cmd_args , relpath )
if run_in_floaterm then
if optarg [ ' F ' ] or _GO_NVIM_CFG.run_in_floaterm then
install ( ' richgo ' )
local term = require ( ' go.term ' ) . run
cmd_args = richgo ( cmd_args )
cmd_args = table.concat ( cmd_args , ' ' )
local cmd_args_str = table.concat ( cmd_args , ' ' )
log ( cmd_args )
term ( { cmd = cmd_args , autoclose = false } )
term ( { cmd = cmd_args_str , autoclose = false } )
return cmd_args
end
if _GO_NVIM_CFG.test_runner == ' dlv ' then
cmd_args = { ' dlv ' , ' test ' , relpath , ' -- ' , ' -test.run ' , tests }
cmd_args = table.concat ( cmd_args , ' ' )
local term = require ( ' go.term ' ) . run
term ( { cmd = cmd_args , autoclose = false } )
term ( { cmd = table.concat ( cmd_args , ' ' ) , autoclose = false } )
log ( cmd_args )
return cmd_args
end
vim.cmd ( [[setl makeprg=]] .. _GO_NVIM_CFG.go .. [[\ test]] )
log ( cmd_args )
local cmdret = require ( ' go.asyncmake ' ) . make( unpack ( cmd_args ) )
local cmdret = require ( ' go.asyncmake ' ) . runjob ( cmd_args , ' go test ' , args )
utils.log ( ' test cmd: ' , cmdret , ' finished ' )
return cmdret