Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when kittest fails #5427

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/crates/egui_kittest @lucasmerlin
/crates/egui-wgpu @Wumpf
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ mod tests {

let result = harness.try_wgpu_snapshot_options(&format!("demos/{name}"), &options);
if let Err(err) = result {
errors.push(err);
errors.push(err.to_string());
}
}

Expand Down
41 changes: 31 additions & 10 deletions crates/egui_kittest/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ impl SnapshotOptions {
pub enum SnapshotError {
/// Image did not match snapshot
Diff {
/// Name of the test
name: String,

/// Count of pixels that were different
diff: i32,

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

/// The size of the image did not match the snapshot
SizeMismatch {
/// Name of the test
name: String,

/// Expected size
expected: (u32, u32),

Expand All @@ -89,32 +95,43 @@ pub enum SnapshotError {
},
}

const HOW_TO_UPDATE_SCREENSHOTS: &str =
"Run `UPDATE_SNAPSHOTS=1 cargo test` to update the snapshots.";

impl Display for SnapshotError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Diff { diff, diff_path } => {
Self::Diff {
name,
diff,
diff_path,
} => {
write!(
f,
"Image did not match snapshot. Diff: {diff}, {diff_path:?}"
"'{name}' Image did not match snapshot. Diff: {diff}, {diff_path:?}. {HOW_TO_UPDATE_SCREENSHOTS}"
)
}
Self::OpenSnapshot { path, err } => match err {
ImageError::IoError(io) => match io.kind() {
ErrorKind::NotFound => {
write!(f, "Missing snapshot: {path:?}")
write!(f, "Missing snapshot: {path:?}. {HOW_TO_UPDATE_SCREENSHOTS}")
}
err => {
write!(f, "Error reading snapshot: {err:?}\nAt: {path:?}")
write!(f, "Error reading snapshot: {err:?}\nAt: {path:?}. {HOW_TO_UPDATE_SCREENSHOTS}")
}
},
err => {
write!(f, "Error decoding snapshot: {err:?}\nAt: {path:?}")
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")
}
},
Self::SizeMismatch { expected, actual } => {
Self::SizeMismatch {
name,
expected,
actual,
} => {
write!(
f,
"Image size did not match snapshot. Expected: {expected:?}, Actual: {actual:?}"
"'{name}' Image size did not match snapshot. Expected: {expected:?}, Actual: {actual:?}. {HOW_TO_UPDATE_SCREENSHOTS}"
)
}
Self::WriteSnapshot { path, err } => {
Expand Down Expand Up @@ -194,6 +211,7 @@ pub fn try_image_snapshot_options(
if previous.dimensions() != current.dimensions() {
maybe_update_snapshot(&path, current)?;
return Err(SnapshotError::SizeMismatch {
name: name.to_owned(),
expected: previous.dimensions(),
actual: current.dimensions(),
});
Expand All @@ -217,13 +235,16 @@ pub fn try_image_snapshot_options(
err,
})?;
maybe_update_snapshot(&path, current)?;
return Err(SnapshotError::Diff { diff, diff_path });
Err(SnapshotError::Diff {
name: name.to_owned(),
diff,
diff_path,
})
} else {
// Delete old diff if it exists
std::fs::remove_file(diff_path).ok();
Ok(())
}

Ok(())
}

/// Image snapshot test.
Expand Down
Loading