Skip to content

Commit c7224aa

Browse files
authored
Improve error message when kittest fails (#5427)
* Closes #5423 New output is actionable ``` failures: ---- demo::demo_app_windows::tests::demos_should_match_snapshot stdout ---- thread 'demo::demo_app_windows::tests::demos_should_match_snapshot' panicked at crates/egui_demo_lib/src/demo/demo_app_windows.rs:433:9: Errors: [ "'demos/Code Example' Image size did not match snapshot. Expected: (402, 574), Actual: (415, 574). Run `UPDATE_SNAPSHOTS=1 cargo test` to update the snapshots.", ] note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: demo::demo_app_windows::tests::demos_should_match_snapshot ```
1 parent 3411aba commit c7224aa

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/crates/egui_kittest @lucasmerlin
12
/crates/egui-wgpu @Wumpf

crates/egui_demo_lib/src/demo/demo_app_windows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ mod tests {
426426

427427
let result = harness.try_wgpu_snapshot_options(&format!("demos/{name}"), &options);
428428
if let Err(err) = result {
429-
errors.push(err);
429+
errors.push(err.to_string());
430430
}
431431
}
432432

crates/egui_kittest/src/snapshot.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ impl SnapshotOptions {
5353
pub enum SnapshotError {
5454
/// Image did not match snapshot
5555
Diff {
56+
/// Name of the test
57+
name: String,
58+
5659
/// Count of pixels that were different
5760
diff: i32,
5861

@@ -72,6 +75,9 @@ pub enum SnapshotError {
7275

7376
/// The size of the image did not match the snapshot
7477
SizeMismatch {
78+
/// Name of the test
79+
name: String,
80+
7581
/// Expected size
7682
expected: (u32, u32),
7783

@@ -89,32 +95,43 @@ pub enum SnapshotError {
8995
},
9096
}
9197

98+
const HOW_TO_UPDATE_SCREENSHOTS: &str =
99+
"Run `UPDATE_SNAPSHOTS=1 cargo test` to update the snapshots.";
100+
92101
impl Display for SnapshotError {
93102
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94103
match self {
95-
Self::Diff { diff, diff_path } => {
104+
Self::Diff {
105+
name,
106+
diff,
107+
diff_path,
108+
} => {
96109
write!(
97110
f,
98-
"Image did not match snapshot. Diff: {diff}, {diff_path:?}"
111+
"'{name}' Image did not match snapshot. Diff: {diff}, {diff_path:?}. {HOW_TO_UPDATE_SCREENSHOTS}"
99112
)
100113
}
101114
Self::OpenSnapshot { path, err } => match err {
102115
ImageError::IoError(io) => match io.kind() {
103116
ErrorKind::NotFound => {
104-
write!(f, "Missing snapshot: {path:?}")
117+
write!(f, "Missing snapshot: {path:?}. {HOW_TO_UPDATE_SCREENSHOTS}")
105118
}
106119
err => {
107-
write!(f, "Error reading snapshot: {err:?}\nAt: {path:?}")
120+
write!(f, "Error reading snapshot: {err:?}\nAt: {path:?}. {HOW_TO_UPDATE_SCREENSHOTS}")
108121
}
109122
},
110123
err => {
111-
write!(f, "Error decoding snapshot: {err:?}\nAt: {path:?}")
124+
write!(f, "Error decoding snapshot: {err:?}\nAt: {path:?}. Make sure git-lfs is setup correctly. Read the instructions here: https://github.com/emilk/egui/blob/master/CONTRIBUTING.md#making-a-pr")
112125
}
113126
},
114-
Self::SizeMismatch { expected, actual } => {
127+
Self::SizeMismatch {
128+
name,
129+
expected,
130+
actual,
131+
} => {
115132
write!(
116133
f,
117-
"Image size did not match snapshot. Expected: {expected:?}, Actual: {actual:?}"
134+
"'{name}' Image size did not match snapshot. Expected: {expected:?}, Actual: {actual:?}. {HOW_TO_UPDATE_SCREENSHOTS}"
118135
)
119136
}
120137
Self::WriteSnapshot { path, err } => {
@@ -194,6 +211,7 @@ pub fn try_image_snapshot_options(
194211
if previous.dimensions() != current.dimensions() {
195212
maybe_update_snapshot(&path, current)?;
196213
return Err(SnapshotError::SizeMismatch {
214+
name: name.to_owned(),
197215
expected: previous.dimensions(),
198216
actual: current.dimensions(),
199217
});
@@ -217,13 +235,16 @@ pub fn try_image_snapshot_options(
217235
err,
218236
})?;
219237
maybe_update_snapshot(&path, current)?;
220-
return Err(SnapshotError::Diff { diff, diff_path });
238+
Err(SnapshotError::Diff {
239+
name: name.to_owned(),
240+
diff,
241+
diff_path,
242+
})
221243
} else {
222244
// Delete old diff if it exists
223245
std::fs::remove_file(diff_path).ok();
246+
Ok(())
224247
}
225-
226-
Ok(())
227248
}
228249

229250
/// Image snapshot test.

0 commit comments

Comments
 (0)