From 46dbb1fb285b4d96b86c9846ba1751e6ea5ff21d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 1 Dec 2026 22:41:20 -0800 Subject: [PATCH 1/3] Tests: script save tests remove their folders when an assertion fails folder() in tests/script_save_open.rs returned a PathBuf, and each test removed its folder only on its last line, so a failed assertion or a panicking script left the folder in the system's temporary folder. It now returns a Workspace guard, as tests/script_print.rs does, which removes the folder when it is dropped and clears read-only flags first when the first removal fails (Windows does not delete a read-only file). a_script_gate_does_not_write_over_a_read_only_file in file_access.rs made its file writable again and removed its folder only after its last assertion; a guard now does both when the test ends. --- crates/engine/src/file_access.rs | 42 +++++++++++-- crates/engine/src/tests/script_save_open.rs | 68 ++++++++++++++++++++- 2 files changed, 103 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/file_access.rs b/crates/engine/src/file_access.rs index 7daa75e0ae..87b2823d90 100644 --- a/crates/engine/src/file_access.rs +++ b/crates/engine/src/file_access.rs @@ -1216,16 +1216,48 @@ mod tests { let document_write = || with_script_gate(documents.clone(), || check_write(&kept).is_ok()); assert_eq!(check(&kept), (true, true)); assert!(document_write(), "an open document's file is written"); + let _cleanup = ReadOnlyFixture { file: out.clone(), folder: base.clone() }; let mut perms = std::fs::metadata(&out).unwrap().permissions(); perms.set_readonly(true); - std::fs::set_permissions(&out, perms.clone()).unwrap(); + std::fs::set_permissions(&out, perms).unwrap(); assert_eq!(check(&kept), (false, true), "a read-only file is read but not written"); assert!(check(&fresh).0, "a new file next to it is written"); assert!(!document_write(), "also when it is an open document's file"); - #[allow(clippy::permissions_set_readonly_false)] - perms.set_readonly(false); - std::fs::set_permissions(&out, perms).unwrap(); - let _ = std::fs::remove_dir_all(base); + } + + /// Makes `file` writable again and removes `folder` when the test ends, a failed assertion + /// included: Windows does not delete a read-only file. + struct ReadOnlyFixture { + file: PathBuf, + folder: PathBuf, + } + + impl Drop for ReadOnlyFixture { + fn drop(&mut self) { + if let Ok(meta) = std::fs::metadata(&self.file) { + let mut perms = meta.permissions(); + #[allow(clippy::permissions_set_readonly_false)] + perms.set_readonly(false); + let _ = std::fs::set_permissions(&self.file, perms); + } + let _ = std::fs::remove_dir_all(&self.folder); + } + } + + #[test] + fn a_read_only_fixture_is_removed_when_its_test_fails() { + let (base, _) = folders("read-only-fixture"); + let file = base.join("out/locked.svg"); + std::fs::write(&file, "").unwrap(); + let failed = std::panic::catch_unwind(|| { + let _cleanup = ReadOnlyFixture { file: file.clone(), folder: base.clone() }; + let mut perms = std::fs::metadata(&file).unwrap().permissions(); + perms.set_readonly(true); + std::fs::set_permissions(&file, perms).unwrap(); + panic!("a failed assertion"); + }); + assert!(failed.is_err()); + assert!(!base.exists(), "{} is left", base.display()); } #[test] diff --git a/crates/engine/src/tests/script_save_open.rs b/crates/engine/src/tests/script_save_open.rs index 12db366846..c56fc4098a 100644 --- a/crates/engine/src/tests/script_save_open.rs +++ b/crates/engine/src/tests/script_save_open.rs @@ -10,13 +10,77 @@ use crate::*; const DENIED: &str = "Script file access denied"; +/// A test's folder, removed when the test ends, a failed assertion included. A read-only file in +/// it, which Windows does not delete, is made writable first when the first removal fails. +struct Workspace(PathBuf); + +impl std::ops::Deref for Workspace { + type Target = Path; + + fn deref(&self) -> &Path { + &self.0 + } +} + +impl AsRef for Workspace { + fn as_ref(&self) -> &Path { + &self.0 + } +} + +impl Drop for Workspace { + fn drop(&mut self) { + if std::fs::remove_dir_all(&self.0).is_err() { + make_writable(&self.0); + let _ = std::fs::remove_dir_all(&self.0); + } + } +} + +/// Clears the read-only flag of every file and folder in `path`, `path` included. Links are not +/// followed. +fn make_writable(path: &Path) { + let Ok(meta) = std::fs::symlink_metadata(path) else { return }; + if meta.file_type().is_symlink() { + return; + } + let mut perms = meta.permissions(); + if perms.readonly() { + #[allow(clippy::permissions_set_readonly_false)] + perms.set_readonly(false); + let _ = std::fs::set_permissions(path, perms); + } + if meta.is_dir() { + for entry in std::fs::read_dir(path).into_iter().flatten().flatten() { + make_writable(&entry.path()); + } + } +} + /// 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-save-{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()) +} + +/// A test that fails leaves no folder behind, also when it made a file in it read-only. +#[test] +fn a_failed_test_leaves_no_folder() { + let base = folder("failed"); + let path = base.to_path_buf(); + let failed = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || { + let locked = base.join("main/locked.txt"); + std::fs::write(&locked, b"x").unwrap(); + let mut perms = std::fs::metadata(&locked).unwrap().permissions(); + perms.set_readonly(true); + std::fs::set_permissions(&locked, perms).unwrap(); + panic!("a failed assertion"); + })); + assert!(failed.is_err()); + assert!(!path.exists(), "{} is left", path.display()); } /// A session whose scripts keep their data folders in `base/settings/Script Data`. From 81c9588b2683353ced97bb4fe0f0a183dc0d9701 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 1 Dec 2026 22:54:36 -0800 Subject: [PATCH 2/3] Tests: read-only gate test sets up its cleanup before the first assertion a_script_gate_does_not_write_over_a_read_only_file created its ReadOnlyFixture guard after two assertions, so a failure in either left the test folder behind. The guard is now created right after the file path is made, before the file is written. --- crates/engine/src/file_access.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/engine/src/file_access.rs b/crates/engine/src/file_access.rs index 87b2823d90..64a87119ff 100644 --- a/crates/engine/src/file_access.rs +++ b/crates/engine/src/file_access.rs @@ -1200,6 +1200,7 @@ mod tests { fn a_script_gate_does_not_write_over_a_read_only_file() { let (base, _) = folders("gate-read-only"); let out = base.join("out/kept.svg"); + let _cleanup = ReadOnlyFixture { file: out.clone(), folder: base.clone() }; std::fs::write(&out, "").unwrap(); let gate = ScriptGate { read: ScriptScope::default(), @@ -1216,7 +1217,6 @@ mod tests { let document_write = || with_script_gate(documents.clone(), || check_write(&kept).is_ok()); assert_eq!(check(&kept), (true, true)); assert!(document_write(), "an open document's file is written"); - let _cleanup = ReadOnlyFixture { file: out.clone(), folder: base.clone() }; let mut perms = std::fs::metadata(&out).unwrap().permissions(); perms.set_readonly(true); std::fs::set_permissions(&out, perms).unwrap(); From 8c6ce6d5c996170fb05a2435a8d6a5ec09f6eefa Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 1 Dec 2026 23:53:39 -0800 Subject: [PATCH 3/3] Tests: read-only fixture test checks it caught its own panic The test caught any panic from its closure, so a failed set_permissions would also pass it while the file was still writable. It now asserts the file is read-only before its panic and checks the caught payload is that panic's message. --- crates/engine/src/file_access.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/file_access.rs b/crates/engine/src/file_access.rs index 64a87119ff..d0270e8171 100644 --- a/crates/engine/src/file_access.rs +++ b/crates/engine/src/file_access.rs @@ -1254,9 +1254,11 @@ mod tests { let mut perms = std::fs::metadata(&file).unwrap().permissions(); perms.set_readonly(true); std::fs::set_permissions(&file, perms).unwrap(); + assert!(std::fs::metadata(&file).unwrap().permissions().readonly(), "the fixture is read-only"); panic!("a failed assertion"); }); - assert!(failed.is_err()); + let payload = failed.err().unwrap_or_else(|| panic!("the closure did not panic")); + assert_eq!(payload.downcast_ref::<&str>(), Some(&"a failed assertion"), "the test's own panic, not a setup failure"); assert!(!base.exists(), "{} is left", base.display()); }