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
use std::{path::Path, time::Duration};
use similar::{Algorithm, ChangeTag, TextDiff};
use crate::snapshot::{MetaData, Snapshot};
use crate::utils::{format_rust_expression, style, term_width};
pub fn print_snapshot_summary(
workspace_root: &Path,
snapshot: &Snapshot,
snapshot_file: Option<&Path>,
mut line: Option<u32>,
) {
if line.is_none() {
line = snapshot.metadata().assertion_line();
}
if let Some(snapshot_file) = snapshot_file {
let snapshot_file = workspace_root
.join(snapshot_file)
.strip_prefix(workspace_root)
.ok()
.map(|x| x.to_path_buf())
.unwrap_or_else(|| snapshot_file.to_path_buf());
println!(
"Snapshot file: {}",
style(snapshot_file.display()).cyan().underlined()
);
}
if let Some(name) = snapshot.snapshot_name() {
println!("Snapshot: {}", style(name).yellow());
} else {
println!("Snapshot: {}", style("<inline>").dim());
}
if let Some(ref value) = snapshot.metadata().get_relative_source(workspace_root) {
println!(
"Source: {}{}",
style(value.display()).cyan(),
if let Some(line) = line {
format!(":{}", style(line).bold())
} else {
"".to_string()
}
);
}
if let Some(ref value) = snapshot.metadata().input_file() {
println!("Input file: {}", style(value).cyan());
}
}
pub fn print_snapshot_diff(
workspace_root: &Path,
new: &Snapshot,
old_snapshot: Option<&Snapshot>,
snapshot_file: Option<&Path>,
mut line: Option<u32>,
show_info: bool,
) {
if line.is_none() {
line = new.metadata().assertion_line();
}
print_snapshot_summary(workspace_root, new, snapshot_file, line);
let old_contents = old_snapshot.as_ref().map_or("", |x| x.contents_str());
let new_contents = new.contents_str();
print_changeset(old_contents, new_contents, new.metadata(), show_info);
}
pub fn print_snapshot_diff_with_title(
workspace_root: &Path,
new_snapshot: &Snapshot,
old_snapshot: Option<&Snapshot>,
line: u32,
snapshot_file: Option<&Path>,
) {
let width = term_width();
println!(
"{title:━^width$}",
title = style(" Snapshot Differences ").bold(),
width = width
);
print_snapshot_diff(
workspace_root,
new_snapshot,
old_snapshot,
snapshot_file,
Some(line),
true,
);
}
pub fn print_snapshot_summary_with_title(
workspace_root: &Path,
new_snapshot: &Snapshot,
old_snapshot: Option<&Snapshot>,
line: u32,
snapshot_file: Option<&Path>,
) {
let _old_snapshot = old_snapshot;
let width = term_width();
println!(
"{title:━^width$}",
title = style(" Snapshot Summary ").bold(),
width = width
);
print_snapshot_summary(workspace_root, new_snapshot, snapshot_file, Some(line));
println!("{title:━^width$}", title = "", width = width);
}
pub fn print_changeset(old: &str, new: &str, metadata: &MetaData, show_info: bool) {
let width = term_width();
let diff = TextDiff::configure()
.algorithm(Algorithm::Patience)
.timeout(Duration::from_millis(500))
.diff_lines(old, new);
println!("{:─^1$}", "", width);
if show_info {
if let Some(expr) = metadata.expression() {
println!("Expression: {}", style(format_rust_expression(expr)));
println!("{:─^1$}", "", width);
}
if let Some(descr) = metadata.description() {
println!("{}", descr);
println!("{:─^1$}", "", width);
}
if let Some(info) = metadata.private_info() {
let out = serde_yaml::to_string(&info).unwrap();
println!("{}", out.trim().strip_prefix("---").unwrap().trim_start());
println!("{:─^1$}", "", width);
}
}
if !old.is_empty() {
println!("{}", style("-old snapshot").red());
println!("{}", style("+new results").green());
} else {
println!("{}", style("+new results").green());
}
println!("────────────┬{:─^1$}", "", width.saturating_sub(13));
let mut has_changes = false;
for (idx, group) in diff.grouped_ops(4).iter().enumerate() {
if idx > 0 {
println!("┈┈┈┈┈┈┈┈┈┈┈┈┼{:┈^1$}", "", width.saturating_sub(13));
}
for op in group {
for change in diff.iter_inline_changes(op) {
match change.tag() {
ChangeTag::Insert => {
has_changes = true;
print!(
"{:>5} {:>5} │{}",
"",
style(change.new_index().unwrap()).cyan().dim().bold(),
style("+").green(),
);
for &(emphasized, change) in change.values() {
if emphasized {
print!("{}", style(change).green().underlined());
} else {
print!("{}", style(change).green());
}
}
}
ChangeTag::Delete => {
has_changes = true;
print!(
"{:>5} {:>5} │{}",
style(change.old_index().unwrap()).cyan().dim(),
"",
style("-").red(),
);
for &(emphasized, change) in change.values() {
if emphasized {
print!("{}", style(change).red().underlined());
} else {
print!("{}", style(change).red());
}
}
}
ChangeTag::Equal => {
print!(
"{:>5} {:>5} │ ",
style(change.old_index().unwrap()).cyan().dim(),
style(change.new_index().unwrap()).cyan().dim().bold(),
);
for &(_, change) in change.values() {
print!("{}", style(change).dim());
}
}
}
if change.missing_newline() {
println!();
}
}
}
}
if !has_changes {
println!(
"{:>5} {:>5} │{}",
"",
style("-").dim(),
style(" snapshots are matching").cyan(),
);
}
println!("────────────┴{:─^1$}", "", width.saturating_sub(13),);
}