Features:

1) count the overall test coverage per-file and per-project
2) add gotestsum as a test runner option
3) handle windows GOPATH and
4) minor fix: stop GoFmt if no formatter is available
pull/151/head
ray-x 2 years ago
parent ee39b539b9
commit 981adf9b39

@ -15,7 +15,7 @@ The plugin covers most features required for a gopher.
- Dlv Debug: with [nvim-dap](https://github.com/mfussenegger/nvim-dap) and [Dap UI](https://github.com/rcarriga/nvim-dap-ui).
- Load vscode launch configuration
- Unit test: generate unit test framework with [gotests](https://github.com/cweill/gotests). Run test with
richgo/ginkgo/go test
richgo/ginkgo/gotestsum/go test
- Add and remove tag for struct with tag modify(gomodifytags)
- Code format: Supports LSP format and GoFmt(with golines)
- CodeLens : gopls codelens and codelens action support

@ -258,11 +258,7 @@ function go.setup(cfg)
if _GO_NVIM_CFG.textobjects then
require("go.ts.textobjects").setup()
end
gobin = vfn.getenv("GOBIN")
if gobin == vim.NIL then
return
end
require('go.env').append('PATH', gobin)
require('go.env').setup()
end
go.doc_complete = require("go.godoc").doc_complete

@ -182,6 +182,9 @@ if vim.tbl_isempty(vfn.sign_getdefined(sign_uncover)) then
end
M.read_cov = function(covfn)
local total_lines = 0
local total_covered = 0
if vfn.filereadable(covfn) == 0 then
vim.notify(string.format("cov file not exist: %s please run cover test first", covfn), vim.lsp.log_levels.WARN)
return
@ -190,15 +193,25 @@ M.read_cov = function(covfn)
-- log(vim.inspect(cov))
for _, line in pairs(cov) do
local cl = parse_line(line)
local file_lines = 0
local file_covered = 0
if cl.filename ~= nil or cl.range ~= nil then
-- log("cl", vim.inspect(cl))
total_lines = total_lines + cl.num
if coverage[cl.filename] == nil then
coverage[cl.filename] = {}
end
coverage[cl.filename].file_lines = (coverage[cl.filename].file_lines or 0) + cl.num
file_lines = file_lines + cl.num
if cl.cnt > 0 then
coverage[cl.filename].file_covered = (coverage[cl.filename].file_covered or 0) + cl.num
total_covered = total_covered + cl.num
end
table.insert(coverage[cl.filename], cl)
end
end
coverage.total_lines = total_lines
coverage.total_covered = total_covered
local bufnrs = all_bufnr()
log("buffers", bufnrs)
local added = {}
@ -280,7 +293,9 @@ M.run = function(...)
if vim.fn.filereadable(covfn) == 0 then
vim.notify("no cov file specified or existed, will rerun coverage test", vim.lsp.log_levels.INFO)
else
return M.read_cov(covfn)
local cov = M.read_cov(covfn)
utils.notify(string.format("total coverage: %%d", cov.total_covered / cov.total_lines * 100))
return cov
end
end
if load == "-t" then
@ -364,6 +379,7 @@ M.run = function(...)
local lp = table.concat(lines, "\n")
vim.notify(string.format("test finished:\n %s", lp), vim.lsp.log_levels.INFO)
coverage = M.read_cov(cov)
utils.notify(string.format("total coverage: %%d", coverage.total_covered / coverage.total_lines * 100))
if load == "-m" then
M.toggle(true)
return M.show_func()

@ -19,6 +19,12 @@ function M.append(env, val)
if val == vim.NIL or string.find(oldval, val) then
return
end
if oldval == vim.NIL then
util.notify("failed to get env var: " .. env)
end
if oldval:find(val) then -- presented
return
end
local newval = oldval .. ":" .. val
vfn.setenv(env, newval)
end
@ -47,4 +53,29 @@ function M.load_env(env, setToEnv)
return envs
end
-- best effort to enabl $GOBIN
function M.setup()
local home = "HOME"
if util.is_windows() then
home = "USERPROFILE"
end
local gohome = vfn.getenv("GOHOME")
local gobin = vfn.getenv("GOBIN")
local user_home = vfn.getenv(home)
if gobin == vim.NIL then
if gohome == vim.NIL then
if user_home == vim.NIL then
util.notify("failed to setup $GOBIN")
return
end
gobin = user_home .. sep .. "go" .. sep .. "bin"
else
local gohome1 = vim.split(gohome, ":")[1]
gobin = gohome1 .. require("go.utils").sep() .. "bin"
vfn.setenv("GOBIN", gobin)
end
end
M.append("PATH", gobin)
end
return M

@ -7,6 +7,7 @@ local max_len = _GO_NVIM_CFG.max_line_len or 120
local goimport = _GO_NVIM_CFG.goimport or "goimports"
local gofmt = _GO_NVIM_CFG.gofmt or "gofumpt"
local vfn = vim.fn
local install = require("go.install").install
local gofmt_args = _GO_NVIM_CFG.gofmt_args or {
"--max-len=" .. tostring(max_len),
"--base-formatter=" .. gofmt,
@ -106,8 +107,14 @@ M.gofmt = function(...)
if optarg["a"] then
all_buf = true
end
require("go.install").install(gofmt)
require("go.install").install("golines")
if not install(gofmt) then
utils.notify("installing ".. gofmt .. " please retry after installation")
return
end
if not install("golines") then
utils.notify("installing golines , please rerun format after install finished")
return
end
local a = {}
utils.copy_array(gofmt_args, a)
if all_buf then

@ -2,7 +2,11 @@ local M = {}
local util = require("go.utils")
local log = util.log
local health = require("health")
local health = vim.health
if not vim.health then
health = require("health")
end
local tools = require("go.install").tools
local start = health.report_start
@ -28,28 +32,29 @@ local function binary_check()
info("Tool installed: " .. val)
else
warn("Missing tool: " .. val)
no_err = false
end
end
if no_err then
ok("All binaries installed")
end
if vfn.executable('sed') == 1 then
info("sed installed.")
else
warn("sed is not installed.")
end
if vfn.executable('sed') == 1 then
info("sed installed.")
info("sed installed. gotests may not fully work")
else
no_err = false
warn("sed is not installed.")
end
if vfn.executable('curl') == 1 then
info("curl installed.")
else
warn("curl is not installed.")
no_err = false
warn("curl is not installed, gocheat will not work.")
end
if no_err then
ok("All binaries installed")
else
warn("Some binaries are not installed, please check if your $HOME/go/bin or $GOBIN $exists and in your $PATH")
end
end
@ -107,9 +112,28 @@ local function plugin_check()
end
end
function env_check()
local envs = {'GOPATH', 'GOROOT', 'GOBIN'}
local any_warn = false
for _, env in ipairs(envs) do
if vim.env[env] == nil then
info(string.format("%s is not set", env))
any_warn = true
else
ok(string.format("%s is set", env))
end
end
if any_warn then
info("Not all enviroment variables set")
else
ok("All enviroment variables set")
end
end
function M.check()
binary_check()
plugin_check()
env_check()
end
return M

@ -20,6 +20,7 @@ local url = {
dlv = "github.com/go-delve/delve/cmd/dlv",
ginkgo = "github.com/onsi/ginkgo/ginkgo",
richgo = "github.com/kyoh86/richgo",
gotestsum = "gotest.tools/gotestsum"
}
local tools = {}
@ -63,7 +64,7 @@ local function go_install(pkg)
if #data > 1 then
msg = msg .. data
end
vim.notify(msg, vim.lsp.log_levels.DEBUG)
vim.notify(msg, vim.lsp.log_levels.INFO)
end,
})
end

@ -18,6 +18,10 @@ function util.sep()
return "/"
end
function util.is_windows()
return is_windows
end
local function get_path_sep()
if is_windows then
return ";"

@ -5,7 +5,7 @@ local busted = require("plenary/busted")
describe("should read coveragefile", 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 read coverage file", function()
--
local path = cur_dir .. "/lua/tests/fixtures/coverage/coverage.out" -- %:p:h ? %:p
@ -27,6 +27,11 @@ describe("should read coveragefile", function()
eq(result[n][1].file, "github.com/go.nvim/branch.go")
eq(result[n][1].range, range)
eq(result[n].file_lines, 9)
eq(result[n].file_covered, 4)
eq(result.total_lines, 9)
eq(result.total_covered, 4)
-- eq(result[n][1], "github.com/go.nvim/branch.go")
end)
it("should generate sign list", function()

Loading…
Cancel
Save