Skip to content

Commit

Permalink
Pipe generated tests through rustfmt (#1766)
Browse files Browse the repository at this point in the history
This improves the workflow of generating tests for exercises and
synchronizing them with problem-specifications.
After generating the tests, they would have to be formatted manually.
This is now done automatically when generating the tests.
  • Loading branch information
senekor authored Nov 14, 2023
1 parent 0f927c9 commit d111213
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions rust-tooling/src/exercise_generation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::collections::HashMap;
use std::{
collections::HashMap,
io::Write,
process::{Command, Stdio},
};

use tera::Context;

Expand Down Expand Up @@ -110,9 +114,28 @@ fn generate_tests(slug: &str, fn_names: Vec<String>) -> String {
context.insert("fn_names", &fn_names);
context.insert("cases", &single_cases);

template
.render("test_template.tera", &context)
let rendered = template.render("test_template.tera", &context).unwrap();
let rendered = rendered.trim_start();

let mut child = Command::new("rustfmt")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("failed to spawn process");

child
.stdin
.as_mut()
.unwrap()
.trim_start()
.into()
.write_all(rendered.as_bytes())
.unwrap();
let rustfmt_out = child.wait_with_output().unwrap();

if rustfmt_out.status.success() {
String::from_utf8(rustfmt_out.stdout).unwrap()
} else {
// if rustfmt fails, still return the unformatted
// content to be written to the file
rendered.into()
}
}

0 comments on commit d111213

Please sign in to comment.