team-ljyzipelo

공개
105줄3.2 KB
1use crate::{fixtures::*, support::*};
2use job::{Event, FileJob, Limits, Stop, gzip, publication};
3use std::fs;
4
5fn boundary(length: usize) {
6 let sandbox = Sandbox::new();
7 let bytes = pattern(length);
8 let archive = stored(&bytes);
9 let input = sandbox.file("input.gz", &archive);
10 let before = identity(&input);
11 let parent = sandbox.parent();
12 let (report, trace) = run(&input, &parent, Limits::default());
13 observations(&report, (before.0, before.1), &archive, &bytes, 10);
14 committed(&report, &parent, &bytes);
15 assert_eq!(identity(&input), before);
16 assert_eq!(fs::read(&input).unwrap(), archive);
17 assert!(
18 matches!(trace.points.last().unwrap().event, Event::Publication(point) if point.stage == publication::Stage::BeforeCommit)
19 );
20 assert!(owned_descriptors(&sandbox.root).is_empty());
21 sandbox.finish();
22}
23
24#[test]
25fn empty_output() {
26 boundary(0);
27}
28#[test]
29fn one_byte_output() {
30 boundary(1);
31}
32#[test]
33fn known_bytes_output() {
34 boundary(3);
35}
36#[test]
37fn chunk_minus_one_output() {
38 boundary(65_535);
39}
40#[test]
41fn chunk_output() {
42 boundary(65_536);
43}
44#[test]
45fn chunk_plus_one_output() {
46 boundary(65_537);
47}
48
49#[test]
50fn header_name_and_comment_never_select_destination() {
51 let sandbox = Sandbox::new();
52 let bytes = b"abc";
53 assert_eq!(crc32(b"123456789"), 0xcbf4_3926);
54 let archive = named(bytes);
55 let header = 10 + b"../header-name\0../comment-name\0".len();
56 let input = sandbox.file("input.gz", &archive);
57 let before = identity(&input);
58 let parent = sandbox.parent();
59 let (report, _) = run(&input, &parent, Limits::default());
60 observations(&report, (before.0, before.1), &archive, bytes, header);
61 committed(&report, &parent, bytes);
62 assert_eq!(
63 names(&sandbox.root),
64 vec![
65 std::ffi::OsString::from("input.gz"),
66 std::ffi::OsString::from("parent")
67 ]
68 );
69 assert_eq!(identity(&input), before);
70 sandbox.finish();
71}
72
73#[test]
74fn replacing_input_after_snapshot_uses_captured_owned_bytes() {
75 let sandbox = Sandbox::new();
76 let archive = stored(b"original");
77 let replacement = stored(b"replacement");
78 let input = sandbox.file("input.gz", &archive);
79 let before = identity(&input);
80 let parent = sandbox.parent();
81 let (task, cancel) = FileJob::new();
82 let mut changed = false;
83 let mut trace = Trace::default();
84 let report = task.run(request(&input, &parent, Limits::default()), |point| {
85 trace.push(point);
86 if matches!(point.event, Event::Decode(native) if native.stage == gzip::Stage::Start) {
87 fs::rename(&input, sandbox.path("old.gz")).unwrap();
88 sandbox.file("input.gz", &replacement);
89 changed = true;
90 }
91 Ok::<_, Stop<()>>(())
92 });
93 assert!(changed);
94 trace.finish(&report);
95 assert_eq!(cancel.request(), publication::CancelDecision::Finished);
96 observations(&report, (before.0, before.1), &archive, b"original", 10);
97 committed(&report, &parent, b"original");
98 assert_eq!(fs::read(&input).unwrap(), replacement);
99 assert_eq!(fs::read(sandbox.path("old.gz")).unwrap(), archive);
100 assert_ne!(
101 (identity(&input).0, identity(&input).1),
102 (before.0, before.1)
103 );
104 sandbox.finish();
105}