organic/build.rs
2023-10-18 12:41:12 -04:00

72 lines
1.9 KiB
Rust

#[cfg(feature = "compare")]
use std::env;
#[cfg(feature = "compare")]
use std::fs::File;
#[cfg(feature = "compare")]
use std::io::Write;
#[cfg(feature = "compare")]
use std::path::Path;
#[cfg(feature = "compare")]
use walkdir::WalkDir;
#[cfg(feature = "compare")]
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let destination = Path::new(&out_dir).join("tests.rs");
let mut test_file = File::create(destination).unwrap();
// Re-generate the tests if any org-mode files change
println!("cargo:rerun-if-changed=org_mode_samples");
let test_files = WalkDir::new("org_mode_samples")
.into_iter()
.filter(|e| match e {
Ok(dir_entry) => {
dir_entry.file_type().is_file()
&& Path::new(dir_entry.file_name())
.extension()
.map(|ext| ext.to_ascii_lowercase() == "org")
.unwrap_or(false)
}
Err(_) => true,
})
.collect::<Result<Vec<_>, _>>()
.unwrap();
for test in test_files {
write_test(&mut test_file, &test);
}
}
#[cfg(not(feature = "compare"))]
fn main() {}
#[cfg(feature = "compare")]
fn write_test(test_file: &mut File, test: &walkdir::DirEntry) {
let test_name = test
.path()
.strip_prefix("org_mode_samples/")
.expect("Paths should be under org_mode_samples/")
.to_string_lossy()
.to_lowercase()
.strip_suffix(".org")
.expect("Should have .org extension")
.replace('/', "_");
write!(
test_file,
include_str!("./tests/test_template"),
name = test_name,
path = test.path().display(),
expect_fail = is_expect_fail(test_name.as_str())
.map(|_| "#[ignore]\n")
.unwrap_or("")
)
.unwrap();
}
#[cfg(feature = "compare")]
fn is_expect_fail(_name: &str) -> Option<&str> {
None
}