update GoTest adding more flags

pull/135/head
ray-x 2 years ago
parent 002d7dfd73
commit ad84a0806c

@ -209,9 +209,12 @@ first run of `GoFmt` may fail. It is recommended to run `GoInstallBinaries` to i
| GoStop {job_id} | `stop the job started with GoRun` |
| GoTest | go test ./... |
| GoTest -c | go test -c current_file_path |
| GoTest -tags=yourtags | go test ./... -tags=yourtags |
| GoTest package_path -tags=yourtags | go test packagepath -tags=yourtags |
| GoTest package_path -tags=yourtags other_args | go test packagepath -tags=yourtags other_args |
| GoTest -n | test nearest, see GoTestFunc |
| GoTest -f | test current file, see GoTestFile |
| GoTest -p | test current package, see GoTestPkg |
| GoTest -t yourtags | go test ./... -tags=yourtags |
| 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 |
| GoGet {package_url} | go get package_url and restart gopls. Note1 |
| GoVet | go vet |

@ -175,16 +175,19 @@ COMMANDS *go-nvim-commands*
:GoStop *:GoStop*
stop the task started with GoRun
:GoTest {-c} {-tags=tagname} {pakcage_name} *:GoTest*
:GoTest {-cnfpt} {-t tagname} {pakcage_name} *:GoTest*
-c: compile test in current package
-tags: compile tag
-n: test nearest
-f: test current file
-p: test current package
-t: compile tag
package_name: test package
:GoTestFile {-tags=tagname} *:GoTestFile*
:GoTestFile {-t tagname} *:GoTestFile*
Test current file
:GoTestFunc {args} {-tags=tagname} *:GoTestFunc*
:GoTestFunc {args} {-t tagname} *:GoTestFunc*
Test current function
{args} -s: select the function you want to run

@ -120,8 +120,6 @@ function go.setup(cfg)
vim.cmd([[command! -nargs=* GoStop lua require("go.asyncmake").stopjob(<f-args>)]])
-- if you want to output to quickfix
-- vim.cmd(
-- [[command! -nargs=* GoTest :setl makeprg=go\ test\ -v\ ./...| lua require'go.asyncmake'.make(<f-args>)]])
local sep = require("go.utils").sep()
-- example to running test in split buffer
@ -144,10 +142,10 @@ function go.setup(cfg)
vim.cmd([[command! -nargs=* GoProject lua require('go.project').setup(<f-args>)]])
vim.cmd([[command! -nargs=* GoCheat lua require('go.chtsh').run(<f-args>)]])
-- e.g. GoTestFunc unit
vim.cmd([[command! -nargs=* GoTestFunc lua require('go.gotest').test_func(<f-args>)]])
vim.cmd([[command! -nargs=* GoTestFunc lua require('go.gotest').test_func(<f-args>)]])
-- e.g. GoTestFile unit
vim.cmd([[command! -nargs=* GoTestFile lua require('go.gotest').test_file(<f-args>)]])
vim.cmd([[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestFile lua require('go.gotest').test_file(<f-args>)]])
vim.cmd(
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestPkg lua require('go.gotest').test_package(<f-args>)]]
)

@ -133,13 +133,33 @@ function alt_getopt.get_opts(arg, sh_opts, long_opts)
if optarg[i] then
ret[v] = optarg[i]
else
ret[v] = 1
ret[v] = true
end
end
return ret, optind, unparsed
end
function alt_getopt.rebuid_args(opts, reminder)
local ret = {}
for i, v in pairs(opts) do
if opts[i] == true then
table.insert(ret, "-" .. i)
else
table.insert(ret, "-" .. i)
table.insert(ret, v)
end
end
if not vim.tbl_isempty(reminder) then
vim.list_extend(ret, reminder)
end
if next(ret) == nil then
return nil
end
return ret
end
function test_arg(arg)
local long_opts = {
verbose = "v",

@ -125,6 +125,15 @@ function M.make(...)
end
local cmd = vim.fn.split(makeprg, " ")
if optarg["t"] then
local tag = optarg['t']
local f = tag:find("=")
if not f then
table.insert(cmd, "-tags=" .. tag)
else
table.insert(cmd, "-tags=" .. tag:sub(f+1))
end
end
if makeprg:find("test") then
log("go test")
@ -138,6 +147,9 @@ function M.make(...)
log("run test")
table.insert(cmd, "-run")
end
if compile_test then
table.insert(cmd, "-c")
end
end
if args and #args > 0 then
@ -256,8 +268,10 @@ function M.make(...)
end
end
log("cmd ", cmd)
_GO_NVIM_CFG.job_id = vim.fn.jobstart(cmd, {
local cmdstr = vim.fn.join(cmd, " ") -- cmd list run without shell, cmd string run with shell
-- releative dir does not work without shell
log("cmd ", cmdstr)
_GO_NVIM_CFG.job_id = vim.fn.jobstart(cmdstr, {
on_stderr = on_event,
on_stdout = on_event,
on_exit = on_event,

@ -238,8 +238,8 @@ M.run = function(...)
local args2 = {}
if not empty(args) then
tags, args2 = get_build_tags(args)
if tags ~= {} then
vim.list_extend(cmd, tags)
if tags ~= nil then
table.insert(cmd, tags)
end
end

@ -2,11 +2,24 @@
local M = {}
local utils = require("go.utils")
local log = utils.log
local long_opts = {
verbose = "v",
compile = "c",
tags = "t",
bench = "b",
select = "s",
floaterm = "F",
}
local getopt = require("go.alt_getopt")
local short_opts = "vct:bsF"
local function get_build_tags(args)
local tags = {}
if args ~= nil then
table.insert(tags, args)
local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts)
if optarg['t'] then
table.insert(tags, optarg['t'])
end
if _GO_NVIM_CFG.build_tags ~= "" then
@ -17,7 +30,7 @@ local function get_build_tags(args)
return ""
end
return [[-tags\ ]] .. table.concat(tags, ",")
return [[-tags=]] .. table.concat(tags, ",")
end
local function find_describe(lines)
@ -50,6 +63,7 @@ end
M.test_func = function(...)
local args = { ... }
log(args)
local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts)
local fpath = vim.fn.expand("%:p:h")
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
@ -77,10 +91,10 @@ M.test_func = function(...)
term({ cmd = cmd, autoclose = false })
return
end
local cmd = [[setl makeprg=]] .. test_runner
cmd = [[setl makeprg=]] .. test_runner
vim.cmd(cmd)
local args = { [[ --focus=']] .. describe .. [[']], get_build_tags(args), fpath }
args = { [[ --focus=']] .. describe .. [[']], get_build_tags(args), fpath }
require("go.asyncmake").make(unpack(args))
utils.log("test cmd", cmd)
return true

@ -47,31 +47,29 @@ M.efm = function()
return efm
end
local function get_build_tags(args)
-- return "-tags=tag1,tag2"
M.get_build_tags = function(args)
-- local tags = "-tags"
local tags
local space = [[\ ]]
if _GO_NVIM_CFG.run_in_floaterm then
space = " "
end
local tags = {}
if _GO_NVIM_CFG.build_tags ~= "" then
tags = "-tags=" .. _GO_NVIM_CFG.build_tags
tags = { _GO_NVIM_CFG.build_tags }
end
local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts)
if optarg["t"] then
if tags then
tags = tags .. space .. optarg["t"]
else
tags = "-tags=" .. optarg["t"]
end
table.insert(tags, optarg["t"])
end
if tags then
return { tags }, reminder
if #tags > 0 then
return "-tags=" .. table.concat(tags, ","), reminder
end
end
M.get_build_tags = get_build_tags
local function richgo(cmd)
if cmd[1] == "go" and vim.fn.executable("richgo") then
cmd[1] = "richgo"
end
return cmd
end
local function get_test_filebufnr()
local fn = vim.fn.expand("%")
@ -91,13 +89,14 @@ local function get_test_filebufnr()
return bufnr
end
-- {-c: compile, -v: verbose, -t: tags, -b: bench, -s: select}
local function run_test(path, args)
log(args)
local compile = false
local bench = false
local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts)
if optarg["c"] then
path = utils.rel_path() -- vim.fn.expand("%:p:h") can not resolve releative path
path = utils.rel_path(true) -- vim.fn.expand("%:p:h") can not resolve releative path
compile = true
end
if optarg["b"] then
@ -112,7 +111,7 @@ local function run_test(path, args)
require("go.install").install(test_runner)
end
local tags = get_build_tags(args)
local tags = M.get_build_tags(args)
log(tags)
local cmd = {}
@ -136,6 +135,7 @@ local function run_test(path, args)
if compile == true then
if path ~= "" then
table.insert(cmd, "-c")
table.insert(cmd, path)
end
elseif bench == true then
@ -150,12 +150,13 @@ local function run_test(path, args)
table.insert(cmd, path)
else
local argsstr = "." .. utils.sep() .. "..."
cmd = table.insert(cmd, argsstr)
table.insert(cmd, argsstr)
end
end
utils.log(cmd, args)
if run_in_floaterm then
local term = require("go.term").run
cmd = richgo(cmd)
term({ cmd = cmd, autoclose = false })
return cmd
end
@ -170,14 +171,50 @@ M.test = function(...)
local args = { ... }
log(args)
local test_opts = {
verbose = "v",
compile = "c",
tags = "t",
bench = "b",
floaterm = "F",
nearest = "n",
file = "f",
package = "p",
}
local test_short_opts = "vct:bsfnpF"
local optarg, optind, reminder = getopt.get_opts(args, test_short_opts, test_opts)
vim.fn.setqflist({})
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
local opts = getopt.rebuid_args(optarg, reminder) or {}
return M.test_file(unpack(opts))
end
if optarg["p"] then -- current package
optarg["p"] = nil
local opts = getopt.rebuid_args(optarg, reminder) or {}
return M.test_package(unpack(opts))
end
local workfolder = utils.work_path()
if workfolder == nil then
workfolder = "."
end
local fpath = workfolder .. utils.sep() .. "..."
utils.log("fpath :" .. fpath)
if #reminder > 0 then
fpath = reminder[1]
end
utils.log("fpath :" .. fpath)
run_test(fpath, args)
end
@ -197,11 +234,8 @@ end
M.test_package = function(...)
local args = { ... }
log(args)
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":~:.") .. sep .. "..."
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":.:S") .. sep .. "..."
utils.log("fpath: " .. fpath)
-- args[#args + 1] = fpath
return run_test(fpath, args)
end
@ -226,19 +260,17 @@ M.get_test_func_name = function()
return ns
end
--options {s:select, F: floaterm}
M.test_func = function(...)
local args = { ... }
log(args)
-- fpath = fpath:gsub(" ", [[\ ]])
-- fpath = fpath:gsub("-", [[\-]])
-- log(fpath)
ns = M.get_test_func_name()
if empty(ns) then
return M.select_tests()
end
local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts)
local tags = get_build_tags(args)
local tags = M.get_build_tags(args)
utils.log("parnode" .. vim.inspect(ns))
local test_runner = _GO_NVIM_CFG.go
@ -251,7 +283,7 @@ M.test_func = function(...)
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
@ -267,8 +299,8 @@ M.test_func = function(...)
table.insert(cmd, "-v")
end
if not empty(tags) then
cmd = vim.list_extend(cmd, tags)
if tags and tags ~= "" then
table.insert(cmd, tags)
end
if ns.name:find("Bench") then
@ -276,11 +308,11 @@ M.test_func = function(...)
table.insert(cmd, bench)
vim.list_extend(cmd, bench_opts)
else
vim.list_extend(cmd, run_flags)
table.insert(cmd, run_flags)
table.insert(cmd, [[^]] .. ns.name)
end
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":~:.")
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":.:S")
table.insert(cmd, fpath)
if test_runner == "dlv" then
@ -293,6 +325,7 @@ M.test_func = function(...)
if run_in_floaterm then
utils.log(cmd)
local term = require("go.term").run
cmd = richgo(cmd)
term({ cmd = cmd, autoclose = false })
return
end
@ -313,7 +346,7 @@ M.test_file = function(...)
-- local testcases = [[sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\\|/g']]
-- local fpath = vim.fn.expand("%:p")
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:p"), ":~:.")
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:p"), ":.:S")
-- 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
@ -334,7 +367,7 @@ M.test_file = function(...)
return
end
local tags, args2 = get_build_tags(args)
local tags = M.get_build_tags(args)
local test_runner = _GO_NVIM_CFG.go
if _GO_NVIM_CFG.test_runner ~= "go" then
@ -358,19 +391,21 @@ M.test_file = function(...)
table.insert(cmd_args, "-v")
end
if tags ~= nil and #tags > 1 then
cmd_args = vim.list_extend(cmd_args, tags)
if tags ~= nil then
table.insert(cmd_args, tags)
end
if args2 then
cmd_args = vim.list_extend(cmd_args, args2)
if next(reminder) then
vim.list_extend(cmd_args, reminder)
end
table.insert(cmd_args, "-run")
table.insert(cmd_args, tests)
table.insert(cmd_args, "'" .. tests .. "'") -- shell script | is a pipe
table.insert(cmd_args, relpath)
if run_in_floaterm then
local term = require("go.term").run
cmd_args = richgo(cmd_args)
term({ cmd = cmd_args, autoclose = false })
return
end
@ -387,7 +422,7 @@ M.test_file = function(...)
local cmdret = require("go.asyncmake").make(unpack(cmd_args))
utils.log("test cmd: ", cmd, " finished")
utils.log("test cmd: ", cmdret, " finished")
return cmdret
end

@ -160,6 +160,9 @@ M.get_all_nodes = function(query, lang, defaults, bufnr, pos_row, pos_col)
op = string.sub(path, idx + 1, #path)
local a1, b1, c1, d1 = ts_utils.get_node_range(node)
local dbg_txt = get_node_text(node, bufnr) or ""
if #dbg_txt > 100 then
dbg_txt = string.sub(dbg_txt, 1, 100) .. "..."
end
type = string.sub(path, 1, idx - 1)
ulog(

@ -434,7 +434,7 @@ function util.rel_path(folder)
if workfolders ~= nil and next(workfolders) then
fpath = "." .. fpath:sub(#workfolders[1] + 1)
end
return "." .. util.sep() .. fn.fnamemodify(vim.fn.expand(mod), ":~:.")
return "." .. util.sep() .. fn.fnamemodify(vim.fn.expand(mod), ":.:S")
end
function util.rtrim(s)

@ -21,7 +21,7 @@ describe("should run func test", function()
vim.fn.setpos(".", { 0, 5, 11, 0 })
local cmd = require("go.gotest").test_func()
eq({ "go", "test", "-v", "-run", "^Test_branch", "./lua/tests/fixtures/coverage" }, cmd)
eq({ "go", "test", "-v", "-run", "^Test_branch", "./'lua/tests/fixtures/coverage'" }, cmd)
end)
it("should test function inside a source code", function()
--
@ -38,14 +38,14 @@ describe("should run func test", function()
vim.fn.setpos(".", { 0, 6, 11, 0 })
local cmd = require("go.gotest").test_func()
eq({ "go", "test", "-v", "-run", "^Test_branch", "./lua/tests/fixtures/coverage" }, cmd)
eq({ "go", "test", "-v", "-run", "^Test_branch", "./'lua/tests/fixtures/coverage'" }, cmd)
end)
end)
describe("should run test file", function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
status = require("plenary.reload").reload_module("go.nvim")
local status = require("plenary.reload").reload_module("go.nvim")
it("should test function", function()
--
-- go.nvim may not auto loaded
@ -61,14 +61,37 @@ describe("should run test file", function()
vim.fn.setpos(".", { 0, 5, 11, 0 })
local cmd = require("go.gotest").test_file()
eq({ "go", "test", "-v", "-run", "Test_branch|TestBranch", "./lua/tests/fixtures/coverage" }, cmd)
eq({ "go", "test", "-v", "-run", "'Test_branch|TestBranch'", "./'lua/tests/fixtures/coverage'" }, cmd)
end)
end)
describe("should run test file with flags", function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
local status = require("plenary.reload").reload_module("go.nvim")
it("should test function", function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])
local path = cur_dir .. "/lua/tests/fixtures/coverage/branch_test.go" -- %:p:h ? %:p
require("go").setup({
trace = true,
lsp_cfg = true,
log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log",
})
vim.cmd("silent exe 'e " .. path .. "'")
vim.fn.setpos(".", { 0, 5, 11, 0 })
local cmd = require("go.gotest").test_file("-t", "tag1")
eq({ "go", "test", "-tags=tag1", "-v", "-run", "'Test_branch|TestBranch'", "./'lua/tests/fixtures/coverage'" }, cmd)
end)
end)
describe("should run test package", function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
status = require("plenary.reload").reload_module("go.nvim")
local status = require("plenary.reload").reload_module("go.nvim")
it("should test function", function()
--
-- go.nvim may not auto loaded
@ -84,7 +107,29 @@ describe("should run test package", function()
vim.fn.setpos(".", { 0, 1, 1, 0 })
local cmd = require("go.gotest").test_package()
eq({ "go", "test", "-v", "./lua/tests/fixtures/coverage/..." }, cmd)
eq({ "go", "test", "-v", "./'lua/tests/fixtures/coverage'/..." }, cmd)
end)
end)
describe("should run test ", function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
local status = require("plenary.reload").reload_module("go.nvim")
it("should test function", function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])
local path = cur_dir .. "/lua/tests/fixtures/coverage/branch_test.go" -- %:p:h ? %:p
require("go").setup({
trace = true,
lsp_cfg = true,
})
vim.cmd("silent exe 'e " .. path .. "'")
vim.fn.setpos(".", { 0, 6, 1, 0 })
local cmd = require("go.gotest").test("-n", "-t", "tags1")
eq({ "go", "test", "-tags=tags1", "-v", "-run", "^Test_branch", "./'lua/tests/fixtures/coverage'" }, cmd)
end)
end)
@ -92,7 +137,7 @@ end)
describe("should allow select test func", function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
status = require("plenary.reload").reload_module("go.nvim")
local status = require("plenary.reload").reload_module("go.nvim")
it("should test function", function()
--
-- go.nvim may not auto loaded

Loading…
Cancel
Save