-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcli_errors.rs
More file actions
175 lines (160 loc) · 5.45 KB
/
cli_errors.rs
File metadata and controls
175 lines (160 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![cfg(feature = "cli")]
pub mod _utils;
pub use _utils::*;
use command_extra::CommandExtra;
use pipe_trait::Pipe;
use pretty_assertions::assert_eq;
use std::process::{Command, Output, Stdio};
use text_block_macros::text_block;
#[cfg(unix)]
use maplit::btreeset;
#[cfg(unix)]
use parallel_disk_usage::{
bytes_format::BytesFormat,
data_tree::DataTree,
fs_tree_builder::FsTreeBuilder,
get_size::GetApparentSize,
hardlink::HardlinkIgnorant,
os_string_display::OsStringDisplay,
reporter::{ErrorOnlyReporter, ErrorReport},
visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer},
};
#[cfg(unix)]
use std::{collections::BTreeSet, path::Path};
fn stdio(command: Command) -> Command {
command
.with_stdin(Stdio::null())
.with_stdout(Stdio::piped())
.with_stderr(Stdio::piped())
}
#[cfg(unix)]
fn fs_permission(path: impl AsRef<Path>, permission: &'static str, recursive: bool) {
let Output { status, stderr, .. } = Command::new("chmod")
.pipe(|cmd| if recursive { cmd.with_arg("-R") } else { cmd })
.with_arg(permission)
.with_arg(path.as_ref())
.with_stdin(Stdio::null())
.with_stdout(Stdio::null())
.with_stderr(Stdio::piped())
.output()
.expect("run chmod command");
inspect_stderr(&stderr);
assert!(status.success(), "chmod fails {status:?}");
}
#[test]
fn min_ratio_1() {
let workspace = SampleWorkspace::default();
let Output {
status,
stdout,
stderr,
} = Command::new(PDU)
.with_current_dir(&workspace)
.with_arg("--min-ratio=1")
.pipe(stdio)
.output()
.expect("spawn command");
let stderr = String::from_utf8(stderr).expect("parse stderr as UTF-8");
let stderr = stderr.trim_end();
dbg!(&status);
eprintln!("STDERR:\n{stderr}\n");
assert!(!status.success());
assert_eq!(
stderr,
text_block! {
"error: invalid value '1' for '--min-ratio <MIN_RATIO>': greater than or equal to 1"
""
"For more information, try '--help'."
}
);
assert_eq!(&stdout, &[] as &[u8]);
}
#[test]
fn max_depth_0() {
let workspace = SampleWorkspace::default();
let Output {
status,
stdout,
stderr,
} = Command::new(PDU)
.with_current_dir(&workspace)
.with_arg("--max-depth=0")
.pipe(stdio)
.output()
.expect("spawn command");
let stderr = String::from_utf8(stderr).expect("parse stderr as UTF-8");
let stderr = stderr.trim_end();
dbg!(&status);
eprintln!("STDERR:\n{stderr}\n");
assert!(!status.success());
assert_eq!(
stderr,
text_block! {
r#"error: invalid value '0' for '--max-depth <MAX_DEPTH>': Value is neither "inf" nor a positive integer: number would be zero for non-zero type"#
""
"For more information, try '--help'."
}
);
assert_eq!(&stdout, &[] as &[u8]);
}
#[cfg(unix)]
#[test]
fn fs_errors() {
if unsafe { libc::geteuid() } == 0 {
panic!(
"{}\n{}",
"error: This test must not be run as root because running with elevated privileges would affect its accuracy.",
"hint: Either run this test as a non-root user or rerun via `TEST_SKIP='fs_errors' ./test.sh` to skip this test.",
);
}
let workspace = SampleWorkspace::default();
fs_permission(workspace.join("empty-dir"), "-r", false);
fs_permission(workspace.join("nested").join("0"), "-r", false);
let Output {
status,
stdout,
stderr,
} = Command::new(PDU)
.with_current_dir(&workspace)
.with_arg("--min-ratio=0")
.with_arg("--total-width=100")
.with_arg("--quantity=apparent-size")
.pipe(stdio)
.output()
.expect("spawn command");
let stderr = String::from_utf8(stderr).expect("parse stderr as UTF-8");
let stdout = String::from_utf8(stdout).expect("parse stdout as UTF-8");
dbg!(&status);
eprintln!("STDERR+STDOUT:\n{stderr}{stdout}\n");
let builder = FsTreeBuilder {
root: workspace.to_path_buf(),
size_getter: GetApparentSize,
hardlinks_recorder: &HardlinkIgnorant,
reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT),
max_depth: 10,
};
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();
data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse());
*data_tree.name_mut() = OsStringDisplay::os_string_from(".");
let visualizer = Visualizer {
data_tree: &data_tree,
bytes_format: BytesFormat::MetricUnits,
direction: Direction::BottomUp,
bar_alignment: BarAlignment::Left,
column_width_distribution: ColumnWidthDistribution::total(100),
};
let expected_stdout = format!("{visualizer}");
eprintln!("EXPECTED STDOUT:\n{}\n", &expected_stdout);
fs_permission(&workspace, "+rwx", true); // to allow SampleWorkspace destructor to clean itself
let actual_stderr_lines: BTreeSet<_> = stderr
.trim_end()
.lines()
.map(|line| line.trim_start_matches('\r'))
.collect();
let expected_stderr_lines = btreeset! {
"[error] read_dir \"./nested/0\": Permission denied (os error 13)",
"[error] read_dir \"./empty-dir\": Permission denied (os error 13)",
};
assert_eq!(actual_stderr_lines, expected_stderr_lines);
assert_eq!(stdout, expected_stdout);
}