diff --git a/pttodoer/src/main.rs b/pttodoer/src/main.rs index 29b15e5..cf0210f 100644 --- a/pttodoer/src/main.rs +++ b/pttodoer/src/main.rs @@ -63,7 +63,7 @@ impl Flags { } } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub struct DB { tasks_and_metadatas: Vec, cwd: String, @@ -243,6 +243,28 @@ impl DB { mod test_db { use super::*; + #[test] + fn conflicting_save_fails() { + let d = TempDir::new(&TS::now().to_string()).expect("failed to create a temp dir"); + let d = d.path().display().to_string(); + + { + let p = format!("{}/f", &d); + eprintln!("p={}", p); + let mut f = std::fs::File::create(p).expect("failed to create a file in cwd"); + f.write_all(b"- x").expect("failed to create a file"); + } + + let mut db = DB::new(d.clone()).expect("failed to open tempd"); + assert_eq!(1, db.tasks_and_metadatas[0].tasks.len()); + db.tasks_and_metadatas[0].tasks.0.push(Task::new()); + let stale_db = db.clone(); + + db.save(false).expect("failed to save db with new task"); + assert_eq!(false, stale_db.save(false).is_ok()); + } + + #[test] fn edit_append_duplicate_keeps_both() { use std::io::Seek;