-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathsnapshot.rs
79 lines (65 loc) · 2.17 KB
/
snapshot.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use core::panic;
use std::path::{Path, PathBuf};
#[derive(Clone, Debug)]
pub struct Location {
location: &'static panic::Location<'static>,
name: String,
}
impl Location {
#[track_caller]
pub fn new<N: core::fmt::Display>(name: N) -> Self {
let location = panic::Location::caller();
let name = name.to_string();
Self { location, name }
}
#[track_caller]
#[allow(clippy::manual_map)] // using `Option::map` messes up the track_caller
pub fn from_thread_name() -> Option<Self> {
let thread = std::thread::current();
// only create a location if insta can figure out the test name from the
// thread
if let Some(name) = thread.name().filter(|name| *name != "main") {
let name = name
.split("::")
.chain(Some("events"))
.collect::<Vec<_>>()
.join("__");
Some(Self::new(name))
} else {
None
}
}
pub fn snapshot_log(&self, output: &[String]) {
// miri doesn't support the syscalls that insta uses
if cfg!(miri) {
return;
}
let value = output.join("\n");
let name = self.name.as_str();
let mut settings = insta::Settings::clone_current();
// we want to use the actual caller's module
settings.set_prepend_module_to_snapshot(false);
settings.set_input_file(self.location.file());
settings.set_snapshot_path(self.snapshot_path());
settings.set_omit_expression(true);
settings.bind(|| {
insta::assert_snapshot!(name, &value);
});
}
fn snapshot_path(&self) -> PathBuf {
let ws = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."));
let file = Path::new(self.location.file());
let file = if file.is_relative() {
ws.join(file)
} else {
file.to_path_buf()
};
file.canonicalize()
.unwrap()
.parent()
.unwrap()
.join("snapshots")
}
}