Skip to content

Commit 0f5deb6

Browse files
committed
testsuite: Support anyhow error chains in error messages.
This is intended to help with adding more usage of anyhow in the testsuite, which can help show context for errors. This also includes some small improvements to the error messages to provide more information.
1 parent 2021865 commit 0f5deb6

File tree

1 file changed

+24
-7
lines changed
  • crates/cargo-test-support/src

1 file changed

+24
-7
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use std::env;
1010
use std::ffi::OsStr;
11+
use std::fmt::Write;
1112
use std::fs;
1213
use std::os;
1314
use std::path::{Path, PathBuf};
@@ -27,11 +28,26 @@ macro_rules! t {
2728
($e:expr) => {
2829
match $e {
2930
Ok(e) => e,
30-
Err(e) => panic!("{} failed with {}", stringify!($e), e),
31+
Err(e) => $crate::panic_error(&format!("failed running {}", stringify!($e)), e),
3132
}
3233
};
3334
}
3435

36+
#[track_caller]
37+
pub fn panic_error(what: &str, err: impl Into<anyhow::Error>) -> ! {
38+
let err = err.into();
39+
pe(what, err);
40+
#[track_caller]
41+
fn pe(what: &str, err: anyhow::Error) -> ! {
42+
let mut result = format!("{}\nerror: {}", what, err);
43+
for cause in err.chain().skip(1) {
44+
drop(writeln!(result, "\nCaused by:"));
45+
drop(write!(result, "{}", cause));
46+
}
47+
panic!("\n{}", result);
48+
}
49+
}
50+
3551
pub use cargo_test_macro::cargo_test;
3652

3753
pub mod cross_compile;
@@ -737,7 +753,7 @@ impl Execs {
737753
self.ran = true;
738754
let p = (&self.process_builder).clone().unwrap();
739755
if let Err(e) = self.match_process(&p) {
740-
panic!("\n{}", e)
756+
panic_error(&format!("test failed running {}", p), e);
741757
}
742758
}
743759

@@ -748,7 +764,7 @@ impl Execs {
748764
self.ran = true;
749765
let p = (&self.process_builder).clone().unwrap();
750766
match self.match_process(&p) {
751-
Err(e) => panic!("\n{}", e),
767+
Err(e) => panic_error(&format!("test failed running {}", p), e),
752768
Ok(output) => serde_json::from_slice(&output.stdout).unwrap_or_else(|e| {
753769
panic!(
754770
"\nfailed to parse JSON: {}\n\
@@ -764,7 +780,7 @@ impl Execs {
764780
pub fn run_output(&mut self, output: &Output) {
765781
self.ran = true;
766782
if let Err(e) = self.match_output(output) {
767-
panic!("\n{}", e)
783+
panic_error("process did not return the expected result", e)
768784
}
769785
}
770786

@@ -858,9 +874,10 @@ impl Execs {
858874
match self.expect_exit_code {
859875
None => Ok(()),
860876
Some(expected) if code == Some(expected) => Ok(()),
861-
Some(_) => bail!(
862-
"exited with {:?}\n--- stdout\n{}\n--- stderr\n{}",
863-
code,
877+
Some(expected) => bail!(
878+
"process exited with code {} (expected {})\n--- stdout\n{}\n--- stderr\n{}",
879+
code.unwrap_or(-1),
880+
expected,
864881
String::from_utf8_lossy(stdout),
865882
String::from_utf8_lossy(stderr)
866883
),

0 commit comments

Comments
 (0)