[feature] go coverage partially tested, skip covered 🎄🎆

This commit is contained in:
ray-x 2022-12-28 11:46:31 +11:00
parent 02fbdd126c
commit 17adc5c600
4 changed files with 109 additions and 100 deletions

View File

@ -93,7 +93,9 @@ _GO_NVIM_CFG = {
gopls_cmd = nil, --- you can provide gopls path and cmd if it not in PATH, e.g. cmd = { "/home/ray/.local/nvim/data/lspinstall/go/gopls" }
gopls_remote_auto = true,
gocoverage_sign = "",
sign_covered_hl = "String", --- highlight group for test covered sign, you can either
gocoverage_skip_covered = false,
sign_covered_hl = "String", --- highlight group for test covered sign
sign_partial_hl = "WarningMsg", --- highlight group for test partically covered sign
sign_uncovered_hl = "Error", -- highlight group for uncovered code
launch_json = nil, -- the launch.json file path, default to .vscode/launch.json
-- launch_json = vfn.getcwd() .. "/.vscode/launch.json",

View File

@ -9,12 +9,13 @@ local visible = false
-- _GO_NVIM_CFG = _GO_NVIM_CFG or {}
local sign_define_cache = {}
M.sign_map = { covered = 'goCoverageCovered', uncover = 'goCoverageUncovered' }
M.sign_map = { covered = 'goCoverageCovered', uncover = 'goCoverageUncovered', partial = 'goCoveragePartial' }
local ns = 'gocoverage_ns'
local sign_covered = M.sign_map.covered
local sign_uncover = M.sign_map.uncover
local sign_partial = M.sign_map.partial
local function sign_get(bufnr, name)
if sign_define_cache[bufnr] == nil then
@ -79,24 +80,33 @@ end
function M.add(bufnr, signs)
local to_place = {}
local placed = {}
for _, s in ipairs(signs or {}) do
local covered = s.covered
local sign_group = 'goCoverageCovered'
local sign_name = 'goCoverageCovered'
if covered == 0 then
sign_group = 'goCoverageUncovered'
sign_name = 'goCoverageUncovered'
end
M.define(bufnr, sign_group, { text = _GO_NVIM_CFG.gocoverage_sign, texthl = sign_group })
M.define(bufnr, sign_name, { text = _GO_NVIM_CFG.gocoverage_sign, texthl = sign_name })
for lnum = s.range.start.line, s.range['end'].line do
log(lnum, covered, bufnr) --verbose
to_place[#to_place + 1] = {
id = lnum,
group = ns,
name = sign_group,
buffer = bufnr,
lnum = lnum,
priority = _GO_NVIM_CFG.sign_priority,
}
local sg = sign_name
if placed[lnum] then
sg = 'goCoveragePartial'
end
log(lnum, covered, sign_name, bufnr) --verbose
if (covered == 1 and not _GO_NVIM_CFG.gocoverage_skip_covered) or covered == 0 then
to_place[#to_place + 1] = {
id = lnum,
group = ns,
name = sg,
buffer = bufnr,
lnum = lnum,
priority = _GO_NVIM_CFG.sign_priority,
}
end
placed[lnum] = true
end
end
@ -106,7 +116,17 @@ function M.add(bufnr, signs)
end
M.highlight = function()
for _, sign_name in pairs(M.sign_map) do
if vim.tbl_isempty(vfn.sign_getdefined(sign_name)) then
vfn.sign_define(sign_name, {
text = _GO_NVIM_CFG.gocoverage_sign,
texthl = sign_name,
})
end
end
vim.api.nvim_set_hl(0, 'goCoverageCovered', { link = _GO_NVIM_CFG.sign_covered_hl, default = true })
vim.api.nvim_set_hl(0, 'goCoveragePartial', { link = _GO_NVIM_CFG.sign_partial_hl, default = true })
vim.api.nvim_set_hl(0, 'goCoverageUncovered', { link = _GO_NVIM_CFG.sign_uncovered_hl, default = true })
end
@ -184,20 +204,6 @@ local function parse_line(line)
}
end
if vim.tbl_isempty(vfn.sign_getdefined(sign_covered)) then
vfn.sign_define(sign_covered, {
text = _GO_NVIM_CFG.gocoverage_sign,
texthl = 'goCoverageCovered',
})
end
if vim.tbl_isempty(vfn.sign_getdefined(sign_uncover)) then
vfn.sign_define(sign_uncover, {
text = _GO_NVIM_CFG.gocoverage_sign,
texthl = 'goCoverageUncovered',
})
end
M.read_cov = function(covfn)
local total_lines = 0
local total_covered = 0

View File

@ -30,6 +30,8 @@ describe("should read coveragefile", function()
eq(result[n].file_lines, 9)
eq(result[n].file_covered, 4)
range = {['end'] = {character = 13, line = 11}, start = {character = 2, line = 11}}
eq(result[n][3].range, range)
eq(result.total_lines, 9)
eq(result.total_covered, 4)
-- eq(result[n][1], "github.com/go.nvim/branch.go")

View File

@ -1,164 +1,163 @@
local eq = assert.are.same
local cur_dir = vim.fn.expand("%:p:h")
local busted = require("plenary/busted")
local cur_dir = vim.fn.expand('%:p:h')
local busted = require('plenary/busted')
describe("should run gofmt", function()
describe('should run gofmt', function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
require("plenary.reload").reload_module("go.nvim")
it("should run fmt", function()
local name = vim.fn.tempname() .. ".go"
print("tmp:" .. name)
require('plenary.reload').reload_module('go.nvim')
it('should run fmt', function()
local name = vim.fn.tempname() .. '.go'
print('tmp:' .. name)
--
local path = cur_dir .. "/lua/tests/fixtures/fmt/hello.go" -- %:p:h ? %:p
print("test:" .. path)
local path = cur_dir .. '/lua/tests/fixtures/fmt/hello.go' -- %:p:h ? %:p
print('test:' .. path)
local lines = vim.fn.readfile(path)
vim.fn.writefile(lines, name)
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/hello_golden.go"), "\n")
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. '/lua/tests/fixtures/fmt/hello_golden.go'), '\n')
local cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
local l = vim.api.nvim_buf_get_lines(0, 0, -1, true)
print("buf read: " .. vim.inspect(l))
print('buf read: ' .. vim.inspect(l))
vim.bo.filetype = "go"
vim.bo.filetype = 'go'
print("exp:" .. vim.inspect(expected))
print("tmp" .. name)
print('exp:' .. vim.inspect(expected))
print('tmp' .. name)
local gofmt = require("go.format")
local gofmt = require('go.format')
gofmt.gofmt()
-- enable the channel response
vim.wait(400, function() end)
local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
print("fmt" .. fmt)
local fmt = vim.fn.join(vim.fn.readfile(name), '\n')
print('fmt' .. fmt)
vim.fn.assert_equal(fmt, expected)
eq(expected, fmt)
cmd = "bd! " .. name
cmd = 'bd! ' .. name
vim.cmd(cmd)
end)
it("should run fmt sending from buffer", function()
local name = vim.fn.tempname() .. ".go"
print("tmp:" .. name)
it('should run fmt sending from buffer', function()
local name = vim.fn.tempname() .. '.go'
print('tmp:' .. name)
--
local path = cur_dir .. "/lua/tests/fixtures/fmt/hello.go" -- %:p:h ? %:p
print("test:" .. path)
local path = cur_dir .. '/lua/tests/fixtures/fmt/hello.go' -- %:p:h ? %:p
print('test:' .. path)
local lines = vim.fn.readfile(path)
vim.fn.writefile(lines, name)
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/hello_golden.go"), "\n")
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. '/lua/tests/fixtures/fmt/hello_golden.go'), '\n')
local cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
local l = vim.api.nvim_buf_get_lines(0, 0, -1, true)
print("buf read: " .. vim.inspect(l))
print('buf read: ' .. vim.inspect(l))
vim.bo.filetype = "go"
vim.bo.filetype = 'go'
print("exp:" .. vim.inspect(expected))
print("tmp" .. name)
print('exp:' .. vim.inspect(expected))
print('tmp' .. name)
local gofmt = require("go.format")
local gofmt = require('go.format')
gofmt.gofmt()
-- enable the channel response
vim.wait(400, function() end)
local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
print("fmt" .. fmt)
local fmt = vim.fn.join(vim.fn.readfile(name), '\n')
print('fmt' .. fmt)
vim.fn.assert_equal(fmt, expected)
eq(expected, fmt)
cmd = "bd! " .. name
cmd = 'bd! ' .. name
vim.cmd(cmd)
end)
it("should run import from file", function()
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports.go" -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/goimports_golden.go"), "\n")
local name = vim.fn.tempname() .. ".go"
it('should run import from file', function()
local path = cur_dir .. '/lua/tests/fixtures/fmt/goimports.go' -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. '/lua/tests/fixtures/fmt/goimports_golden.go'), '\n')
local name = vim.fn.tempname() .. '.go'
print(name)
local lines = vim.fn.readfile(path)
vim.fn.writefile(lines, name)
local cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
require("go").setup({ goimport = "goimports" })
require('go').setup({ goimport = 'goimports' })
vim.cmd([[cd %:p:h]])
require("go.format").goimport()
print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
vim.wait(400, function() end)
local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
require('go.format').goimport()
print('workspaces:', vim.inspect(vim.lsp.buf.list_workspace_folders()))
vim.wait(500, function() end)
local fmt = vim.fn.join(vim.fn.readfile(name), '\n')
eq(expected, fmt)
cmd = "bd! " .. name
cmd = 'bd! ' .. name
vim.cmd(cmd)
end)
it("should run import from file with goimport with package name", function()
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports.go" -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/goimports_golden.go"), "\n")
local name = vim.fn.tempname() .. ".go"
it('should run import from file with goimport with package name', function()
local path = cur_dir .. '/lua/tests/fixtures/fmt/goimports.go' -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. '/lua/tests/fixtures/fmt/goimports_golden.go'), '\n')
local name = vim.fn.tempname() .. '.go'
print(name)
local lines = vim.fn.readfile(path)
local cmd = " silent exe 'e " .. name .. "'"
vim.fn.writefile(lines, name)
vim.cmd(cmd)
vim.cmd([[cd %:p:h]])
print("code write to " .. name)
print('code write to ' .. name)
require("go").setup({ goimport = "goimports", gofmt = "gofmt" })
local gofmt = require("go.format")
gofmt.goimport("fmt")
require('go').setup({ goimport = 'goimports', gofmt = 'gofmt' })
local gofmt = require('go.format')
gofmt.goimport('fmt')
vim.wait(400, function() end)
vim.cmd([[w]])
local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
local fmt = vim.fn.join(vim.fn.readfile(name), '\n')
print(fmt)
eq(expected, fmt)
end)
it("should run import from file with gopls", function()
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports2.go" -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/goimports2_golden.go"), "\n")
_GO_NVIM_CFG.goimport = "gopls"
it('should run import from file with gopls', function()
local path = cur_dir .. '/lua/tests/fixtures/fmt/goimports2.go' -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. '/lua/tests/fixtures/fmt/goimports2_golden.go'), '\n')
local cmd = " silent exe 'e " .. path .. "'"
vim.cmd(cmd)
vim.cmd([[cd %:p:h]])
vim.cmd([[packadd go.nvim]])
require("go").setup({ goimport = "gopls", lsp_cfg = true })
vim.wait(2000, function() end)
require('go').setup({ goimport = 'gopls', lsp_cfg = true })
_GO_NVIM_CFG.goimport = 'gopls'
vim.wait(1000, function() end)
require("go.format").goimport()
require('go.format').goimport()
print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
vim.wait(500, function() end)
print('workspaces:', vim.inspect(vim.lsp.buf.list_workspace_folders()))
vim.wait(700, function() end)
vim.cmd([[w]])
local fmt = vim.fn.join(vim.fn.readfile(path), "\n")
local fmt = vim.fn.join(vim.fn.readfile(path), '\n')
print(vim.inspect(fmt))
eq(expected, fmt)
eq(1, 1) -- still not working
cmd = "bd! " .. path
cmd = 'bd! ' .. path
vim.cmd(cmd)
end)
it("should run import from file with gopls", function()
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports3.go" -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. "/lua/tests/fixtures/fmt/goimports3_golden.go"), "\n")
it('should run import from file with gopls', function()
local path = cur_dir .. '/lua/tests/fixtures/fmt/goimports3.go' -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir .. '/lua/tests/fixtures/fmt/goimports3_golden.go'), '\n')
_GO_NVIM_CFG.goimport = "gopls"
_GO_NVIM_CFG.goimport = 'gopls'
local cmd = " silent exe 'e " .. path .. "'"
vim.cmd(cmd)
vim.cmd([[cd %:p:h]])
vim.cmd([[packadd go.nvim]])
require("go").setup({ goimport = "gopls", lsp_cfg = true })
require('go').setup({ goimport = 'gopls', lsp_cfg = true })
vim.wait(2000, function() end)
require("go.format").goimport()
require('go.format').goimport()
print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
print('workspaces:', vim.inspect(vim.lsp.buf.list_workspace_folders()))
vim.wait(500, function() end)
vim.cmd([[w]])
local fmt = vim.fn.join(vim.fn.readfile(path), "\n")
local fmt = vim.fn.join(vim.fn.readfile(path), '\n')
print(vim.inspect(fmt))
eq(expected, fmt)
eq(1, 1) -- still not working
cmd = "bd! " .. path
cmd = 'bd! ' .. path
vim.cmd(cmd)
end)
end)