infer the test func name with treesitter

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

@ -44,6 +44,7 @@ _GO_NVIM_CFG = {
lsp_diag_virtual_text = { space = 0, prefix = "" },
lsp_diag_signs = true,
lsp_diag_update_in_insert = false,
lsp_fmt_async = true, -- async lsp.buf.format
go_boilplater_url = "https://github.com/thockin/go-build-template.git",
gopls_cmd = nil, --- you can provide gopls path and cmd if it not in PATH, e.g. cmd = { "/home/ray/.local/nvim/data/lspinstall/go/gopls" }
gopls_remote_auto = true,
@ -143,7 +144,7 @@ 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_fun(<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>)]])

@ -1,5 +1,5 @@
local M = {}
function M.switch(bang, cmd)
function M.alternate()
local file = vim.fn.expand('%')
local alt_file = ""
if #file <= 1 then
@ -15,6 +15,11 @@ function M.switch(bang, cmd)
else
vim.notify('not a go file', vim.lsp.log_levels.ERROR)
end
return alt_file
end
function M.switch(bang, cmd)
local alt_file = M.alternate()
if not vim.fn.filereadable(alt_file) and not vim.fn.bufexists(alt_file) and not bang then
vim.notify("couldn't find " .. alt_file, vim.lsp.log_levels.ERROR)
return

@ -214,6 +214,11 @@ function M.make(...)
end
if event == "exit" then
if type(cmd) == "table" then
cmd = table.concat(cmd, " ")
end
local info = cmd .. " finished "
local level = vim.lsp.log_levels.INFO
if #errorlines > 0 then
if #lines > 0 then
vim.list_extend(errorlines, lines)
@ -233,22 +238,21 @@ function M.make(...)
})
end
if type(cmd) == "table" then
cmd = table.concat(cmd, " ")
end
if tonumber(data) ~= 0 then
failed = true
vim.notify(cmd .. " exited with code: " .. tostring(data), vim.lsp.log_levels.WARN)
info = info .. " exited with code: " .. tostring(data)
level = vim.lsp.log_levels.ERROR
end
vim.notify(cmd .. " finished", vim.lsp.log_levels.INFO)
_GO_NVIM_CFG.job_id = nil
if failed then
vim.notify("go test failed", vim.lsp.log_levels.WARN)
cmd = cmd .. " go test failed"
level = vim.lsp.log_levels.WARN
vim.cmd("botright copen")
end
itemn = 1
failed = false
vim.notify(info, level)
end
end

@ -191,10 +191,6 @@ local stdout, stderr, handle
M.run = function(...)
local args = { ... }
local mode = select(1, ...)
if mode == "test" or mode == "nearest" then
utils.warn("option test is deprecated, options are " .. opts .. " ")
return utils.info(help())
end
local optarg, optind = getopt.get_opts(args, opts, long_opts)
log(optarg, optind)
@ -257,10 +253,16 @@ M.run = function(...)
return
end
local run_cur = optarg["r"]
local testfunc
if not run_cur then
keybind()
else
M.stop()
testfunc = require('go.gotest').get_test_func_name()
if not string.find(testfunc.name, "[T|t]est") then
log("no test func found", testfunc.name)
testfunc = nil -- no test func avalible
end
end
if _GO_NVIM_CFG.dap_debug_gui and not run_cur then
@ -287,13 +289,13 @@ M.run = function(...)
local addr = string.format("%s:%d", host, port)
local function onread(err, data)
if err then
log(err, data)
-- print('ERROR: ', err)
vim.notify("dlv exited with code " + tostring(err), vim.lsp.log_levels.WARN)
end
if not data or data == "" then
return
end
log(data)
if data:find("couldn't start") then
utils.error(data)
end
@ -334,7 +336,7 @@ M.run = function(...)
end
vim.defer_fn(function()
callback({ type = "server", host = host, port = port })
end, 500)
end, 1000)
end
local dap_cfg = {
@ -349,10 +351,7 @@ M.run = function(...)
row, col = row, col + 1
local empty = utils.empty
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
if empty(ns) then
log("ts not not found, debug while file")
end
local launch = require("go.launch")
local cfg_exist, cfg_file = launch.vs_launch()
@ -371,18 +370,23 @@ M.run = function(...)
-- dap_cfg.program = "${workspaceFolder}"
-- dap_cfg.program = "${file}"
dap_cfg.program = sep .. "${relativeFileDirname}"
if testfunc then
dap_cfg.args = { "-test.run", "^" .. testfunc.name }
end
dap.configurations.go = { dap_cfg }
log(dap_cfg)
dap.continue()
elseif optarg["n"] then
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
if empty(ns) then
log("ts not not found, debug while file")
end
dap_cfg.name = dap_cfg.name .. " test_nearest"
dap_cfg.mode = "test"
dap_cfg.request = "launch"
dap_cfg.program = sep .. "${relativeFileDirname}"
dap_cfg.args = { "-test.run", "^" .. ns.name }
log(dap_cfg)
if not empty(ns)then
dap_cfg.args = { "-test.run", "^" .. ns.name }
end
dap.configurations.go = { dap_cfg }
dap.continue()
elseif optarg["a"] then
@ -390,24 +394,28 @@ M.run = function(...)
dap_cfg.mode = "local"
dap_cfg.request = "attach"
dap_cfg.processId = require("dap.utils").pick_process
log(dap_cfg)
dap.configurations.go = { dap_cfg }
dap.continue()
elseif optarg["p"] then
dap_cfg.name = dap_cfg.name .. " package"
dap_cfg.mode = "test"
dap_cfg.request = "launch"
dap_cfg.program = sep .. "${fileDirname}"
log(dap_cfg)
dap.configurations.go = { dap_cfg }
dap.continue()
elseif run_cur then
dap_cfg.name = dap_cfg.name .. " run current"
-- dap_cfg.mode = "test"
dap_cfg.request = "launch"
dap_cfg.mode = "debug"
dap_cfg.request = "launch"
if testfunc then
dap_cfg.args = { "-test.run", "^" .. testfunc.name }
dap_cfg.mode = "test"
end
dap_cfg.program = sep .. "${relativeFileDirname}"
log(dap_cfg)
dap.configurations.go = { dap_cfg }
dap.continue()
-- dap.run_to_cursor()
elseif cfg_exist then
log("using cfg")
launch.load()
@ -466,8 +474,8 @@ M.stop = function(unm)
local has_dap, dap = pcall(require, "dap")
if has_dap then
require("dap").disconnect()
require("dap").repl.close()
dap.disconnect()
dap.repl.close()
end
local has_dapui, dapui = pcall(require, "dapui")
if has_dapui then
@ -520,7 +528,7 @@ function M.ultest_post()
end,
}
ul = utils.load_plugin("vim-ultest", "ultest")
local ul = utils.load_plugin("vim-ultest", "ultest")
if ul then
ul.setup({ builders = builders })
end

@ -24,7 +24,9 @@ local run = function(fmtargs, bufnr, cmd)
if not vim.api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
end
vim.lsp.buf.format({ async = true, bufnr = bufnr })
api.cmd("write")
vim.lsp.buf.format({ async = _GO_NVIM_CFG.lsp_fmt_async, bufnr = bufnr, name = "gopls" })
return
end

@ -47,7 +47,7 @@ end
-- }))
-- Run with ginkgo Description
M.test_fun = function(...)
M.test_func = function(...)
local args = { ... }
log(args)
local fpath = vim.fn.expand("%:p:h")

@ -73,6 +73,24 @@ end
M.get_build_tags = get_build_tags
local function get_test_filebufnr()
local fn = vim.fn.expand("%")
log(fn)
local bufnr = vim.api.nvim_get_current_buf()
if not fn:find("test%.go$") then
fn = require("go.alternate").alternate()
fn = vim.fn.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 not vim.api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
end
end
return bufnr
end
local function run_test(path, args)
log(args)
local compile = false
@ -187,21 +205,38 @@ M.test_package = function(...)
return run_test(fpath, args)
end
M.test_fun = function(...)
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(row, col)
if empty(ns) then
return nil
end
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
log(fn, ns.name)
if string.find(fn:lower(), ns.name:lower()) then
ns = { name = fn }
return ns
end
end
end
return ns
end
M.test_func = function(...)
local args = { ... }
log(args)
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":~:.")
-- fpath = fpath:gsub(" ", [[\ ]])
-- fpath = fpath:gsub("-", [[\-]])
-- log(fpath)
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(row, col)
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)
utils.log("parnode" .. vim.inspect(ns))
@ -212,10 +247,12 @@ M.test_fun = function(...)
require("go.install").install(test_runner)
test_runner = _GO_NVIM_CFG.test_runner
if test_runner == "ginkgo" then
ginkgo.test_fun(...)
ginkgo.test_func(...)
end
end
local run_flags = { "-run" }
local cmd = {}
local run_in_floaterm = optarg["F"] or _GO_NVIM_CFG.run_in_floaterm
if run_in_floaterm then
@ -226,7 +263,7 @@ M.test_fun = function(...)
if optarg["s"] then
return M.select_tests()
end
if _GO_NVIM_CFG.verbose_tests then
if _GO_NVIM_CFG.verbose_tests and _GO_NVIM_CFG.test_runner == "go" then
table.insert(cmd, "-v")
end
@ -239,11 +276,20 @@ M.test_fun = function(...)
table.insert(cmd, bench)
vim.list_extend(cmd, bench_opts)
else
table.insert(cmd, "-run")
vim.list_extend(cmd, run_flags)
table.insert(cmd, [[^]] .. ns.name)
end
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":~:.")
table.insert(cmd, fpath)
if test_runner == "dlv" then
cmd = { "dlv", "test", fpath, "--", "-test.run", "^" .. ns.name }
local term = require("go.term").run
term({ cmd = cmd, autoclose = false })
return
end
if run_in_floaterm then
utils.log(cmd)
local term = require("go.term").run
@ -255,6 +301,7 @@ M.test_fun = function(...)
vim.cmd([[setl makeprg=]] .. test_runner .. [[\ test]])
-- set_efm()
utils.log("test cmd", cmd)
return require("go.asyncmake").make(unpack(cmd))
end
@ -294,7 +341,7 @@ M.test_file = function(...)
test_runner = _GO_NVIM_CFG.test_runner
require("go.install").install(test_runner)
if test_runner == "ginkgo" then
ginkgo.test_fun(...)
ginkgo.test_func(...)
end
end
@ -328,8 +375,15 @@ M.test_file = function(...)
return
end
if _GO_NVIM_CFG.test_runner == "dlv" then
cmd_args = { "dlv", "test", relpath, "--", "-test.run", tests }
local term = require("go.term").run
term({ cmd = cmd_args, autoclose = false })
return
end
vim.cmd([[setl makeprg=]] .. _GO_NVIM_CFG.go .. [[\ test]])
log (cmd_args)
log(cmd_args)
local cmdret = require("go.asyncmake").make(unpack(cmd_args))
@ -361,8 +415,10 @@ M.run_file = function()
end
M.get_testfunc = function()
local bufnr = vim.api.nvim_get_current_buf()
local tree = vim.treesitter.get_parser(bufnr):parse()[1]
local bufnr = get_test_filebufnr()
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 test_names = {}
@ -402,7 +458,7 @@ M.select_tests = function()
})
end)
end
local test_names = M.get_testfunc()
vim.ui.select(test_names, {
prompt = "select test to run:",
kind = "codelensaction",

@ -2,7 +2,7 @@ local eq = assert.are.same
local cur_dir = vim.fn.expand("%:p:h")
local busted = require("plenary/busted")
describe("should run test", function()
describe("should run func test", 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")
@ -19,7 +19,24 @@ describe("should run test", function()
})
vim.cmd("silent exe 'e " .. path .. "'")
vim.fn.setpos(".", { 0, 5, 11, 0 })
local cmd = require("go.gotest").test_fun()
local cmd = require("go.gotest").test_func()
eq({ "go", "test", "-v", "-run", "^Test_branch", "./lua/tests/fixtures/coverage" }, cmd)
end)
it("should test function inside a source code", function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])
local path = cur_dir .. "/lua/tests/fixtures/coverage/branch.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, 6, 11, 0 })
local cmd = require("go.gotest").test_func()
eq({ "go", "test", "-v", "-run", "^Test_branch", "./lua/tests/fixtures/coverage" }, cmd)
end)

Loading…
Cancel
Save