Debian Codegraph @

src/assertions/matches_snapshot.rs

raw symbols SCIP
1 use crate::string_diff::colored_diff;
2 use anyhow::{Context, Result};
3 use colored::*;
4 use std::path::{Path, PathBuf};
5
6 const SNAPSHOT_DIR: &str = "__k9_snapshots__";
7
8 pub fn get_snapshot_dir(source_file: &str) -> PathBuf {
9 let mut c = Path::new(source_file).components();
10 let source_file_name = c.next_back().unwrap().as_os_str().to_string_lossy();
11 let mut p: PathBuf = c.collect();
12 p.push(SNAPSHOT_DIR);
13 p.push(source_file_name.replace(".rs", ""));
14 p
15 }
16
17 pub fn get_test_name() -> String {
18 let t = std::thread::current();
19 t.name()
20 .expect("Can't extract the test name")
21 .to_string()
22 .replace("::", "_")
23 }
24
25 pub fn get_test_snap_path(snapshot_dir: &Path, test_name: &str) -> PathBuf {
26 let mut p = snapshot_dir.to_owned();
27 p.push(format!("{}.snap", test_name));
28 p
29 }
30
31 pub fn ensure_snap_dir_exists(snapshot_path: &Path) -> Result<()> {
32 let snapshot_dir_path = snapshot_path.parent().with_context(|| {
33 format!(
34 "Can't determine snapshot directory. Snapshot path: `{}`",
35 snapshot_path.display()
36 )
37 })?;
38 std::fs::create_dir_all(snapshot_dir_path).with_context(|| {
39 format!(
40 "Failed to create snapshot directory: `{}`",
41 snapshot_dir_path.display()
42 )
43 })?;
44 Ok(())
45 }
46
47 pub fn snap_internal<T: std::fmt::Display>(
48 thing: T,
49 _line: u32,
50 _column: u32,
51 file: &str,
52 ) -> Option<String> {
53 let thing_str = thing.to_string();
54
55 let snapshot_dir = get_snapshot_dir(file);
56 let test_name = get_test_name();
57 let relative_snap_path = get_test_snap_path(&snapshot_dir, &test_name)
58 .display()
59 .to_string();
60 let crate_root = crate::paths::find_crate_root(file).unwrap();
61
62 let mut absolute_snap_path = crate_root;
63 absolute_snap_path.push(&relative_snap_path);
64
65 let string_desc = "string".red();
66 let snapshot_desc = "snapshot".green();
67
68 if crate::config::CONFIG.update_mode {
69 ensure_snap_dir_exists(&absolute_snap_path).unwrap();
70 std::fs::write(&absolute_snap_path, thing_str).unwrap();
71 None
72 } else if absolute_snap_path.exists() {
73 let snapshot_content = std::fs::read_to_string(absolute_snap_path.display().to_string())
74 .expect("can't read snapshot file");
75 let diff = colored_diff(&snapshot_content, &thing_str);
76
77 diff.map(|diff| {
78 format!(
79 "Expected {string_desc} to match {snapshot_desc} stored in
80 {file}
81
82 Difference:
83 {diff}
84
85 {update_instructions}
86 ",
87 string_desc = string_desc,
88 snapshot_desc = snapshot_desc,
89 file = relative_snap_path.green(),
90 diff = diff,
91 update_instructions = crate::config::update_instructions(),
92 )
93 })
94 } else {
95 Some(format!(
96 "Expected {string_desc} to match {snapshot_desc} stored in
97 {file}
98
99 but that snapshot file does not exist.
100
101 {update_instructions}
102 ",
103 string_desc = string_desc,
104 snapshot_desc = snapshot_desc,
105 file = relative_snap_path.green(),
106 update_instructions = crate::config::update_instructions(),
107 ))
108 }
109 }