refactor(gotests): use new runner, clean code

This commit is contained in:
Smirnov Oleksandr 2023-08-09 12:03:45 +03:00
parent 6c2f2749ca
commit 16b9a5c8f4
2 changed files with 14 additions and 35 deletions

View file

@ -1,19 +1,17 @@
local c = require("gopher.config").commands local c = require("gopher.config").commands
local u = require "gopher._utils"
local ts_utils = require "gopher._utils.ts" local ts_utils = require "gopher._utils.ts"
local r = require "gopher._utils.runner" local r = require "gopher._utils.runner"
local gotests = {} local gotests = {}
---@param args table ---@param args table
local function add_test(args) local function add_test(args)
local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
table.insert(args, "-w") table.insert(args, "-w")
table.insert(args, fpath) table.insert(args, vim.fn.expand "%")
return r.spawn(c.gotests, { return r.sync(c.gotests, {
args = args, args = args,
on_exit = function(ok, data) on_exit = function(data, status)
if not ok then if not status == 0 then
vim.notify("gotests failed: " .. data, vim.log.levels.ERROR) vim.notify("gotests failed: " .. data, vim.log.levels.ERROR)
return return
end end
@ -24,43 +22,24 @@ local function add_test(args)
end end
---generate unit test for one function ---generate unit test for one function
---@param parallel boolean function gotests.func_test()
function gotests.func_test(parallel)
local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil or ns.name == nil then if ns == nil or ns.name == nil then
u.deferred_notify("cursor on func/method and execute the command again", vim.log.levels.INFO) vim.notify("cursor on func/method and execute the command again", vim.log.levels.INFO)
return return
end end
local cmd_args = { "-only", ns.name } add_test { "-only", ns.name }
if parallel then
table.insert(cmd_args, "-parallel")
end
add_test(cmd_args)
end end
---generate unit tests for all functions in current file ---generate unit tests for all functions in current file
---@param parallel boolean function gotests.all_tests()
function gotests.all_tests(parallel) add_test { "-all" }
local cmd_args = { "-all" }
if parallel then
table.insert(cmd_args, "-parallel")
end
add_test(cmd_args)
end end
---generate unit tests for all exported functions ---generate unit tests for all exported functions
---@param parallel boolean function gotests.all_exported_tests()
function gotests.all_exported_tests(parallel) add_test { "-exported" }
local cmd_args = {}
if parallel then
table.insert(cmd_args, "-parallel")
end
table.insert(cmd_args, "-exported")
add_test(cmd_args)
end end
return gotests return gotests

View file

@ -1,8 +1,8 @@
command! -nargs=* GoTagAdd :lua require"gopher".tags_add(<f-args>) command! -nargs=* GoTagAdd :lua require"gopher".tags_add(<f-args>)
command! -nargs=* GoTagRm :lua require"gopher".tags_rm(<f-args>) command! -nargs=* GoTagRm :lua require"gopher".tags_rm(<f-args>)
command! -nargs=* GoTestAdd :lua require"gopher".test_add(<f-args>) command! GoTestAdd :lua require"gopher".test_add()
command! -nargs=* GoTestsAll :lua require"gopher".tests_all(<f-args>) command! GoTestsAll :lua require"gopher".tests_all()
command! -nargs=* GoTestsExp :lua require"gopher".test_exported(<f-args>) command! GoTestsExp :lua require"gopher".test_exported()
command! -nargs=* GoMod :lua require"gopher".mod(<f-args>) command! -nargs=* GoMod :lua require"gopher".mod(<f-args>)
command! -nargs=* GoGet :lua require"gopher".get(<f-args>) command! -nargs=* GoGet :lua require"gopher".get(<f-args>)
command! -nargs=* GoWork :lua require"gopher".work(<f-args>) command! -nargs=* GoWork :lua require"gopher".work(<f-args>)