issue #229 extra args to go

pull/230/head
ray-x 2 years ago
parent 737223c999
commit e9d1c6c021

@ -223,6 +223,7 @@ first run of `GoFmt` may fail. It is recommended to run `GoInstallBinaries` to i
| GoTest -n 1 | -count=1 flag |
| GoTest -p | test current package, see GoTestPkg |
| GoTest -t yourtags | go test ./... -tags=yourtags, see notes |
| GoTest -a your_ags | go test ./... -ags=yourags, see notes |
| GoTest package_path -t yourtags | go test packagepath -tags=yourtags |
| GoTest package_path -t yourtags other_args | go test packagepath -tags=yourtags other_args |
| GoLint | golangci-lint |
@ -238,6 +239,7 @@ Note:
1. if package_url not provided, will check current line is a valid package url or not, if it is valid, will
fetch current url
2. tags: if ``//+build tags`` exist it will be added automatically
3. args: if multiple args is provided, you need toconcatenate it with '\ ', e.g. GoTest -args yourtags\ other_args
Show test coverage:

@ -178,12 +178,14 @@ COMMANDS *go-nvim-commands*
:GoStop *:GoStop*
stop the task started with GoRun
:GoTest {-cnfpt} {-t tagname} {pakcage_name} *:GoTest*
:GoTest {-cnfpt} {-t tagname} {pakcage_name} {-a args} *:GoTest*
-c: compile test in current package
-n: test nearest
-f: test current file
-p: test current package
-t: compile tag
-a: extra args for test, check `go help testflag` concatenate
with '\ ' if there are any spaces or mulitple args
-n {count}: disable cache -count={count}
package_name: test package

@ -56,12 +56,13 @@ local long_opts = {
verbose = "v",
compile = "c",
tags = "t",
args = "a",
bench = "b",
run = "r",
floaterm = "F",
}
local short_opts = "vct:bFr:"
local short_opts = "a:vct:bFr:"
local bench_opts = { "-benchmem", "-cpuprofile", "profile.out" }
function M.make(...)
@ -171,6 +172,11 @@ function M.make(...)
log("run test", efm)
table.insert(cmd, "-run")
end
if optarg["a"] then
log("extra args", optarg["a"])
table.insert(cmd, "-args")
table.insert(cmd, optarg["a"])
end
if not bench and compile_test then
table.insert(cmd, "-c")
end

@ -1,29 +1,31 @@
-- run `go test`
local M = {}
local utils = require("go.utils")
local utils = require('go.utils')
local log = utils.log
local trace = utils.trace
-- local trace = utils.trace
local trace = log
local empty = utils.empty
local ginkgo = require("go.ginkgo")
local getopt = require("go.alt_getopt")
local install = require("go.install").install
local ginkgo = require('go.ginkgo')
local getopt = require('go.alt_getopt')
local install = require('go.install').install
local vfn = vim.fn
local long_opts = {
verbose = "v",
compile = "c",
coverage = "C",
count = "n",
tags = "t",
bench = "b",
metric = "m",
select = "s",
floaterm = "F",
verbose = 'v',
compile = 'c',
coverage = 'C',
count = 'n',
tags = 't',
bench = 'b',
metric = 'm',
select = 's',
args = 'a',
floaterm = 'F',
}
local sep = require("go.utils").sep()
local short_opts = "vcC:t:bsFmn:"
local bench_opts = { "-benchmem", "-cpuprofile", "profile.out" }
local sep = require('go.utils').sep()
local short_opts = 'a:cC:t:bsFmn:v'
local bench_opts = { '-benchmem', '-cpuprofile', 'profile.out' }
M.efm = function()
-- local indent = [[%\\%( %\\)]]
@ -33,24 +35,24 @@ M.efm = function()
efm = efm .. [[,%G" .. indent .. "%#--- FAIL: %m (%.%#)]]
efm = efm .. [[,%A" .. indent .. "%\\+%[%^:]%\\+: %f:%l: %m]]
efm = efm .. [[,%+Gpanic: test timed out after %.%\\+]]
efm = efm .. ",%+Afatal error: %.%# [recovered]"
efm = efm .. ',%+Afatal error: %.%# [recovered]'
efm = efm .. [[,%+Afatal error: %.%#]]
efm = efm .. [[,%+Apanic: %.%#]]
-- exit
efm = efm .. ",%-Cexit status %[0-9]%\\+"
efm = efm .. ",exit status %[0-9]%\\+"
efm = efm .. ',%-Cexit status %[0-9]%\\+'
efm = efm .. ',exit status %[0-9]%\\+'
-- failed lines
efm = efm .. ",%-CFAIL%\\t%.%#"
efm = efm .. ",FAIL%\\t%.%#"
efm = efm .. ',%-CFAIL%\\t%.%#'
efm = efm .. ',FAIL%\\t%.%#'
-- compiling error
efm = efm .. ",%A%f:%l:%c: %m"
efm = efm .. ",%A%f:%l: %m"
efm = efm .. ",%G%\\t%m"
efm = efm .. ",%-C%.%#"
efm = efm .. ",%-G%.%#"
efm = string.gsub(efm, " ", [[\ ]])
efm = efm .. ',%A%f:%l:%c: %m'
efm = efm .. ',%A%f:%l: %m'
efm = efm .. ',%G%\\t%m'
efm = efm .. ',%-C%.%#'
efm = efm .. ',%-G%.%#'
efm = string.gsub(efm, ' ', [[\ ]])
log(efm)
return efm
end
@ -60,13 +62,13 @@ M.get_build_tags = function(args, tbl)
-- local tags = "-tags"
args = args or {}
local tags = {}
if _GO_NVIM_CFG.build_tags ~= "" then
if _GO_NVIM_CFG.build_tags ~= '' then
tags = { _GO_NVIM_CFG.build_tags }
end
local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts)
if optarg["t"] then
table.insert(tags, optarg["t"])
if optarg['t'] then
table.insert(tags, optarg['t'])
end
local rt = utils.get_build_tags()
@ -76,43 +78,42 @@ M.get_build_tags = function(args, tbl)
if #tags > 0 then
if tbl then
return {"-tags", table.concat(tags, ",")}
return { '-tags', table.concat(tags, ',') }
end
return "-tags=" .. table.concat(tags, ","), reminder
return '-tags=' .. table.concat(tags, ','), reminder
end
end
local function get_test_path()
local path = vim.fn.expand("%:p:h")
local relative_path = vim.fn.fnamemodify(path, ":.")
local path = vim.fn.expand('%:p:h')
local relative_path = vim.fn.fnamemodify(path, ':.')
if path == relative_path then
return path
end
return "." .. sep .. relative_path
return '.' .. sep .. relative_path
end
local function richgo(cmd)
if cmd[1] == "go" and vfn.executable("richgo") == 1 then
cmd[1] = "richgo"
if cmd[1] == 'go' and vfn.executable('richgo') == 1 then
cmd[1] = 'richgo'
end
return cmd
end
local function get_test_filebufnr()
local fn = vfn.expand("%")
local fn = vfn.expand('%')
trace(fn)
local bufnr = vim.api.nvim_get_current_buf()
if not fn:find("test%.go$") then
fn = require("go.alternate").alternate()
fn = vfn.fnamemodify(fn, ":p") -- expand to full path
if not fn:find('test%.go$') then
fn = require('go.alternate').alternate()
fn = vfn.fnamemodify(fn, ':p') -- expand to full path
local uri = vim.uri_from_fname(fn)
bufnr = vim.uri_to_bufnr(uri)
log(fn, bufnr, uri)
if vfn.filereadable(vim.uri_to_fname(uri)) == 0 then
-- no test file existed
return 0, "no test file"
return 0, 'no test file'
end
if not vim.api.nvim_buf_is_loaded(bufnr) then
vfn.bufload(bufnr)
@ -126,15 +127,21 @@ local function run_test(path, args)
log(args)
local compile = false
local bench = false
local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts)
trace(optarg)
if optarg["c"] then
local extra_args = ''
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
if optarg['b'] then
bench = true
end
if optarg['a'] then
extra_args = optarg['a']
end
if next(reminder) then
path = reminder[1]
end
@ -142,7 +149,7 @@ 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
test_runner = "go"
test_runner = 'go'
end
end
@ -151,55 +158,61 @@ local function run_test(path, args)
log(tags)
local cmd = {}
local run_in_floaterm = optarg["F"] or _GO_NVIM_CFG.run_in_floaterm
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")
table.insert(cmd, test_runner or 'go')
table.insert(cmd, 'test')
end
if _GO_NVIM_CFG.verbose_tests then
table.insert(cmd, "-v")
table.insert(cmd, '-v')
end
if not empty(tags) then
cmd = vim.list_extend(cmd, { tags })
end
if optarg["C"] then
table.insert(cmd, "-coverprofile=" .. optarg["C"])
if optarg['C'] then
table.insert(cmd, '-coverprofile=' .. optarg['C'])
end
if optarg["n"] then
table.insert(cmd, "-count=" .. optarg["n"] or "1")
if optarg['n'] then
table.insert(cmd, '-count=' .. optarg['n'] or '1')
end
if not empty(reminder) then
log('****', reminder)
cmd = vim.list_extend(cmd, reminder)
end
if compile == true then
if path ~= "" then
table.insert(cmd, "-c")
if path ~= '' then
table.insert(cmd, '-c')
table.insert(cmd, path)
end
elseif bench == true then
if path ~= "" then
table.insert(cmd, "-bench=" .. path)
if path ~= '' then
table.insert(cmd, '-bench=' .. path)
else
table.insert(cmd, "-bench=.")
table.insert(cmd, '-bench=.')
end
vim.list_extend(cmd, bench_opts)
else
if path ~= "" then
if path ~= '' then
table.insert(cmd, path)
else
local argsstr = "." .. utils.sep() .. "..."
local argsstr = '.' .. utils.sep() .. '...'
table.insert(cmd, argsstr)
end
end
if #extra_args > 0 then
table.insert(cmd, '-a')
table.insert(cmd, extra_args)
end
utils.log(cmd, args)
if run_in_floaterm then
install("richgo")
local term = require("go.term").run
install('richgo')
local term = require('go.term').run
cmd = richgo(cmd)
log(cmd)
term({ cmd = cmd, autoclose = false })
@ -208,8 +221,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))
utils.log('test cmd', cmd)
return require('go.asyncmake').make(unpack(cmd))
end
M.test = function(...)
@ -217,51 +230,55 @@ M.test = function(...)
log(args)
local test_opts = {
verbose = "v",
compile = "c",
coverage = "C",
tags = "t",
bench = "b",
metrics = "m",
floaterm = "F",
nearest = "n",
file = "f",
package = "p",
verbose = 'v',
compile = 'c',
coverage = 'C',
tags = 't',
bench = 'b',
metrics = 'm',
floaterm = 'F',
nearest = 'n',
file = 'f',
args = 'a',
package = 'p',
}
local test_short_opts = "vcC:t:bsfmnpF"
local test_short_opts = 'a:vcC:t:bsfmnpF'
local optarg, _, reminder = getopt.get_opts(args, test_short_opts, test_opts)
vfn.setqflist({})
if optarg["n"] then --nearest
optarg["n"] = nil
if optarg['n'] then --nearest
optarg['n'] = nil
local opts = getopt.rebuid_args(optarg, reminder) or {}
return M.test_func(unpack(opts))
end
if optarg["f"] then -- currentfile
optarg["f"] = nil
if optarg['f'] then -- currentfile
optarg['f'] = nil
local opts = getopt.rebuid_args(optarg, reminder) or {}
return M.test_file(unpack(opts))
end
if optarg["p"] then -- current package
optarg["p"] = nil
if optarg['p'] then -- current package
optarg['p'] = nil
local opts = getopt.rebuid_args(optarg, reminder) or {}
return M.test_package(unpack(opts))
end
if optarg['a'] then -- current package
log('args', optarg['a'])
end
local workfolder = utils.work_path()
if workfolder == nil then
workfolder = "."
workfolder = '.'
end
local fpath = workfolder .. utils.sep() .. "..."
local fpath = workfolder .. utils.sep() .. '...'
if #reminder > 0 then
fpath = reminder[1]
end
utils.log("fpath :" .. fpath)
utils.log('fpath :' .. fpath)
run_test(fpath, args)
end
@ -271,9 +288,9 @@ M.test_suit = function(...)
local workfolder = utils.work_path()
utils.log(args)
local fpath = workfolder .. utils.sep() .. "..."
local fpath = workfolder .. utils.sep() .. '...'
utils.log("fpath" .. fpath)
utils.log('fpath' .. fpath)
run_test(fpath, args)
end
@ -281,22 +298,22 @@ end
M.test_package = function(...)
local args = { ... }
log(args)
local fpath = get_test_path() .. sep .. "..."
utils.log("fpath: " .. fpath)
local fpath = get_test_path() .. sep .. '...'
utils.log('fpath: ' .. fpath)
return run_test(fpath, args)
end
M.get_test_func_name = function()
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row, col = row, col + 1
local ns = require("go.ts.go").get_func_method_node_at_pos()
local ns = require('go.ts.go').get_func_method_node_at_pos()
if empty(ns) then
return nil
end
if ns == nil or ns.name == nil then
return nil
end
if not string.find(ns.name, "[T|t]est") then
if not string.find(ns.name, '[T|t]est') then
-- not in a test function
local fns = M.get_testfunc()
for _, fn in ipairs(fns) do
@ -321,49 +338,49 @@ M.test_func = function(...)
end
local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts)
local tags = M.get_build_tags(args)
utils.log("tags: ", tags)
utils.log("parnode" .. vim.inspect(ns))
utils.log('tags: ', tags)
utils.log('parnode' .. vim.inspect(ns))
local test_runner = _GO_NVIM_CFG.go
if _GO_NVIM_CFG.test_runner ~= "go" then
if _GO_NVIM_CFG.test_runner ~= 'go' then
test_runner = _GO_NVIM_CFG.test_runner
if not install(test_runner) then
test_runner = "go"
test_runner = 'go'
end
if test_runner == "ginkgo" then
if test_runner == 'ginkgo' then
ginkgo.test_func(...)
end
end
local run_flags = "-run"
local run_flags = '-run'
local cmd = {}
local run_in_floaterm = optarg["F"] or _GO_NVIM_CFG.run_in_floaterm
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")
table.insert(cmd, 'test')
end
if optarg["s"] then
if optarg['s'] then
return M.select_tests()
end
if _GO_NVIM_CFG.verbose_tests and _GO_NVIM_CFG.test_runner == "go" then
table.insert(cmd, "-v")
if _GO_NVIM_CFG.verbose_tests and _GO_NVIM_CFG.test_runner == 'go' then
table.insert(cmd, '-v')
end
if tags and tags ~= "" then
if tags and tags ~= '' then
table.insert(cmd, tags)
end
if ns == nil or ns.name == nil then
return
end
if optarg["n"] then
table.insert(cmd, "-count=" .. optarg["n"] or "1")
if optarg['n'] then
table.insert(cmd, '-count=' .. optarg['n'] or '1')
end
if ns.name:find("Bench") then
local bench = "-bench=" .. ns.name
if ns.name:find('Bench') then
local bench = '-bench=' .. ns.name
table.insert(cmd, bench)
vim.list_extend(cmd, bench_opts)
else
@ -374,21 +391,21 @@ M.test_func = function(...)
local fpath = get_test_path()
table.insert(cmd, fpath)
if test_runner == "dlv" then
if tags and #tags>0 then
cmd = { "dlv", "test", fpath, "--build-flags", tags, "--", "-test.run", "^" .. ns.name }
if test_runner == 'dlv' then
if tags and #tags > 0 then
cmd = { 'dlv', 'test', fpath, '--build-flags', tags, '--', '-test.run', '^' .. ns.name }
else
cmd = { "dlv", "test", fpath, "--", "-test.run", "^" .. ns.name }
cmd = { 'dlv', 'test', fpath, '--', '-test.run', '^' .. ns.name }
end
local term = require("go.term").run
local term = require('go.term').run
term({ cmd = cmd, autoclose = false })
return
end
if run_in_floaterm then
utils.log(cmd)
install("richgo")
local term = require("go.term").run
install('richgo')
local term = require('go.term').run
cmd = richgo(cmd)
term({ cmd = cmd, autoclose = false })
return
@ -397,9 +414,9 @@ M.test_func = function(...)
vim.cmd([[setl makeprg=]] .. test_runner .. [[\ test]])
-- set_efm()
utils.log("test cmd", cmd)
utils.log('test cmd', cmd)
return require("go.asyncmake").make(unpack(cmd))
return require('go.asyncmake').make(unpack(cmd))
end
M.test_file = function(...)
@ -410,28 +427,28 @@ M.test_file = function(...)
-- local testcases = [[sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\\|/g']]
-- local fpath = vfn.expand("%:p")
local fpath = "." .. sep .. vfn.fnamemodify(vfn.expand("%:p"), ":.")
local fpath = '.' .. sep .. vfn.fnamemodify(vfn.expand('%:p'), ':.')
-- utils.log(args)
local cmd = [[cat ]] .. fpath .. [[| sed -n 's/func.*\(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
if vfn.executable('sed') == 0 then
M.test_package(...)
return
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
local run_in_floaterm = optarg['F'] or _GO_NVIM_CFG.run_in_floaterm
local tests = vfn.systemlist(cmd)
if vim.v.shell_error ~= 0 then
utils.warn("iferr failed" .. vim.inspect(tests))
utils.warn('iferr failed' .. vim.inspect(tests))
return
end
utils.log(cmd, tests)
tests = tests[1]
if vfn.empty(tests) == 1 then
vim.notify("no test found fallback to package test", vim.lsp.log_levels.DEBUG)
vim.notify('no test found fallback to package test', vim.lsp.log_levels.DEBUG)
M.test_package(...)
return
end
@ -439,12 +456,12 @@ M.test_file = function(...)
local tags = M.get_build_tags(args)
local test_runner = _GO_NVIM_CFG.go
if _GO_NVIM_CFG.test_runner ~= "go" then
if _GO_NVIM_CFG.test_runner ~= 'go' then
test_runner = _GO_NVIM_CFG.test_runner
if not install(test_runner) then
test_runner = "go"
test_runner = 'go'
end
if test_runner == "ginkgo" then
if test_runner == 'ginkgo' then
ginkgo.test_func(...)
end
end
@ -455,11 +472,11 @@ M.test_file = function(...)
local cmd_args = {}
if run_in_floaterm then
table.insert(cmd_args, test_runner)
table.insert(cmd_args, "test")
table.insert(cmd_args, 'test')
end
if _GO_NVIM_CFG.verbose_tests then
table.insert(cmd_args, "-v")
table.insert(cmd_args, '-v')
end
if tags ~= nil then
@ -469,33 +486,33 @@ M.test_file = function(...)
if next(reminder) then
vim.list_extend(cmd_args, reminder)
end
table.insert(cmd_args, "-run")
table.insert(cmd_args, '-run')
if optarg["n"] then
table.insert(cmd_args, "-count=" .. optarg["n"] or "1")
if optarg['n'] then
table.insert(cmd_args, '-count=' .. optarg['n'] or '1')
end
local sh = vim.o.shell
if sh:find("fish") then
if sh:find('fish') then
tests = "'" .. tests .. "'"
end
table.insert(cmd_args, tests) -- shell script | is a pipe
table.insert(cmd_args, relpath)
if run_in_floaterm then
install("richgo")
local term = require("go.term").run
install('richgo')
local term = require('go.term').run
cmd_args = richgo(cmd_args)
cmd_args = table.concat(cmd_args, " ")
cmd_args = table.concat(cmd_args, ' ')
log(cmd_args)
term({ cmd = cmd_args, 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
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 })
return cmd_args
end
@ -503,9 +520,9 @@ M.test_file = function(...)
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').make(unpack(cmd_args))
utils.log("test cmd: ", cmdret, " finished")
utils.log('test cmd: ', cmdret, ' finished')
return cmdret
end
@ -514,19 +531,19 @@ end
M.run_file = function()
local bufnr = vim.api.nvim_get_current_buf()
local tree = vim.treesitter.get_parser(bufnr):parse()[1]
local query = vim.treesitter.parse_query("go", require("go.ts.textobjects").query_test_func)
local query = vim.treesitter.parse_query('go', require('go.ts.textobjects').query_test_func)
local test_names = {}
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
if name == "test_name" then
if name == 'test_name' then
table.insert(test_names, vim.treesitter.query.get_node_text(node, bufnr))
end
end
vim.schedule(function()
vim.lsp.buf.execute_command({
command = "gopls.run_tests",
command = 'gopls.run_tests',
arguments = { { URI = vim.uri_from_bufnr(0), Tests = test_names } },
})
end)
@ -537,12 +554,12 @@ M.get_testfunc = function()
local parser = vim.treesitter.get_parser(bufnr)
local tree = parser:parse()
tree = tree[1]
local query = vim.treesitter.parse_query("go", require("go.ts.go").query_test_func)
local query = vim.treesitter.parse_query('go', require('go.ts.go').query_test_func)
local test_names = {}
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
if name == "test_name" then
if name == 'test_name' then
table.insert(test_names, vim.treesitter.query.get_node_text(node, bufnr))
end
end
@ -552,11 +569,11 @@ end
-- GUI to select test?
M.select_tests = function()
local guihua = utils.load_plugin("guihua.lua", "guihua.gui")
local guihua = utils.load_plugin('guihua.lua', 'guihua.gui')
local original_select = vim.ui.select
if guihua then
vim.ui.select = require("guihua.gui").select
vim.ui.select = require('guihua.gui').select
end
vim.defer_fn(function()
@ -571,15 +588,15 @@ M.select_tests = function()
log(uri, item, idx)
vim.schedule(function()
vim.lsp.buf.execute_command({
command = "gopls.run_tests",
command = 'gopls.run_tests',
arguments = { { URI = uri, Tests = { item } } },
})
end)
end
local test_names = M.get_testfunc()
vim.ui.select(test_names, {
prompt = "select test to run:",
kind = "codelensaction",
prompt = 'select test to run:',
kind = 'codelensaction',
}, onselect)
return test_names
end

Loading…
Cancel
Save