GoTestSum and sidepanel

pull/227/head
ray-x 2 years ago
parent b7f07ab887
commit 5b33d782f3

@ -215,6 +215,7 @@ first run of `GoFmt` may fail. It is recommended to run `GoInstallBinaries` to i
| GoRun {args} | e.g. GoRun equal to `go run .`; or `GoRun ./cmd` equal to `go run ./cmd, Additional args: -F run in floaterm` |
| GoStop {job_id} | `stop the job started with GoRun` |
| GoTest | go test ./... |
| GoTestSum {pkgname} | run gotestsum and show result in side panel |
| GoTest -c | go test -c current_file_path |
| GoTest -n | test nearest, see GoTestFunc |
| GoTest -f | test current file, see GoTestFile |

@ -187,6 +187,9 @@ COMMANDS *go-nvim-commands*
-n {count}: disable cache -count={count}
package_name: test package
:GoTestSum {packagename} *:GoTestSum*
package_name: test package
:GoTestFile {-t tagname} *:GoTestFile*
Test current file

@ -172,6 +172,10 @@ return {
require('go.gotest').test(unpack(opts.fargs))
end, { complete = package.loaded.go.package_complete, nargs = '*' })
create_cmd('GoTestSum', function(opts)
require('go.gotestsum').run(unpack(opts.fargs))
end, { complete = package.loaded.go.package_complete, nargs = '*' })
create_cmd('GoCoverage', function(opts)
require('go.coverage').run(unpack(opts.fargs))
end, { complete = package.loaded.go.package_complete, nargs = '*' })

@ -1,11 +1,16 @@
local runner = require("go.runner")
local utils = require("go.utils")
local runner = require('go.runner')
local utils = require('go.utils')
local M = {}
local util = require('go.utils')
local log = util.log
local vfn = vim.fn
local api = vim.api
function M.watch(args)
args = args or {}
local cmd = { "gotestsum", "--watch" }
local cmd = { 'gotestsum', '--watch' }
vim.list_extend(cmd, args)
local opts = {
@ -20,4 +25,83 @@ function M.watch(args)
return cmd, opts
end
local test_result = {}
local test_panel
local show_panel = function()
local panel = util.load_plugin('guihua.lua', 'guihua.panel')
if not panel then
vim.notify('guihua not installed')
return
end
if test_panel == nil then
test_panel = panel:new({
header = '   go test  ',
render = function(buf)
-- log(test_result)
return test_result
end,
})
test_panel:open(true)
else
test_panel:redraw(false)
end
return test_panel
end
local function handle_data_out(_, data, ev)
data = util.handle_job_data(data)
if not data then
return
end
local get_fname_num = utils.get_fname_num
for _, val in ipairs(data) do
-- first strip the filename
local item = {}
-- item.lnum = 1
item.text = val
item.node_text = val
local fname, lnum = get_fname_num(val)
if fname then
local bnr = vfn.bufnr(fname, true)
item.bufnr = bnr
item.lnum = lnum
if bnr > 0 then
item.uri = vim.uri_from_bufnr(bnr)
end
item.fname = fname
item.lnum = lnum
end
-- item.filename = fname
table.insert(test_result, item)
log(item)
end
return show_panel()
end
function M.run(...)
if not require('go.install').install('gotestsum') then
util.warn('please wait for gotstssum to be installed and re-run the command')
return
end
local args = { ... }
test_result = {}
log(debug.traceback())
local cmd = { 'gotestsum', unpack(args) }
vfn.jobstart(cmd, {
on_stdout = handle_data_out,
on_exit = function(e, data, _)
if data ~= 0 then
log('no packege info data ' .. e .. tostring(data))
return
end
-- show_panel()
end,
})
return test_result
end
return M

@ -131,6 +131,17 @@ function util.interface_list(pkg)
return ifaces
end
-- https://alpha2phi.medium.com/neovim-101-regular-expression-f15a6d782add
function util.get_fname_num(line)
line = util.trim(line)
local reg = [[\(.\+\.go\)\:\(\d\+\):]]
local f = fn.matchlist(line, reg)
if f[1] and f[1] ~= '' then
return f[2], tonumber(f[3])
end
end
function util.smartrun()
local cmd
if util.has_main() then
@ -544,15 +555,15 @@ function util.restart(cmd_args)
local old_lsp_clients = vim.lsp.get_active_clients()
local configs = require('lspconfig.configs')
for _, client in pairs(old_lsp_clients) do
if client.name=="gopls" then
vim.lsp.stop_client(client.id)
end
if client.name == 'gopls' then
vim.lsp.stop_client(client.id)
end
end
if configs['gopls']~=nil then
if configs['gopls'] ~= nil then
vim.defer_fn(function()
configs['gopls'].launch()
end,500)
configs['gopls'].launch()
end, 500)
end
end

@ -0,0 +1,38 @@
local eq = assert.are.same
local busted = require('plenary/busted')
local cur_dir = vim.fn.expand('%:p:h')
describe('should get file name and number ', function()
require('plenary.reload').reload_module('go.nvim')
require('go').setup({
verbose = true,
trace = true,
lsp_cfg = true,
log_path = vim.fn.expand('$HOME') .. '/tmp/gonvim.log',
})
local utils = require('go.utils')
it('should get file name and number in windows', function()
local f2, f3 = utils.get_fname_num(
"C:\\Users\\user\\go\\src\\github.com\\user\\project\\main.go:12:2: expected declaration, found 'IDENT' main"
)
eq(f2, 'C:\\Users\\user\\go\\src\\github.com\\user\\project\\main.go')
eq(f3, 12)
end)
it('should get file name and number in windows', function()
local f2, f3 = utils.get_fname_num(
"C:\\Users\\user\\go\\src\\github.com\\user\\project\\main.go:12: expected declaration, found 'IDENT' main"
)
eq(f2, 'C:\\Users\\user\\go\\src\\github.com\\user\\project\\main.go')
eq(f3, 12)
end)
it('should get file name and number in linux', function()
local f2, f3 = utils.get_fname_num(
"/home/user/go/src/github.com/user/project/main.go:12:2: expected declaration, found 'IDENT' main"
)
eq(f2, '/home/user/go/src/github.com/user/project/main.go')
eq(f3, 12)
end)
end)
Loading…
Cancel
Save