Skip to content

Commit 486ed0e

Browse files
committed
test: Make sure that --json-input and --json-output are correct
1 parent a3a9320 commit 486ed0e

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

tests/json.rs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
#![cfg(feature = "cli")]
2+
#![cfg(feature = "json")]
3+
4+
pub mod _utils;
5+
pub use _utils::*;
6+
7+
use command_extra::CommandExtra;
8+
use parallel_disk_usage::{
9+
bytes_format::BytesFormat,
10+
data_tree::{DataTree, Reflection},
11+
fs_tree_builder::FsTreeBuilder,
12+
json_data::{JsonData, SchemaVersion},
13+
reporter::{ErrorOnlyReporter, ErrorReport},
14+
size::Bytes,
15+
size_getters::GET_APPARENT_SIZE,
16+
visualizer::{ColumnWidthDistribution, Direction, Visualizer},
17+
};
18+
use pipe_trait::Pipe;
19+
use pretty_assertions::assert_eq;
20+
use std::{
21+
convert::TryInto,
22+
io::Write,
23+
process::{Command, Output, Stdio},
24+
};
25+
26+
type SampleName = String;
27+
type SampleData = Bytes;
28+
type SampleReflection = Reflection<SampleName, SampleData>;
29+
type SampleTree = DataTree<SampleName, SampleData>;
30+
31+
fn stdout_text(
32+
Output {
33+
status,
34+
stdout,
35+
stderr,
36+
}: Output,
37+
) -> String {
38+
inspect_stderr(&stderr);
39+
assert!(
40+
status.success(),
41+
"progress exits with non-zero status: {:?}",
42+
status
43+
);
44+
stdout
45+
.pipe(String::from_utf8)
46+
.expect("parse stdout as UTF-8")
47+
.trim()
48+
.to_string()
49+
}
50+
51+
fn sample_tree() -> SampleTree {
52+
let dir = |name: &'static str, children: Vec<SampleTree>| {
53+
SampleTree::dir(name.to_string(), 1024.into(), children)
54+
};
55+
let file =
56+
|name: &'static str, size: u64| SampleTree::file(name.to_string(), Bytes::from(size));
57+
dir(
58+
"root",
59+
vec![
60+
file("foo", 2530),
61+
file("bar", 52),
62+
dir(
63+
"hello",
64+
vec![dir("world", vec![file("hello", 45), file("world", 54)])],
65+
),
66+
dir("empty dir", vec![]),
67+
dir(
68+
"directory with a really long name",
69+
vec![dir(
70+
"subdirectory with a really long name",
71+
vec![file("file with a really long name", 475)],
72+
)],
73+
),
74+
],
75+
)
76+
.into_par_sorted(|left, right| left.data().cmp(&right.data()).reverse())
77+
}
78+
79+
#[test]
80+
fn json_output() {
81+
let workspace = SampleWorkspace::default();
82+
let actual = Command::new(PDU)
83+
.with_current_dir(workspace.as_path())
84+
.with_arg("--json-output")
85+
.with_arg("--quantity=len")
86+
.with_arg("--min-ratio=0")
87+
.with_arg(workspace.as_path())
88+
.with_stdin(Stdio::null())
89+
.with_stdout(Stdio::piped())
90+
.with_stderr(Stdio::piped())
91+
.output()
92+
.expect("spawn command")
93+
.pipe(stdout_text)
94+
.pipe_as_ref(serde_json::from_str::<JsonData>)
95+
.expect("parse stdout as JsonData")
96+
.unit_and_tree
97+
.pipe(TryInto::<SampleReflection>::try_into)
98+
.expect("extract reflection")
99+
.pipe(sanitize_tree_reflection);
100+
dbg!(&actual);
101+
let builder = FsTreeBuilder {
102+
root: workspace.to_path_buf(),
103+
get_data: GET_APPARENT_SIZE,
104+
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
105+
};
106+
let expected = builder
107+
.pipe(DataTree::<_, Bytes>::from)
108+
.into_reflection()
109+
.par_convert_names_to_utf8()
110+
.expect("convert all names from raw strings to UTF-8")
111+
.pipe(sanitize_tree_reflection);
112+
dbg!(&expected);
113+
assert_eq!(actual, expected);
114+
}
115+
116+
#[test]
117+
fn json_input() {
118+
let json_data = JsonData {
119+
schema_version: SchemaVersion,
120+
binary_version: None,
121+
unit_and_tree: sample_tree().into_reflection().into(),
122+
};
123+
let json = serde_json::to_string_pretty(&json_data).expect("convert sample tree to JSON");
124+
eprintln!("JSON: {}", json);
125+
let workspace = Temp::new_dir().expect("create temporary directory");
126+
let mut child = Command::new(PDU)
127+
.with_current_dir(workspace.as_path())
128+
.with_arg("--json-input")
129+
.with_arg("--bytes-format=metric")
130+
.with_arg("--total-width=255")
131+
.with_arg("--max-depth=10")
132+
.with_stdin(Stdio::piped())
133+
.with_stdout(Stdio::piped())
134+
.with_stderr(Stdio::piped())
135+
.spawn()
136+
.expect("spawn command");
137+
child
138+
.stdin
139+
.as_mut()
140+
.expect("get stdin of child process")
141+
.write_all(json.as_bytes())
142+
.expect("write JSON string to child process's stdin");
143+
let actual = child
144+
.wait_with_output()
145+
.expect("wait for output of child process")
146+
.pipe(stdout_text);
147+
let actual = actual.trim();
148+
149+
let visualizer = Visualizer {
150+
data_tree: &sample_tree(),
151+
bytes_format: BytesFormat::MetricUnits,
152+
direction: Direction::BottomUp,
153+
column_width_distribution: ColumnWidthDistribution::Total { width: 255 },
154+
max_depth: 10.try_into().unwrap(),
155+
};
156+
let expected = format!("{}", visualizer);
157+
let expected = expected.trim();
158+
159+
assert_eq!(actual, expected);
160+
}
161+
162+
#[test]
163+
fn json_output_json_input() {
164+
let workspace = SampleWorkspace::default();
165+
166+
let json_output = Command::new(PDU)
167+
.with_current_dir(workspace.as_path())
168+
.with_arg("--json-output")
169+
.with_arg("--quantity=len")
170+
.with_arg(workspace.as_path())
171+
.with_stdin(Stdio::null())
172+
.with_stdout(Stdio::piped())
173+
.with_stderr(Stdio::piped())
174+
.spawn()
175+
.expect("spawn command with --json-output");
176+
let actual = Command::new(PDU)
177+
.with_current_dir(workspace.as_path())
178+
.with_arg("--json-input")
179+
.with_arg("--bytes-format=metric")
180+
.with_arg("--total-width=255")
181+
.with_arg("--max-depth=10")
182+
.with_stdin(
183+
json_output
184+
.stdout
185+
.expect("get stdout of command with --json-output")
186+
.into(),
187+
)
188+
.with_stdout(Stdio::piped())
189+
.with_stderr(Stdio::piped())
190+
.output()
191+
.expect("spawn command with --json-input")
192+
.pipe(stdout_text);
193+
194+
let expected = Command::new(PDU)
195+
.with_current_dir(workspace.as_path())
196+
.with_arg("--bytes-format=metric")
197+
.with_arg("--total-width=255")
198+
.with_arg("--max-depth=10")
199+
.with_arg(workspace.as_path())
200+
.with_stdin(Stdio::piped())
201+
.with_stdout(Stdio::piped())
202+
.with_stderr(Stdio::piped())
203+
.output()
204+
.expect("spawn command for expected")
205+
.pipe(stdout_text);
206+
207+
assert_eq!(actual, expected);
208+
}

0 commit comments

Comments
 (0)