|
| 1 | +#![cfg(unix)] // This feature is not available in Windows |
| 2 | +#![cfg(feature = "cli")] |
| 3 | + |
| 4 | +pub mod _utils; |
| 5 | +pub use _utils::*; |
| 6 | + |
| 7 | +use command_extra::CommandExtra; |
| 8 | +use parallel_disk_usage::{ |
| 9 | + json_data::{JsonData, UnitAndTree}, |
| 10 | + size::Bytes, |
| 11 | +}; |
| 12 | +use pipe_trait::Pipe; |
| 13 | +use pretty_assertions::assert_eq; |
| 14 | +use std::{ |
| 15 | + ops::{Add, Mul}, |
| 16 | + process::{Command, Stdio}, |
| 17 | +}; |
| 18 | + |
| 19 | +fn stdio(command: Command) -> Command { |
| 20 | + command |
| 21 | + .with_stdin(Stdio::null()) |
| 22 | + .with_stdout(Stdio::piped()) |
| 23 | + .with_stderr(Stdio::piped()) |
| 24 | +} |
| 25 | + |
| 26 | +#[test] |
| 27 | +fn deduplicate_multiple_hardlinks_to_a_single_file() { |
| 28 | + let workspace = SampleWorkspace::multiple_hardlinks_to_a_single_file(); |
| 29 | + |
| 30 | + let json = Command::new(PDU) |
| 31 | + .with_current_dir(&workspace) |
| 32 | + .with_arg("--quantity=apparent-size") |
| 33 | + .with_arg("--deduplicate-hardlinks") |
| 34 | + .with_arg("--json-output") |
| 35 | + .pipe(stdio) |
| 36 | + .output() |
| 37 | + .expect("spawn command") |
| 38 | + .pipe(stdout_text) |
| 39 | + .pipe_as_ref(serde_json::from_str::<JsonData>) |
| 40 | + .expect("parse stdout as JsonData"); |
| 41 | + |
| 42 | + let actual_size = match &json.unit_and_tree { |
| 43 | + UnitAndTree::Blocks(_) => panic!("expecting Bytes, but got {:?}", &json.unit_and_tree), |
| 44 | + UnitAndTree::Bytes(tree) => tree.size, |
| 45 | + }; |
| 46 | + |
| 47 | + let expected_size = workspace |
| 48 | + .join("file.txt") |
| 49 | + .pipe_as_ref(read_apparent_size) |
| 50 | + .mul(2) // TODO: fix the algorithm and remove this line |
| 51 | + .add(read_apparent_size(&workspace)) |
| 52 | + .pipe(Bytes::new); |
| 53 | + |
| 54 | + assert_eq!(actual_size, expected_size); |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn do_not_deduplicate_multiple_hardlinks_to_a_single_file() { |
| 59 | + let workspace = SampleWorkspace::multiple_hardlinks_to_a_single_file(); |
| 60 | + |
| 61 | + let json = Command::new(PDU) |
| 62 | + .with_current_dir(&workspace) |
| 63 | + .with_arg("--quantity=apparent-size") |
| 64 | + .with_arg("--json-output") |
| 65 | + .pipe(stdio) |
| 66 | + .output() |
| 67 | + .expect("spawn command") |
| 68 | + .pipe(stdout_text) |
| 69 | + .pipe_as_ref(serde_json::from_str::<JsonData>) |
| 70 | + .expect("parse stdout as JsonData"); |
| 71 | + |
| 72 | + let actual_size = match &json.unit_and_tree { |
| 73 | + UnitAndTree::Blocks(_) => panic!("expecting Bytes, but got {:?}", &json.unit_and_tree), |
| 74 | + UnitAndTree::Bytes(tree) => tree.size, |
| 75 | + }; |
| 76 | + |
| 77 | + let expected_size = workspace |
| 78 | + .join("file.txt") |
| 79 | + .pipe_as_ref(read_apparent_size) |
| 80 | + .mul(11) |
| 81 | + .add(read_apparent_size(&workspace)) |
| 82 | + .pipe(Bytes::new); |
| 83 | + |
| 84 | + assert_eq!(actual_size, expected_size); |
| 85 | +} |
0 commit comments