xtask: Add proper diff printing in case of incorrect results

This commit is contained in:
Henrik Tjäder 2023-02-06 13:03:15 +01:00
parent 6ed64610c9
commit d5471f2da4
2 changed files with 11 additions and 5 deletions

View file

@ -11,3 +11,4 @@ clap = { version = "4", features = ["derive"] }
env_logger = "0.10.0" env_logger = "0.10.0"
log = "0.4.17" log = "0.4.17"
rayon = "1.6.1" rayon = "1.6.1"
diffy = "0.3.0"

View file

@ -130,19 +130,23 @@ pub enum TestRunError {
CommandError(RunResult), CommandError(RunResult),
IncompatibleCommand, IncompatibleCommand,
} }
use diffy::{create_patch, PatchFormatter};
impl fmt::Display for TestRunError { impl fmt::Display for TestRunError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
TestRunError::FileCmpError { expected, got } => { TestRunError::FileCmpError { expected, got } => {
let patch = create_patch(expected, got);
writeln!(f, "Differing output in files.\n")?; writeln!(f, "Differing output in files.\n")?;
writeln!(f, "Expected:")?; let pf = PatchFormatter::new().with_color();
writeln!(f, "{expected}\n")?; writeln!(f, "{}", pf.fmt_patch(&patch))?;
writeln!(f, "Got:")?; write!(
write!(f, "{got}") f,
"See flag --overwrite-expected to create/update expected output."
)
} }
TestRunError::FileError { file } => { TestRunError::FileError { file } => {
write!(f, "File error on: {file}\nSee flag overwrite.") write!(f, "File error on: {file}\nSee flag --overwrite-expected to create/update expected output.")
} }
TestRunError::CommandError(e) => { TestRunError::CommandError(e) => {
write!( write!(
@ -403,6 +407,7 @@ fn arm_example(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()> {
file: expected_output_file.clone(), file: expected_output_file.clone(),
} }
})?; })?;
info!("Flag --overwrite-expected enabled");
info!("Creating/updating file: {expected_output_file}"); info!("Creating/updating file: {expected_output_file}");
file_handle.write_all(cargo_run_result.output.as_bytes())?; file_handle.write_all(cargo_run_result.output.as_bytes())?;
}; };