-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
runtime.rs
248 lines (225 loc) · 7.17 KB
/
runtime.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use std::collections::BTreeMap;
use std::env;
use std::fmt;
use std::fs;
use std::io::Write;
use std::io::{BufRead, BufReader, Read};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use chrono::Utc;
use console::{style, Color};
use difference::Changeset;
use failure::Error;
use lazy_static::lazy_static;
use serde::Deserialize;
use serde_json;
#[cfg(feature = "serialization")]
use {serde::Serialize, serde_yaml};
lazy_static! {
static ref WORKSPACES: Mutex<BTreeMap<String, &'static Path>> = Mutex::new(BTreeMap::new());
}
struct RunHint<'a>(&'a Path, Option<&'a Snapshot>);
impl<'a> fmt::Display for RunHint<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let file = style(self.0.display())
.underlined()
.fg(if fs::metadata(&self.0).is_ok() {
Color::Cyan
} else {
Color::Red
});
write!(
f,
"\n{title:-^width$}\nSnapshot: {file}\n",
file = file,
title = style(" Snapshot Information ").bold(),
width = 74
)?;
if let Some(ref old) = self.1 {
for (key, value) in old.metadata.iter() {
writeln!(f, "{}: {}", key, style(value).cyan())?;
}
}
write!(
f,
"\n{hint}\n",
hint = style("To update the snapshots re-run the tests with INSTA_UPDATE=1.").dim(),
)?;
Ok(())
}
}
fn should_update_snapshot() -> bool {
match env::var("INSTA_UPDATE").ok().as_ref().map(|x| x.as_str()) {
None | Some("") => false,
Some("1") => true,
_ => panic!("invalid value for INSTA_UPDATE"),
}
}
fn get_cargo_workspace(manifest_dir: &str) -> &Path {
let mut workspaces = WORKSPACES.lock().unwrap();
if let Some(rv) = workspaces.get(manifest_dir) {
rv
} else {
#[derive(Deserialize)]
struct Manifest {
workspace_root: String,
}
let output = std::process::Command::new(env!("CARGO"))
.arg("metadata")
.arg("--format-version=1")
.current_dir(manifest_dir)
.output()
.unwrap();
let manifest: Manifest = serde_json::from_slice(&output.stdout).unwrap();
let path = Box::leak(Box::new(PathBuf::from(manifest.workspace_root)));
workspaces.insert(manifest_dir.to_string(), path.as_path());
workspaces.get(manifest_dir).unwrap()
}
}
pub fn get_snapshot_filename(
name: &str,
manifest_dir: &str,
module_path: &str,
base: &str,
) -> PathBuf {
let cargo_workspace = get_cargo_workspace(manifest_dir);
let root = Path::new(cargo_workspace);
let base = Path::new(base);
root.join(base.parent().unwrap())
.join("snapshots")
.join(format!(
"{}__{}.snap",
module_path.rsplit("::").next().unwrap(),
name
))
}
#[derive(Debug)]
pub struct Snapshot {
path: PathBuf,
metadata: BTreeMap<String, String>,
snapshot: String,
}
impl Snapshot {
pub fn from_file<P: AsRef<Path>>(p: &P) -> Result<Snapshot, Error> {
let mut f = BufReader::new(fs::File::open(p)?);
let mut buf = String::new();
let mut metadata = BTreeMap::new();
loop {
buf.clear();
f.read_line(&mut buf)?;
if buf.trim().is_empty() {
break;
}
let mut iter = buf.splitn(2, ':');
if let Some(key) = iter.next() {
if let Some(value) = iter.next() {
metadata.insert(key.to_string(), value.trim().to_string());
}
}
}
buf.clear();
f.read_to_string(&mut buf)?;
if buf.ends_with('\n') {
buf.truncate(buf.len() - 1);
}
Ok(Snapshot {
path: p.as_ref().to_path_buf(),
metadata,
snapshot: buf,
})
}
pub fn save(&self) -> Result<(), Error> {
if let Some(folder) = self.path.parent() {
fs::create_dir_all(&folder)?;
}
let mut f = fs::File::create(&self.path)?;
for (key, value) in self.metadata.iter() {
writeln!(f, "{}: {}", key, value)?;
}
f.write_all(b"\n")?;
f.write_all(self.snapshot.as_bytes())?;
f.write_all(b"\n")?;
Ok(())
}
}
pub fn assert_snapshot(
name: &str,
new_snapshot: &str,
manifest_dir: &str,
module_path: &str,
file: &str,
line: u32,
) -> Result<(), Error> {
let snapshot_file = get_snapshot_filename(name, manifest_dir, module_path, file);
let old = Snapshot::from_file(&snapshot_file).ok();
// if the snapshot matches we're done.
if old.as_ref().map_or(false, |x| x.snapshot == new_snapshot) {
return Ok(());
}
if should_update_snapshot() {
let mut metadata = BTreeMap::new();
metadata.insert("Created".to_string(), Utc::now().to_rfc3339());
metadata.insert(
"Creator".to_string(),
format!("{}@{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
);
metadata.insert("Source".to_string(), file.to_string());
let snapshot = Snapshot {
path: snapshot_file.to_path_buf(),
metadata,
snapshot: new_snapshot.to_string(),
};
snapshot.save()?;
match old {
Some(ref old) => {
let title = Changeset::new("- old snapshot", "+ new snapshot", "\n");
let changeset = Changeset::new(&old.snapshot, new_snapshot, "\n");
writeln!(
std::io::stderr(),
" {} {}\n{}\n{}",
style("updated snapshot").green(),
style(snapshot_file.display()).cyan().underlined(),
title,
changeset,
)?;
}
None => {
writeln!(
std::io::stderr(),
" {} {}\n{}",
style("created snapshot").green(),
style(snapshot_file.display()).cyan().underlined(),
style(new_snapshot).dim(),
)?;
}
}
} else {
match old.as_ref().map(|x| &x.snapshot) {
None => panic!(
"Missing snapshot '{}' in line {}\n{}\n{}",
name,
line,
style(new_snapshot).dim(),
RunHint(&snapshot_file, old.as_ref()),
),
Some(ref old_snapshot) => {
let title = Changeset::new("- got this run", "+ expected snapshot", "\n");
let changeset = Changeset::new(new_snapshot, old_snapshot, "\n");
assert!(
false,
"snapshot '{}' mismatched in line {}:\n{}{}{}",
name,
line,
title,
changeset,
RunHint(&snapshot_file, old.as_ref()),
);
}
}
}
Ok(())
}
#[cfg(feature = "serialization")]
pub fn serialize_value<S: Serialize>(s: &S) -> String {
serde_yaml::to_string(s).unwrap()
}