Update document

Smart build tags detection
pull/140/head
ray-x 2 years ago
parent 0bf69f9315
commit 040c7bd110

@ -213,7 +213,7 @@ first run of `GoFmt` may fail. It is recommended to run `GoInstallBinaries` to i
| 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 -t yourtags | go test ./... -tags=yourtags, 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 |
@ -224,8 +224,10 @@ first run of `GoFmt` may fail. It is recommended to run `GoInstallBinaries` to i
| GoCoverage {flags} | -t : toggle, -r: remove signs, -R remove sings from all files |
| GoTermClose | `closes the floating term` |
Note1: if package_url not provided, will check current line is a valid package url or not, if it is valid, will
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
Show test coverage:

@ -126,12 +126,12 @@ function M.make(...)
local cmd = vim.fn.split(makeprg, " ")
if optarg["t"] then
local tag = optarg['t']
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))
table.insert(cmd, "-tags=" .. tag:sub(f + 1))
end
end
if makeprg:find("test") then
@ -185,6 +185,9 @@ function M.make(...)
if data then
for _, value in ipairs(data) do
if value ~= "" then
if value:find("=== RUN") or value:find("no test file") then
goto continue
end
value = handle_color(value)
if value:find("FAIL") then
failed = true
@ -192,7 +195,7 @@ function M.make(...)
local changed = false
if vim.fn.empty(vim.fn.glob(args[#args])) == 0 then
changed = true
if value:find("=== RUN") == nil and value:find("FAIL") == nil then
if value:find("FAIL") == nil then
value = args[4] .. util.sep() .. util.ltrim(value)
end
else
@ -208,6 +211,7 @@ function M.make(...)
itemn = #lines
end
end
::continue::
end
end
end

@ -84,8 +84,10 @@ local function keybind()
end
local function get_build_flags()
if _GO_NVIM_CFG.build_tags ~= "" then
return "-tags " .. _GO_NVIM_CFG.build_tags
local get_build_tags = require("go.gotest").get_build_tags
local tags = get_build_tags({})
if tags then
return tags
else
return ""
end
@ -258,7 +260,7 @@ M.run = function(...)
keybind()
else
M.stop()
testfunc = require('go.gotest').get_test_func_name()
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
@ -352,21 +354,20 @@ M.run = function(...)
local empty = utils.empty
local launch = require("go.launch")
local cfg_exist, cfg_file = launch.vs_launch()
log(mode, cfg_exist, cfg_file)
-- if breakpoint is not set add breakpoint at current pos
local pts = require("dap.breakpoints").get()
if utils.empty(pts) then
if utils.empty(pts) then
require("dap").set_breakpoint()
end
testfunc = require('go.gotest').get_test_func_name()
testfunc = require("go.gotest").get_test_func_name()
if testfunc then
optarg["t"] = true
optarg["t"] = true
end
if optarg["t"] then
dap_cfg.name = dap_cfg.name .. " test"
@ -388,7 +389,7 @@ M.run = function(...)
dap_cfg.mode = "test"
dap_cfg.request = "launch"
dap_cfg.program = sep .. "${relativeFileDirname}"
if not empty(ns)then
if not empty(ns) then
dap_cfg.args = { "-test.run", "^" .. ns.name }
end
dap.configurations.go = { dap_cfg }

@ -85,7 +85,8 @@ M.test_func = function(...)
local test_runner = "ginkgo"
require("go.install").install(test_runner)
local cmd = { test_runner, [[ --focus=']] .. describe .. [[']], get_build_tags(args), fpath }
-- local cmd = { test_runner, [[ --focus=']] .. describe .. [[']], get_build_tags(args), fpath }
local cmd = { test_runner, [[ --focus-file=']] .. describe .. [[']], get_build_tags(args), fpath }
log(cmd)
if _GO_NVIM_CFG.run_in_floaterm then
local term = require("go.term").run
@ -95,7 +96,7 @@ M.test_func = function(...)
cmd = [[setl makeprg=]] .. test_runner
vim.cmd(cmd)
args = { [[ --focus=']] .. describe .. [[']], get_build_tags(args), fpath }
args = { [[ --focus-file=']] .. describe .. [[']], get_build_tags(args), fpath }
require("go.asyncmake").make(unpack(args))
utils.log("test cmd", cmd)
return true
@ -118,9 +119,9 @@ M.test_file = function(...)
require("go.install").install(test_runner)
local cmd_args = {
[[--regexScansFilePath=true]],
-- [[--regexScansFilePath=true]], v1
get_build_tags(args),
[[ --focus ]],
[[ --focus-file= ]],
fname,
fpath,
}

@ -61,7 +61,13 @@ M.get_build_tags = function(args)
if optarg["t"] then
table.insert(tags, optarg["t"])
end
if type(tags) == 'table' and #tags > 0 then
local rt = utils.get_build_tags()
if not utils.empty(rt) then
vim.list_extend(tags, rt)
end
if #tags > 0 then
return "-tags=" .. table.concat(tags, ","), reminder
end
end
@ -129,7 +135,7 @@ local function run_test(path, args)
end
if not empty(tags) then
cmd = vim.list_extend(cmd, {tags})
cmd = vim.list_extend(cmd, { tags })
end
if optarg["C"] then
@ -282,7 +288,7 @@ M.test_func = function(...)
return M.select_tests()
end
local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts)
local tags = M.get_build_tags(args)
local tags = M.get_build_tags(args, get_test_filebufnr())
utils.log("parnode" .. vim.inspect(ns))
local test_runner = _GO_NVIM_CFG.go
@ -452,7 +458,7 @@ M.run_file = function()
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
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
table.insert(test_names, vim.treesitter.query.get_node_text(node, bufnr))

@ -579,4 +579,27 @@ function util.set_nulls()
end
end
end
-- parse //+build integration unit
function util.get_build_tags(buf)
local tags = {}
buf = buf or "%"
local pattern = [[^//\s*+build\s\+\(.\+\)]]
local cnt = vim.fn.getbufinfo(buf)[1]["linecount"]
cnt = math.min(cnt, 10)
for i = 1, cnt do
local line = vim.fn.trim(vim.fn.getbufline(buf, i)[1])
if string.find(line, "package") then
break
end
local t = vim.fn.substitute(line, pattern, [[\1]], "")
if t ~= line then -- tag found
t = vim.fn.substitute(t, [[ \+]], ",", "g")
table.insert(tags, t)
end
end
if #tags > 0 then
return tags
end
end
return util

@ -0,0 +1,7 @@
//+build integration,unit
package "testing"
func TestTag(t *testing.T) {
t.Log("TestTag")
}

@ -61,7 +61,7 @@ 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)
@ -84,7 +84,7 @@ describe("should run test file with flags", function()
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)
eq({ "go", "test", "-tags=tag1", "-v", "-run", [[Test_branch\|TestBranch]], "./lua/tests/fixtures/coverage" }, cmd)
end)
end)
@ -129,7 +129,7 @@ describe("should run test ", function()
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)
eq({ "go", "test", "-tags=tags1", "-v", "-run", "^Test_branch", "./lua/tests/fixtures/coverage" }, cmd)
end)
end)
@ -156,3 +156,37 @@ describe("should allow select test func", function()
eq({ "Test_branch", "TestBranch" }, cmd)
end)
end)
describe("should run test file with flags inside file", 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/tag_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(".", { 1, 1, 1, 0 })
local cmd = require("go.gotest").test_file("-t", "tag1")
eq(
{
"go",
"test",
"-tags=tag1,integration,unit",
"-v",
"-run",
"TestTag",
"./lua/tests/fixtures/coverage",
},
cmd
)
end)
end)

Loading…
Cancel
Save