2021-11-23 23:13:40 +00:00
|
|
|
-- gonkgo test
|
|
|
|
local M = {}
|
2022-08-25 02:54:57 +00:00
|
|
|
local utils = require('go.utils')
|
2021-11-23 23:13:40 +00:00
|
|
|
local log = utils.log
|
2022-06-01 11:29:13 +00:00
|
|
|
local vfn = vim.fn
|
2022-05-30 14:04:52 +00:00
|
|
|
local long_opts = {
|
2022-08-25 02:54:57 +00:00
|
|
|
verbose = 'v',
|
|
|
|
compile = 'c',
|
|
|
|
tags = 't',
|
|
|
|
bench = 'b',
|
|
|
|
select = 's',
|
|
|
|
floaterm = 'F',
|
2022-05-30 14:04:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 02:54:57 +00:00
|
|
|
local getopt = require('go.alt_getopt')
|
|
|
|
local short_opts = 'vct:bsF'
|
2022-05-30 14:04:52 +00:00
|
|
|
|
2021-11-23 23:13:40 +00:00
|
|
|
local function get_build_tags(args)
|
|
|
|
local tags = {}
|
|
|
|
|
2022-06-01 11:29:13 +00:00
|
|
|
local optarg = getopt.get_opts(args, short_opts, long_opts)
|
2022-05-30 14:04:52 +00:00
|
|
|
if optarg['t'] then
|
|
|
|
table.insert(tags, optarg['t'])
|
2021-11-23 23:13:40 +00:00
|
|
|
end
|
|
|
|
|
2022-08-25 02:54:57 +00:00
|
|
|
if _GO_NVIM_CFG.build_tags ~= '' then
|
2021-11-23 23:13:40 +00:00
|
|
|
table.insert(tags, _GO_NVIM_CFG.build_tags)
|
|
|
|
end
|
|
|
|
|
|
|
|
if #tags == 0 then
|
2022-08-25 02:54:57 +00:00
|
|
|
return ''
|
2021-11-23 23:13:40 +00:00
|
|
|
end
|
|
|
|
|
2022-08-25 02:54:57 +00:00
|
|
|
return [[-tags=]] .. table.concat(tags, ',')
|
2021-11-23 23:13:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function find_describe(lines)
|
|
|
|
local describe
|
|
|
|
local pat = [[Describe%(%".*%",%sfunc]]
|
|
|
|
local despat = [[%(%".*%"]]
|
|
|
|
for i = #lines, 1, -1 do
|
|
|
|
local line = lines[i]
|
|
|
|
local fs, fe = string.find(line, pat)
|
|
|
|
if fs then
|
2022-08-25 02:54:57 +00:00
|
|
|
line = string.sub(line, fs + #'Describe', fe)
|
|
|
|
fs, fe = string.find(line, despat)
|
|
|
|
if fs ~= nil then
|
|
|
|
if fe - fs <= 2 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
describe = line:sub(fs + 2, fe - 1)
|
|
|
|
return describe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- It
|
|
|
|
pat = [[It%(%".*%",%sfunc]]
|
|
|
|
for i = #lines, 1, -1 do
|
|
|
|
local line = lines[i]
|
|
|
|
local fs, fe = string.find(line, pat)
|
|
|
|
if fs then
|
|
|
|
line = string.sub(line, fs + #'It', fe)
|
|
|
|
fs, fe = string.find(line, despat)
|
|
|
|
if fs ~= nil then
|
|
|
|
if fe - fs <= 2 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
describe = line:sub(fs + 2, fe - 1)
|
|
|
|
return describe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
pat = [[Context%(%".*%",%sfunc]]
|
|
|
|
for i = #lines, 1, -1 do
|
|
|
|
local line = lines[i]
|
|
|
|
local fs, fe = string.find(line, pat)
|
|
|
|
if fs then
|
|
|
|
line = string.sub(line, fs + #'Context', fe)
|
2021-11-23 23:13:40 +00:00
|
|
|
fs, fe = string.find(line, despat)
|
|
|
|
if fs ~= nil then
|
|
|
|
if fe - fs <= 2 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
describe = line:sub(fs + 2, fe - 1)
|
|
|
|
return describe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- print(find_describe({
|
|
|
|
-- [[ var _ = Describe("Integration test hourly data EstimateCombinedRules without ws ID", func() { ]]
|
|
|
|
-- }))
|
|
|
|
|
|
|
|
-- Run with ginkgo Description
|
2022-05-29 13:07:39 +00:00
|
|
|
M.test_func = function(...)
|
2021-12-16 06:46:52 +00:00
|
|
|
local args = { ... }
|
2021-11-23 23:13:40 +00:00
|
|
|
log(args)
|
2022-06-01 11:29:13 +00:00
|
|
|
local optarg = getopt.get_opts(args, short_opts, long_opts)
|
2022-08-25 02:54:57 +00:00
|
|
|
local fpath = vfn.expand('%:p:h')
|
2021-12-16 06:46:52 +00:00
|
|
|
|
2021-11-23 23:13:40 +00:00
|
|
|
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
|
|
|
row, col = row, col + 1
|
|
|
|
|
|
|
|
local fnum = row - 3
|
|
|
|
if fnum < 0 then
|
|
|
|
fnum = 0
|
|
|
|
end
|
|
|
|
local lines = vim.api.nvim_buf_get_lines(0, fnum, row + 1, true)
|
|
|
|
|
|
|
|
local describe = find_describe(lines)
|
2022-08-25 02:54:57 +00:00
|
|
|
log('testing: ', describe)
|
2021-11-23 23:13:40 +00:00
|
|
|
if describe == nil then
|
2022-08-25 02:54:57 +00:00
|
|
|
log('failed to find test function, test file instead')
|
2021-11-23 23:13:40 +00:00
|
|
|
return M.test_file(args)
|
|
|
|
end
|
2022-08-25 02:54:57 +00:00
|
|
|
local test_runner = 'ginkgo'
|
|
|
|
require('go.install').install(test_runner)
|
2021-11-23 23:13:40 +00:00
|
|
|
|
2022-06-15 04:11:10 +00:00
|
|
|
-- local cmd = { test_runner, [[ --focus=']] .. describe .. [[']], get_build_tags(args), fpath }
|
|
|
|
local cmd = { test_runner, [[ --focus-file=']] .. describe .. [[']], get_build_tags(args), fpath }
|
2021-12-16 06:46:52 +00:00
|
|
|
log(cmd)
|
2021-11-23 23:13:40 +00:00
|
|
|
if _GO_NVIM_CFG.run_in_floaterm then
|
2022-08-25 02:54:57 +00:00
|
|
|
local term = require('go.term').run
|
2021-12-16 06:46:52 +00:00
|
|
|
term({ cmd = cmd, autoclose = false })
|
2021-11-23 23:13:40 +00:00
|
|
|
return
|
|
|
|
end
|
2022-05-30 14:04:52 +00:00
|
|
|
cmd = [[setl makeprg=]] .. test_runner
|
2021-11-23 23:13:40 +00:00
|
|
|
vim.cmd(cmd)
|
2021-12-16 06:46:52 +00:00
|
|
|
|
2022-06-15 04:11:10 +00:00
|
|
|
args = { [[ --focus-file=']] .. describe .. [[']], get_build_tags(args), fpath }
|
2022-08-25 02:54:57 +00:00
|
|
|
require('go.asyncmake').make(unpack(args))
|
|
|
|
utils.log('test cmd', cmd)
|
2021-11-23 23:13:40 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
M.test_file = function(...)
|
2021-12-16 06:46:52 +00:00
|
|
|
local args = { ... }
|
2021-11-23 23:13:40 +00:00
|
|
|
log(args)
|
|
|
|
-- require sed
|
2022-08-25 02:54:57 +00:00
|
|
|
local fpath = vfn.expand('%:p:h')
|
|
|
|
local fname = vfn.expand('%:p')
|
2021-11-23 23:13:40 +00:00
|
|
|
|
|
|
|
log(fpath, fname)
|
|
|
|
|
2021-12-16 07:15:21 +00:00
|
|
|
local workfolder = utils.work_path()
|
2022-08-25 02:54:57 +00:00
|
|
|
fname = '.' .. fname:sub(#workfolder + 1)
|
2021-11-23 23:13:40 +00:00
|
|
|
|
|
|
|
log(workfolder, fname)
|
2022-08-25 02:54:57 +00:00
|
|
|
local test_runner = 'ginkgo'
|
|
|
|
require('go.install').install(test_runner)
|
2021-11-23 23:13:40 +00:00
|
|
|
|
2021-12-16 06:46:52 +00:00
|
|
|
local cmd_args = {
|
2022-06-15 04:11:10 +00:00
|
|
|
-- [[--regexScansFilePath=true]], v1
|
2021-12-16 06:46:52 +00:00
|
|
|
get_build_tags(args),
|
2022-06-15 04:11:10 +00:00
|
|
|
[[ --focus-file= ]],
|
2021-12-16 06:46:52 +00:00
|
|
|
fname,
|
|
|
|
fpath,
|
|
|
|
}
|
|
|
|
|
2021-11-23 23:13:40 +00:00
|
|
|
if _GO_NVIM_CFG.run_in_floaterm then
|
2021-12-16 06:46:52 +00:00
|
|
|
table.insert(cmd_args, 1, test_runner)
|
|
|
|
utils.log(args)
|
2022-08-25 02:54:57 +00:00
|
|
|
local term = require('go.term').run
|
2021-12-16 06:46:52 +00:00
|
|
|
term({ cmd = cmd_args, autoclose = false })
|
2021-11-23 23:13:40 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
fname = utils.relative_to_cwd(fname) .. [[\ ]]
|
2022-08-25 02:54:57 +00:00
|
|
|
vim.cmd('setl makeprg=ginkgo')
|
|
|
|
utils.log('test cmd', cmd_args)
|
|
|
|
require('go.asyncmake').make(unpack(cmd_args))
|
2021-11-23 23:13:40 +00:00
|
|
|
end
|
|
|
|
|
2023-03-18 09:46:44 +00:00
|
|
|
M.run = function(opts)
|
|
|
|
local test_runner = 'ginkgo'
|
|
|
|
require('go.install').install(test_runner)
|
|
|
|
local cmd = { test_runner }
|
|
|
|
|
|
|
|
local cwd
|
|
|
|
local args = {}
|
|
|
|
|
|
|
|
if opts[1] then
|
|
|
|
cmd = { test_runner, opts[1] }
|
|
|
|
if opts[1] == 'bootstrap' then
|
|
|
|
|
|
|
|
if not opts[2] then
|
|
|
|
cwd = vim.fn.expand('%:p:h')
|
|
|
|
args = {cwd = cwd}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if opts[2] then
|
|
|
|
cmd = { test_runner, opts[1], opts[2] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
cwd = vim.fn.expand('%:p:h')
|
|
|
|
local runner = require('go.runner')
|
|
|
|
runner.run(cmd, {}, args)
|
|
|
|
end
|
|
|
|
|
2021-11-23 23:13:40 +00:00
|
|
|
return M
|