snapshot_testing/
lib.rs

1/// Assert that `value` matches the snapshot at `snapshot_path`. If there is a
2/// mismatch the function will panic with a helpful diff that shows what
3/// changed.
4///
5/// If the env var `UPDATE_SNAPSHOTS` is set to `1`, `yes` or `true` then
6/// `value` will be written to `snapshot_file` instead of being asserted to
7/// match.
8///
9/// Set the env var `CLICOLOR_FORCE` to `1` to force colors in diffs in e.g. CI
10/// logs. See <https://github.com/console-rs/console/blob/a51fcead7cda/src/utils.rs#L18>
11/// which is what our dependency `similar-asserts` uses.
12pub 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}