Skip to content
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

fix(check): wait until all diffs are shown before exiting #53

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ fn render_multi_output(
.map(|(key, iterable)| iterable.into_iter().map(move |v| (key.clone(), v)))
.multi_cartesian_product()
.collect::<Vec<_>>();
let mut check_results: Vec<CheckResult> = Vec::with_capacity(iterables.len());

for iterable in iterables {
let mut ctx = ctx.clone();
Expand Down Expand Up @@ -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(())
}

Expand All @@ -570,7 +576,12 @@ fn maybe_create_parents(filename: &Path) -> anyhow::Result<()> {
Ok(())
}

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

fn check_result_with_file<P>(path: &P, result: &str) -> anyhow::Result<CheckResult>
where
P: AsRef<Path>,
{
Expand All @@ -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<P>(actual: &str, expected_path: P) -> anyhow::Result<()>
Expand Down