|
| 1 | +use gix_fs::SharedFileSnapshotMut; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +#[test] |
| 5 | +fn journey() -> Result<(), Box<dyn std::error::Error>> { |
| 6 | + let tmp = tempfile::tempdir().unwrap(); |
| 7 | + if !has_nanosecond_times(tmp.path())? { |
| 8 | + return Ok(()); |
| 9 | + } |
| 10 | + |
| 11 | + let file_path = tmp.path().join("content"); |
| 12 | + let smut = SharedFileSnapshotMut::<String>::new(); |
| 13 | + |
| 14 | + let check = || file_path.metadata().ok()?.modified().ok(); |
| 15 | + let open = || { |
| 16 | + Ok(match std::fs::read_to_string(&file_path) { |
| 17 | + Ok(s) => Some(s), |
| 18 | + Err(err) if err.kind() == std::io::ErrorKind::NotFound => None, |
| 19 | + Err(err) => return Err(err), |
| 20 | + }) |
| 21 | + }; |
| 22 | + let snap = smut.recent_snapshot(check, open)?; |
| 23 | + assert!(snap.is_none()); |
| 24 | + |
| 25 | + std::fs::write(&file_path, "content")?; |
| 26 | + let snap = smut.recent_snapshot(check, open)?.expect("content read"); |
| 27 | + assert_eq!(&**snap, "content", "it read the file for the first time"); |
| 28 | + |
| 29 | + std::fs::write(&file_path, "change")?; |
| 30 | + let snap = smut.recent_snapshot(check, open)?.expect("content read"); |
| 31 | + assert_eq!(&**snap, "change", "it picks up the change"); |
| 32 | + |
| 33 | + std::fs::remove_file(&file_path)?; |
| 34 | + let snap = smut.recent_snapshot(check, open)?; |
| 35 | + assert!(snap.is_none(), "file deleted, nothing to see here"); |
| 36 | + |
| 37 | + std::fs::write(&file_path, "new")?; |
| 38 | + let snap = smut.recent_snapshot(check, open)?.expect("content read again"); |
| 39 | + let owned: String = snap.into_owned_or_cloned(); |
| 40 | + assert_eq!(owned, "new", "owned versions are possible easily and efficiently"); |
| 41 | + Ok(()) |
| 42 | +} |
| 43 | + |
| 44 | +fn has_nanosecond_times(root: &Path) -> std::io::Result<bool> { |
| 45 | + let test_file = root.join("nanosecond-test"); |
| 46 | + |
| 47 | + std::fs::write(&test_file, "a")?; |
| 48 | + let first_time = test_file.metadata()?.modified()?; |
| 49 | + |
| 50 | + std::fs::write(&test_file, "b")?; |
| 51 | + let second_time = test_file.metadata()?.modified()?; |
| 52 | + |
| 53 | + Ok(first_time != second_time) |
| 54 | +} |
0 commit comments