Merge pull request #6732 from dotCooCoo/fix/plugin-removed-rename-back

Plug-ins: a failed rename back of a removed plug-in's settings is reported and undone later
This commit is contained in:
BFlat
2026-11-12 09:07:24 +07:00
committed by GitHub
6 changed files with 291 additions and 34 deletions
+119
View File
@@ -1973,6 +1973,125 @@ fn a_grant_that_cannot_be_removed_stops_plugin_remove() {
assert!(!std::path::Path::new(&file).exists());
}
/// When `plugin.remove` can't remove the grant and can't move the settings back either, its error
/// says where the settings are, and the plug-in gets no storage until they are back. A second
/// removal that can't remove the grant moves them back rather than deleting them, and so does the
/// next load of the plug-in.
#[test]
fn settings_that_cannot_be_moved_back_are_reported_and_moved_back_later() {
let folder = crate::tests_links::Folder::new("plugin-store-failed-move-back");
let id = "test.engine.permissions.movebackfail";
let approved = asking(id, r#"["doc.read"]"#, &json!({"objects": []}));
let grants = folder.0.join("grants.json");
let mut s = session();
s.attach_plugin_store(folder.0.clone());
run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&approved), "grant": ["doc.read"]}));
let recorded = std::fs::read(&grants).unwrap();
// `1e9` is written back as `1000000000.0`, which makes the rewritten file larger than the 1 MiB that is read.
let entry = serde_json::from_slice::<Vec<Value>>(&recorded).unwrap().remove(0);
let full = format!(r#"[{entry},{{"n":[{}]}}]"#, vec!["1e9"; 200_000].join(",")).into_bytes();
std::fs::write(&grants, &full).unwrap();
let data = crate::store::plugin_data_dir(&folder.0, id);
let settings = br#"{"zoom":"aGVsbG8="}"#;
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("settings.json"), settings).unwrap();
let mut aside = data.clone().into_os_string();
aside.push(".removed");
let aside = std::path::PathBuf::from(aside);
crate::store::MOVE_BACK_FAILS.set(true);
let refused = s.execute("plugin.remove", &json!({"id": id})).unwrap_err().to_string();
crate::store::MOVE_BACK_FAILS.set(false);
assert!(refused.contains(crate::store::GRANT_NOT_REMOVED), "{refused}");
assert!(refused.contains(&aside.display().to_string()) && refused.contains("the folder is in use"), "{refused}");
assert!(vectorcraft_plugins::registry::get(id).is_some(), "the plug-in stays installed");
assert!(!data.exists());
assert_eq!(std::fs::read(aside.join("settings.json")).unwrap(), settings);
assert!(vectorcraft_plugins::registry::storage(id).is_none(), "no empty settings take their place");
let refused = s.execute("plugin.remove", &json!({"id": id})).unwrap_err().to_string();
assert_eq!(refused, crate::store::GRANT_NOT_REMOVED);
assert_eq!(std::fs::read(data.join("settings.json")).unwrap(), settings, "the second removal moves the settings back");
assert!(!aside.exists());
crate::store::MOVE_BACK_FAILS.set(true);
assert!(s.execute("plugin.remove", &json!({"id": id})).is_err());
crate::store::MOVE_BACK_FAILS.set(false);
assert!(!data.exists());
// A folder of the plug-in's own made meanwhile is used, and a removal that can't remove the
// grant leaves both.
let newer = br#"{"zoom":"bmV3"}"#;
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("settings.json"), newer).unwrap();
let refused = s.execute("plugin.remove", &json!({"id": id})).unwrap_err().to_string();
assert_eq!(refused, crate::store::GRANT_NOT_REMOVED);
assert_eq!(std::fs::read(data.join("settings.json")).unwrap(), newer);
assert_eq!(std::fs::read(aside.join("settings.json")).unwrap(), settings, "the kept settings stay");
std::fs::remove_dir_all(&data).unwrap();
// A restart loads the plug-in again with its settings.
std::fs::write(&grants, &recorded).unwrap();
vectorcraft_plugins::registry::remove(id);
let mut next = session();
next.attach_plugin_store(folder.0.clone());
assert!(vectorcraft_plugins::registry::get(id).is_some(), "the plug-in loads");
assert_eq!(std::fs::read(data.join("settings.json")).unwrap(), settings, "the load moves the settings back");
assert!(!aside.exists());
run(&mut next, "plugin.remove", json!({"id": id}));
assert!(vectorcraft_plugins::registry::get(id).is_none());
assert!(!data.exists() && !aside.exists(), "the removal deletes the settings");
assert_eq!(std::fs::read_dir(data.parent().unwrap()).unwrap().count(), 0, "nothing is left beside them");
}
/// What a deletion left in a plug-in's renamed settings folder, marked by the `.delete` file beside
/// it, isn't moved back. When that file can't be written, nothing is deleted, so the folder stays
/// whole and counts as kept settings.
#[test]
fn a_removal_deletes_only_folders_it_marked() {
let folder = crate::tests_links::Folder::new("plugin-store-delete-mark");
let id = "test.engine.permissions.deletemark";
let approved = asking(id, r#"["doc.read"]"#, &json!({"objects": []}));
let install = json!({"dataBase64": vectorcraft_format::base64_encode(&approved), "grant": ["doc.read"]});
let mut next = session();
next.attach_plugin_store(folder.0.clone());
let data = crate::store::plugin_data_dir(&folder.0, id);
let settings = br#"{"zoom":"aGVsbG8="}"#;
let mut aside = data.clone().into_os_string();
aside.push(".removed");
let aside = std::path::PathBuf::from(aside);
let mut marker = aside.clone().into_os_string();
marker.push(".delete");
// The next install starts with no settings, and its removal deletes the leftover and the mark.
std::fs::create_dir_all(&aside).unwrap();
std::fs::write(aside.join("settings.json"), settings).unwrap();
std::fs::write(&marker, b"").unwrap();
run(&mut next, "plugin.install", install.clone());
assert!(!data.exists(), "a leftover of a deletion isn't moved back");
run(&mut next, "plugin.remove", json!({"id": id}));
assert_eq!(std::fs::read_dir(data.parent().unwrap()).unwrap().count(), 0, "the leftover and its mark are deleted");
// When the mark can't be written, the removal deletes nothing: installing the id again needs
// `replace`, and a removal of the id that isn't installed deletes the folder.
run(&mut next, "plugin.install", install.clone());
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("settings.json"), settings).unwrap();
crate::store::MARKER_WRITE_FAILS.set(true);
let removed = next.execute("plugin.remove", &json!({"id": id}));
crate::store::MARKER_WRITE_FAILS.set(false);
assert_eq!(removed.unwrap(), json!({"removed": id}));
assert!(vectorcraft_plugins::registry::get(id).is_none());
assert_eq!(std::fs::read(aside.join("settings.json")).unwrap(), settings, "nothing is deleted without the mark");
assert!(!std::path::Path::new(&marker).exists());
assert!(next.execute("plugin.install", &install).is_err(), "the kept settings go to a new install only with `replace`");
assert!(vectorcraft_plugins::registry::get(id).is_none());
assert_eq!(std::fs::read(aside.join("settings.json")).unwrap(), settings);
run(&mut next, "plugin.remove", json!({"id": id}));
assert_eq!(std::fs::read_dir(data.parent().unwrap()).unwrap().count(), 0, "the removal of the id deletes the kept folder");
}
/// Windows keeps a file another program holds open without delete sharing, so the last grant can't
/// be removed by deleting `grants.json`.
#[cfg(windows)]