issue #75, better default package path for tests

pull/77/head
ray-x 2 years ago
parent ff9a34bffb
commit 85f2300e72

@ -0,0 +1,200 @@
-- Copyright (c) 2009 Aleksey Cheusov <vle@gmx.net>
-- Copyright info:
-- https://github.com/cheusov/lua-alt-getopt/blob/f495c21d6a203ab280603aa5799e636fb5651ae7/alt_getopt.lua#L3-L20
-- updated for neovim lua JIT by ray-x
local type, pairs, ipairs, io, os = type, pairs, ipairs, io, os
alt_getopt = {}
local function convert_short2long(opts)
local ret = {}
for short_opt, accept_arg in opts:gmatch("(%w)(:?)") do
ret[short_opt] = #accept_arg
end
return ret
end
local function err_unknown_opt(opt)
vim.notify("Unknown option `-" .. (#opt > 1 and "-" or "") .. opt, vim.lsp.log_levels.ERROR)
end
local function canonize(options, opt)
if not options[opt] then
err_unknown_opt(opt)
end
while type(options[opt]) == "string" do
opt = options[opt]
if not options[opt] then
err_unknown_opt(opt)
end
end
return opt
end
function alt_getopt.get_ordered_opts(arg, sh_opts, long_opts)
local i = 1
local count = 1
local opts = {}
local optarg = {}
local options = convert_short2long(sh_opts)
for k, v in pairs(long_opts) do
options[k] = v
end
while i <= #arg do
local a = arg[i]
if a == "--" then
i = i + 1
break
elseif a == "-" then
break
elseif a:sub(1, 2) == "--" then
local pos = a:find("=", 1, true)
if pos then
local opt = a:sub(3, pos - 1)
opt = canonize(options, opt)
if options[opt] == 0 then
vim.notify("Bad usage of option `" .. a, vim.lsp.log_levels.ERROR)
end
optarg[count] = a:sub(pos + 1)
opts[count] = opt
else
local opt = a:sub(3)
opt = canonize(options, opt)
if options[opt] == 0 then
opts[count] = opt
else
if i == #arg then
vim.notify("Missed value for option `" .. a, vim.lsp.log_levels.ERROR)
return
end
optarg[count] = arg[i + 1]
opts[count] = opt
i = i + 1
end
end
count = count + 1
elseif a:sub(1, 1) == "-" then
for j = 2, a:len() do
local opt = canonize(options, a:sub(j, j))
if options[opt] == 0 then
opts[count] = opt
count = count + 1
elseif a:len() == j then
if i == #arg then
vim.notify("Missed value for option `-" .. opt, vim.lsp.log_levels.ERROR)
end
optarg[count] = arg[i + 1]
opts[count] = opt
i = i + 1
count = count + 1
break
else
optarg[count] = a:sub(j + 1)
opts[count] = opt
count = count + 1
break
end
end
else
break
end
i = i + 1
end
return opts, i, optarg
end
function alt_getopt.get_opts(arg, sh_opts, long_opts)
local ret = {}
local opts, optind, optarg = alt_getopt.get_ordered_opts(arg, sh_opts, long_opts)
for i, v in ipairs(opts) do
if optarg[i] then
ret[v] = optarg[i]
else
ret[v] = 1
end
end
return ret, optind
end
function test_arg(arg)
local long_opts = {
verbose = "v",
help = "h",
test = "t",
stop = "s",
restart = "r",
-- fake = 0,
-- len = 1,
tags = "g",
output = "o",
set_value = "S",
["set-output"] = "o",
}
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)
print("ordered opts")
vim.inspect(opts)
vim.inspect(optind)
vim.inspect(optarg)
print("ordered opts end")
for i, v in ipairs(opts) do
if optarg[i] then
print("option `" .. v .. "': " .. vim.inspect(optarg[i]))
else
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")
end
table.sort(fin_options)
-- -- print(table.concat(fin_options))
--
-- -- for i = optind, #arg do
-- -- print(string.format("ARGV [%s] = %s\n", i, arg[i]))
-- -- end
end
-- test_arg()
--
-- print("test 2")
--
-- test_arg({ "--tags", "unit,integration", "--restart" })
return alt_getopt

@ -149,6 +149,7 @@ function M.make(...)
end
end
log("cmd ", cmd)
local job_id = vim.fn.jobstart(cmd, {
on_stderr = on_event,
on_stdout = on_event,

@ -152,6 +152,9 @@ M.test_fun = function(...)
if empty(ns) then
return false
end
local repath = utils.rel_path() or ""
local package = repath .. utils.sep() .. "..."
local tags, args2 = get_build_tags(args)
local argsstr = ""
@ -179,7 +182,7 @@ M.test_fun = function(...)
if not empty(args2) then
cmd = vim.list_extend(cmd, args2)
else
argsstr = "." .. utils.sep() .. "..."
argsstr = package
table.insert(cmd, argsstr)
end
@ -216,11 +219,12 @@ M.test_file = function(...)
-- local testcases = [[sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\\|/g']]
local fpath = vim.fn.expand("%:p")
-- utils.log(args)
local cmd = [[cat ]] .. fpath .. [[| sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\\|/g']]
local cmd = [[cat ]] .. fpath .. [[| sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\|/g']]
-- TODO maybe with treesitter or lsp list all functions in current file and regex with Test
local tests = vim.fn.systemlist(cmd)[1]
local tests = vim.fn.systemlist(cmd)
utils.log(cmd, tests)
if empty(tests) then
tests = tests[1]
if vim.fn.empty(tests) == 1 then
vim.notify("no test found fallback to package test", vim.lsp.log_levels.DEBUG)
M.test_package(...)
return
@ -265,7 +269,7 @@ M.test_file = function(...)
vim.cmd([[setl makeprg=]] .. _GO_NVIM_CFG.go .. [[\ test]])
require("go.asyncmake").make(unpack(cmd_args))
utils.log("test cmd", cmd)
utils.log("test cmd: ", cmd, " finished")
end
return M

@ -209,12 +209,9 @@ util.log = function(...)
return
end
local arg = { ... }
local log_default = string.format(
"%s%s%s.log",
vim.api.nvim_call_function("stdpath", { "data" }),
util.sep(),
"gonvim"
)
local cache_dir = vim.fn.stdpath("cache")
local log_default = string.format("%s%sgonvim.log", cache_dir, util.sep())
local log_path = _GO_NVIM_CFG.log_path or log_default
local str = ""
@ -456,4 +453,16 @@ function util.empty(t)
return next(t) == nil
end
local open = io.open
function util.read_file(path)
local file = open(path, "rb") -- r read mode and b binary mode
if not file then
return nil
end
local content = file:read("*a") -- *a or *all reads the whole file
file:close()
return content
end
return util

Loading…
Cancel
Save