issue #65 failed to run coverage tests. Notify updates.

pull/67/head
ray-x 2 years ago
parent 192486892b
commit 264efc57c1

@ -2,6 +2,7 @@ local utils = require("go.utils")
local log = utils.log
local coverage = {}
local api = vim.api
local empty = utils.empty
local M = {}
local visable = false
_GO_NVIM_CFG = _GO_NVIM_CFG
@ -28,31 +29,27 @@ local function sign_get(bufnr, name)
end
-- all windows and buffers
local function all_win_bufnr()
local winids = {}
local function all_bufnr()
local bufnrl = {}
for i = 1, vim.fn.tabpagenr("$") do
for j = 1, vim.fn.tabpagewinnr(i, "$") do
local winid = vim.fn.win_getid(j, i)
local bufnr = vim.fn.winbufnr(winid)
if vim.fn.buflisted(bufnr) then
local fn = vim.fn.bufname(bufnr)
local ext = string.sub(fn, #fn - 2)
if ext == ".go" then
table.insert(winids, winid)
table.insert(bufnrl, bufnr)
end
local buffers = vim.fn.getbufinfo({ bufloaded = 1, buflisted = 1 })
for _, b in pairs(buffers) do
if not (vim.fn.empty(b.name) == 1 or b.hidden == 1) then
local name = b.name
local ext = string.sub(name, #name - 2)
if ext == ".go" then
table.insert(bufnrl, b.bufnr)
end
end
end
return winids, bufnrl
end
return bufnrl
end -- log(bufnr, name, opts, redefine)
function M.define(bufnr, name, opts, redefine)
if sign_define_cache[bufnr] == nil then
sign_define_cache[bufnr] = {}
end
-- log(bufnr, name, opts, redefine)
-- vim.notify(bufnr .. name .. opts .. redefine, vim.lsp.log_levels.DEBUG)
if redefine then
sign_define_cache[bufnr][name] = nil
@ -73,7 +70,7 @@ function M.remove(bufnr, lnum)
end
local function remove_all()
local _, bufnrs = all_win_bufnr()
local bufnrs = all_bufnr()
for _, bid in pairs(bufnrs) do
M.remove(bid)
end
@ -90,6 +87,7 @@ function M.add(bufnr, signs)
M.define(bufnr, stype, { text = _GO_NVIM_CFG.gocoverage_sign, texthl = stype })
for lnum = s.range.start.line, s.range["end"].line + 1 do
log(lnum, bufnr)
to_place[#to_place + 1] = {
id = lnum,
group = ns,
@ -126,7 +124,7 @@ local function augroup()
end
local function enable_all()
local _, bufnrs = all_win_bufnr()
local bufnrs = all_bufnr()
for _, bufnr in pairs(bufnrs) do
-- enable
-- local bufnr = vim.fn.winbufnr(id)
@ -155,7 +153,7 @@ end
local function parse_line(line)
local m = vim.fn.matchlist(line, [[\v([^:]+):(\d+)\.(\d+),(\d+)\.(\d+) (\d+) (\d+)]])
if m == nil or #m == 0 then
if empty(m) then
return {}
end
local path = m[2]
@ -187,6 +185,10 @@ if vim.tbl_isempty(vim.fn.sign_getdefined(sign_uncover)) then
end
M.read_cov = function(covfn)
if vim.fn.filereadable(covfn) == 0 then
vim.notify(string.format("cov file not exist: %s", covfn), vim.lsp.log_levels.WARN)
return
end
local cov = vim.fn.readfile(covfn)
-- log(vim.inspect(cov))
for _, line in pairs(cov) do
@ -200,16 +202,18 @@ M.read_cov = function(covfn)
end
end
local _, bufnrs = all_win_bufnr()
local bufnrs = all_bufnr()
log("buffers", bufnrs)
local added = {}
for _, bid in pairs(bufnrs) do
local bufnr = vim.fn.winbufnr(bid)
local fn = vim.fn.bufname(bufnr)
-- if added[bid] == nil then
local fn = vim.fn.bufname(bid)
fn = vim.fn.fnamemodify(fn, ":t")
log(bid, fn)
M.add(bid, coverage[fn])
visable = true
added[bid] = true
-- end
end
return coverage
end
@ -217,7 +221,8 @@ end
M.run = function(...)
local get_build_tags = require("go.gotest").get_build_tags
-- local cov = vim.fn.tempname()
local cov = vim.fn.expand("%:p:h") .. "/cover.cov"
local pwd = vim.fn.getcwd()
local cov = pwd .. utils.sep() .. "cover.cov"
local args = { ... }
log(args)
@ -231,28 +236,30 @@ M.run = function(...)
local cmd = { test_runner, "test", "-coverprofile", cov }
local tags = ""
local args2 = {}
if args ~= nil and args ~= {} then
if not empty(args) then
tags, args2 = get_build_tags(args)
if tags ~= {} then
vim.list_extend(cmd, tags)
end
vim.list_extend(cmd, args2)
end
if not empty(args2) then
log(args2)
cmd = vim.list_extend(cmd, args2)
else
argsstr = "." .. utils.sep() .. "..."
table.insert(cmd, argsstr)
end
local lines = { "" }
coverage = {}
if args == {} then
-- pkg provided?
table.insert(cmd, "." .. utils.spe() .. vim.fn.expand("%:.:h"))
end
log("run coverage", cmd)
local argsstr = ""
if _GO_NVIM_CFG.run_in_floaterm then
cmd = table.concat(cmd, " ")
if args2 == {} then
if empty(args2) then
cmd = cmd .. "." .. utils.sep() .. "..."
end
utils.log(cmd)
@ -276,9 +283,9 @@ M.run = function(...)
.. vim.inspect(cmd)
.. "error: "
.. vim.inspect(data)
.. "job"
.. "job "
.. tostring(job_id)
.. "ev"
.. "ev "
.. event,
vim.lsp.log_levels.ERROR
)
@ -287,19 +294,18 @@ M.run = function(...)
if event ~= "exit" then
vim.notify(string.format("%s %s %s", job_id, event, vim.inspect(data)), vim.lsp.log_levels.ERROR)
end
log("test finished")
coverage = M.read_cov(cov)
log("coverage", coverage)
vim.fn.delete(cov)
local lp = table.concat(lines, "\n")
vim.notify(string.format("test finished %s", vim.inspect(lp)), vim.lsp.log_levels.INFO)
coverage = M.read_cov(cov)
vim.fn.setqflist({}, " ", {
title = cmd,
lines = lines,
-- efm = vim.api.nvim_buf_get_option(bufnr, "errorformat")
efm = vim.o.efm .. [[,]] .. require("go.gotest").efm(),
})
vim.api.nvim_command("doautocmd QuickFixCmdPost")
vim.cmd([[botright copen]])
-- vim.fn.delete(cov)
end,
})
end

@ -157,8 +157,9 @@ M.run = function(...)
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row, col = row, col + 1
local empty = utils.empty
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
if ns == nil or ns == {} then
if empty(ns) then
log("ts not not found, debug while file")
if mode == "nearest" then
mode = "test"

@ -1,6 +1,7 @@
local M = {}
local utils = require("go.utils")
local log = utils.log
local empty = utils.empty
local ginkgo = require("go.ginkgo")
M.efm = function()
@ -71,18 +72,18 @@ local function run_test(path, args)
else
cmd = { "-v" }
end
if tags ~= {} then
if not empty(tags) then
cmd = vim.list_extend(cmd, tags)
end
if args2 ~= nil and args2 ~= {} then
if not empty(args2) then
cmd = vim.list_extend(cmd, args2)
else
local argsstr = "." .. utils.sep() .. "..."
cmd = vim.list_extend(cmd, argsstr)
end
if path ~= "" then
table.insert(cmd, path)
else
local argsstr = "." .. utils.sep() .. "..."
cmd = table.insert(cmd, argsstr)
end
utils.log(cmd)
if _GO_NVIM_CFG.run_in_floaterm then
@ -148,7 +149,7 @@ M.test_fun = 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 ns == nil or ns == {} then
if empty(ns) then
return false
end
@ -172,14 +173,14 @@ M.test_fun = function(...)
else
cmd = { "-v" }
end
if tags ~= {} then
if not empty(tags) then
cmd = vim.list_extend(cmd, tags)
end
if args2 ~= nil and args2 ~= {} then
if not empty(args2) then
cmd = vim.list_extend(cmd, args2)
else
argsstr = "." .. utils.sep() .. "..."
cmd = vim.list_extend(cmd, argsstr)
table.insert(cmd, argsstr)
end
if ns.name:find("Bench") then
@ -219,7 +220,7 @@ M.test_file = function(...)
-- TODO maybe with treesitter or lsp list all functions in current file and regex with Test
local tests = vim.fn.systemlist(cmd)[1]
utils.log(cmd, tests)
if tests == nil or tests == {} then
if empty(tests) then
print("no test found fallback to package test")
M.test_package(args)
return
@ -238,7 +239,6 @@ M.test_file = function(...)
local relpath = utils.rel_path()
local cmd_args
if _GO_NVIM_CFG.run_in_floaterm then

@ -5,6 +5,7 @@ local gotests = "gotests"
local test_dir = _GO_NVIM_CFG.test_dir or ""
local test_template = vim.go_nvim_test_template or ""
local utils = require("go.utils")
local empty = utils.empty
local run = function(setup)
print(vim.inspect(setup))
local j = vim.fn.jobstart(setup, {
@ -12,9 +13,8 @@ local run = function(setup)
print("unit tests generate " .. vim.inspect(data))
end,
on_stderr = function(_, data, _)
print("generate tests finished with message: " .. vim.inspect(setup) .. "error: "
.. vim.inspect(data))
end
print("generate tests finished with message: " .. vim.inspect(setup) .. "error: " .. vim.inspect(data))
end,
})
end
@ -31,7 +31,7 @@ local add_test = function(args)
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row, col = row + 1, col + 1
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
if ns == nil or ns == {} then
if empty(ns) then
return
end
@ -44,7 +44,7 @@ ut.fun_test = function(parallel)
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row, col = row + 1, col + 1
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
if ns == nil or ns == {} then
if empty(ns) then
return
end
@ -52,7 +52,7 @@ ut.fun_test = function(parallel)
local funame = ns.name
-- local rs, re = ns.dim.s.r, ns.dim.e.r
local gofile = vim.fn.expand("%")
local args = {gotests, "-w", "-only", funame, gofile}
local args = { gotests, "-w", "-only", funame, gofile }
if parallel then
table.insert(args, "-parallel")
end
@ -62,7 +62,7 @@ end
ut.all_test = function(parallel)
parallel = parallel or false
local gofile = vim.fn.expand("%")
local args = {gotests, "-all", "-w", gofile}
local args = { gotests, "-all", "-w", gofile }
if parallel then
table.insert(args, "-parallel")
end
@ -72,7 +72,7 @@ end
ut.exported_test = function(parallel)
parallel = parallel or false
local gofile = vim.fn.expand("%")
local args = {gotests, "-exported", "-w", gofile}
local args = { gotests, "-exported", "-w", gofile }
if parallel then
table.insert(args, "-parallel")
end

@ -90,7 +90,7 @@ local run = function(cmd, opts)
uv.read_start(stderr, function(err, data)
assert(not err, err)
if data then
vim.notify(string.format("stderr chunk", stderr, data), vim.lsp.log_levels.DEBUG)
vim.notify(string.format("stderr chunk %s %s", stderr, vim.inspect(data)), vim.lsp.log_levels.DEBUG)
end
end)
stdout:read_start(update_chunk)

@ -10,7 +10,12 @@ local tags = {}
-- gomodifytags -file demo.go -line 8,11 -clear-tags xml
local opts = {
"-add-tags", "-add-options", "-remove-tags", "-remove-options", "-clear-tags", "-clear-options"
"-add-tags",
"-add-options",
"-remove-tags",
"-remove-options",
"-clear-tags",
"-clear-options",
}
local gomodify = "gomodifytags"
@ -20,28 +25,28 @@ tags.modify = function(...)
local fname = vim.fn.expand("%") -- %:p:h ? %:p
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local ns = require("go.ts.go").get_struct_node_at_pos(row, col)
if ns == nil or ns == {} then
if utils.empty(ns) then
return
end
-- vim.notify("parnode" .. vim.inspect(ns), vim.lsp.log_levels.DEBUG)
local struct_name = ns.name
local rs, re = ns.dim.s.r, ns.dim.e.r
local setup = {gomodify, "-format", "json", "-file", fname, '-w'}
local setup = { gomodify, "-format", "json", "-file", fname, "-w" }
if struct_name == nil then
local _, csrow, _, _ = unpack(vim.fn.getpos('.'))
table.insert(setup, '-line')
local _, csrow, _, _ = unpack(vim.fn.getpos("."))
table.insert(setup, "-line")
table.insert(setup, csrow)
else
table.insert(setup, '-struct')
table.insert(setup, "-struct")
table.insert(setup, struct_name)
end
if transform then
table.insert(setup.args, "-transform")
table.insert(setup.args, transform)
end
local arg = {...}
local arg = { ... }
for i, v in ipairs(arg) do
table.insert(setup, v)
end
@ -69,15 +74,15 @@ tags.modify = function(...)
vim.api.nvim_buf_set_lines(0, tagged["start"] - 1, tagged["start"] - 1 + #tagged.lines, false, tagged.lines)
vim.cmd("write")
vim.notify("struct updated ", vim.lsp.log_levels.DEBUG)
end
end,
})
end
tags.add = function(...)
local cmd = {"-add-tags"}
local arg = {...}
local cmd = { "-add-tags" }
local arg = { ... }
if #arg == 0 then
arg = {'json'}
arg = { "json" }
end
for _, v in ipairs(arg) do
table.insert(cmd, v)
@ -87,10 +92,10 @@ tags.add = function(...)
end
tags.rm = function(...)
local cmd = {"-remove-tags"}
local arg = {...}
local cmd = { "-remove-tags" }
local arg = { ... }
if #arg == 0 then
arg = {'json'}
arg = { "json" }
end
for _, v in ipairs(arg) do
table.insert(cmd, v)
@ -99,7 +104,7 @@ tags.rm = function(...)
end
tags.clear = function()
local cmd = {"-clear-tags"}
local cmd = { "-clear-tags" }
tags.modify(unpack(cmd))
end

@ -443,4 +443,11 @@ function util.work_path()
return workfolders[1] or fpath
end
function util.empty(t)
if t == nil then
return true
end
return next(t) == nil
end
return util

Loading…
Cancel
Save