-
Notifications
You must be signed in to change notification settings - Fork 25
TestAssertionFailure
: retain full error chain in message
#666
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
Open
rursprung
wants to merge
1
commit into
google:main
Choose a base branch
from
rursprung:print-error-chain
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -236,7 +236,21 @@ impl Debug for TestAssertionFailure { | |||||
impl<T: std::error::Error> From<T> for TestAssertionFailure { | ||||||
#[track_caller] | ||||||
fn from(value: T) -> Self { | ||||||
TestAssertionFailure::create(format!("{value}")) | ||||||
// print the full error chain, not just the first error. | ||||||
let mut description = String::new(); | ||||||
description.push_str(&format!("Error: {value}\n")); | ||||||
let mut source = value.source(); | ||||||
if source.is_some() { | ||||||
description.push_str("\nCaused by:"); | ||||||
let mut i = 1; | ||||||
while let Some(e) = source { | ||||||
description.push_str(&format!("\n{i}: {e}")); | ||||||
source = e.source(); | ||||||
i += 1; | ||||||
} | ||||||
} | ||||||
|
||||||
TestAssertionFailure::create(description) | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -246,3 +260,34 @@ impl From<TestAssertionFailure> for proptest::test_runner::TestCaseError { | |||||
proptest::test_runner::TestCaseError::Fail(format!("{value}").into()) | ||||||
} | ||||||
} | ||||||
|
||||||
#[cfg(test)] | ||||||
mod tests { | ||||||
use crate::internal::test_outcome::TestAssertionFailure; | ||||||
use std::fmt::{Debug, Display, Formatter}; | ||||||
|
||||||
#[test] | ||||||
fn error_chain_from_error() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#[derive(Debug)] | ||||||
struct CustomError { | ||||||
message: String, | ||||||
source: Option<Box<CustomError>>, | ||||||
} | ||||||
impl Display for CustomError { | ||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||||||
f.write_str(&self.message) | ||||||
} | ||||||
} | ||||||
impl std::error::Error for CustomError { | ||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||||||
self.source.as_ref().map(|e| e.as_ref() as _) | ||||||
} | ||||||
} | ||||||
|
||||||
let source1 = CustomError { message: "test1".to_string(), source: None }; | ||||||
let source2 = CustomError { message: "test2".to_string(), source: Some(source1.into()) }; | ||||||
let error = CustomError { message: "test3".to_string(), source: Some(source2.into()) }; | ||||||
let assertion_failure = TestAssertionFailure::from(error); | ||||||
assert_eq!(assertion_failure.description, "Error: test3\n\nCaused by:\n1: test2\n2: test1"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
... and change the return type to |
||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you do a bit more testing:
Check whether we can / should update the error message verification in
test_can_return_anyhow_generated_error()
in integration_tests/src/integration_tests.rsAdd a new integration test that returns a Result with a custom error type like integration_tests/src/test_returning_anyhow_error.rs