Skip to content

Commit

Permalink
Merge pull request #21 from evenfurther/tester
Browse files Browse the repository at this point in the history
Add functions to help test results
  • Loading branch information
samueltardieu authored Sep 24, 2023
2 parents f3a2784 + 1c53220 commit 8694191
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions aoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ color-eyre = "0.6.2"
eyre = "0.6.8"
itertools = "0.11.0"
lazy_static = "1.4.0"
mktemp = "0.5.1"
thiserror = "1.0.48"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions aoc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod error;
pub mod input;
mod run;
mod runners;
pub mod test;

pub use run::run;
pub use runners::register_runner;
59 changes: 59 additions & 0 deletions aoc/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use eyre::{bail, Context};
use std::path::Path;
use std::process::Command;

const ENV_VAR: &str = "RECORD_RESULTS";

fn run_with(args: &[&str]) -> eyre::Result<String> {
let output = Command::new("cargo")
.args(["run", "--release", "--", "-a"])
.args(args)
.output()?;
Ok(String::from_utf8(output.stdout)?)
}

fn equal_content<P: AsRef<Path>>(actual: &str, expected: P, show_diff: bool) -> eyre::Result<bool> {
let expected_content = std::fs::read_to_string(expected.as_ref()).context(format!(
"cannot read {}",
expected.as_ref().to_string_lossy()
))?;
if actual == expected_content {
Ok(true)
} else {
if show_diff {
let temp = mktemp::Temp::new_file()?;
std::fs::write(&temp, actual)?;
let diff = Command::new("diff")
.arg("-u")
.arg(expected.as_ref())
.arg(temp.as_path())
.output()?;
if !diff.stderr.is_empty() {
bail!(String::from_utf8(diff.stderr)?);
}
println!(
"Actual does not meet expected:\n{}",
String::from_utf8(diff.stdout)?
);
println!("\nRe-run with {ENV_VAR}=1 to update reference files");
}
Ok(false)
}
}

pub fn check_results<P: AsRef<Path>>(expected: P, main_only: bool) -> eyre::Result<bool> {
let actual = if main_only {
run_with(&["-m"])?
} else {
run_with(&[])?
};
let update = std::env::var(ENV_VAR).is_ok();
if update {
if !matches!(equal_content(&actual, &expected, false), Ok(true)) {
std::fs::write(expected, actual)?;
}
Ok(true)
} else {
equal_content(&actual, &expected, !update)
}
}
18 changes: 18 additions & 0 deletions aoc/tests/dummy-year.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ fn all_days_main() {
Day 2 - part 2: 3842356
"###);
}

#[test]
#[serial]
fn check_expected_main() {
let dir = std::env::current_dir().unwrap();
std::env::set_current_dir("../dummy-year").unwrap();
assert!(aoc::test::check_results("expected.txt", true).unwrap());
std::env::set_current_dir(dir).unwrap();
}

#[test]
#[serial]
fn check_expected() {
let dir = std::env::current_dir().unwrap();
std::env::set_current_dir("../dummy-year").unwrap();
assert!(aoc::test::check_results("expected-all.txt", false).unwrap());
std::env::set_current_dir(dir).unwrap();
}
7 changes: 7 additions & 0 deletions dummy-year/expected-all.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Day 1 - part 1: 232
Day 1 - part 1 — str_slice: 232
Day 1 - part 2: 1783
Day 1 - part 2 — result: 1783
Day 1 - part 2 — result_string: 1783
Day 2 - part 1: 1606483
Day 2 - part 2: 3842356
4 changes: 4 additions & 0 deletions dummy-year/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Day 1 - part 1: 232
Day 1 - part 2: 1783
Day 2 - part 1: 1606483
Day 2 - part 2: 3842356

0 comments on commit 8694191

Please sign in to comment.