| 1 | use crate::support::*; |
| 2 | use publication::{CancelDecision, Limits, Outcome, Publication, Request, Stage, Stop}; |
| 3 | use std::{ffi::OsStr, fs, os::unix::fs::MetadataExt}; |
| 4 | |
| 5 | fn replace_parent(stage: Stage) { |
| 6 | let sandbox = Sandbox::new(); |
| 7 | let parent = sandbox.parent(); |
| 8 | let before = fs::metadata(&parent).unwrap(); |
| 9 | let moved = sandbox.path("moved"); |
| 10 | let mut opening = 0; |
| 11 | let mut changed = false; |
| 12 | let (task, _) = Publication::new(); |
| 13 | let report = task.publish( |
| 14 | Request { |
| 15 | parent: &parent, |
| 16 | leaf: OsStr::new("output"), |
| 17 | bytes: b"new-output", |
| 18 | limits: Limits::default(), |
| 19 | }, |
| 20 | |point| { |
| 21 | if point.stage == Stage::Opening { |
| 22 | opening += 1; |
| 23 | } |
| 24 | if !changed && point.stage == stage && (stage != Stage::Opening || opening == 2) { |
| 25 | fs::rename(&parent, &moved).unwrap(); |
| 26 | sandbox.dir("parent"); |
| 27 | sandbox.file("parent/output", b"replacement-parent"); |
| 28 | changed = true; |
| 29 | } |
| 30 | Ok::<_, Stop<()>>(()) |
| 31 | }, |
| 32 | ); |
| 33 | assert!(changed); |
| 34 | assert_eq!( |
| 35 | ( |
| 36 | fs::metadata(&moved).unwrap().dev(), |
| 37 | fs::metadata(&moved).unwrap().ino() |
| 38 | ), |
| 39 | (before.dev(), before.ino()) |
| 40 | ); |
| 41 | assert_eq!( |
| 42 | fs::read(parent.join("output")).unwrap(), |
| 43 | b"replacement-parent" |
| 44 | ); |
| 45 | committed(report, &moved, OsStr::new("output"), b"new-output"); |
| 46 | sandbox.finish(); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn parent_replaced_after_open_commits_to_held_directory() { |
| 51 | replace_parent(Stage::Opening); |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn parent_replaced_before_commit_keeps_receipt_on_old_parent() { |
| 56 | replace_parent(Stage::BeforeCommit); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn destination_created_at_last_checkpoint_is_preserved() { |
| 61 | let sandbox = Sandbox::new(); |
| 62 | let parent = sandbox.parent(); |
| 63 | let (task, _) = Publication::new(); |
| 64 | let mut inserted = false; |
| 65 | let report = task.publish( |
| 66 | Request { |
| 67 | parent: &parent, |
| 68 | leaf: OsStr::new("output"), |
| 69 | bytes: b"new", |
| 70 | limits: Limits::default(), |
| 71 | }, |
| 72 | |point| { |
| 73 | if point.stage == Stage::BeforeCommit { |
| 74 | sandbox.file("parent/output", b"late-existing"); |
| 75 | inserted = true; |
| 76 | } |
| 77 | Ok::<_, Stop<()>>(()) |
| 78 | }, |
| 79 | ); |
| 80 | assert!(inserted); |
| 81 | conflict(report, 3); |
| 82 | assert_eq!(fs::read(parent.join("output")).unwrap(), b"late-existing"); |
| 83 | sandbox.finish(); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn removed_parent_returns_uncertain_original_errno_without_receipt() { |
| 88 | let sandbox = Sandbox::new(); |
| 89 | let parent = sandbox.parent(); |
| 90 | let (task, cancel) = Publication::new(); |
| 91 | let mut removed = false; |
| 92 | let report = task.publish( |
| 93 | Request { |
| 94 | parent: &parent, |
| 95 | leaf: OsStr::new("output"), |
| 96 | bytes: b"abc", |
| 97 | limits: Limits::default(), |
| 98 | }, |
| 99 | |point| { |
| 100 | if point.stage == Stage::BeforeCommit { |
| 101 | assert!(names(&parent).is_empty()); |
| 102 | fs::remove_dir(&parent).unwrap(); |
| 103 | removed = true; |
| 104 | } |
| 105 | Ok::<_, Stop<()>>(()) |
| 106 | }, |
| 107 | ); |
| 108 | assert!(removed); |
| 109 | match report.outcome { |
| 110 | Outcome::CommitUncertain { source } => { |
| 111 | assert_eq!(source.raw_os_error(), Some(libc::ENOENT)) |
| 112 | } |
| 113 | Outcome::NotCommitted(error) => panic!("uncertainty erased: {error:?}"), |
| 114 | Outcome::Committed { .. } => panic!("unexpected receipt"), |
| 115 | } |
| 116 | full_usage(report.usage, 3, false); |
| 117 | cleanup(&report.cleanup, 4); |
| 118 | assert_eq!(cancel.request(), CancelDecision::Finished); |
| 119 | assert!(!parent.exists()); |
| 120 | assert!(names(&sandbox.root).is_empty()); |
| 121 | sandbox.finish(); |
| 122 | } |