Skip to content

Commit 7da1030

Browse files
committed
Auto merge of #12463 - stupendoussuperpowers:nocapture_test_msg, r=epage
prompt the use of `--nocapture` flag if `cargo test` process is terminated via a signal. Fixes #10855 As per the discussion on this issue, we want to prompt the user to use `--nocapture` if a test is terminated abnormally. The motivation for this change is described in the issue. We check for 3 things before we display this flag. - - `!is_simple` (if the test ended with a non 101 status code) - `harness` (if the standard test harness was used), and - `!nocapture` (whether or not the `--nocapture` flag was already passed to the test) There's further tests added to `test::nonzero_exit_status` that check that the `stderr` is correct for the various combinations possible when a test ends with a non-101 status code. The new expected behavior is - - Display `--nocapture` note for only non-zero exit statuses, when the `--nocapture` flag is not passed. - Only display the note if we use a standard test harness since custom test harnesses do not implement the `--nocapture` flag. To implement the check for the `--nocapture` flag, the function definition for `report_test_errors` was changed to add the `test_args: &[&str]` parameter. This parameter is passed from the immediate calling function. This private function is only called twice change and is not causing regression after making the appropriate changes to both the places it's called in.
2 parents 54550ef + dd8cd9c commit 7da1030

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/cargo/ops/cargo_test.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn run_unit_tests(
149149
unit: unit.clone(),
150150
kind: test_kind,
151151
};
152-
report_test_error(ws, &options.compile_opts, &unit_err, e);
152+
report_test_error(ws, test_args, &options.compile_opts, &unit_err, e);
153153
errors.push(unit_err);
154154
if !options.no_fail_fast {
155155
return Err(CliError::code(code));
@@ -275,7 +275,7 @@ fn run_doc_tests(
275275
unit: unit.clone(),
276276
kind: TestKind::Doctest,
277277
};
278-
report_test_error(ws, &options.compile_opts, &unit_err, e);
278+
report_test_error(ws, test_args, &options.compile_opts, &unit_err, e);
279279
errors.push(unit_err);
280280
if !options.no_fail_fast {
281281
return Err(CliError::code(code));
@@ -407,6 +407,7 @@ fn no_fail_fast_err(
407407
/// Displays an error on the console about a test failure.
408408
fn report_test_error(
409409
ws: &Workspace<'_>,
410+
test_args: &[&str],
410411
opts: &ops::CompileOptions,
411412
unit_err: &UnitTestError,
412413
test_error: anyhow::Error,
@@ -420,13 +421,23 @@ fn report_test_error(
420421
let mut err = format_err!("{}, to rerun pass `{}`", which, unit_err.cli_args(ws, opts));
421422
// Don't show "process didn't exit successfully" for simple errors.
422423
// libtest exits with 101 for normal errors.
423-
let is_simple = test_error
424+
let (is_simple, executed) = test_error
424425
.downcast_ref::<ProcessError>()
425426
.and_then(|proc_err| proc_err.code)
426-
.map_or(false, |code| code == 101);
427+
.map_or((false, false), |code| (code == 101, true));
428+
427429
if !is_simple {
428430
err = test_error.context(err);
429431
}
430432

431433
crate::display_error(&err, &mut ws.config().shell());
434+
435+
let harness: bool = unit_err.unit.target.harness();
436+
let nocapture: bool = test_args.contains(&"--nocapture");
437+
438+
if !is_simple && executed && harness && !nocapture {
439+
drop(ws.config().shell().note(
440+
"test exited abnormally; to see the full output pass --nocapture to the harness.",
441+
));
442+
}
432443
}

tests/testsuite/test.rs

+24
Original file line numberDiff line numberDiff line change
@@ -4792,6 +4792,21 @@ error: test failed, to rerun pass `--test t1`
47924792
[RUNNING] tests/t2.rs (target/debug/deps/t2[..])
47934793
error: test failed, to rerun pass `--test t2`
47944794
4795+
Caused by:
4796+
process didn't exit successfully: `[ROOT]/foo/target/debug/deps/t2[..]` (exit [..]: 4)
4797+
note: test exited abnormally; to see the full output pass --nocapture to the harness.
4798+
",
4799+
)
4800+
.with_status(4)
4801+
.run();
4802+
4803+
p.cargo("test --test t2 -- --nocapture")
4804+
.with_stderr(
4805+
"\
4806+
[FINISHED] test [..]
4807+
[RUNNING] tests/t2.rs (target/debug/deps/t2[..])
4808+
error: test failed, to rerun pass `--test t2`
4809+
47954810
Caused by:
47964811
process didn't exit successfully: `[ROOT]/foo/target/debug/deps/t2[..]` (exit [..]: 4)
47974812
",
@@ -4811,11 +4826,20 @@ error: test failed, to rerun pass `--test t2`
48114826
48124827
Caused by:
48134828
process didn't exit successfully: `[ROOT]/foo/target/debug/deps/t2[..]` (exit [..]: 4)
4829+
note: test exited abnormally; to see the full output pass --nocapture to the harness.
48144830
error: 2 targets failed:
48154831
`--test t1`
48164832
`--test t2`
48174833
",
48184834
)
48194835
.with_status(101)
48204836
.run();
4837+
4838+
p.cargo("test --no-fail-fast -- --nocapture")
4839+
.with_stderr_does_not_contain("test exited abnormally; to see the full output pass --nocapture to the harness.")
4840+
.with_stderr_contains("[..]thread 't' panicked [..] tests/t1[..]")
4841+
.with_stderr_contains("note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace")
4842+
.with_stderr_contains("[..]process didn't exit successfully: `[ROOT]/foo/target/debug/deps/t2[..]` (exit [..]: 4)")
4843+
.with_status(101)
4844+
.run();
48214845
}

0 commit comments

Comments
 (0)