From 08451ffbd3ce52ed937cb3a45ce762fe89912270 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 13 Nov 2026 20:23:52 +0000 Subject: [PATCH] Propagate errors from export test helpers --- .../image_processor_formats_tests.rs | 43 ++++++++++--------- .../src/dialogs/image_processor_ui/tests.rs | 24 +++++------ 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/crates/engine/src/cmd/file_cmds/image_processor_formats_tests.rs b/crates/engine/src/cmd/file_cmds/image_processor_formats_tests.rs index 6d843550d6..9687339947 100644 --- a/crates/engine/src/cmd/file_cmds/image_processor_formats_tests.rs +++ b/crates/engine/src/cmd/file_cmds/image_processor_formats_tests.rs @@ -8,24 +8,25 @@ use crate::Session; const COMMAND: &str = "file.scripts.imageProcessor"; -fn document(s: &mut Session, name: &str, depth: u32) { - s.execute("file.new", json!({"name": name, "width": 40, "height": 20, "depth": depth, "resolution": 300, "background": "#b05980"})).unwrap(); - s.execute("layer.new.layer", json!({"name": "Artwork"})).unwrap(); +fn document(s: &mut Session, name: &str, depth: u32) -> crate::Result<()> { + s.execute("file.new", json!({"name": name, "width": 40, "height": 20, "depth": depth, "resolution": 300, "background": "#b05980"}))?; + s.execute("layer.new.layer", json!({"name": "Artwork"}))?; + Ok(()) } -fn paths(result: &Value) -> Vec { - result["files"].as_array().unwrap().iter().map(|path| PathBuf::from(path.as_str().unwrap())).collect() +fn paths(result: &Value) -> Result, serde_json::Error> { + serde_json::from_value(result["files"].clone()) } -fn reopen(path: &Path) -> Document { - photocraft_io::import(path.to_str().unwrap(), &std::fs::read(path).unwrap()).unwrap().document +fn reopen(path: &Path) -> Result> { + Ok(photocraft_io::import(&path.to_string_lossy(), &std::fs::read(path)?)?.document) } #[test] fn one_run_reopens_three_independently_sized_formats() { let dir = tempfile::tempdir().unwrap(); let mut s = Session::new(); - document(&mut s, "Source", 16); + document(&mut s, "Source", 16).unwrap(); s.execute("edit.assignProfile", json!({"profile": "display-p3"})).unwrap(); s.execute("select.all", json!({})).unwrap(); let before = s.active().unwrap(); @@ -43,11 +44,11 @@ fn one_run_reopens_three_independently_sized_formats() { ) .unwrap(); assert_eq!(result["errors"], json!([]), "{result}"); - let files = paths(&result); + let files = paths(&result).unwrap(); assert_eq!(files, ["Source.jpg", "Source.psd", "Source.tif"].map(|name| dir.path().join(name))); - let jpeg = reopen(&files[0]); - let psd = reopen(&files[1]); - let tiff = reopen(&files[2]); + let jpeg = reopen(&files[0]).unwrap(); + let psd = reopen(&files[1]).unwrap(); + let tiff = reopen(&files[2]).unwrap(); let tiff_info = photocraft_codecs::tiff_info(&std::fs::read(&files[2]).unwrap()).unwrap(); assert_eq!(tiff_info.pages[0].compression, 5, "TIFF uses LZW compression"); assert_eq!((jpeg.size.width, jpeg.size.height), (20, 10)); @@ -76,8 +77,8 @@ fn one_run_reopens_three_independently_sized_formats() { #[test] fn every_nonempty_format_combination_processes_all_open_documents() { let mut s = Session::new(); - document(&mut s, "First", 8); - document(&mut s, "Second", 8); + document(&mut s, "First", 8).unwrap(); + document(&mut s, "Second", 8).unwrap(); let formats = [("jpeg", "jpg"), ("psd", "psd"), ("tiff", "tif")]; for mask in 1..8 { let dir = tempfile::tempdir().unwrap(); @@ -92,7 +93,7 @@ fn every_nonempty_format_combination_processes_all_open_documents() { } let result = s.execute(COMMAND, params).unwrap(); assert_eq!(result["errors"], json!([]), "{result}"); - assert_eq!(paths(&result), expected, "mask {mask}"); + assert_eq!(paths(&result).unwrap(), expected, "mask {mask}"); } } @@ -100,7 +101,7 @@ fn every_nonempty_format_combination_processes_all_open_documents() { fn invalid_settings_fail_before_any_format_writes() { let dir = tempfile::tempdir().unwrap(); let mut s = Session::new(); - document(&mut s, "Source", 8); + document(&mut s, "Source", 8).unwrap(); for settings in [ json!({"jpeg": false, "psd": {"enabled": false}, "tiff": null}), json!({"jpeg": {}, "psd": 7}), @@ -122,7 +123,7 @@ fn invalid_settings_fail_before_any_format_writes() { fn folder_errors_identify_the_format_and_do_not_replace_sources() { let dir = tempfile::tempdir().unwrap(); let mut s = Session::new(); - document(&mut s, "Source", 8); + document(&mut s, "Source", 8).unwrap(); let source = dir.path().join("source.psd"); s.execute("file.saveACopy", json!({"path": source})).unwrap(); let original = std::fs::read(&source).unwrap(); @@ -134,7 +135,7 @@ fn folder_errors_identify_the_format_and_do_not_replace_sources() { }), ) .unwrap(); - assert_eq!(paths(&result), [dir.path().join("source.jpg")]); + assert_eq!(paths(&result).unwrap(), [dir.path().join("source.jpg")]); assert_eq!(result["errors"].as_array().unwrap().len(), 1); assert_eq!(result["errors"][0]["format"], "psd"); assert_eq!(std::fs::read(&source).unwrap(), original); @@ -144,7 +145,7 @@ fn folder_errors_identify_the_format_and_do_not_replace_sources() { fn legacy_format_and_resize_parameters_still_work() { let dir = tempfile::tempdir().unwrap(); let mut s = Session::new(); - document(&mut s, "Source", 8); + document(&mut s, "Source", 8).unwrap(); let result = s .execute( COMMAND, @@ -154,8 +155,8 @@ fn legacy_format_and_resize_parameters_still_work() { ) .unwrap(); assert_eq!(result["errors"], json!([])); - let files = paths(&result); + let files = paths(&result).unwrap(); assert_eq!(files, [dir.path().join("Source.png")]); - let png = reopen(&files[0]); + let png = reopen(&files[0]).unwrap(); assert_eq!((png.size.width, png.size.height), (20, 10)); } diff --git a/crates/ui-egui/src/dialogs/image_processor_ui/tests.rs b/crates/ui-egui/src/dialogs/image_processor_ui/tests.rs index 950149ef2c..2e104dfac4 100644 --- a/crates/ui-egui/src/dialogs/image_processor_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/image_processor_ui/tests.rs @@ -9,21 +9,21 @@ use crate::{PhotocraftApp, Services, state::DialogKind}; const COMMAND: &str = "file.scripts.imageProcessor"; -fn open(gpu: bool) -> (Harness<'static, PhotocraftApp>, u64) { +fn open(gpu: bool) -> Result<(Harness<'static, PhotocraftApp>, u64), String> { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default()); - app.run("file.new", json!({"width": 40, "height": 20})).unwrap(); + app.run("file.new", json!({"width": 40, "height": 20}))?; let builder = Harness::builder().with_size(egui::vec2(1000.0, 760.0)); let builder = if gpu { builder.wgpu() } else { builder }; let mut h = builder.build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app); PhotocraftApp::setup_context(&h.ctx, Default::default()); let ctx = h.ctx.clone(); - let id = crate::menus::invoke(h.state_mut(), &ctx, COMMAND, json!({})).unwrap()["dialog"].as_u64().unwrap(); + let id = crate::menus::invoke(h.state_mut(), &ctx, COMMAND, json!({}))?["dialog"].as_u64().ok_or("Image Processor did not open a dialog")?; h.run_steps(4); - (h, id) + Ok((h, id)) } fn field(h: &Harness<'_, PhotocraftApp>, id: u64, key: &str) -> Value { - h.state().ui.dialogs.iter().find(|dialog| dialog.id == id).unwrap().fields[key].clone() + h.state().ui.dialogs.iter().find(|dialog| dialog.id == id).and_then(|dialog| dialog.fields.get(key)).cloned().unwrap_or(Value::Null) } fn edit_number(h: &mut Harness<'static, PhotocraftApp>, label: &str, input: &str) { @@ -41,9 +41,9 @@ fn edit_number(h: &mut Harness<'static, PhotocraftApp>, label: &str, input: &str } fn alert(h: &Harness<'_, PhotocraftApp>, message: &str) { - let dialog = h.state().ui.dialogs.last().unwrap(); - assert_eq!(dialog.kind, DialogKind::Error); - assert_eq!(dialog.fields["message"], message); + let dialog = h.state().ui.dialogs.last(); + assert_eq!(dialog.map(|dialog| dialog.kind), Some(DialogKind::Error)); + assert_eq!(dialog.and_then(|dialog| dialog.fields.get("message")).and_then(Value::as_str), Some(message)); } fn dismiss(h: &mut Harness<'_, PhotocraftApp>) { @@ -53,7 +53,7 @@ fn dismiss(h: &mut Harness<'_, PhotocraftApp>) { #[test] fn defaults_and_no_format_alert_preserve_the_dialog() { - let (mut h, id) = open(false); + let (mut h, id) = open(false).unwrap(); assert_eq!(field(&h, id, "jpeg"), json!({"enabled": true, "quality": 5.0, "convertToSrgb": false, "resize": false, "width": 1.0, "height": 1.0})); assert_eq!(field(&h, id, "psd"), json!({"enabled": false, "maximizeCompatibility": true, "resize": false, "width": 1.0, "height": 1.0})); assert_eq!(field(&h, id, "tiff"), json!({"enabled": false, "lzw": false, "resize": false, "width": 1.0, "height": 1.0})); @@ -75,7 +75,7 @@ fn defaults_and_no_format_alert_preserve_the_dialog() { #[test] fn run_dispatches_all_enabled_formats_with_their_own_dimensions() { - let (mut h, id) = open(false); + let (mut h, id) = open(false).unwrap(); let directory = tempfile::tempdir().unwrap(); let fields = &mut h.state_mut().ui.dialog_mut(id).unwrap().fields; fields.insert("output".into(), json!(directory.path())); @@ -102,7 +102,7 @@ fn run_dispatches_all_enabled_formats_with_their_own_dimensions() { #[test] fn numeric_fields_clamp_and_restore_the_last_value() { - let (mut h, id) = open(false); + let (mut h, id) = open(false).unwrap(); edit_number(&mut h, "Quality", "7.6"); assert_eq!(field(&h, id, "jpeg")["quality"], 7.6); for (input, value, message) in [ @@ -138,7 +138,7 @@ fn image_processor_gpu_snapshots() { let Some(path) = std::env::var_os("PHOTOCRAFT_PROCESSOR_SNAPSHOTS") else { return }; let path = std::path::PathBuf::from(path); let _gpu = photocraft_testkit::gpu_lock(); - let (mut h, _) = open(true); + let (mut h, _) = open(true).unwrap(); h.render().unwrap().save(path.join("dialog.png")).unwrap(); edit_number(&mut h, "Quality", "20"); alert(&h, super::closest_value("quality"));