| 1 | use crate::{fixtures::*, support::*}; |
| 2 | use job::{Event, FileJob, Limits, Outcome, Stop, publication, snapshot}; |
| 3 | use std::{ffi::OsStr, fs, os::unix::fs::symlink}; |
| 4 | |
| 5 | #[test] |
| 6 | fn existing_file_bytes_and_identity_survive_conflict() { |
| 7 | let sandbox = Sandbox::new(); |
| 8 | let input = sandbox.file("input.gz", &stored(b"abc")); |
| 9 | let existing = sandbox.file("parent/output", b"preserve"); |
| 10 | let before = identity(&existing); |
| 11 | let (report, _) = run(&input, &sandbox.parent(), Limits::default()); |
| 12 | conflict(&report, 3); |
| 13 | assert_eq!(fs::read(&existing).unwrap(), b"preserve"); |
| 14 | assert_eq!(identity(&existing), before); |
| 15 | assert_eq!( |
| 16 | names(&sandbox.parent()), |
| 17 | vec![std::ffi::OsString::from("output")] |
| 18 | ); |
| 19 | sandbox.finish(); |
| 20 | } |
| 21 | |
| 22 | #[test] |
| 23 | fn existing_directory_and_child_survive_conflict() { |
| 24 | let sandbox = Sandbox::new(); |
| 25 | let input = sandbox.file("input.gz", &stored(b"abc")); |
| 26 | let existing = sandbox.dir("parent/output"); |
| 27 | let child = sandbox.file("parent/output/child", b"preserve"); |
| 28 | let before = identity(&existing); |
| 29 | let child_before = identity(&child); |
| 30 | let (report, _) = run(&input, &sandbox.parent(), Limits::default()); |
| 31 | conflict(&report, 3); |
| 32 | assert_eq!(identity(&existing), before); |
| 33 | assert_eq!(identity(&child), child_before); |
| 34 | assert_eq!(fs::read(child).unwrap(), b"preserve"); |
| 35 | assert_eq!(names(&existing), vec![std::ffi::OsString::from("child")]); |
| 36 | sandbox.finish(); |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn existing_symlink_and_target_survive_conflict() { |
| 41 | let sandbox = Sandbox::new(); |
| 42 | let input = sandbox.file("input.gz", &stored(b"abc")); |
| 43 | let victim = sandbox.file("victim", b"preserve"); |
| 44 | let existing = sandbox.parent().join("output"); |
| 45 | symlink(&victim, &existing).unwrap(); |
| 46 | let before = identity(&existing); |
| 47 | let victim_before = identity(&victim); |
| 48 | let (report, _) = run(&input, &sandbox.parent(), Limits::default()); |
| 49 | conflict(&report, 3); |
| 50 | assert_eq!(identity(&existing), before); |
| 51 | assert_eq!(identity(&victim), victim_before); |
| 52 | assert_eq!(fs::read_link(existing).unwrap(), victim); |
| 53 | assert_eq!(fs::read(victim).unwrap(), b"preserve"); |
| 54 | sandbox.finish(); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn identical_input_and_output_name_preserves_archive() { |
| 59 | let sandbox = Sandbox::new(); |
| 60 | let archive = stored(b"abc"); |
| 61 | let input = sandbox.file("input.gz", &archive); |
| 62 | let before = identity(&input); |
| 63 | let (task, cancel) = FileJob::new(); |
| 64 | let mut trace = Trace::default(); |
| 65 | let report = task.run( |
| 66 | job::Request { |
| 67 | input: &input, |
| 68 | output_parent: &sandbox.root, |
| 69 | output_leaf: OsStr::new("input.gz"), |
| 70 | limits: Limits::default(), |
| 71 | }, |
| 72 | |point| { |
| 73 | trace.push(point); |
| 74 | Ok::<_, Stop<()>>(()) |
| 75 | }, |
| 76 | ); |
| 77 | trace.finish(&report); |
| 78 | conflict(&report, 3); |
| 79 | assert_eq!(cancel.request(), publication::CancelDecision::Finished); |
| 80 | assert_eq!(identity(&input), before); |
| 81 | assert_eq!(fs::read(input).unwrap(), archive); |
| 82 | sandbox.finish(); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn missing_owned_input_preserves_native_open_error() { |
| 87 | let sandbox = Sandbox::new(); |
| 88 | let (report, trace) = run( |
| 89 | &sandbox.path("missing.gz"), |
| 90 | &sandbox.parent(), |
| 91 | Limits::default(), |
| 92 | ); |
| 93 | match &report.outcome { |
| 94 | Outcome::SnapshotFailed(snapshot::Failure { |
| 95 | cause: snapshot::Error::Io { operation, source }, |
| 96 | usage, |
| 97 | }) => { |
| 98 | assert_eq!(*operation, snapshot::IoOperation::PinPath); |
| 99 | assert_eq!(source.raw_os_error(), Some(libc::ENOENT)); |
| 100 | assert_eq!( |
| 101 | *usage, |
| 102 | snapshot::Usage { |
| 103 | open_calls: 1, |
| 104 | ..snapshot::Usage::default() |
| 105 | } |
| 106 | ); |
| 107 | } |
| 108 | _ => panic!("원래 입력 open 오류가 아님"), |
| 109 | } |
| 110 | trace.finish(&report); |
| 111 | assert!(names(&sandbox.parent()).is_empty()); |
| 112 | sandbox.finish(); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn replaced_parent_path_does_not_redirect_pinned_commit() { |
| 117 | let sandbox = Sandbox::new(); |
| 118 | let input = sandbox.file("input.gz", &stored(b"abc")); |
| 119 | let parent = sandbox.parent(); |
| 120 | let (task, cancel) = FileJob::new(); |
| 121 | let mut trace = Trace::default(); |
| 122 | let mut replaced = false; |
| 123 | let report = task.run(request(&input, &parent, Limits::default()), |point| { |
| 124 | trace.push(point); |
| 125 | if matches!(point.event, Event::Publication(native) if native.stage == publication::Stage::BeforeCommit) { |
| 126 | fs::rename(&parent, sandbox.path("old-parent")).unwrap(); |
| 127 | sandbox.dir("parent"); |
| 128 | replaced = true; |
| 129 | } |
| 130 | Ok::<_, Stop<()>>(()) |
| 131 | }); |
| 132 | assert!(replaced); |
| 133 | trace.finish(&report); |
| 134 | assert_eq!(cancel.request(), publication::CancelDecision::Finished); |
| 135 | committed(&report, &sandbox.path("old-parent"), b"abc"); |
| 136 | assert!(names(&parent).is_empty()); |
| 137 | sandbox.finish(); |
| 138 | } |