Skip to content

Commit 8df46c8

Browse files
author
Marc Jakobi
committed
feat(lsp): improved parsing of test result error messages
1 parent 13d4e81 commit 8df46c8

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ All notable changes to this project will be documented in this file.
66
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

9+
## [4.3.0] - 2024-01-31
10+
11+
### Changed
12+
13+
- LSP: Improved parsing of test result failure messages.
14+
915
## [4.2.1] - 2024-01-30
1016

1117
### Fixed

lua/rustaceanvim/executors/background.lua

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,47 @@ end
1111
local M = {}
1212

1313
---@package
14+
---@param file_name string
1415
---@param output string
1516
---@return vim.Diagnostic[]
1617
---@diagnostic disable-next-line: inject-field
17-
M.parse_diagnostics = function(output)
18+
M.parse_diagnostics = function(file_name, output)
19+
output = output:gsub('\r\n', '\n')
20+
local lines = vim.split(output, '\n')
1821
---@type vim.Diagnostic[]
1922
local diagnostics = {}
20-
for line, col, message in output:gmatch("thread '[^']+' panicked at [^:]+:(%d+):(%d+):\n([^\n]*)") do
21-
diagnostics[#diagnostics + 1] = {
22-
lnum = tonumber(line) - 1,
23-
col = tonumber(col) or 0,
24-
message = message,
25-
source = 'rustaceanvim',
26-
severity = vim.diagnostic.severity.ERROR,
27-
}
28-
end
29-
if #diagnostics == 0 then
30-
--- Fall back to old format
31-
for message, line, col in output:gmatch("thread '[^']+' panicked at '([^']+)', [^:]+:(%d+):(%d+)") do
32-
diagnostics[#diagnostics + 1] = {
33-
lnum = tonumber(line) - 1,
23+
for i, line in ipairs(lines) do
24+
local message = ''
25+
local file, lnum, col = line:match("thread '[^']+' panicked at ([^:]+):(%d+):(%d+):")
26+
if lnum and col and message and vim.endswith(file_name, file) then
27+
local next_i = i + 1
28+
while #lines >= next_i and lines[next_i] ~= '' do
29+
message = message .. lines[next_i] .. '\n'
30+
next_i = next_i + 1
31+
end
32+
local diagnostic = {
33+
lnum = tonumber(lnum) - 1,
3434
col = tonumber(col) or 0,
3535
message = message,
3636
source = 'rustaceanvim',
3737
severity = vim.diagnostic.severity.ERROR,
3838
}
39+
table.insert(diagnostics, diagnostic)
40+
end
41+
end
42+
if #diagnostics == 0 then
43+
--- Fall back to old format
44+
for message, file, lnum, col in output:gmatch("thread '[^']+' panicked at '([^']+)', ([^:]+):(%d+):(%d+)") do
45+
if vim.endswith(file_name, file) then
46+
local diagnostic = {
47+
lnum = tonumber(lnum) - 1,
48+
col = tonumber(col) or 0,
49+
message = message,
50+
source = 'rustaceanvim',
51+
severity = vim.diagnostic.severity.ERROR,
52+
}
53+
table.insert(diagnostics, diagnostic)
54+
end
3955
end
4056
end
4157
return diagnostics
@@ -56,6 +72,7 @@ M.execute_command = function(command, args, cwd, opts)
5672
local notify_prefix = (is_single_test and 'test ' or 'tests ')
5773
local compat = require('rustaceanvim.compat')
5874
local cmd = vim.list_extend({ command }, args)
75+
local fname = vim.api.nvim_buf_get_name(opts.bufnr)
5976
compat.system(cmd, { cwd = cwd }, function(sc)
6077
---@cast sc vim.SystemCompleted
6178
if sc.code == 0 then
@@ -65,8 +82,8 @@ M.execute_command = function(command, args, cwd, opts)
6582
end)
6683
return
6784
end
68-
local output = sc.stderr or ''
69-
local diagnostics = M.parse_diagnostics(output)
85+
local output = (sc.stderr or '') .. '\n' .. (sc.stdout or '')
86+
local diagnostics = M.parse_diagnostics(fname, output)
7087
local summary = get_test_summary(sc.stdout or '')
7188
vim.schedule(function()
7289
vim.diagnostic.set(diag_namespace, opts.bufnr, diagnostics)

spec/parse_test_result_spec.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ Location:
1717
rocks-lib/src/rocks/dependency.rs:62:22
1818
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1919
]]
20-
local diagnostics = test_executor.parse_diagnostics(fixture)
20+
local fname = 'rocks-lib/src/rocks/dependency.rs'
21+
local diagnostics = test_executor.parse_diagnostics(fname, fixture)
2122
local expected = {
2223
{
2324
lnum = 85,
2425
col = 64,
25-
message = 'called `Result::unwrap()` on an `Err` value: unexpected end of input while parsing min or version number',
26+
message = [[called `Result::unwrap()` on an `Err` value: unexpected end of input while parsing min or version number
27+
Location:
28+
rocks-lib/src/rocks/dependency.rs:62:22
29+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
30+
]],
2631
severity = vim.diagnostic.severity.ERROR,
2732
source = 'rustaceanvim',
2833
},
@@ -40,7 +45,8 @@ thread 'tests::failed_math' panicked at 'assertion failed: `(left == right)`
4045
right: `3`', src/main.rs:16:9
4146
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
4247
]]
43-
local diagnostics = test_executor.parse_diagnostics(fixture)
48+
local fname = 'src/main.rs'
49+
local diagnostics = test_executor.parse_diagnostics(fname, fixture)
4450
local expected = {
4551
{
4652
lnum = 15,

0 commit comments

Comments
 (0)