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