From 6c0ab39d5e9f26c1b471f3e8fbeab81d97ace897 Mon Sep 17 00:00:00 2001 From: sanchitlegalai <111577955+sanchitlegalai@users.noreply.github.com> Date: Sat, 5 Aug 2023 02:02:05 +0530 Subject: [PATCH] Adding a note to hint --nocapture usage dont display error if not executed --- src/cargo/ops/cargo_test.rs | 19 +++++++++++++++---- tests/testsuite/test.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index 1ddf7755fc57..1166ea6832cb 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -149,7 +149,7 @@ fn run_unit_tests( unit: unit.clone(), kind: test_kind, }; - report_test_error(ws, &options.compile_opts, &unit_err, e); + report_test_error(ws, test_args, &options.compile_opts, &unit_err, e); errors.push(unit_err); if !options.no_fail_fast { return Err(CliError::code(code)); @@ -275,7 +275,7 @@ fn run_doc_tests( unit: unit.clone(), kind: TestKind::Doctest, }; - report_test_error(ws, &options.compile_opts, &unit_err, e); + report_test_error(ws, test_args, &options.compile_opts, &unit_err, e); errors.push(unit_err); if !options.no_fail_fast { return Err(CliError::code(code)); @@ -407,6 +407,7 @@ fn no_fail_fast_err( /// Displays an error on the console about a test failure. fn report_test_error( ws: &Workspace<'_>, + test_args: &[&str], opts: &ops::CompileOptions, unit_err: &UnitTestError, test_error: anyhow::Error, @@ -420,13 +421,23 @@ fn report_test_error( let mut err = format_err!("{}, to rerun pass `{}`", which, unit_err.cli_args(ws, opts)); // Don't show "process didn't exit successfully" for simple errors. // libtest exits with 101 for normal errors. - let is_simple = test_error + let (is_simple, executed) = test_error .downcast_ref::() .and_then(|proc_err| proc_err.code) - .map_or(false, |code| code == 101); + .map_or((false, false), |code| (code == 101, true)); + if !is_simple { err = test_error.context(err); } crate::display_error(&err, &mut ws.config().shell()); + + let harness: bool = unit_err.unit.target.harness(); + let nocapture: bool = test_args.contains(&"--nocapture"); + + if !is_simple && executed && harness && !nocapture { + drop(ws.config().shell().note( + "test exited abnormally; to see the full output pass --nocapture to the harness.", + )); + } } diff --git a/tests/testsuite/test.rs b/tests/testsuite/test.rs index 455246b852b1..fa782761628d 100644 --- a/tests/testsuite/test.rs +++ b/tests/testsuite/test.rs @@ -4792,6 +4792,21 @@ error: test failed, to rerun pass `--test t1` [RUNNING] tests/t2.rs (target/debug/deps/t2[..]) error: test failed, to rerun pass `--test t2` +Caused by: + process didn't exit successfully: `[ROOT]/foo/target/debug/deps/t2[..]` (exit [..]: 4) +note: test exited abnormally; to see the full output pass --nocapture to the harness. +", + ) + .with_status(4) + .run(); + + p.cargo("test --test t2 -- --nocapture") + .with_stderr( + "\ +[FINISHED] test [..] +[RUNNING] tests/t2.rs (target/debug/deps/t2[..]) +error: test failed, to rerun pass `--test t2` + Caused by: process didn't exit successfully: `[ROOT]/foo/target/debug/deps/t2[..]` (exit [..]: 4) ", @@ -4823,6 +4838,28 @@ error: test failed, to rerun pass `--test t1` [RUNNING] tests/t2.rs (target/debug/deps/t2[..]) error: test failed, to rerun pass `--test t2` +Caused by: + process didn't exit successfully: `[ROOT]/foo/target/debug/deps/t2[..]` (exit [..]: 4) +note: test exited abnormally; to see the full output pass --nocapture to the harness. +error: 2 targets failed: + `--test t1` + `--test t2` +", + ) + .with_status(101) + .run(); + + p.cargo("test --no-fail-fast -- --nocapture") + .with_stderr( + "\ +[FINISHED] test [..] +[RUNNING] tests/t1.rs (target/debug/deps/t1[..]) +thread 't' panicked at 'this is a normal error', tests/t1[..] +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace +error: test failed, to rerun pass `--test t1` +[RUNNING] tests/t2.rs (target/debug/deps/t2[..]) +error: test failed, to rerun pass `--test t2` + Caused by: process didn't exit successfully: `[ROOT]/foo/target/debug/deps/t2[..]` (exit [..]: 4) error: 2 targets failed: