mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 00:07:56 +00:00
Merge pull request #6303 from Hassan1305/feat-4936-dictionary-followups
Part of #4936: follow-ups to #6296 (read the dictionary file whole; keep other windows' words when a save fails)
This commit is contained in:
@@ -200,7 +200,26 @@ fn dictionary_file_readable(path: &std::path::Path) -> std::result::Result<(), S
|
||||
Err(e) => Err(e.to_string()),
|
||||
Ok(m) if !m.is_file() => Err("it isn't a file".into()),
|
||||
Ok(m) if m.len() > MAX_DICTIONARY_FILE_BYTES => Err(format!("it is larger than {MAX_DICTIONARY_FILE_BYTES} bytes")),
|
||||
Ok(_) => std::fs::File::open(path).map(drop).map_err(|e| e.to_string()),
|
||||
Ok(_) => {
|
||||
// Read it whole, as craft-proof will: a file that opens but then can't be read would
|
||||
// read as no words there.
|
||||
use std::io::Read;
|
||||
let mut bytes = Vec::new();
|
||||
let file = std::fs::File::open(path).map_err(|e| e.to_string())?;
|
||||
file.take(MAX_DICTIONARY_FILE_BYTES + 1).read_to_end(&mut bytes).map_err(|e| e.to_string())?;
|
||||
if bytes.len() as u64 > MAX_DICTIONARY_FILE_BYTES {
|
||||
return Err(format!("it is larger than {MAX_DICTIONARY_FILE_BYTES} bytes"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// After a failed save took a command's change back: take in the words the file holds again, so
|
||||
/// a word another window saved meanwhile, which the change had added too, stays.
|
||||
fn retake_file_words(s: &Session) {
|
||||
if let Some(path) = s.dictionary_file.as_deref().filter(|p| dictionary_file_readable(p).is_ok()) {
|
||||
craft_proof::load_user_dictionary(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,6 +250,7 @@ fn add_word(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Only this command's word: words the save took in from the file stay.
|
||||
if added {
|
||||
craft_proof::remove_word(&word);
|
||||
retake_file_words(s);
|
||||
}
|
||||
})?;
|
||||
Ok(json!({ "word": word, "added": added }))
|
||||
@@ -265,6 +285,7 @@ fn change_word(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
save_dictionary(s, std::slice::from_ref(&word), C).inspect_err(|_| {
|
||||
if to_is_new {
|
||||
craft_proof::remove_word(&to);
|
||||
retake_file_words(s);
|
||||
}
|
||||
craft_proof::add_word(&word);
|
||||
})?;
|
||||
|
||||
@@ -244,3 +244,29 @@ fn a_change_that_cant_be_saved_is_taken_back() {
|
||||
assert!(in_dictionary(&mut s, "wkqzvx") && !in_dictionary(&mut s, "wkqzvy"), "a change that wasn't saved leaves the old word");
|
||||
run(&mut s, "text.spelling.removeWord", json!({"word": "wkqzvx"}));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_word_another_window_saved_stays_when_this_save_fails() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let dir = vectorcraft_testkit::temp_dir("spelling-readonly");
|
||||
let file = dir.join("Custom Dictionary.txt");
|
||||
std::fs::write(&file, "").unwrap();
|
||||
let mut s = session();
|
||||
s.attach_custom_dictionary(file.clone());
|
||||
// Another window saves a word this session hasn't taken in, then the folder can't be written:
|
||||
// the save reads the file, takes the word in, and fails writing.
|
||||
std::fs::write(&file, "zvkqxw\n").unwrap();
|
||||
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o500)).unwrap();
|
||||
let probe = dir.join("probe");
|
||||
let writable = std::fs::write(&probe, "").is_ok();
|
||||
let r = (!writable).then(|| s.execute("text.spelling.addWord", &json!({"word": "zvkqxw"})));
|
||||
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||
let Some(r) = r else {
|
||||
// Run as root, the folder stays writable: there is no failing save to check.
|
||||
return;
|
||||
};
|
||||
assert!(r.is_err(), "{r:?}");
|
||||
assert!(in_dictionary(&mut s, "zvkqxw"), "the word in the file stays, though this Add was taken back");
|
||||
run(&mut s, "text.spelling.removeWord", json!({"word": "zvkqxw"}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user