extend simple test listing to a list-detail-view window

This commit is contained in:
2025-12-27 22:33:46 +01:00
parent 519fc38769
commit 60960322ac
4 changed files with 1108 additions and 20 deletions

View File

@@ -452,6 +452,115 @@ function M.new(opts)
return { passes = passes, failures = failures, skips = skips, display = display }
end
local function split_output_lines(text)
if not text or text == "" then
return {}
end
local lines = vim.split(text, "\n", { plain = true })
if #lines > 0 and lines[#lines] == "" then
table.remove(lines, #lines)
end
return lines
end
local function append_output(out, name, text)
if not name or name == "" or not text or text == "" then
return
end
if not out[name] then
out[name] = {}
end
for _, line in ipairs(split_output_lines(text)) do
table.insert(out[name], line)
end
end
local function decode_json_from_output(output)
local ok, data = pcall(vim.json.decode, output)
if ok and type(data) == "table" then
return data
end
if not output or output == "" then
return nil
end
local start_idx = output:find("{", 1, true)
if not start_idx then
return nil
end
local end_idx = nil
for i = #output, start_idx, -1 do
if output:sub(i, i) == "}" then
end_idx = i
break
end
end
if not end_idx then
return nil
end
local candidate = output:sub(start_idx, end_idx)
local ok2, data2 = pcall(vim.json.decode, candidate)
if ok2 and type(data2) == "table" then
return data2
end
return nil
end
local function collect_jest_failure_output(data)
local out = {}
for _, result in ipairs(data.testResults or {}) do
for _, assertion in ipairs(result.assertionResults or {}) do
if assertion.status == "failed" then
local name = assertion.fullName or assertion.title
for _, msg in ipairs(assertion.failureMessages or {}) do
append_output(out, name, msg)
end
end
end
end
return out
end
local function collect_mocha_failure_output(output)
local out = {}
local ok, data = pcall(vim.json.decode, output)
if ok and type(data) == "table" then
for _, failure in ipairs(data.failures or {}) do
local name = failure.fullTitle or failure.title
local err = failure.err or failure
local message = nil
if type(err) == "table" then
message = err.stack or err.message
elseif type(err) == "string" then
message = err
end
append_output(out, name, message)
end
return out
end
for line in (output or ""):gmatch("[^\n]+") do
local ok_line, entry = pcall(vim.json.decode, line)
if ok_line and type(entry) == "table" then
local event = entry.event or entry[1] or entry["1"]
local payload = entry
if not entry.event then
payload = entry[2] or entry["2"] or {}
end
if event == "fail" then
local name = payload.fullTitle or payload.title
local err = payload.err or payload
local message = nil
if type(err) == "table" then
message = err.stack or err.message
elseif type(err) == "string" then
message = err
end
append_output(out, name, message)
end
end
end
return out
end
local function parse_jest_like(output)
local ok, data = pcall(vim.json.decode, output)
if ok and type(data) == "table" then
@@ -648,6 +757,17 @@ function M.new(opts)
return parse_output(output)
end
function runner.parse_test_output(output)
if runner.framework == "mocha" then
return collect_mocha_failure_output(output)
end
local data = decode_json_from_output(output)
if not data then
return {}
end
return collect_jest_failure_output(data)
end
function runner.output_parser()
local state = { raw = {}, done = false, saw_stream = false }
local failures = {}