-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggest --nocapture
flag when a test fails with abort
#10855
Comments
I'd love to help out here, could someone guide me on the relevant files which contain error strings for this? |
I see that the lines in Would the correct implementation to be to add the "--nocapture" note in here? This would work only if we want the note to be displayed after every failed test.
I can make this change, if this is the correct implementation. Please suggest any alternate implementation that would make more sense of this issue. |
Could you create mockups of what you are proposing so we have something more concrete to focus the discussion on? |
(note this is tagged with "needs mentor" rather than "accepted" which means if you take this on, you'll have to take a lot more responsibility for your path with this issue which you are already showing by finding some relevant code) |
A passing by comment: I would suggest only giving that suggestion on a nonstandard exit (like a signal), only if it is using the standard test harness, and only if that flag is not already specified. |
I've made the relevant changes here - https://github.com/stupendoussuperpowers/cargo/blob/97f5f99ef2e9bf009a3dd3e80db3532ff6bdf403/src/cargo/ops/cargo_test.rs#L428C3-L438C6 File: 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
.downcast_ref::<ProcessError>()
.and_then(|proc_err| proc_err.code)
.map_or(false, |code| code == 101);
if !is_simple {
err = test_error.context(err);
}
crate::display_error(&err, &mut ws.config().shell());
+ if !is_simple {
+ drop(ws.config().shell().note(
+ "test was terminated by the signal, stderr might be truncated, pass `--nocapture` disable output buffering.",
+ ));
+ } Turns out, we already track whether a process was terminated abruptly with the I'm utilizing this to add the note that was suggested in the issue. I've built and tested this on linux-x86, and it seems to be generating the output as expected. Test terminated by a signal (outputs the note) :
Regular failing test (does not show the note) :
Open for suggestions here on how to proceed further on this, if I should add more checks before displaying the message, etc. |
I looked into this, and this might require a larger change to accomplish. We do not pass the cli args used to run the test to the function where we check for failures/log messages. This is the logging function -
We do have access to
However, I don't think we can process the output message in the Reference: /src/cargo/ops/cargo_test.rs |
To clarify something else that ehuss said
This is referring to the harness field. This might end up being higher in the stack as well though I eventually want to leverage related information for more interesting processing of tests. |
Something else to note is that this that |
changing the string to "pass |
okay, we can access the harness flag using |
libtest is the default harness and has a |
so, just to summarize, we have two implementations we can go with -
I don't have much experience with custom test harnesses, so I am not sure if it's a fair assumption to make that they would implement the |
The two most common libtest harnesses have a
This is because full Especially if you have easy access to the |
I've made the changes here: master...stupendoussuperpowers:cargo:issue-10855 Let me know if I should go ahead and submit a pull request. |
|
Thanks for the feedback. I'll make these changes and submit a PR. |
I've staged the changes here: diff I've modified and added tests so that they cover all the cases mentioned above. The tests are passing, but I need someone to advise if I followed the guidelines for writing tests properly. For tracking the
However, please let me know if you see a better way of handling this. |
At this point the discussion is mroe on the implementation side. How about we move it to a PR? |
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.
Problem
Consider the following test
cargo test
for it produces the following output:Note how it's super unclear what exactly failed and why. Passing
--nocapture
flags helps a bit, but only an expert user would realize that such a flag might be helpful!What happens here is that the test calls
std::process::abort
. Because, by default,cargo test
captures stderr, and because abort doesn't unwind, libtest doesn't get a chance to flush stderr. This seems like a fundamental limitation of libtest.Proposed Solution
It'd be cool if cargo realised that
cargo test
process was killed with a singnal, and suggested re-running with--nocapture
in this case. Note that this won't always help (it might be the case that a test died without printing anything to stderr), but it doesn't seem that the note like the following would hurt:Notes
No response
The text was updated successfully, but these errors were encountered: