Skip to content

Commit

Permalink
Fail tests on missing snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Oct 4, 2024
1 parent 5e6618f commit b015733
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
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 @@ -420,7 +420,7 @@ mod tests {
harness.run();

let image = TestRenderer::new().render(&harness);
let result = try_image_snapshot(image, &format!("demos/{name}"));
let result = try_image_snapshot(&image, &format!("demos/{name}"));
if let Err(err) = result {
errors.push(err);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/widget_gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,6 @@ mod tests {

let image = TestRenderer::new().render(&harness);

image_snapshot(image, "widget_gallery");
image_snapshot(&image, "widget_gallery");
}
}
33 changes: 18 additions & 15 deletions crates/egui_kittest/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@ use std::fmt::Display;
use std::path::{Path, PathBuf};

#[derive(Debug)]
pub struct SnapshotError {
pub diff: i32,
pub diff_path: PathBuf,
pub enum SnapshotError {
Diff { diff: i32, diff_path: PathBuf },
MissingSnapshot { path: PathBuf },
}

impl Display for SnapshotError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Image did not match snapshot. Diff: {}, {:?}",
self.diff, self.diff_path
)
match self {
Self::Diff { diff, diff_path } => {
write!(
f,
"Image did not match snapshot. Diff: {diff}, {diff_path:?}"
)
}
Self::MissingSnapshot { path } => {
write!(f, "Missing snapshot: {path:?}")
}
}
}
}

/// Image snapshot test.
///
/// # Errors
/// Returns a [`SnapshotError`] if the image does not match the snapshot.
pub fn try_image_snapshot(current: image::RgbaImage, name: &str) -> Result<(), SnapshotError> {
pub fn try_image_snapshot(current: &image::RgbaImage, name: &str) -> Result<(), SnapshotError> {
let snapshots_path = Path::new("tests/snapshots");

let path = snapshots_path.join(format!("{name}.png"));
Expand All @@ -30,8 +35,6 @@ pub fn try_image_snapshot(current: image::RgbaImage, name: &str) -> Result<(), S
let diff_path = snapshots_path.join(format!("{name}.diff.png"));
let current_path = snapshots_path.join(format!("{name}.new.png"));

std::fs::create_dir_all("tests/snapshots").ok();

current.save(&current_path).unwrap();

let previous = match image::open(&path) {
Expand All @@ -41,7 +44,7 @@ pub fn try_image_snapshot(current: image::RgbaImage, name: &str) -> Result<(), S
println!("Saving current image as {path:?}");
current.save(&path).unwrap();

current.clone()
return Err(SnapshotError::MissingSnapshot { path });
}
};

Expand All @@ -54,7 +57,7 @@ pub fn try_image_snapshot(current: image::RgbaImage, name: &str) -> Result<(), S
current.save(&path).unwrap();
println!("Updated snapshot: {path:?}");
} else {
return Err(SnapshotError { diff, diff_path });
return Err(SnapshotError::Diff { diff, diff_path });
}
} else {
// Delete old diff if it exists
Expand All @@ -68,7 +71,7 @@ pub fn try_image_snapshot(current: image::RgbaImage, name: &str) -> Result<(), S
///
/// # Panics
/// Panics if the image does not match the snapshot.
pub fn image_snapshot(current: image::RgbaImage, name: &str) {
pub fn image_snapshot(current: &image::RgbaImage, name: &str) {
match try_image_snapshot(current, name) {
Ok(_) => {}
Err(err) => {
Expand Down

0 comments on commit b015733

Please sign in to comment.