Skip to content

Commit

Permalink
Add fix to test.rs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tfpk committed Apr 11, 2023
1 parent bbec43e commit b90e076
Showing 1 changed file with 45 additions and 34 deletions.
79 changes: 45 additions & 34 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ pub fn test(exercise: String) -> Result<(), Box<dyn Error>> {
"never"
};

let build_output = Command::new("cargo")
.arg("build")
.arg("--color")
.arg(color)
.arg("--quiet")
.arg("--bin")
.arg(&exercise)
.output()
.unwrap();

let stderr_is_empty = build_output.stderr.is_empty();
if !stderr_is_empty || !build_output.status.success() {
println!("The following errors were encountered:");
io::stderr().write_all(&build_output.stderr)?;
return Err("Build failed".into());
}

let main_output = Command::new("cargo")
.arg("expand")
.arg("--color")
Expand All @@ -21,45 +38,39 @@ pub fn test(exercise: String) -> Result<(), Box<dyn Error>> {
.output()
.unwrap();

let stderr_is_empty = main_output.stderr.is_empty();
if !stderr_is_empty || !main_output.status.success() {
println!("Got some errors when expanding the macro:");
println!();
io::stderr().write_all(&main_output.stderr)?;
} else {
println!("This is the expansion you produced:");
println!();
io::stdout().write_all(&main_output.stdout)?;
println!();
println!("This is the expansion you produced:");
println!();
io::stdout().write_all(&main_output.stdout)?;

let soln_output = Command::new("cargo")
.arg("expand")
.arg("--color")
.arg(color)
.arg("--bin")
.arg(format!("{exercise}_soln"))
.arg("main")
.output()
.unwrap();
let soln_output = Command::new("cargo")
.arg("expand")
.arg("--color")
.arg(color)
.arg("--bin")
.arg(format!("{exercise}_soln"))
.arg("main")
.output()
.unwrap();

println!("\nThe expansion we expected is:\n");
println!("\nThe expansion we expected is:\n");

io::stdout().write_all(&soln_output.stdout)?;
io::stdout().write_all(&soln_output.stdout)?;

let before = String::from_utf8_lossy(&main_output.stdout);
let after = String::from_utf8_lossy(&soln_output.stdout);
let input = InternedInput::new(before.as_ref(), after.as_ref());
let the_diff = diff(
Algorithm::Histogram,
&input,
UnifiedDiffBuilder::new(&input),
);
let before = String::from_utf8_lossy(&main_output.stdout);
let after = String::from_utf8_lossy(&soln_output.stdout);
let input = InternedInput::new(before.as_ref(), after.as_ref());
let the_diff = diff(
Algorithm::Histogram,
&input,
UnifiedDiffBuilder::new(&input),
);

if the_diff.is_empty() {
println!("\nCongratulations! You solved it.\n");
} else {
println!("\nThe diff is:\n");
println!("{the_diff}");
}
if the_diff.is_empty() {
println!("\nCongratulations! You solved it.\n");
} else {
println!("\nThe diff is:\n");
println!("{the_diff}");
}

Ok(())
Expand Down

0 comments on commit b90e076

Please sign in to comment.