Add GoTest -c option, compile test package issue#76

pull/77/head
ray-x 2 years ago
parent e9e42ab175
commit e5ebbd9460

@ -188,6 +188,7 @@ first run of `GoFmt` may fail. It is recommended to run `GoInstallBinaries` to i
| GoGenerate | |
| GoRun | e.g. GoRun equal to `go run .`; or `GoRun ./cmd` equal to `go run ./cmd` |
| GoTest | go test ./... |
| GoTest -c | go test -c current_file_path |
| GoTest -tags=yourtags | go test ./... -tags=yourtags |
| GoTest package_path -tags=yourtags | go test packagepath -tags=yourtags |
| GoTest package_path -tags=yourtags other_args | go test packagepath -tags=yourtags other_args |

@ -170,8 +170,11 @@ COMMANDS *go-nvim-commands*
:GoBuild {-tags=tagname}{pakcage_name} *:GoBuild*
Build current package
:GoTest {-tags=tagname}{pakcage_name} *:GoTest*
Test package
:GoTest {-c} {-tags=tagname} {pakcage_name} *:GoTest*
-c: compile test in current package
-tags: compile tag
package_name: test package
:GoTestFile {-tags=tagname} *:GoTestFile*
Test current file

@ -19,7 +19,7 @@ local function convert_short2long(opts)
end
local function err_unknown_opt(opt)
vim.notify("Unknown option `-" .. (#opt > 1 and "-" or "") .. opt, vim.lsp.log_levels.ERROR)
-- vim.notify("Unknown option `-" .. (#opt > 1 and "-" or "") .. opt, vim.lsp.log_levels.INFO)
end
local function canonize(options, opt)
@ -156,8 +156,8 @@ function test_arg(arg)
local opts
local optarg
local optind
arg = arg or { "-t", "-r", "-g", "unit,integration" }
opts, optind, optarg = alt_getopt.get_ordered_opts(arg, "g:hvo:n:rS:st", long_opts)
arg = arg or { "-t", "-r", "-c", "-g", "unit,integration" }
opts, optind, optarg = alt_getopt.get_ordered_opts(arg, "cg:hvo:n:rS:st", long_opts)
print("ordered opts")
vim.inspect(opts)
@ -166,21 +166,17 @@ function test_arg(arg)
print("ordered opts end")
for i, v in ipairs(opts) do
if optarg[i] then
print("option `" .. v .. "': " .. vim.inspect(optarg[i]))
print("option " .. v .. ": " .. vim.inspect(optarg[i]))
else
print("option `" .. v)
print("option " .. v)
end
end
optarg, optind = alt_getopt.get_opts(arg, "g:hVvo:n:rS:st", long_opts)
print("opts")
vim.inspect(optarg)
vim.inspect(optind)
print("opts")
local fin_options = {}
for k, v in pairs(optarg) do
table.insert(fin_options, "fin-option `" .. k .. "': " .. vim.inspect(v) .. "\n")
table.insert(fin_options, "fin-option " .. k .. ": " .. vim.inspect(v) .. "\n")
end
table.sort(fin_options)
@ -196,5 +192,6 @@ end
-- print("test 2")
--
-- test_arg({ "--tags", "unit,integration", "--restart" })
-- test_arg({ "--tags", "unit,integration", "-c", "--restart" })
return alt_getopt

@ -3,6 +3,12 @@ local utils = require("go.utils")
local log = utils.log
local empty = utils.empty
local ginkgo = require("go.ginkgo")
local getopt = require("go.alt_getopt")
local long_opts = {
verbose = "v",
compile = "c",
}
M.efm = function()
local indent = [[%\\%( %\\)]]
@ -46,6 +52,7 @@ local function get_build_tags(args)
for i, value in pairs(args) do
if value:find("-tags") then
log("f:", value:find("-tags"))
table.insert(tags, value)
table.remove(args, i)
break
@ -56,7 +63,7 @@ end
M.get_build_tags = get_build_tags
local function run_test(path, args)
local function run_test(path, args, compile)
log(args)
local test_runner = _GO_NVIM_CFG.go
if _GO_NVIM_CFG.test_runner ~= test_runner then
@ -66,6 +73,7 @@ local function run_test(path, args)
local tags, args2 = get_build_tags(args)
log(tags, args2)
local cmd
if _GO_NVIM_CFG.run_in_floaterm then
cmd = { test_runner, "test", "-v" }
@ -79,13 +87,19 @@ local function run_test(path, args)
cmd = vim.list_extend(cmd, args2)
end
if path ~= "" then
table.insert(cmd, path)
if compile == true then
if path ~= "" then
table.insert(cmd, path)
end
else
local argsstr = "." .. utils.sep() .. "..."
cmd = table.insert(cmd, argsstr)
if path ~= "" then
table.insert(cmd, path)
else
local argsstr = "." .. utils.sep() .. "..."
cmd = table.insert(cmd, argsstr)
end
end
utils.log(cmd)
utils.log(cmd, args, args2)
if _GO_NVIM_CFG.run_in_floaterm then
local term = require("go.term").run
term({ cmd = cmd, autoclose = false })
@ -102,13 +116,22 @@ M.test = function(...)
local args = { ... }
log(args)
vim.fn.setqflist({})
local compile
local workfolder = utils.work_path()
if workfolder == nil then
workfolder = "."
end
local fpath = workfolder .. utils.sep() .. "..."
utils.log("fpath :" .. fpath)
run_test(fpath, args)
local optarg, optind = getopt.get_opts(args, "c", long_opts)
if optarg["c"] then
fpath = vim.fn.expand("%:p:h")
compile = true
end
run_test(fpath, args, compile)
end
M.test_suit = function(...)

Loading…
Cancel
Save