From 8193a8828f0cbfd87af99389cb3919e41006394f Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sat, 12 Oct 2024 09:47:51 +0100 Subject: [PATCH 1/4] fix(check): exit after all diffs are shown --- src/main.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5010f4a..19aa4f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -522,6 +522,7 @@ fn render_multi_output( .map(|(key, iterable)| iterable.into_iter().map(move |v| (key.clone(), v))) .multi_cartesian_product() .collect::>(); + let mut check_results: Vec = Vec::with_capacity(iterables.len()); for iterable in iterables { let mut ctx = ctx.clone(); @@ -549,12 +550,17 @@ fn render_multi_output( .context("Filename template render failed")?; if args.check.is_some() { - check_result_with_file(&filename, &result).context("Check mode failed")?; + check_results + .push(check_result_with_file(&filename, &result).context("Check mode failed")?); } else { write_template(args.dry_run, &filename, result)?; } } + if check_results.iter().any(|r| matches!(r, CheckResult::Fail)) { + std::process::exit(1); + } + Ok(()) } @@ -570,7 +576,12 @@ fn maybe_create_parents(filename: &Path) -> anyhow::Result<()> { Ok(()) } -fn check_result_with_file

(path: &P, result: &str) -> anyhow::Result<()> +enum CheckResult { + Pass, + Fail, +} + +fn check_result_with_file

(path: &P, result: &str) -> anyhow::Result where P: AsRef, { @@ -581,12 +592,13 @@ where path.display() ) })?; - if *result != expected { + if *result == expected { + Ok(CheckResult::Pass) + } else { eprintln!("Output does not match {}", path.display()); invoke_difftool(result, path)?; - std::process::exit(1); + Ok(CheckResult::Fail) } - Ok(()) } fn invoke_difftool

(actual: &str, expected_path: P) -> anyhow::Result<()> From 1ed74d0e4e1781cff26c656e52b66b42cedc68f2 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sat, 12 Oct 2024 10:03:35 +0100 Subject: [PATCH 2/4] i haven't tested this --- src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 19aa4f3..dd07e06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -498,7 +498,9 @@ fn render_single_output( .context("Template render failed")?; if let Some(path) = check { - check_result_with_file(&path, &result).context("Check mode failed")?; + if matches!(check_result_with_file(&path, &result).context("Check mode failed")?, CheckResult::Fail) { + std::process::exit(1); + } } else if let Some(filename) = filename { write_template(dry_run, &filename, result)?; } else { From cad26a12199ff0771a8154911b8db6fb3eb72a97 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sat, 12 Oct 2024 10:08:19 +0100 Subject: [PATCH 3/4] screw you rustfmt --- src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index dd07e06..9e2b4bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -498,7 +498,10 @@ fn render_single_output( .context("Template render failed")?; if let Some(path) = check { - if matches!(check_result_with_file(&path, &result).context("Check mode failed")?, CheckResult::Fail) { + if matches!( + check_result_with_file(&path, &result).context("Check mode failed")?, + CheckResult::Fail + ) { std::process::exit(1); } } else if let Some(filename) = filename { From a1f55bf0c8b286999f4607b8ac463bc9fe8cd710 Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sat, 12 Oct 2024 09:10:27 +0000 Subject: [PATCH 4/4] feat: mark CheckResult as must_use --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index 9e2b4bf..530f0cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -581,6 +581,7 @@ fn maybe_create_parents(filename: &Path) -> anyhow::Result<()> { Ok(()) } +#[must_use] enum CheckResult { Pass, Fail,