diff --git a/CHANGELOG.md b/CHANGELOG.md index 409f7c1e..0014c218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lua/rustaceanvim/neotest/init.lua b/lua/rustaceanvim/neotest/init.lua index 20e40546..2465d177 100644 --- a/lua/rustaceanvim/neotest/init.lua +++ b/lua/rustaceanvim/neotest/init.lua @@ -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 = { @@ -318,25 +326,6 @@ local function get_file_root(tree) return tree end ----@param results table ----@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 @@ -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 diff --git a/lua/rustaceanvim/neotest/parser.lua b/lua/rustaceanvim/neotest/parser.lua new file mode 100644 index 00000000..4ef3647e --- /dev/null +++ b/lua/rustaceanvim/neotest/parser.lua @@ -0,0 +1,39 @@ +local M = {} + +local trans = require('rustaceanvim.neotest.trans') + +---NOTE: This mutates results +--- +---@param results table +---@param context rustaceanvim.neotest.RunContext +---@param output_content string +---@return table 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 diff --git a/spec/neotest_spec.lua b/spec/neotest_spec.lua new file mode 100644 index 00000000..be5650cd --- /dev/null +++ b/spec/neotest_spec.lua @@ -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)