better test coverage for gotest

pull/130/head
ray-x 2 years ago
parent 323cfae1fa
commit 125d6fa3d3

@ -136,13 +136,13 @@ local function run_test(path, args)
if _GO_NVIM_CFG.run_in_floaterm then
local term = require("go.term").run
term({ cmd = cmd, autoclose = false })
return
return cmd
end
vim.cmd([[setl makeprg=]] .. _GO_NVIM_CFG.go .. [[\ test]])
utils.log("test cmd", cmd)
require("go.asyncmake").make(unpack(cmd))
return require("go.asyncmake").make(unpack(cmd))
end
M.test = function(...)
@ -177,12 +177,11 @@ M.test_package = function(...)
local args = { ... }
log(args)
local repath = utils.rel_path() or "."
local fpath = repath .. utils.sep() .. "..."
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":~:.") .. sep .. "..."
utils.log("fpath: " .. fpath)
-- args[#args + 1] = fpath
run_test(fpath, args)
return run_test(fpath, args)
end
M.test_fun = function(...)
@ -259,7 +258,8 @@ M.test_file = function(...)
-- require sed
-- local testcases = [[sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\\|/g']]
local fpath = vim.fn.expand("%:p")
-- local fpath = vim.fn.expand("%:p")
local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.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
@ -321,8 +321,11 @@ M.test_file = function(...)
end
vim.cmd([[setl makeprg=]] .. _GO_NVIM_CFG.go .. [[\ test]])
require("go.asyncmake").make(unpack(cmd_args))
local cmdret = require("go.asyncmake").make(unpack(cmd_args))
utils.log("test cmd: ", cmd, " finished")
return cmdret
end
-- TS based run func
@ -348,6 +351,8 @@ M.run_file = function()
end)
end
-- GUI to select test?
M.select_tests = function()
local bufnr = vim.api.nvim_get_current_buf()
local tree = vim.treesitter.get_parser(bufnr):parse()[1]

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

@ -20,8 +20,53 @@ 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 lines = vim.fn.readfile(path)
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")
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()
eq({ "go", "test", "-v", "-run", "Test_branch", "./lua/tests/fixtures/coverage/branch_test.go" }, 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")
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, 1, 1, 0 })
local cmd = require("go.gotest").test_package()
eq({ "go", "test", "-v", "./lua/tests/fixtures/coverage/..." }, cmd)
end)
end)

Loading…
Cancel
Save