From 981adf9b39ebd808c67aacbb484cda0b8ae064af Mon Sep 17 00:00:00 2001 From: ray-x Date: Fri, 1 Jul 2022 19:15:12 +1000 Subject: [PATCH] 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 --- README.md | 2 +- lua/go.lua | 6 +---- lua/go/coverage.lua | 20 +++++++++++++-- lua/go/env.lua | 31 +++++++++++++++++++++++ lua/go/format.lua | 11 ++++++-- lua/go/health.lua | 46 ++++++++++++++++++++++++++-------- lua/go/install.lua | 3 ++- lua/go/utils.lua | 4 +++ lua/tests/go_coverage_spec.lua | 7 +++++- 9 files changed, 107 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 6ece9b0..5a8641d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/go.lua b/lua/go.lua index 7d524ec..0ff1b74 100644 --- a/lua/go.lua +++ b/lua/go.lua @@ -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 diff --git a/lua/go/coverage.lua b/lua/go/coverage.lua index b48a5cc..9086fbb 100644 --- a/lua/go/coverage.lua +++ b/lua/go/coverage.lua @@ -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() diff --git a/lua/go/env.lua b/lua/go/env.lua index d384883..2ea5221 100644 --- a/lua/go/env.lua +++ b/lua/go/env.lua @@ -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 diff --git a/lua/go/format.lua b/lua/go/format.lua index 277679d..2741f2f 100644 --- a/lua/go/format.lua +++ b/lua/go/format.lua @@ -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 diff --git a/lua/go/health.lua b/lua/go/health.lua index 469e2db..434f99f 100644 --- a/lua/go/health.lua +++ b/lua/go/health.lua @@ -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 diff --git a/lua/go/install.lua b/lua/go/install.lua index c7d4322..e851c7e 100644 --- a/lua/go/install.lua +++ b/lua/go/install.lua @@ -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 diff --git a/lua/go/utils.lua b/lua/go/utils.lua index 5bb3df6..6044359 100644 --- a/lua/go/utils.lua +++ b/lua/go/utils.lua @@ -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 ";" diff --git a/lua/tests/go_coverage_spec.lua b/lua/tests/go_coverage_spec.lua index 6cb5be3..70d518a 100644 --- a/lua/tests/go_coverage_spec.lua +++ b/lua/tests/go_coverage_spec.lua @@ -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()