From f44ecb3741fd5bf916fe759303011d2310d1902b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 30 Nov 2026 14:48:45 -0800 Subject: [PATCH] Scripts: refusals name files as the save was given them; locked folders give READ_ONLY_LOCATION A save job now takes its path naming from whether the plan was given a path, so a refused multi-file save without a path names its files by their file names for every caller of `save_job`, as `document.save` already did. Merging the save, export and open branch had left the job naming full paths unless `save` set the field, which failed `refused_multi_file_save_never_calls_the_writer`. A multi-file save or export into a folder that may not be read no longer fails with "automation path rejected: cannot resolve": a path whose real path cannot be found is compared as written, and its write reports READ_ONLY_LOCATION as other saves there do. The print tests remove their folders when an assertion fails, and Check Script has a test for printing the document `app['open']` returns. --- crates/engine/src/cmd/fileio/export_names.rs | 5 ++- crates/engine/src/cmd/fileio/save.rs | 8 ++-- crates/engine/src/tests/save.rs | 28 ++++++++++++++ crates/engine/src/tests/script_print.rs | 40 +++++++++++++++++++- crates/scriptcheck/src/tests.rs | 16 ++++++++ 5 files changed, 91 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/fileio/export_names.rs b/crates/engine/src/cmd/fileio/export_names.rs index 3c80bfb72f..c9e34a5ca8 100644 --- a/crates/engine/src/cmd/fileio/export_names.rs +++ b/crates/engine/src/cmd/fileio/export_names.rs @@ -110,11 +110,14 @@ pub(super) fn write_encoded(path: Option<&str>, name: &str, doc: &vectorcraft_do Ok(merge(out, extra)) } +/// An error when two of `paths` are the same file. A path whose real path cannot be found (a folder +/// that may not be read, a link that leads nowhere) is compared as written, and its write then +/// reports its own error. #[cfg(not(target_arch = "wasm32"))] pub(super) fn distinct_write_targets<'a>(paths: impl IntoIterator) -> Result<()> { let mut targets = std::collections::HashSet::new(); for path in paths { - let real = crate::file_access::resolve(path).map_err(EngineError::Other)?; + let real = crate::file_access::resolve(path).or_else(|_| std::path::absolute(path)).unwrap_or_else(|_| std::path::PathBuf::from(path)); if !targets.insert(real) { return Err(EngineError::Other("output destinations resolve to the same file".into())); } diff --git a/crates/engine/src/cmd/fileio/save.rs b/crates/engine/src/cmd/fileio/save.rs index 8a7987496d..f53c00332b 100644 --- a/crates/engine/src/cmd/fileio/save.rs +++ b/crates/engine/src/cmd/fileio/save.rs @@ -604,6 +604,8 @@ pub struct SaveJob { stamped: Option, /// The artboards also saved to files of their own (`separateArtboards`). boards: Vec, + /// Whether a refusal names a file by its file name: the plan was given no path + /// ([`SavePlan::path_given`]). redact_paths: bool, } @@ -966,7 +968,8 @@ fn job_for_stamped(s: &Session, st: &DocState, plan: SavePlan, stamped: Option Result { let plan = save_plan(s, mode, p)?; - let mut job = save_job(s, plan)?; + let job = save_job(s, plan)?; let uid = s.doc()?.uid; let given = job.plan.path_given; - job.redact_paths = !given; // Without a path, the files written are the document's own file and those beside it. let mut write = |path: &str, fill: &mut dyn FnMut(&mut dyn std::io::Write) -> std::io::Result<()>| { super::write_save_file_with(path, fill).map_err(|e| if given { e } else { super::access::naming_in_error(e, path) }) diff --git a/crates/engine/src/tests/save.rs b/crates/engine/src/tests/save.rs index f6a2f5158c..2beeb53fd6 100644 --- a/crates/engine/src/tests/save.rs +++ b/crates/engine/src/tests/save.rs @@ -217,6 +217,34 @@ fn refused_multi_file_save_never_calls_the_writer() { let _ = std::fs::remove_dir_all(d); } +/// A save of several files into a folder that may not be read fails as a write there fails, with +/// READ_ONLY_LOCATION, and not because the files' real paths could not be compared. +#[cfg(unix)] +#[test] +fn a_multi_file_save_into_a_locked_folder_is_read_only() { + use std::os::unix::fs::PermissionsExt; + + let d = dir("locked-multi"); + let locked = d.join("locked"); + std::fs::create_dir_all(&locked).unwrap(); + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 200, "height": 100, "artboards": 2})).unwrap(); + let own = path(&locked, "doc.vectorcraft"); + std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o000)).unwrap(); + // Where permissions don't hold (root), the folder reads as any other. + let enforced = std::fs::read_dir(&locked).is_err(); + let r = s.execute("document.save", &json!({"path": own, "separateArtboards": true})); + std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o755)).unwrap(); + if enforced { + let e = r.unwrap_err(); + assert!(matches!(e, EngineError::ReadOnlyLocation(_)), "{e}"); + assert!(std::fs::read_dir(&locked).unwrap().next().is_none()); + } else { + assert!(r.is_ok(), "{r:?}"); + } + let _ = std::fs::remove_dir_all(d); +} + #[test] fn an_svg_path_writes_svg_and_save_writes_it_back() { let d = dir("svg"); diff --git a/crates/engine/src/tests/script_print.rs b/crates/engine/src/tests/script_print.rs index dd3f6dca75..9bf3b72bd8 100644 --- a/crates/engine/src/tests/script_print.rs +++ b/crates/engine/src/tests/script_print.rs @@ -26,13 +26,49 @@ fn postscript_suffix_recognizes_windows_filename_aliases() { assert!(postscript_name("proof.ps", false)); } +/// A test's folder, removed when the test ends, also when an assertion fails. +struct Workspace(PathBuf); + +impl std::ops::Deref for Workspace { + type Target = PathBuf; + fn deref(&self) -> &PathBuf { + &self.0 + } +} + +impl AsRef for Workspace { + fn as_ref(&self) -> &Path { + &self.0 + } +} + +impl Drop for Workspace { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + /// A fresh folder for test `name`, by its real path, with `main` for the script. -fn folder(name: &str) -> PathBuf { +fn folder(name: &str) -> Workspace { let base = std::env::temp_dir().join(format!("vc-script-print-{name}-{}", std::process::id())); let _ = std::fs::remove_dir_all(&base); std::fs::create_dir_all(base.join("main")).unwrap(); std::fs::create_dir_all(base.join("outside")).unwrap(); - std::fs::canonicalize(&base).unwrap() + Workspace(std::fs::canonicalize(&base).unwrap()) +} + +#[test] +fn a_workspace_is_removed_when_its_test_panics() { + let base = folder("removed-on-panic"); + let path = base.to_path_buf(); + assert!( + std::panic::catch_unwind(move || { + let _held = base; + panic!("a failed assertion"); + }) + .is_err() + ); + assert!(!path.exists()); } /// A session whose scripts keep their data folders in `base/settings/Script Data`, with a diff --git a/crates/scriptcheck/src/tests.rs b/crates/scriptcheck/src/tests.rs index c6d6efaf7c..708fcaabca 100644 --- a/crates/scriptcheck/src/tests.rs +++ b/crates/scriptcheck/src/tests.rs @@ -710,6 +710,22 @@ fn print_on_bracketed_document_chains_writes_a_file() { assert!(r.members.iter().all(|m| m.name != "app.activeDocument.print()")); } +/// The document `app.open` returns prints to a file when `open` is named in brackets too, and the +/// call reads the opened file. +#[test] +fn print_on_a_document_app_open_returns_writes_a_file() { + let r = report(concat!( + "var o = new PrintOptions(); o.jobOptions.file = new File(p);\n", + "app['open'](new File(p)).print(o);\n", + "app.open(new File(p))['print'](o);\n", + "app[\"open\"](new File(p))[\"print\"](o);\n", + )); + let lines = &[2, 3, 4][..]; + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(lines)); + assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::>(), [("filesWritten", lines)]); + assert_eq!(feature(&r, "file.read").map(|f| f.lines.as_slice()), Some(lines)); +} + /// A bracketed document chain keeps its document-model form across scans and included files. #[test] fn print_on_bracketed_document_chains_in_included_files_writes_a_file() {