diff --git a/apps/photocraft-cli/tests/it/cli.rs b/apps/photocraft-cli/tests/it/cli.rs index 21db8f55d7..c572c460fa 100644 --- a/apps/photocraft-cli/tests/it/cli.rs +++ b/apps/photocraft-cli/tests/it/cli.rs @@ -515,11 +515,11 @@ fn batch_applies_actions_to_directory() { .stdout; assert!(out.contains("2 succeeded, 0 failed"), "{out}"); let clean_log = std::fs::read(&log).unwrap(); - assert!(clean_log.starts_with(&[0xff, 0xfe])); + assert!(clean_log.starts_with(&[0xfe, 0xff])); let (pairs, remainder) = clean_log[2..].as_chunks::<2>(); assert!(remainder.is_empty()); - let body = String::from_utf16(&pairs.iter().map(|pair| u16::from_le_bytes(*pair)).collect::>()).unwrap(); - assert_eq!(body, "Batch Start\r\n\r\nBatch End\r\n"); + let body = String::from_utf16(&pairs.iter().map(|pair| u16::from_be_bytes(*pair)).collect::>()).unwrap(); + assert_eq!(body, "Start Batch\r\n\r\nEnd Batch\r\n"); for n in ["one", "two"] { let info = ok(bin().arg("info").arg(outp.join(format!("{n}.pcraft")))).stdout; let v: Value = serde_json::from_str(&info).unwrap(); @@ -557,11 +557,11 @@ fn batch_recursive_naming_stop_and_error_log() { assert!(output.join(file).is_file(), "{file}: {stdout}"); } let bytes = std::fs::read(log).unwrap(); - assert!(bytes.starts_with(&[0xff, 0xfe])); + assert!(bytes.starts_with(&[0xfe, 0xff])); let (pairs, remainder) = bytes[2..].as_chunks::<2>(); assert!(remainder.is_empty()); - let errors = String::from_utf16(&pairs.iter().map(|pair| u16::from_le_bytes(*pair)).collect::>()).unwrap(); - assert!(errors.contains("Batch Start\r\n") && errors.contains("b.png") && errors.ends_with("Batch End\r\n"), "{errors}"); + let errors = String::from_utf16(&pairs.iter().map(|pair| u16::from_be_bytes(*pair)).collect::>()).unwrap(); + assert!(errors.contains("Start Batch\r\n") && errors.contains("b.png") && errors.ends_with("End Batch\r\n"), "{errors}"); let stopped = root.join("stopped"); let result = run(&stopped, &["--recursive", "--stop-on-error"]); diff --git a/crates/engine/src/cmd/file_cmds.rs b/crates/engine/src/cmd/file_cmds.rs index 7d3f1b021f..b8d99a72ca 100644 --- a/crates/engine/src/cmd/file_cmds.rs +++ b/crates/engine/src/cmd/file_cmds.rs @@ -457,6 +457,10 @@ pub fn specs() -> Vec { #[path = "file_cmds/tests.rs"] mod tests; +#[cfg(test)] +#[path = "file_cmds/batch_log_format_tests.rs"] +mod batch_log_format_tests; + #[cfg(test)] #[path = "file_cmds/export_clut_cube_header_tests.rs"] mod export_clut_cube_header_tests; diff --git a/crates/engine/src/cmd/file_cmds/batch_log_format_tests.rs b/crates/engine/src/cmd/file_cmds/batch_log_format_tests.rs new file mode 100644 index 0000000000..02fb5cd32a --- /dev/null +++ b/crates/engine/src/cmd/file_cmds/batch_log_format_tests.rs @@ -0,0 +1,76 @@ +//! The Batch error log format (#13665): UTF-16 big-endian with BOM `FE FF`, CRLF line ends, the +//! first line `Start Batch`, the last line `End Batch` plus CRLF, and `File:` paths with the +//! platform's separators. + +use super::*; + +#[cfg(windows)] +fn tmp(name: &str) -> String { + let d = std::env::temp_dir().join(format!("photocraft-batch-log-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d.to_string_lossy().into_owned() +} + +fn utf16_be(text: &str) -> Vec { + let mut bytes = vec![0xfe, 0xff]; + for word in text.encode_utf16() { + bytes.extend_from_slice(&word.to_be_bytes()); + } + bytes +} + +fn decode_be(bytes: &[u8]) -> String { + assert_eq!(&bytes[..2], &[0xfe, 0xff], "BOM"); + let (pairs, remainder) = bytes[2..].as_chunks::<2>(); + assert!(remainder.is_empty(), "even length"); + String::from_utf16(&pairs.iter().map(|pair| u16::from_be_bytes(*pair)).collect::>()).unwrap() +} + +#[test] +fn the_log_has_a_big_endian_bom_and_big_endian_code_units() { + let bytes = encode_batch_error_log([("a.png", "boom")]); + assert_eq!(&bytes[..4], &[0xfe, 0xff, 0x00, b'S'], "BOM then the big-endian S of `Start Batch`"); +} + +#[test] +fn the_log_starts_with_start_batch_and_ends_with_end_batch_and_crlf() { + let text = decode_be(&encode_batch_error_log([("a.png", "boom"), ("b.png", "bang")])); + assert!(text.starts_with("Start Batch\r\n\r\n"), "{text:?}"); + assert!(text.ends_with("\r\nEnd Batch\r\n"), "{text:?}"); + assert_eq!(text, "Start Batch\r\n\r\nFile: \"a.png\"\r\nError: boom\r\n\r\nFile: \"b.png\"\r\nError: bang\r\n\r\nEnd Batch\r\n"); +} + +#[test] +fn an_error_free_log_is_exactly_bom_start_blank_end() { + let no_errors: [(&str, &str); 0] = []; + assert_eq!(encode_batch_error_log(no_errors), utf16_be("Start Batch\r\n\r\nEnd Batch\r\n")); +} + +#[cfg(windows)] +#[test] +fn a_folder_given_with_slashes_is_written_with_backslashes_on_windows() { + let dir = tmp("batch-log-slashes").replace('\\', "/"); + let missing = format!("{dir}/in/missing.png"); + let log = format!("{dir}/errors.txt"); + let result = + Session::new().execute("file.automate.batch", json!({"steps": [], "input": [missing], "output": format!("{dir}/out"), "errorLog": log})).unwrap(); + assert_eq!(result["errors"].as_array().map(Vec::len), Some(1), "{result}"); + let text = decode_be(&std::fs::read(&log).unwrap()); + let expected = format!("File: \"{}\"\r\n", missing.replace('/', "\\")); + assert!(text.contains(&expected), "{text:?} should contain {expected:?}"); +} + +#[cfg(windows)] +#[test] +fn the_encoder_writes_windows_separators_for_every_file_line() { + let text = decode_be(&encode_batch_error_log([("C:/pc/in\\notes.txt", "x")])); + assert!(text.contains("File: \"C:\\pc\\in\\notes.txt\"\r\n"), "{text:?}"); +} + +#[cfg(not(windows))] +#[test] +fn slashes_are_kept_off_windows() { + let text = decode_be(&encode_batch_error_log([("/pc/in/notes.txt", "x")])); + assert!(text.contains("File: \"/pc/in/notes.txt\"\r\n"), "{text:?}"); +} diff --git a/crates/engine/src/cmd/file_cmds/batch_run.rs b/crates/engine/src/cmd/file_cmds/batch_run.rs index 997c46f0ea..985706eb6c 100644 --- a/crates/engine/src/cmd/file_cmds/batch_run.rs +++ b/crates/engine/src/cmd/file_cmds/batch_run.rs @@ -429,18 +429,24 @@ impl BatchPolicy { /// Photoshop-compatible Batch log encoding, shared by the desktop command and CLI. pub fn encode_batch_error_log<'a>(errors: impl IntoIterator) -> Vec { - let mut body = String::from("Batch Start\r\n\r\n"); + let mut body = String::from("Start Batch\r\n\r\n"); for (input, reason) in errors { + let input = native_separators(input); body.push_str(&format!("File: \"{input}\"\r\nError: {reason}\r\n\r\n")); } - body.push_str("Batch End\r\n"); - let mut bytes = vec![0xff, 0xfe]; + body.push_str("End Batch\r\n"); + let mut bytes = vec![0xfe, 0xff]; for word in body.encode_utf16() { - bytes.extend_from_slice(&word.to_le_bytes()); + bytes.extend_from_slice(&word.to_be_bytes()); } bytes } +/// Paths in the log use the platform's separators: backslashes on Windows, also for a folder given with `/`. +fn native_separators(path: &str) -> String { + if cfg!(windows) { path.replace('/', "\\") } else { path.to_string() } +} + fn write_error_log(path: &str, errors: &[Value]) -> Result<()> { let bytes = encode_batch_error_log(errors.iter().map(|error| { (error.get("file").or_else(|| error.get("document")).and_then(Value::as_str).unwrap_or("?"), error.get("error").and_then(Value::as_str).unwrap_or("?")) diff --git a/crates/engine/src/cmd/file_cmds/tests.rs b/crates/engine/src/cmd/file_cmds/tests.rs index 6b86ab6dc7..9671a4ccf9 100644 --- a/crates/engine/src/cmd/file_cmds/tests.rs +++ b/crates/engine/src/cmd/file_cmds/tests.rs @@ -823,10 +823,10 @@ fn composite(s: &Session, x: i32, y: i32) -> [f32; 4] { fn batch_log(path: &str) -> String { let bytes = std::fs::read(path).unwrap(); - assert!(bytes.starts_with(&[0xff, 0xfe]), "Batch log needs a UTF-16 LE BOM"); + assert!(bytes.starts_with(&[0xfe, 0xff]), "Batch log needs a UTF-16 BE BOM"); let (pairs, remainder) = bytes[2..].as_chunks::<2>(); assert!(remainder.is_empty(), "UTF-16 bytes must have even length"); - let words: Vec = pairs.iter().map(|pair| u16::from_le_bytes(*pair)).collect(); + let words: Vec = pairs.iter().map(|pair| u16::from_be_bytes(*pair)).collect(); String::from_utf16(&words).unwrap() } @@ -845,8 +845,10 @@ fn batch_stop_on_error_reports_skipped_inputs_and_writes_the_error_log() { assert_eq!(result["stopped"], json!(missing), "{result}"); assert_eq!(result["skipped"], json!([good]), "{result}"); let text = batch_log(&log); - assert!(text.starts_with("Batch Start\r\n\r\n") && text.contains("missing.png") && text.contains("File: \"") && text.contains("Error: "), "{text}"); - assert!(text.ends_with("Batch End\r\n"), "{text}"); + // The whole path as given, in the platform's separators (`native_separators`), then the error. + let file = if cfg!(windows) { missing.replace('/', "\\") } else { missing.clone() }; + assert!(text.starts_with("Start Batch\r\n\r\n") && text.contains(&format!("File: \"{file}\"\r\nError: ")), "{text}"); + assert!(text.ends_with("End Batch\r\n"), "{text}"); assert!(!std::path::Path::new(&join(&output, "good.png")).exists()); } @@ -859,7 +861,7 @@ fn batch_writes_a_utf16_error_log_even_when_every_file_succeeds() { let result = Session::new().execute("file.automate.batch", json!({"steps": [], "input": [input], "output": output, "errorLog": log})).unwrap(); assert_eq!(result["errors"], json!([]), "{result}"); assert_eq!(result["errorLog"], json!(log)); - assert_eq!(batch_log(&log), "Batch Start\r\n\r\nBatch End\r\n"); + assert_eq!(batch_log(&log), "Start Batch\r\n\r\nEnd Batch\r\n"); } #[test] diff --git a/docs/atn.md b/docs/atn.md index 712277c533..7570d8a0e8 100644 --- a/docs/atn.md +++ b/docs/atn.md @@ -1154,17 +1154,18 @@ Measured with a folder `in` holding `a.png`, `b.png`, `notes.txt` and `sub/a.png - **Errors, Stop For Errors** (no `Log ` key): the batch stops at the failing file; files before it are saved, nothing after it is processed. - **Errors, Log Errors To File:** the batch goes on, and at the end a message says an error occurred and - the details are in the log. The log file is **UTF-16 LE with BOM and CRLF**, and is written even when - nothing failed. In German the lines are (the application's own language): + the details are in the log. The log file is **UTF-16 BE with BOM (`FE FF`) and CRLF**, and is written even when + nothing failed. The first line is `Start Batch` and the last is `End Batch` followed by CRLF; `File:` paths + use the platform separators (backslashes on Windows). The measured layout: ``` - Stapelanfang + Start Batch - Datei: "" - Fehler: () + File: "" + Error: …one pair per failed file… - Stapelende + End Batch ``` - **Cancel:** Esc during a batch stops the running command and asks "A command was cancelled or stopped during batch processing. Continue with the next file or stop?" (Continue / Stop). Stop ends the batch @@ -1182,7 +1183,7 @@ Batch cancellation conformance (Photoshop 2026 observation above; PhotoCraft's p | Ask Continue or Stop | Continue skips the interrupted input; Stop returns prior outputs and skips the rest | Implemented and tested in engine and UI | | Keep headless cancellation direct | `jobs.cancel` returns partial results without an interactive question | Implemented and tested, including a worker waiting for a desktop decision | | No progress bar | The desktop Batch preview is the visible feedback | Implemented, UI tested and visually checked | -| Preserve error policy and log | Continue retains earlier errors and writes one final UTF-16 LE log | Implemented and tested with errors before and after Esc | +| Preserve error policy and log | Continue retains earlier errors and writes one final UTF-16 BE log | Implemented and tested with errors before and after Esc | - **Return value:** when any file failed, the Batch step itself reports "cancelled" (the dialog returns no descriptor); a script run with Stop For Errors reports success even though it stopped.