1pub fn assert_eq_or_update(value: impl AsRef<str>, snapshot_path: impl AsRef<std::path::Path>) {
13 let value = value.as_ref();
14 let snapshot_path = snapshot_path.as_ref();
15
16 if update_snapshot() {
17 ensure_parent_dir_exists(snapshot_path);
18
19 std::fs::write(snapshot_path, value)
20 .unwrap_or_else(|e| panic!("Error writing {snapshot_path:?}: {e}"));
21 } else {
22 let snapshot = std::fs::read_to_string(snapshot_path)
23 .unwrap_or_else(|e| panic!("Error reading {snapshot_path:?}: {e}"));
24
25 similar_asserts::assert_eq!(value, snapshot);
26 }
27}
28
29fn ensure_parent_dir_exists(snapshot_path: &std::path::Path) {
30 if !snapshot_path.exists() {
31 std::fs::create_dir_all(snapshot_path.parent().unwrap())
32 .unwrap_or_else(|e| panic!("Error creating directory for {snapshot_path:?}: {e}"));
33 }
34}
35
36fn update_snapshot() -> bool {
37 std::env::var("UPDATE_SNAPSHOTS")
38 .map(|s| s.to_lowercase())
39 .is_ok_and(|s| s == "1" || s == "yes" || s == "true")
40}