oxedyne/fe2o3/fe2o3_core/tests/file_save_atomic_if.rs
3.0 KiB, 1 run
created by r2430189635:79234, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! `save_atomic_if`, through the public API: a save guarded by the file read replaces only that file, one guarded as |
| 2 | //! new never replaces a name, and a part file the caller names is the temporary, gone once the call returns. |
| 3 | #![cfg(unix)] |
| 4 | |
| 5 | use oxedyne_fe2o3_core::{ |
| 6 | prelude::*, |
| 7 | file::{ |
| 8 | SaveIf, |
| 9 | SaveMode, |
| 10 | save_atomic_if, |
| 11 | }, |
| 12 | }; |
| 13 | |
| 14 | use std::{ |
| 15 | fs, |
| 16 | io::Write, |
| 17 | os::unix::fs::MetadataExt, |
| 18 | path::PathBuf, |
| 19 | }; |
| 20 | |
| 21 | |
| 22 | fn scratch(label: &str) -> PathBuf { |
| 23 | let dir = std::env::temp_dir().join(fmt!("fe2o3_core_save_atomic_if_{}_{}", std::process::id(), label)); |
| 24 | let _ = fs::remove_dir_all(&dir); |
| 25 | let _ = fs::create_dir_all(&dir); |
| 26 | dir |
| 27 | } |
| 28 | |
| 29 | fn put(text: &'static [u8]) -> impl FnOnce(&mut fs::File) -> Outcome<()> { |
| 30 | move |f: &mut fs::File| { res!(f.write_all(text)); Ok(()) } |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn unchanged_replaces_the_file_read_and_refuses_one_changed_since() -> Outcome<()> { |
| 35 | let dir = scratch("unchanged"); |
| 36 | let path = dir.join("a.md"); |
| 37 | res!(fs::write(&path, b"read")); |
| 38 | let read = res!(fs::symlink_metadata(&path)); |
| 39 | assert!(res!(save_atomic_if(&path, SaveMode::Keep, SaveIf::Unchanged(read), None, put(b"new")))); |
| 40 | assert_eq!(res!(fs::read(&path)), b"new"); |
| 41 | // Changed since it was read: refused, and the change stays. |
| 42 | let read = res!(fs::symlink_metadata(&path)); |
| 43 | res!(fs::write(&path, b"his edit")); |
| 44 | assert!(!res!(save_atomic_if(&path, SaveMode::Keep, SaveIf::Unchanged(read), None, put(b"lost")))); |
| 45 | assert_eq!(res!(fs::read(&path)), b"his edit"); |
| 46 | assert_eq!(res!(fs::read_dir(&dir)).count(), 1, "no temporary is left"); |
| 47 | let _ = fs::remove_dir_all(&dir); |
| 48 | Ok(()) |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn new_never_replaces_a_name_and_leaves_one_link() -> Outcome<()> { |
| 53 | let dir = scratch("new"); |
| 54 | let (path, part) = (dir.join("b.md"), dir.join(".b.md.part")); |
| 55 | assert!(res!(save_atomic_if(&path, SaveMode::Keep, SaveIf::New, Some(&part), put(b"first")))); |
| 56 | assert_eq!((res!(fs::read(&path)), res!(fs::metadata(&path)).nlink()), (b"first".to_vec(), 1)); |
| 57 | assert!(!part.exists(), "the named part is gone"); |
| 58 | assert!(!res!(save_atomic_if(&path, SaveMode::Keep, SaveIf::New, Some(&part), put(b"second")))); |
| 59 | assert_eq!(res!(fs::read(&path)), b"first"); |
| 60 | // A dangling link holds the name too. |
| 61 | let link = dir.join("c.md"); |
| 62 | res!(std::os::unix::fs::symlink(dir.join("nowhere"), &link)); |
| 63 | assert!(!res!(save_atomic_if(&link, SaveMode::Keep, SaveIf::New, Some(&part), put(b"x")))); |
| 64 | assert_eq!(res!(fs::read_dir(&dir)).count(), 2, "no temporary is left"); |
| 65 | let _ = fs::remove_dir_all(&dir); |
| 66 | Ok(()) |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn a_named_part_already_there_is_refused_whole() -> Outcome<()> { |
| 71 | let dir = scratch("part"); |
| 72 | let (path, part) = (dir.join("d.md"), dir.join(".d.md.part")); |
| 73 | res!(fs::write(&part, b"a crash's")); |
| 74 | assert!(save_atomic_if(&path, SaveMode::Keep, SaveIf::Always, Some(&part), put(b"x")).is_err()); |
| 75 | assert!(!path.exists()); |
| 76 | assert_eq!(res!(fs::read(&part)), b"a crash's", "the caller clears what a crash left"); |
| 77 | let _ = fs::remove_dir_all(&dir); |
| 78 | Ok(()) |
| 79 | } |