mirror of
https://github.com/emilk/egui.git
synced 2026-06-26 22:53:14 -04:00
Add Harness::debug_open_snapshot helper (#7590)
Adds a helper to quickly see whats going on in a kittest test. Not all test have snapshots, but when debugging tests it might still be useful to see whats actually going on, so this adds a helper fn that renders a snapshot image to a temporary file and opens it with the default image viewer: https://github.com/user-attachments/assets/08785850-0a12-4572-b9b5-cea36951081c
This commit is contained in:
@@ -33,7 +33,7 @@ wgpu = [
|
||||
]
|
||||
|
||||
## Adds a dify-based image snapshot utility.
|
||||
snapshot = ["dep:dify", "dep:image", "image/png"]
|
||||
snapshot = ["dep:dify", "dep:image", "dep:open", "dep:tempfile", "image/png"]
|
||||
|
||||
## Allows testing eframe::App
|
||||
eframe = ["dep:eframe", "eframe/accesskit"]
|
||||
@@ -61,6 +61,8 @@ wgpu = { workspace = true, features = [
|
||||
|
||||
# snapshot dependencies
|
||||
dify = { workspace = true, optional = true }
|
||||
open = { workspace = true, optional = true }
|
||||
tempfile = { workspace = true, optional = true }
|
||||
|
||||
# Enable this when generating docs.
|
||||
document-features = { workspace = true, optional = true }
|
||||
|
||||
@@ -544,7 +544,7 @@ pub fn image_snapshot(current: &image::RgbaImage, name: impl Into<String>) {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "wgpu")]
|
||||
#[cfg(any(feature = "wgpu", feature = "snapshot"))]
|
||||
impl<State> Harness<'_, State> {
|
||||
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot
|
||||
/// with custom options.
|
||||
@@ -635,6 +635,49 @@ impl<State> Harness<'_, State> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Render a snapshot, save it to a temp file and open it in the default image viewer.
|
||||
///
|
||||
/// This method is marked as deprecated to trigger errors in CI (so that it's not accidentally
|
||||
/// committed).
|
||||
#[deprecated = "Only for debugging, don't commit this."]
|
||||
pub fn debug_open_snapshot(&mut self) {
|
||||
let image = self
|
||||
.render()
|
||||
.map_err(|err| SnapshotError::RenderError { err })
|
||||
.unwrap();
|
||||
let temp_file = tempfile::Builder::new()
|
||||
.disable_cleanup(true) // we keep the file so it's accessible even after the test ends
|
||||
.prefix("kittest-snapshot")
|
||||
.suffix(".png")
|
||||
.tempfile()
|
||||
.expect("Failed to create temp file");
|
||||
|
||||
let path = temp_file.path();
|
||||
|
||||
image
|
||||
.save(temp_file.path())
|
||||
.map_err(|err| SnapshotError::WriteSnapshot {
|
||||
err,
|
||||
path: path.to_path_buf(),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
#[expect(clippy::print_stdout)]
|
||||
{
|
||||
println!("Wrote debug snapshot to: {}", path.display());
|
||||
}
|
||||
let result = open::that(path);
|
||||
if let Err(err) = result {
|
||||
#[expect(clippy::print_stderr)]
|
||||
{
|
||||
eprintln!(
|
||||
"Failed to open image {} in default image viewer: {err}",
|
||||
path.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Utility to collect snapshot errors and display them at the end of the test.
|
||||
|
||||
Reference in New Issue
Block a user