#![allow(dead_code)]
use std::path::PathBuf;
pub fn assert_golden(out: &str, rel: &str) {
let path = golden_path(rel);
if std::env::var("UPDATE_GOLDEN").is_ok_and(|v| v == "1") {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).expect("create testdata dir");
}
std::fs::write(&path, out).expect("write golden file");
return;
}
let golden = std::fs::read_to_string(&path)
.unwrap_or_else(|e| panic!("failed to read golden file {}: {}", path.display(), e));
let golden_esc = escape_seqs(&golden);
let out_esc = escape_seqs(out);
assert_eq!(
golden_esc,
out_esc,
"output does not match golden file {}",
path.display()
);
}
pub fn golden_path(rel: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/testdata")
.join(rel)
}
fn escape_seqs(in_: &str) -> String {
in_.split('\n')
.map(|l| format!("{l:?}"))
.collect::<Vec<_>>()
.join("\n")
}
pub fn heredoc(s: &str) -> String {
let s = s.strip_prefix('\n').unwrap_or(s);
let lines: Vec<&str> = s.lines().collect();
let indent = lines
.iter()
.filter(|l| !l.trim().is_empty())
.map(|l| l.len() - l.trim_start().len())
.min()
.unwrap_or(0);
let mut out = String::new();
for (i, l) in lines.iter().enumerate() {
if i > 0 {
out.push('\n');
}
out.push_str(&l[indent.min(l.len())..]);
}
out
}