-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path_utils.rs
More file actions
332 lines (305 loc) · 9.89 KB
/
_utils.rs
File metadata and controls
332 lines (305 loc) · 9.89 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use build_fs_tree::{dir, file, Build, MergeableFileSystemTree};
use command_extra::CommandExtra;
use derive_more::{AsRef, Deref};
use parallel_disk_usage::{
data_tree::{DataTree, DataTreeReflection},
fs_tree_builder::FsTreeBuilder,
get_size::{self, GetSize},
os_string_display::OsStringDisplay,
reporter::ErrorOnlyReporter,
size,
};
use pipe_trait::Pipe;
use pretty_assertions::assert_eq;
use rand::{distr::Alphanumeric, rng, Rng};
use rayon::prelude::*;
use std::{
env::temp_dir,
fs::{create_dir, metadata, remove_dir_all},
io::Error,
path::{Path, PathBuf},
process::{Command, Output},
};
/// Default size getter method.
#[cfg(unix)]
pub const DEFAULT_GET_SIZE: get_size::GetBlockSize = get_size::GetBlockSize;
/// Default size getter method.
#[cfg(not(unix))]
pub const DEFAULT_GET_SIZE: get_size::GetApparentSize = get_size::GetApparentSize;
/// Representation of a temporary filesystem item.
///
/// **NOTE:** Delete this once https://github.com/samgiles/rs-mktemp/issues/8 is resolved.
#[derive(Debug, AsRef, Deref)]
#[as_ref(forward)]
#[deref(forward)]
pub struct Temp(PathBuf);
impl Temp {
/// Create a temporary directory.
pub fn new_dir() -> Result<Self, Error> {
let path = rng()
.sample_iter(&Alphanumeric)
.take(15)
.map(char::from)
.collect::<String>()
.pipe(|name| temp_dir().join(name));
if path.exists() {
return Self::new_dir();
}
create_dir(&path)?;
path.pipe(Temp).pipe(Ok)
}
}
impl Drop for Temp {
/// Delete the created temporary directory.
fn drop(&mut self) {
let path = &self.0;
if let Err(error) = remove_dir_all(path) {
eprintln!("warning: Failed to delete {path:?}: {error}");
}
}
}
/// Temporary workspace with sample filesystem tree.
#[derive(Debug, AsRef, Deref)]
#[as_ref(forward)]
#[deref(forward)]
pub struct SampleWorkspace(Temp);
impl Default for SampleWorkspace {
/// Set up a temporary directory for tests.
fn default() -> Self {
let temp = Temp::new_dir().expect("create working directory for sample workspace");
MergeableFileSystemTree::<&str, String>::from(dir! {
"flat" => dir! {
"0" => file!("")
"1" => file!("a".repeat(100_000))
"2" => file!("a".repeat(200_000))
"3" => file!("a".repeat(300_000))
}
"nested" => dir! {
"0" => dir! {
"1" => file!("a".repeat(500_000))
}
}
"empty-dir" => dir! {}
})
.build(&temp)
.expect("build the filesystem tree for the sample workspace");
SampleWorkspace(temp)
}
}
/// Make the snapshot of a [`TreeReflection`] testable.
///
/// The real filesystem is often messy, causing `children` to mess up its order.
/// This function makes the order of `children` deterministic by reordering them recursively.
pub fn sanitize_tree_reflection<Name, Size>(
tree_reflection: DataTreeReflection<Name, Size>,
) -> DataTreeReflection<Name, Size>
where
Name: Ord,
Size: size::Size,
DataTreeReflection<Name, Size>: Send,
{
let DataTreeReflection {
name,
size,
mut children,
} = tree_reflection;
children.sort_by(|left, right| left.name.cmp(&right.name));
let children = children
.into_par_iter()
.map(sanitize_tree_reflection)
.collect();
DataTreeReflection {
name,
size,
children,
}
}
/// Test the result of tree builder on the sample workspace.
pub fn test_sample_tree<Size, SizeGetter>(root: &Path, size_getter: SizeGetter)
where
Size: size::Size<Inner = u64> + From<u64> + Send + Sync,
SizeGetter: GetSize<Size = Size> + Copy + Sync,
{
let suffix_size = |suffix: &str| -> Size {
root.join(suffix)
.pipe(metadata)
.unwrap_or_else(|error| panic!("get_size {suffix}: {error}"))
.pipe(|ref metadata| size_getter.get_size(metadata))
};
macro_rules! suffix_size {
($suffix:expr $(,)?) => {
suffix_size($suffix)
};
($head:expr, $($tail:expr),* $(,)?) => {
suffix_size($head) + suffix_size!($($tail),*)
};
}
let measure = |suffix: &str| {
FsTreeBuilder {
size_getter,
reporter: ErrorOnlyReporter::new(|error| {
panic!("Unexpected call to report_error: {error:?}")
}),
root: root.join(suffix),
max_depth: 10,
}
.pipe(DataTree::<OsStringDisplay, Size>::from)
.into_par_sorted(|left, right| left.name().cmp(right.name()))
.into_reflection()
};
let sub = |suffix: &str| root.join(suffix).pipe(OsStringDisplay::os_string_from);
assert_eq!(
measure("flat"),
sanitize_tree_reflection(DataTreeReflection {
name: sub("flat"),
size: suffix_size!("flat", "flat/0", "flat/1", "flat/2", "flat/3"),
children: vec![
DataTreeReflection {
name: OsStringDisplay::os_string_from("0"),
size: suffix_size("flat/0"),
children: Vec::new(),
},
DataTreeReflection {
name: OsStringDisplay::os_string_from("1"),
size: suffix_size("flat/1"),
children: Vec::new(),
},
DataTreeReflection {
name: OsStringDisplay::os_string_from("2"),
size: suffix_size("flat/2"),
children: Vec::new(),
},
DataTreeReflection {
name: OsStringDisplay::os_string_from("3"),
size: suffix_size("flat/3"),
children: Vec::new(),
},
]
}),
);
assert_eq!(
measure("nested"),
sanitize_tree_reflection(DataTreeReflection {
name: sub("nested"),
size: suffix_size!("nested", "nested/0", "nested/0/1"),
children: vec![DataTreeReflection {
name: OsStringDisplay::os_string_from("0"),
size: suffix_size!("nested/0", "nested/0/1"),
children: vec![DataTreeReflection {
name: OsStringDisplay::os_string_from("1"),
size: suffix_size!("nested/0/1"),
children: Vec::new(),
}]
}],
}),
);
assert_eq!(
measure("empty-dir"),
sanitize_tree_reflection(DataTreeReflection {
name: sub("empty-dir"),
size: suffix_size!("empty-dir"),
children: Vec::new(),
}),
);
}
/// Path to the `pdu` executable
pub const PDU: &str = env!("CARGO_BIN_EXE_pdu");
/// Representation of a `pdu` command.
#[derive(Debug, Default, Clone)]
pub struct CommandRepresentation<'a> {
args: Vec<&'a str>,
}
impl<'a> CommandRepresentation<'a> {
/// Add an argument.
pub fn arg(mut self, arg: &'a str) -> Self {
self.args.push(arg);
self
}
}
/// List of `pdu` commands.
#[derive(Debug, Clone, AsRef, Deref)]
pub struct CommandList<'a>(Vec<CommandRepresentation<'a>>);
impl<'a> Default for CommandList<'a> {
/// Initialize a list with one `pdu` command.
fn default() -> Self {
CommandRepresentation::default()
.pipe(|x| vec![x])
.pipe(CommandList)
}
}
impl<'a> CommandList<'a> {
/// Duplicate the list with a flag argument.
///
/// The resulting list would include the original list with the flag
/// followed by the original list without the flag.
pub fn flag_matrix(self, name: &'a str) -> Self {
Self::assert_flag(name);
let CommandList(list) = self;
list.clone()
.into_iter()
.map(|cmd| cmd.arg(name))
.chain(list)
.collect::<Vec<_>>()
.pipe(CommandList)
}
/// Duplicate the list with one or many option argument(s).
///
/// The resulting list would include the original list with the option(s)
/// followed by the original list without the option(s).
pub fn option_matrix<const LEN: usize>(self, name: &'a str, values: [&'a str; LEN]) -> Self {
Self::assert_flag(name);
let CommandList(tail) = self;
let mut head: Vec<_> = values
.iter()
.copied()
.flat_map(|value| {
tail.clone()
.into_iter()
.map(move |cmd| cmd.arg(name).arg(value))
})
.collect();
head.extend(tail);
CommandList(head)
}
/// Create a list of `pdu` [command](Command).
pub fn commands(&'a self) -> impl Iterator<Item = Command> + 'a {
self.iter()
.map(|cmd| Command::new(PDU).with_args(&cmd.args))
}
/// Make sure a flag name has valid syntax.
fn assert_flag(name: &str) {
match name.len() {
0 | 1 => panic!("{name:?} is not a valid flag"),
2 => assert!(name.starts_with('-'), "{name:?} is not a valid flag"),
_ => assert!(name.starts_with("--"), "{name:?} is not a valid flag"),
}
}
}
/// Make sure that status code is 0, print stderr if it's not empty,
/// and turn stdin into a string.
pub fn stdout_text(
Output {
status,
stdout,
stderr,
}: Output,
) -> String {
inspect_stderr(&stderr);
assert!(
status.success(),
"progress exits with non-zero status: {status:?}",
);
stdout
.pipe(String::from_utf8)
.expect("parse stdout as UTF-8")
.trim_end()
.to_string()
}
/// Print stderr if it's not empty.
pub fn inspect_stderr(stderr: &[u8]) {
let text = String::from_utf8_lossy(stderr);
let text = text.trim();
if !text.is_empty() {
eprintln!("STDERR:\n{text}\n");
}
}