add TSamFailedOnly command and change test output within the floating window

This commit is contained in:
2025-12-25 16:40:50 +01:00
parent cbc3e201ae
commit 1e2e881acd
17 changed files with 1074 additions and 448 deletions

View File

@@ -106,6 +106,18 @@ local function build_pkg_arg(spec)
return "./" .. rel
end
local function collect_unique(list)
local out = {}
local seen = {}
for _, item in ipairs(list) do
if item and item ~= "" and not seen[item] then
seen[item] = true
table.insert(out, item)
end
end
return out
end
function runner.is_test_file(bufnr)
local path = util.get_buf_path(bufnr)
if not path or path == "" then
@@ -170,7 +182,7 @@ end
function runner.build_command(spec)
local pattern = build_run_pattern(spec)
local pkg = build_pkg_arg(spec)
local cmd = { "go", "test", "-v", pkg, "-run", pattern }
local cmd = { "go", "test", "-json", pkg, "-run", pattern }
return {
cmd = cmd,
cwd = spec.cwd,
@@ -188,7 +200,7 @@ function runner.build_file_command(bufnr)
end
local spec = { file = path, cwd = root }
local pkg = build_pkg_arg(spec)
local cmd = { "go", "test", "-v", pkg }
local cmd = { "go", "test", "-json", pkg }
return {
cmd = cmd,
cwd = root,
@@ -204,11 +216,125 @@ function runner.build_all_command(bufnr)
if not root or root == "" then
root = vim.loop.cwd()
end
local cmd = { "go", "test", "-v", "./..." }
local cmd = { "go", "test", "-json", "./..." }
return {
cmd = cmd,
cwd = root,
}
end
function runner.parse_results(output)
if not output or output == "" then
return { passes = {}, failures = {}, skips = {} }
end
local passes = {}
local failures = {}
local skips = {}
for line in output:gmatch("[^\n]+") do
local ok, data = pcall(vim.json.decode, line)
if ok and type(data) == "table" then
if data.Test and data.Test ~= "" then
if data.Action == "pass" then
table.insert(passes, data.Test)
elseif data.Action == "fail" then
table.insert(failures, data.Test)
elseif data.Action == "skip" then
table.insert(skips, data.Test)
end
end
end
end
return {
passes = collect_unique(passes),
failures = collect_unique(failures),
skips = collect_unique(skips),
}
end
function runner.output_parser()
local seen_pass = {}
local seen_fail = {}
local failures = {}
local passes = {}
local skips = {}
return {
on_line = function(line, _state)
local ok, data = pcall(vim.json.decode, line)
if not ok or type(data) ~= "table" then
return nil
end
local name = data.Test
if not name or name == "" then
return nil
end
if data.Action == "pass" and not seen_pass[name] then
seen_pass[name] = true
table.insert(passes, name)
return {
passes = { name },
failures = {},
skips = {},
failures_all = vim.deepcopy(failures),
}
elseif data.Action == "fail" and not seen_fail[name] then
seen_fail[name] = true
table.insert(failures, name)
return {
passes = {},
failures = { name },
skips = {},
failures_all = vim.deepcopy(failures),
}
elseif data.Action == "skip" and not seen_pass[name] then
seen_pass[name] = true
table.insert(skips, name)
return {
passes = {},
failures = {},
skips = { name },
failures_all = vim.deepcopy(failures),
}
end
return nil
end,
on_complete = function(_output, _state)
return nil
end,
}
end
function runner.build_failed_command(last_command, failures, _scope_kind)
if not last_command or type(last_command.cmd) ~= "table" then
return nil
end
local pattern_parts = {}
for _, name in ipairs(failures or {}) do
table.insert(pattern_parts, escape_go_regex(name))
end
if #pattern_parts == 0 then
return nil
end
local pattern = "^(" .. table.concat(pattern_parts, "|") .. ")$"
local cmd = {}
local skip_next = false
for _, arg in ipairs(last_command.cmd) do
if skip_next then
skip_next = false
elseif arg == "-run" then
skip_next = true
else
table.insert(cmd, arg)
end
end
table.insert(cmd, "-run")
table.insert(cmd, pattern)
return {
cmd = cmd,
cwd = last_command.cwd,
}
end
return runner

View File

@@ -4,4 +4,5 @@ return js.new({
name = "js-jest",
framework = "jest",
command = { "npx", "jest" },
json_args = { "--json" },
})

View File

@@ -5,4 +5,5 @@ return js.new({
framework = "mocha",
command = { "npx", "mocha" },
all_glob = "test/**/*.test.js",
json_args = { "--reporter", "json-stream" },
})

View File

@@ -4,4 +4,5 @@ return js.new({
name = "js-vitest",
framework = "vitest",
command = { "npx", "vitest" },
json_args = { "--reporter", "tap-flat" },
})

View File

@@ -28,6 +28,48 @@ local function is_js_test_file(bufnr, filetypes, patterns)
return false
end
local function append_args(cmd, args)
if not args or #args == 0 then
return
end
for _, arg in ipairs(args) do
table.insert(cmd, arg)
end
end
local function escape_regex(s)
s = s or ""
return (s:gsub("([\\.^$|()%%[%]{}*+?%-])", "\\\\%1"))
end
local function build_pattern(names)
local parts = {}
for _, name in ipairs(names or {}) do
if name and name ~= "" then
table.insert(parts, escape_regex(name))
end
end
if #parts == 0 then
return nil
end
return table.concat(parts, "|")
end
local function find_test_file_arg(cmd)
if not cmd then
return nil
end
for i = #cmd, 1, -1 do
local arg = cmd[i]
if type(arg) == "string" and arg:sub(1, 1) ~= "-" then
if arg:match("%.test%.[jt]sx?$") or arg:match("%.spec%.[jt]sx?$") then
return arg
end
end
end
return nil
end
local function find_block_end(lines, start_idx)
local depth = 0
local started = false
@@ -221,6 +263,8 @@ function M.new(opts)
runner.framework = cfg.framework or "jest"
runner.command = cfg.command or { "npx", runner.framework }
runner.all_glob = cfg.all_glob
runner.json_args = cfg.json_args or {}
runner.failed_only_flag = cfg.failed_only_flag
runner.filetypes = {}
if cfg.filetypes then
@@ -271,6 +315,7 @@ function M.new(opts)
local function build_jest(spec)
local cmd = vim.deepcopy(runner.command)
append_args(cmd, runner.json_args)
table.insert(cmd, spec.file)
table.insert(cmd, "-t")
table.insert(cmd, spec.test_name)
@@ -279,6 +324,7 @@ function M.new(opts)
local function build_mocha(spec)
local cmd = vim.deepcopy(runner.command)
append_args(cmd, runner.json_args)
local target = spec.full_name or spec.test_name
table.insert(cmd, "--fgrep")
table.insert(cmd, target)
@@ -288,6 +334,7 @@ function M.new(opts)
local function build_vitest(spec)
local cmd = vim.deepcopy(runner.command)
append_args(cmd, runner.json_args)
table.insert(cmd, spec.file)
table.insert(cmd, "-t")
table.insert(cmd, spec.test_name)
@@ -332,6 +379,7 @@ function M.new(opts)
})
end
local cmd = vim.deepcopy(runner.command)
append_args(cmd, runner.json_args)
table.insert(cmd, path)
return {
cmd = cmd,
@@ -360,6 +408,7 @@ function M.new(opts)
root = vim.loop.cwd()
end
local cmd = vim.deepcopy(runner.command)
append_args(cmd, runner.json_args)
if runner.framework == "mocha" and runner.all_glob then
table.insert(cmd, runner.all_glob)
end
@@ -369,6 +418,226 @@ function M.new(opts)
}
end
local function parse_jest_like(output)
local ok, data = pcall(vim.json.decode, output)
if not ok or type(data) ~= "table" then
return nil
end
local passes = {}
local failures = {}
local skips = {}
for _, result in ipairs(data.testResults or {}) do
for _, assertion in ipairs(result.assertionResults or {}) do
local title = assertion.fullName or assertion.title
if assertion.status == "passed" and title then
table.insert(passes, title)
elseif assertion.status == "failed" and title then
table.insert(failures, title)
elseif (assertion.status == "pending" or assertion.status == "skipped" or assertion.status == "todo")
and title then
table.insert(skips, title)
end
end
end
return { passes = passes, failures = failures, skips = skips }
end
local function parse_mocha(output)
local ok, data = pcall(vim.json.decode, output)
if not ok or type(data) ~= "table" then
return nil
end
local passes = {}
local failures = {}
local skips = {}
if type(data.tests) == "table" then
for _, test in ipairs(data.tests) do
local title = test.fullTitle or test.title
if test.state == "passed" and title then
table.insert(passes, title)
elseif test.state == "failed" and title then
table.insert(failures, title)
elseif test.state == "pending" and title then
table.insert(skips, title)
end
end
elseif type(data.passes) == "table" or type(data.failures) == "table" then
for _, test in ipairs(data.passes or {}) do
local title = test.fullTitle or test.title
if title then
table.insert(passes, title)
end
end
for _, test in ipairs(data.failures or {}) do
local title = test.fullTitle or test.title
if title then
table.insert(failures, title)
end
end
for _, test in ipairs(data.pending or {}) do
local title = test.fullTitle or test.title
if title then
table.insert(skips, title)
end
end
end
return { passes = passes, failures = failures, skips = skips }
end
local function parse_output(output)
if runner.framework == "mocha" then
return parse_mocha(output)
end
return parse_jest_like(output)
end
function runner.parse_results(output)
return parse_output(output)
end
function runner.output_parser()
local state = { raw = {}, done = false }
local failures = {}
local skips = {}
return {
on_line = function(line, _state)
if state.done then
return nil
end
if runner.framework == "mocha" and runner.json_args then
local uses_stream = false
for i = 1, #runner.json_args - 1 do
if runner.json_args[i] == "--reporter" and runner.json_args[i + 1] == "json-stream" then
uses_stream = true
break
end
end
if uses_stream then
local ok, data = pcall(vim.json.decode, line)
if ok and type(data) == "table" and data.event then
if data.event == "pass" and data.fullTitle then
return {
passes = { data.fullTitle },
failures = {},
skips = {},
failures_all = vim.deepcopy(failures),
}
elseif data.event == "fail" and data.fullTitle then
table.insert(failures, data.fullTitle)
return {
passes = {},
failures = { data.fullTitle },
skips = {},
failures_all = vim.deepcopy(failures),
}
elseif data.event == "pending" and data.fullTitle then
table.insert(skips, data.fullTitle)
return {
passes = {},
failures = {},
skips = { data.fullTitle },
failures_all = vim.deepcopy(failures),
}
end
end
return nil
end
end
if runner.framework == "vitest" and runner.json_args then
local uses_tap = false
for i = 1, #runner.json_args - 1 do
if runner.json_args[i] == "--reporter" and (runner.json_args[i + 1] == "tap" or runner.json_args[i + 1] == "tap-flat") then
uses_tap = true
break
end
end
if uses_tap then
local ok_title = line:match("^ok%s+%d+%s+%-%s+(.+)")
if ok_title then
local skip_title = ok_title:match("^(.-)%s+#%s+SKIP.*$")
if skip_title then
skip_title = skip_title:gsub("%s+$", "")
table.insert(skips, skip_title)
return {
passes = {},
failures = {},
skips = { skip_title },
failures_all = vim.deepcopy(failures),
}
end
ok_title = ok_title:gsub("%s+#%s+time=.*$", "")
return {
passes = { ok_title },
failures = {},
skips = {},
failures_all = vim.deepcopy(failures),
}
end
local fail_title = line:match("^not ok%s+%d+%s+%-%s+(.+)")
if fail_title then
fail_title = fail_title:gsub("%s+#%s+time=.*$", "")
table.insert(failures, fail_title)
return {
passes = {},
failures = { fail_title },
skips = {},
failures_all = vim.deepcopy(failures),
}
end
return nil
end
end
table.insert(state.raw, line)
local output = table.concat(state.raw, "\n")
local results = parse_output(output)
if results then
state.done = true
end
return results
end,
on_complete = function(output, _state)
if state.done then
return nil
end
local results = parse_output(output)
if results then
state.done = true
end
return results
end,
}
end
function runner.build_failed_command(last_command, failures, scope_kind)
local pattern = build_pattern(failures)
if not pattern then
return nil
end
local cmd = vim.deepcopy(runner.command)
append_args(cmd, runner.json_args)
if runner.framework == "mocha" then
table.insert(cmd, "--grep")
table.insert(cmd, pattern)
else
table.insert(cmd, "-t")
table.insert(cmd, pattern)
end
if scope_kind ~= "all" and last_command then
local file = find_test_file_arg(last_command.cmd)
if file then
table.insert(cmd, file)
end
end
return {
cmd = cmd,
cwd = last_command and last_command.cwd or nil,
}
end
return runner
end

View File

@@ -1,240 +0,0 @@
local util = require("test-samurai.util")
local runner = {
name = "lua-plenary",
framework = "plenary",
}
local function is_lua_test_path(path)
if not path or path == "" then
return false
end
if not path:match("%.lua$") then
return false
end
if path:match("_spec%.lua$") then
return true
end
if path:match("/tests/") then
return true
end
return false
end
function runner.is_test_file(bufnr)
local path = util.get_buf_path(bufnr)
return is_lua_test_path(path)
end
local function find_repo_root(file)
local root = util.find_root(file, { ".git", "tests", "lua", "plugin" })
if not root or root == "" then
root = vim.loop.cwd()
end
return root
end
local function minimal_init_for(root)
return vim.fs.joinpath(root, "tests", "minimal_init.lua")
end
local function escape_for_ex_double_quotes(s)
s = s or ""
s = s:gsub("\\", "\\\\")
s = s:gsub('"', '\\"')
return s
end
local function count_keyword(line, kw)
local c = 0
local pat = "%f[%w]" .. kw .. "%f[%W]"
for _ in line:gmatch(pat) do
c = c + 1
end
return c
end
local function find_end_lua_block(lines, start_idx)
local depth = 0
local started = false
for i = start_idx, #lines do
local line = lines[i]
local fnc = count_keyword(line, "function")
local endc = count_keyword(line, "end")
if fnc > 0 then
depth = depth + fnc
started = true
end
if started and endc > 0 then
depth = depth - endc
if depth <= 0 then
return i - 1
end
end
end
return #lines - 1
end
local function collect_lua_structs(lines)
local describes = {}
local tests = {}
for i, line in ipairs(lines) do
local dname = line:match("^%s*describe%s*%(%s*['\"`]([^'\"`]+)['\"`]")
if dname then
local start0 = i - 1
local end0 = find_end_lua_block(lines, i)
table.insert(describes, { kind = "describe", name = dname, start = start0, ["end"] = end0 })
else
local tname = line:match("^%s*it%s*%(%s*['\"`]([^'\"`]+)['\"`]")
if tname then
local start0 = i - 1
local end0 = find_end_lua_block(lines, i)
table.insert(tests, { kind = "it", name = tname, start = start0, ["end"] = end0 })
end
end
end
return describes, tests
end
local function build_full_name(lines, idx, leaf_name)
local parts = { leaf_name }
for i = idx - 1, 1, -1 do
local line = lines[i]
local dname = line:match("^%s*describe%s*%(%s*['\"`]([^'\"`]+)['\"`]")
if dname then
table.insert(parts, 1, dname)
end
end
return table.concat(parts, " ")
end
function runner.find_nearest(bufnr, row, _col)
if not runner.is_test_file(bufnr) then
return nil, "not a lua test file"
end
local lines = util.get_buf_lines(bufnr)
local describes, tests = collect_lua_structs(lines)
for _, t in ipairs(tests) do
if row >= t.start and row <= t["end"] then
local full = build_full_name(lines, t.start + 1, t.name)
local file = util.get_buf_path(bufnr)
local root = find_repo_root(file)
return { file = file, cwd = root, test_name = t.name, full_name = full, kind = "it" }
end
end
local best_describe = nil
for _, d in ipairs(describes) do
if row >= d.start and row <= d["end"] then
if not best_describe or d.start >= best_describe.start then
best_describe = d
end
end
end
if best_describe then
local full = build_full_name(lines, best_describe.start + 1, best_describe.name)
local file = util.get_buf_path(bufnr)
local root = find_repo_root(file)
return { file = file, cwd = root, test_name = best_describe.name, full_name = full, kind = "describe" }
end
local start = row + 1
if start > #lines then
start = #lines
elseif start < 1 then
start = 1
end
for i = start, 1, -1 do
local line = lines[i]
local tname = line:match("^%s*it%s*%(%s*['\"`]([^'\"`]+)['\"`]")
if tname then
local full = build_full_name(lines, i, tname)
local file = util.get_buf_path(bufnr)
local root = find_repo_root(file)
return { file = file, cwd = root, test_name = tname, full_name = full, kind = "it" }
end
local dname = line:match("^%s*describe%s*%(%s*['\"`]([^'\"`]+)['\"`]")
if dname then
local full = build_full_name(lines, i, dname)
local file = util.get_buf_path(bufnr)
local root = find_repo_root(file)
return { file = file, cwd = root, test_name = dname, full_name = full, kind = "describe" }
end
end
return nil, "no test call found"
end
local function ex_plenary_busted_file(file, filter_name)
local ex = "PlenaryBustedFile " .. vim.fn.fnameescape(file)
if filter_name and filter_name ~= "" then
local f = escape_for_ex_double_quotes(filter_name)
ex = ex .. ' { busted_args = { "--filter", "' .. f .. '" } }'
end
return ex
end
function runner.build_command(spec)
local root = spec and spec.cwd or vim.loop.cwd()
local minit = minimal_init_for(root)
local cmd = {
"nvim",
"--headless",
"-u",
minit,
"-c",
ex_plenary_busted_file(spec.file, spec.test_name),
"-c",
"qa",
}
return { cmd = cmd, cwd = root }
end
function runner.build_file_command(bufnr)
local file = util.get_buf_path(bufnr)
if not file or file == "" then
return nil
end
local root = find_repo_root(file)
local minit = minimal_init_for(root)
local cmd = {
"nvim",
"--headless",
"-u",
minit,
"-c",
ex_plenary_busted_file(file, nil),
"-c",
"qa",
}
return { cmd = cmd, cwd = root }
end
function runner.build_all_command(bufnr)
local file = util.get_buf_path(bufnr)
local root = find_repo_root(file)
local minit = minimal_init_for(root)
local cmd = {
"nvim",
"--headless",
"-u",
minit,
"-c",
"PlenaryBustedDirectory tests",
"-c",
"qa",
}
return { cmd = cmd, cwd = root }
end
return runner