ginkgo nearest

pull/209/head
ray-x 2 years ago
parent f366151590
commit eee5a02a37

@ -1,19 +1,19 @@
-- gonkgo test
local M = {}
local utils = require("go.utils")
local utils = require('go.utils')
local log = utils.log
local vfn = vim.fn
local long_opts = {
verbose = "v",
compile = "c",
tags = "t",
bench = "b",
select = "s",
floaterm = "F",
verbose = 'v',
compile = 'c',
tags = 't',
bench = 'b',
select = 's',
floaterm = 'F',
}
local getopt = require("go.alt_getopt")
local short_opts = "vct:bsF"
local getopt = require('go.alt_getopt')
local short_opts = 'vct:bsF'
local function get_build_tags(args)
local tags = {}
@ -23,15 +23,15 @@ local function get_build_tags(args)
table.insert(tags, optarg['t'])
end
if _GO_NVIM_CFG.build_tags ~= "" then
if _GO_NVIM_CFG.build_tags ~= '' then
table.insert(tags, _GO_NVIM_CFG.build_tags)
end
if #tags == 0 then
return ""
return ''
end
return [[-tags=]] .. table.concat(tags, ",")
return [[-tags=]] .. table.concat(tags, ',')
end
local function find_describe(lines)
@ -42,7 +42,42 @@ local function find_describe(lines)
local line = lines[i]
local fs, fe = string.find(line, pat)
if fs then
line = string.sub(line, fs + #"Describe", fe)
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)
fs, fe = string.find(line, despat)
if fs ~= nil then
if fe - fs <= 2 then
@ -65,7 +100,7 @@ M.test_func = function(...)
local args = { ... }
log(args)
local optarg = getopt.get_opts(args, short_opts, long_opts)
local fpath = vfn.expand("%:p:h")
local fpath = vfn.expand('%:p:h')
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row, col = row, col + 1
@ -77,19 +112,19 @@ M.test_func = function(...)
local lines = vim.api.nvim_buf_get_lines(0, fnum, row + 1, true)
local describe = find_describe(lines)
log("testing: ", describe)
log('testing: ', describe)
if describe == nil then
log("failed to find test function, test file instead")
log('failed to find test function, test file instead')
return M.test_file(args)
end
local test_runner = "ginkgo"
require("go.install").install(test_runner)
local test_runner = 'ginkgo'
require('go.install').install(test_runner)
-- local cmd = { test_runner, [[ --focus=']] .. describe .. [[']], get_build_tags(args), fpath }
local cmd = { test_runner, [[ --focus-file=']] .. describe .. [[']], get_build_tags(args), fpath }
log(cmd)
if _GO_NVIM_CFG.run_in_floaterm then
local term = require("go.term").run
local term = require('go.term').run
term({ cmd = cmd, autoclose = false })
return
end
@ -97,8 +132,8 @@ M.test_func = function(...)
vim.cmd(cmd)
args = { [[ --focus-file=']] .. describe .. [[']], get_build_tags(args), fpath }
require("go.asyncmake").make(unpack(args))
utils.log("test cmd", cmd)
require('go.asyncmake').make(unpack(args))
utils.log('test cmd', cmd)
return true
end
@ -106,17 +141,17 @@ M.test_file = function(...)
local args = { ... }
log(args)
-- require sed
local fpath = vfn.expand("%:p:h")
local fname = vfn.expand("%:p")
local fpath = vfn.expand('%:p:h')
local fname = vfn.expand('%:p')
log(fpath, fname)
local workfolder = utils.work_path()
fname = "." .. fname:sub(#workfolder + 1)
fname = '.' .. fname:sub(#workfolder + 1)
log(workfolder, fname)
local test_runner = "ginkgo"
require("go.install").install(test_runner)
local test_runner = 'ginkgo'
require('go.install').install(test_runner)
local cmd_args = {
-- [[--regexScansFilePath=true]], v1
@ -129,15 +164,15 @@ M.test_file = function(...)
if _GO_NVIM_CFG.run_in_floaterm then
table.insert(cmd_args, 1, test_runner)
utils.log(args)
local term = require("go.term").run
local term = require('go.term').run
term({ cmd = cmd_args, autoclose = false })
return
end
fname = utils.relative_to_cwd(fname) .. [[\ ]]
vim.cmd("setl makeprg=ginkgo")
utils.log("test cmd", cmd_args)
require("go.asyncmake").make(unpack(cmd_args))
vim.cmd('setl makeprg=ginkgo')
utils.log('test cmd', cmd_args)
require('go.asyncmake').make(unpack(cmd_args))
end
return M

Loading…
Cancel
Save