mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 09:46:29 +01:00
craft-settings: keep the document unchanged when a save fails
The save now merges the settings into a copy of the document and keeps the copy only after the file is written. Before, a save rejected as too large, or one whose write failed, had already removed dropped keys from the document together with their comments, so putting a key back later wrote it without its comment.
This commit is contained in:
1 parent
37c6774c1a
commit
82c484b52d
2 files changed
+32
-6
No files matched your search
@@ -466,15 +466,16 @@ impl<T: Settings> SettingsStore<T> {
|
||||
// writing over the changes this save merges in. An early return releases it.
|
||||
let lock = self.files.lock(&path);
|
||||
let (on_disk, theirs) = self.check_disk(&path)?;
|
||||
merge_into(&mut self.doc, &self.value).map_err(|e| SettingsError::Serialize(e.to_string()))?;
|
||||
set_schema_version(&mut self.doc, T::SCHEMA_VERSION);
|
||||
// The merge is staged and only kept once the file is written, so a save that fails leaves the
|
||||
// The merge is staged on a copy and only kept once the file is written, so a save that fails leaves the
|
||||
// document, the value and the baseline as they were, and the next save merges the disk again.
|
||||
let mut doc = self.doc.clone();
|
||||
merge_into(&mut doc, &self.value).map_err(|e| SettingsError::Serialize(e.to_string()))?;
|
||||
set_schema_version(&mut doc, T::SCHEMA_VERSION);
|
||||
let theirs = theirs.map(|mut theirs| {
|
||||
merge_changes::<T>(&self.base, &self.doc, &mut theirs);
|
||||
merge_changes::<T>(&self.base, &doc, &mut theirs);
|
||||
theirs
|
||||
});
|
||||
let text = document::render(theirs.as_ref().unwrap_or(&self.doc));
|
||||
let text = document::render(theirs.as_ref().unwrap_or(&doc));
|
||||
// A file over the limit would not be read back at the next start; keep the old file.
|
||||
if text.len() as u64 > MAX_CONFIG_BYTES {
|
||||
return Err(SettingsError::Io(too_large()));
|
||||
@@ -486,6 +487,7 @@ impl<T: Settings> SettingsStore<T> {
|
||||
}
|
||||
self.files.write(&path, &text)?;
|
||||
drop(lock);
|
||||
self.doc = doc;
|
||||
if let Some(theirs) = theirs {
|
||||
let (ours, _) = document::deserialize_sections::<T>(&self.doc);
|
||||
self.doc = theirs;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use super::common;
|
||||
|
||||
use common::{config_path, open_in, read_config, write_config};
|
||||
use common::{config_path, open_in, open_keyed, read_config, write_config};
|
||||
use craft_settings::{ReadOnlyReason, SettingsError, Source};
|
||||
|
||||
#[test]
|
||||
@@ -117,6 +117,30 @@ fn rejected_save_keeps_the_adopted_file_as_the_baseline() {
|
||||
assert!(text.contains("language = \"fr\""), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejected_save_keeps_the_comment_of_a_removed_key() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
write_config(tmp.path(), "schema_version = 1\n\n[shortcuts]\n# note on save\nsave = \"Ctrl+S\"\n");
|
||||
let (mut store, _) = open_keyed(tmp.path());
|
||||
|
||||
// The key is removed in a save too large to write, then put back.
|
||||
let huge = "a".repeat(craft_settings::store::MAX_CONFIG_BYTES as usize);
|
||||
store.update(|k| {
|
||||
k.shortcuts.remove("save");
|
||||
k.general.language = huge;
|
||||
});
|
||||
assert!(matches!(store.flush(), Err(SettingsError::Io(e)) if e.kind() == std::io::ErrorKind::FileTooLarge));
|
||||
store.update(|k| {
|
||||
k.shortcuts.insert("save".into(), "Ctrl+S".into());
|
||||
k.general.language = "fr".into();
|
||||
});
|
||||
store.flush().unwrap();
|
||||
|
||||
let text = read_config(tmp.path());
|
||||
assert!(text.contains("# note on save\nsave = \"Ctrl+S\""), "{text}");
|
||||
assert!(text.contains("language = \"fr\""), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn newer_schema_opens_read_only_and_never_overwrites() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user