Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neotest diagnostics #44

Merged
merged 5 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lua/neotest-rust/errors.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local M = {}

--- Parse errors from rustc output
---@param output string
---@return neotest.Error[]
function M.parse_errors(output)
local message, line = output:match("thread '[^']+' panicked at '([^']+)', [^:]+:(%d+):%d+")

-- If we can't parse the output, return an empty table
if message == nil then
return {}
end

-- Note: we have to return the line index, not the line number
return {
{ line = tonumber(line) - 1, message = message },
}
end

return M
10 changes: 8 additions & 2 deletions lua/neotest-rust/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local async = require("neotest.async")
local context_manager = require("plenary.context_manager")
local dap = require("neotest-rust.dap")
local util = require("neotest-rust.util")
local errors = require("neotest-rust.errors")
local Job = require("plenary.job")
local open = context_manager.open
local Path = require("plenary.path")
Expand Down Expand Up @@ -371,9 +372,12 @@ function adapter.results(spec, result, tree)
end
for _, testcase in pairs(testcases) do
if testcase.failure then
local output = testcase.failure[1]

results[testcase._attr.name] = {
status = "failed",
short = testcase.failure[1],
short = output,
errors = errors.parse_errors(output),
}
else
results[testcase._attr.name] = {
Expand All @@ -385,9 +389,11 @@ function adapter.results(spec, result, tree)
elseif spec.context.strategy == "dap" and util.file_exists(output_path) then
results = dap.translate_results(output_path)
else
local output = result.output

results[spec.context.position_id] = {
status = "failed",
output = result.output,
output = output,
}
end

Expand Down
30 changes: 30 additions & 0 deletions tests/errors_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local errors = require("neotest-rust.errors")

describe("parses errors from output", function()
it("non-parsable errors in output", function()
local output = "something\nwent\nwrong"
local results = errors.parse_errors(output)

assert.is_true(#results == 0)
end)

it("assert_eq", function()
local output = "test tests::failed_math ... FAILED\n"
.. "failures:\n\n"
.. "---- tests::failed_math stdout ----\n"
.. "thread 'tests::failed_math' panicked at 'assertion failed: `(left == right)`\n"
.. " left: `2`,\n"
.. " right: `3`', src/main.rs:16:9\n"
.. "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"

local results = errors.parse_errors(output)
local expected = {
{
line = 15,
message = "assertion failed: `(left == right)`\n left: `2`,\n right: `3`",
},
}

assert.are.same(expected, results)
end)
end)
8 changes: 8 additions & 0 deletions tests/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,12 @@ describe("results", function()
["foo::tests::should_fail"] = {
short = "thread 'foo::tests::should_fail' panicked at 'assertion failed: false', src/foo.rs:10:9\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace",
status = "failed",
errors = {
{
line = 9,
message = "assertion failed: false",
},
},
},
["foo::tests::should_pass"] = {
status = "passed",
Expand All @@ -990,13 +996,15 @@ describe("results", function()
["foo::tests::should_fail"] = {
short = "thread 'foo::tests::should_fail' panicked at 'assertion failed: false', src/foo.rs:10:9\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace",
status = "failed",
errors = { { line = 9, message = "assertion failed: false" } },
},
["foo::tests::should_pass"] = {
status = "passed",
},
should_fail = {
short = "thread 'should_fail' panicked at 'assertion failed: false', tests/tests.rs:8:5\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace",
status = "failed",
errors = { { line = 7, message = "assertion failed: false" } },
},
should_pass = {
status = "passed",
Expand Down