From d5471f2da4f9f650a42a745238fe8a5f85eed920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Mon, 6 Feb 2023 13:03:15 +0100 Subject: [PATCH] xtask: Add proper diff printing in case of incorrect results --- xtask/Cargo.toml | 1 + xtask/src/main.rs | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 807741af62..7341215c31 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -11,3 +11,4 @@ clap = { version = "4", features = ["derive"] } env_logger = "0.10.0" log = "0.4.17" rayon = "1.6.1" +diffy = "0.3.0" diff --git a/xtask/src/main.rs b/xtask/src/main.rs index d05e981d33..342d654eaa 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -130,19 +130,23 @@ pub enum TestRunError { CommandError(RunResult), IncompatibleCommand, } +use diffy::{create_patch, PatchFormatter}; impl fmt::Display for TestRunError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { TestRunError::FileCmpError { expected, got } => { + let patch = create_patch(expected, got); writeln!(f, "Differing output in files.\n")?; - writeln!(f, "Expected:")?; - writeln!(f, "{expected}\n")?; - writeln!(f, "Got:")?; - write!(f, "{got}") + let pf = PatchFormatter::new().with_color(); + writeln!(f, "{}", pf.fmt_patch(&patch))?; + write!( + f, + "See flag --overwrite-expected to create/update expected output." + ) } 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) => { write!( @@ -403,6 +407,7 @@ fn arm_example(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()> { file: expected_output_file.clone(), } })?; + info!("Flag --overwrite-expected enabled"); info!("Creating/updating file: {expected_output_file}"); file_handle.write_all(cargo_run_result.output.as_bytes())?; };