From fb12ddb7598d3570d6c95b9c1fc7fd98e477fe4c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 15:06:11 -0800 Subject: [PATCH] Scripts: a renamed file's old path is named in the report's written rename named only the new path in written, so a run that renamed a file an earlier run wrote did not name the path it emptied. It now names both, as remove names the path it deletes. --- crates/engine/src/script/files.rs | 2 ++ crates/engine/src/tests/script_io.rs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index ddc574ee71..ba2f5ec4dc 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -1763,6 +1763,8 @@ fn rename(host: &Host, a: &[Data]) -> Result { file.target = Some(if rest.as_os_str().is_empty() { to.clone() } else { to.join(rest) }); } } + // The old path no longer holds the file, so the report names it as well as the new one. + disk.wrote(&from); disk.wrote(&to); Ok(Data::Object(vec![("fs".into(), Data::String(new))])) } diff --git a/crates/engine/src/tests/script_io.rs b/crates/engine/src/tests/script_io.rs index 39276d737f..75abff6956 100644 --- a/crates/engine/src/tests/script_io.rs +++ b/crates/engine/src/tests/script_io.rs @@ -513,6 +513,29 @@ fn script_files_write_in_the_temporary_and_data_folders() { let _ = std::fs::remove_dir_all(base); } +/// `rename` of a file an earlier run wrote names both its old and its new path in `written`. +#[test] +fn a_renamed_file_names_its_old_and_new_paths_in_written() { + let base = folder("renamed"); + let mut s = with_data(&base); + let first = run_in(&mut s, &base, "var u = new File(Folder.userData.fsName + '/old.txt'); u.open('w'); u.write('x'); vc.log(u.close());"); + assert_eq!(first.unwrap()["log"], json!([true])); + let r = run_in( + &mut s, + &base, + "var u = new File(Folder.userData.fsName + '/old.txt'); var before = u.fsName;\n\ + vc.log(u.rename('new.txt')); vc.log(before); vc.log(u.fsName);", + ) + .unwrap(); + assert_eq!(r["log"][0], json!(true)); + let written: Vec<&str> = r["written"].as_array().unwrap().iter().filter_map(Value::as_str).collect(); + for at in [1, 2] { + let path = r["log"][at].as_str().unwrap(); + assert!(written.contains(&path), "{path} in {written:?}"); + } + let _ = std::fs::remove_dir_all(base); +} + /// The open modes, the encodings that cannot hold a character, `copy` over a file, `remove` of a /// folder only when it is empty, `rename` refusing a name in use, `create`, and setting `length` and /// `readonly`.