mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
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.
This commit is contained in:
1 parent
c46b326843
commit
f44ecb3741
5 files changed
+91
-6
No files matched your search
@@ -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<Item = &'a str>) -> 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()));
|
||||
}
|
||||
|
||||
@@ -604,6 +604,8 @@ pub struct SaveJob {
|
||||
stamped: Option<i64>,
|
||||
/// The artboards also saved to files of their own (`separateArtboards`).
|
||||
boards: Vec<usize>,
|
||||
/// 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<i
|
||||
};
|
||||
super::check_linked_scripts(cmd, plan.format.id, st, &checked)?;
|
||||
super::access::check_native_images(cmd, plan.format.id, &st.doc, crate::cmd::links::LinkBase::of(st), &checked)?;
|
||||
Ok(SaveJob { plan, doc, params, snapshot, source: st.doc.clone(), stamped, boards, redact_paths: false })
|
||||
let redact_paths = !plan.path_given;
|
||||
Ok(SaveJob { plan, doc, params, snapshot, source: st.doc.clone(), stamped, boards, redact_paths })
|
||||
}
|
||||
|
||||
impl SaveJob {
|
||||
@@ -1248,10 +1251,9 @@ pub fn save_with(s: &mut Session, plan: SavePlan, write: impl FnMut(&str, &[u8])
|
||||
/// A save command: plan, then write with the file system (a native file as it is encoded).
|
||||
pub(crate) fn save(s: &mut Session, mode: SaveMode, p: &Value) -> Result<Value> {
|
||||
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) })
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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<Path> 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
|
||||
|
||||
@@ -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::<Vec<_>>(), [("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() {
|
||||
|
||||
Reference in new issue
Block a user