Skip to content

Commit

Permalink
fix(neotest): support nextest 0.9.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jul 27, 2024
1 parent b07c003 commit dea1c7c
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 21 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [5.1.1] - 2024-07-27

### Fixed

- Neotest: One test failure caused all succeeding tests to be marked as failed
when using cargo-nextest 0.9.7 [[#460](https://github.com/mrcjkb/rustaceanvim/issues/460)].
- Neotest: Disable ansi colour coding in output to ensure output can be parsed.

## [5.1.0] - 2024-07-27

### Added
Expand Down
30 changes: 9 additions & 21 deletions lua/rustaceanvim/neotest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ function NeotestAdapter.build_spec(run_args)
else
overrides.undo_debug_sanitize(runnable.args.cargoArgs)
end
local is_cargo_test = args[1] == 'test'
local insert_pos = is_cargo_test and 2 or 3
table.insert(args, insert_pos, '--no-fail-fast')
table.insert(args, insert_pos, '--color=never')
if is_cargo_test then
-- cargo test needs to pass --color=never to the test runner too
table.insert(args, '--color=never')
end
---@type rustaceanvim.neotest.RunSpec
---@diagnostic disable-next-line: missing-fields
local run_spec = {
Expand All @@ -318,25 +326,6 @@ local function get_file_root(tree)
return tree
end

---@param results table<string, neotest.Result>
---@param context rustaceanvim.neotest.RunContext
---@param pass_positions fun():string, ...
local function populate_pass_positions(results, context, pass_positions)
vim
.iter(pass_positions)
---@param pos string
:map(function(pos)
return trans.get_position_id(context.file, pos)
end)
---@param pos string
:each(function(pos)
results[pos] = {
status = 'passed',
}
end)
--
end

---@package
---@param spec neotest.RunSpec
---@param strategy_result neotest.StrategyResult
Expand Down Expand Up @@ -395,8 +384,7 @@ function NeotestAdapter.results(spec, strategy_result)
end
end
if has_failures then
local pass_positions = output_content:gmatch('test%s(%S+)%s...%sok')
populate_pass_positions(results, context, pass_positions)
require('rustaceanvim.neotest.parser').populate_pass_positions(results, context, output_content)
end
return results
end
Expand Down
39 changes: 39 additions & 0 deletions lua/rustaceanvim/neotest/parser.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local M = {}

local trans = require('rustaceanvim.neotest.trans')

---NOTE: This mutates results
---
---@param results table<string, neotest.Result>
---@param context rustaceanvim.neotest.RunContext
---@param output_content string
---@return table<string, neotest.Result> results
function M.populate_pass_positions(results, context, output_content)
-- XXX: match doesn't work here because it needs to
-- match on the end of each line
-- TODO: Use cargo-nextest's JUnit output in the future?
local lines = vim.split(output_content, '\n') or {}
vim
.iter(lines)
---@param line string
:map(function(line)
return line:match('PASS%s.*%s(%S+)$') or line:match('test%s(%S+)%s...%sok')
end)
---@param result string | nil
:filter(function(result)
return result ~= nil
end)
---@param pos string
:map(function(pos)
return trans.get_position_id(context.file, pos)
end)
---@param pos string
:each(function(pos)
results[pos] = {
status = 'passed',
}
end)
return results
end

return M
89 changes: 89 additions & 0 deletions spec/neotest_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
local parser = require('rustaceanvim.neotest.parser')

---@param fixture string
local function run_golden_test(fixture)
---@diagnostic disable-next-line: missing-fields
local results = parser.populate_pass_positions({}, { file = 'test_file.rs' }, fixture)
assert.same({
['test_file.rs::tests::test_ok'] = {
status = 'passed',
},
}, results)
end

describe('rustaceanvim.neotest', function()
describe('parser', function()
it('passing tests in cargo output', function()
--
local fixture = [[
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.06s
Running unittests src/main.rs (target/debug/deps/rustaceanvim_460_repro-ec3e570a1613a034)
running 2 tests
test tests::test_ok ... ok
test tests::test_main ... FAILED
successes:
successes:
tests::test_ok
failures:
---- tests::test_main stdout ----
thread 'tests::test_main' panicked at src/main.rs:9:9:
assertion `left == right` failed
left: 1
right: 2
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::test_main
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `-p rustaceanvim-460-repro --bin rustaceanvim-460-repro`
error: 1 target failed:
`-p rustaceanvim-460-repro --bin rustaceanvim-460-repro`
]]
run_golden_test(fixture)
end)
it('passing tests in cargo-nextest 0.9.7 output', function()
local fixture = [[
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.05s
Starting 2 tests across 1 binary (run ID: ddf007aa-59ed-4c8d-8a5a-d5d7f2508c5b, nextest profile: default)
FAIL [ 0.003s] rustaceanvim-460-repro::bin/rustaceanvim-460-repro tests::test_main
--- STDOUT: rustaceanvim-460-repro::bin/rustaceanvim-460-repro tests::test_main ---
running 1 test
test tests::test_main ... FAILED
failures:
failures:
tests::test_main
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s
--- STDERR: rustaceanvim-460-repro::bin/rustaceanvim-460-repro tests::test_main ---
thread 'tests::test_main' panicked at src/main.rs:9:9:
assertion `left == right` failed
left: 1
right: 2
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
PASS [ 0.003s] rustaceanvim-460-repro::bin/rustaceanvim-460-repro tests::test_ok
------------
Summary [ 0.004s] 2 tests run: 1 passed, 1 failed, 0 skipped
FAIL [ 0.003s] rustaceanvim-460-repro::bin/rustaceanvim-460-repro tests::test_main
error: test run failed
]]
run_golden_test(fixture)
end)
end)
end)

0 comments on commit dea1c7c

Please sign in to comment.