This commit is contained in:
ray-x 2023-11-28 22:16:09 +11:00
parent 9cf2a3f846
commit e0528b5868
2 changed files with 23 additions and 15 deletions

View File

@ -140,7 +140,7 @@ local function get_test_filebufnr()
end end
local function cmd_builder(path, args) local function cmd_builder(path, args)
log(args) log('builder args', args)
local compile = false local compile = false
local bench = false local bench = false
local extra_args = '' local extra_args = ''
@ -153,7 +153,7 @@ local function cmd_builder(path, args)
end end
end end
local optarg, oid, reminder = getopt.get_opts(args, short_opts, long_opts) local optarg, oid, reminder = getopt.get_opts(args, short_opts, long_opts)
trace(optarg, oid, reminder) trace('cmd_builder', optarg, oid, reminder)
if optarg['c'] then if optarg['c'] then
path = utils.rel_path(true) -- vfn.expand("%:p:h") can not resolve releative path path = utils.rel_path(true) -- vfn.expand("%:p:h") can not resolve releative path
compile = true compile = true
@ -266,7 +266,7 @@ end
-- {-c: compile, -v: verbose, -t: tags, -b: bench, -s: select} -- {-c: compile, -v: verbose, -t: tags, -b: bench, -s: select}
local function run_test(path, args) local function run_test(path, args)
log(args) log('run test', args)
local cmd, optarg = cmd_builder(path, args) local cmd, optarg = cmd_builder(path, args)
log(cmd, args) log(cmd, args)
local run_in_floaterm = _GO_NVIM_CFG.run_in_floaterm or optarg['F'] local run_in_floaterm = _GO_NVIM_CFG.run_in_floaterm or optarg['F']
@ -381,7 +381,7 @@ end
M.test_package = function(...) M.test_package = function(...)
local args = { ... } local args = { ... }
log(args) log('test pkg', args)
local fpath = M.get_test_path() .. sep .. '...' local fpath = M.get_test_path() .. sep .. '...'
utils.log('fpath: ' .. fpath) utils.log('fpath: ' .. fpath)
return run_test(fpath, args) return run_test(fpath, args)
@ -550,31 +550,32 @@ M.get_test_cases = function()
end end
-- utils.log(args) -- utils.log(args)
local tests = M.get_testfunc() local tests = M.get_testfunc()
if not tests then if vim.fn.empty(tests) == 1 then
local cmd = [[cat ]]
.. fpath
.. [[| 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 -- TODO maybe with treesitter or lsp list all functions in current file and regex with Test
if vfn.executable('sed') == 0 then if vfn.executable('sed') == 0 then
vim.notify('sed not found', vim.log.levels.WARN) vim.notify('sed not found', vim.log.levels.WARN)
return return
end end
local cmd = [[cat ]]
.. fpath
.. [[| sed -n 's/func\s\+\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\|/g']]
local tests_results = vfn.systemlist(cmd) local tests_results = vfn.systemlist(cmd)
if vim.v.shell_error ~= 0 then if vim.v.shell_error ~= 0 then
utils.warn('go test failed' .. vim.inspect(tests_results)) utils.warn('go test failed' .. cmd .. vim.inspect(tests_results))
return return
end end
log(cmd, vim.v.shell_error, tests_results)
return tests_results[1] return tests_results[1]
end end
local testsstr = vim.fn.join(tests, '|') local testsstr = vim.fn.join(tests, '|')
utils.log(tests, testsstr) log('test test cases', tests, testsstr)
return testsstr, tests return testsstr, tests
end end
M.test_file = function(...) M.test_file = function(...)
local args = { ... } local args = { ... }
log(args) log('test file', args)
-- require sed -- require sed
local tests = M.get_test_cases() local tests = M.get_test_cases()
@ -700,7 +701,7 @@ M.get_testfunc = function()
-- Note: the buffer may not be loaded yet -- Note: the buffer may not be loaded yet
local ok, parser = pcall(vim.treesitter.get_parser, bufnr) local ok, parser = pcall(vim.treesitter.get_parser, bufnr)
if not ok or not parser then if not ok or not parser then
return return log('no parser found')
end end
local tree = parser:parse() local tree = parser:parse()
tree = tree[1] tree = tree[1]
@ -709,11 +710,12 @@ M.get_testfunc = function()
local test_names = {} local test_names = {}
for id, node in query:iter_captures(tree:root(), bufnr, 0, -1) do for id, node in query:iter_captures(tree:root(), bufnr, 0, -1) do
local name = query.captures[id] -- name of the capture in the query local name = query.captures[id] -- name of the capture in the query
log(node)
if name == 'test_name' then if name == 'test_name' then
table.insert(test_names, utils.get_node_text(node, bufnr)) table.insert(test_names, utils.get_node_text(node, bufnr))
end end
end end
log('TS test names', test_names)
return test_names return test_names
end end

View File

@ -536,10 +536,16 @@ function util.rel_path(folder)
mod = '%:p:h' mod = '%:p:h'
end end
local fpath = fn.expand(mod) local fpath = fn.expand(mod)
-- workfolders does not work if user does not setup neovim to follow workspace
local workfolders = vim.lsp.buf.list_workspace_folders() local workfolders = vim.lsp.buf.list_workspace_folders()
local pwd = fn.getcwd()
if fn.empty(workfolders) == 0 then if fn.empty(workfolders) == 0 then
if workfolders[1] ~= pwd then
vim.notify('current dir is not workspace dir', vim.log.levels.DEBUG)
-- change current folder to workspace folder
vim.cmd('cd ' .. workfolders[1])
end
fpath = '.' .. fpath:sub(#workfolders[1] + 1) fpath = '.' .. fpath:sub(#workfolders[1] + 1)
else else
fpath = fn.fnamemodify(fn.expand(mod), ':p:.') fpath = fn.fnamemodify(fn.expand(mod), ':p:.')