Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Merge PR #3122: LC-002: reject non-object script parameters

aiongg328e42373ca3
diff --cc apps/lightcraft-cli/tests/it/cli.rs
index 0e3c472ab,8622e7c18..30ce04984
--- a/apps/lightcraft-cli/tests/it/cli.rs
+++ b/apps/lightcraft-cli/tests/it/cli.rs
@@@ -542,20 -513,32 +542,46 @@@ fn run_cli(args: &[&str], stdin: Option
(o.status.success(), lines, String::from_utf8_lossy(&o.stderr).to_string())
}
+/// Windows PowerShell 5.1 writes a UTF-8 BOM; `--script` (file and stdin) must accept it.
+#[test]
+fn run_script_accepts_a_utf8_bom() {
+ let text = "\u{feff}{\"command\": \"library.info\", \"params\": {}}\n";
+ let script = tmp("bom-steps.jsonl");
+ std::fs::write(&script, text).unwrap();
+ let (ok, lines, err) = run_cli(&["--demo", "--script", script.to_str().unwrap()], None);
+ assert!(ok, "file: {err}");
+ assert_eq!(lines.len(), 1, "{lines:?}");
+ let (ok, lines, err) = run_cli(&["--demo", "--script", "-"], Some(text));
+ assert!(ok, "stdin: {err}");
+ assert_eq!(lines.len(), 1, "{lines:?}");
+}
+
+ #[test]
+ fn run_script_params_validate_before_any_commands() {
+ let path = tmp("run-invalid-params.jsonl");
+ let path_str = path.to_str().unwrap();
+ for params in [json!([]), json!("invalid"), json!(42), json!(true)] {
+ let script = format!("{}\n{}\n", json!({"command": "library.info"}), json!({"command": "develop.reset", "params": params}));
+ std::fs::write(&path, &script).unwrap();
+ for (source, stdin) in [("-", Some(script.as_str())), (path_str, None)] {
+ let (ok, lines, err) = run_cli(&["--script", source], stdin);
+ assert!(!ok, "{source}: {params}: {lines:?}");
+ assert!(lines.is_empty(), "{source}: {params}: {lines:?}");
+ assert!(err.contains(&format!("{source}:2")), "{err}");
+ assert!(err.contains("`params` must be a JSON object"), "{err}");
+ }
+ }
+ for script in
+ ["{\"command\":\"library.info\"}\n", "{\"command\":\"library.info\",\"params\":null}\n", "{\"command\":\"library.info\",\"params\":{}}\n"]
+ {
+ let (ok, lines, err) = run_cli(&["--script", "-"], Some(script));
+ assert!(ok, "{script}: {err}");
+ assert_eq!(lines.len(), 1, "{script}: {lines:?}");
+ assert_eq!(lines[0]["ok"], true, "{script}: {lines:?}");
+ }
+ std::fs::remove_file(path).unwrap();
+ }
+
#[test]
fn run_subcommand_chains_commands_and_persists_a_library() {
let input = tmp("run-in.png");