Skip to content

Commit

Permalink
eprint problematic folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed May 12, 2024
1 parent e78690e commit 73b5072
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 52 deletions.
71 changes: 39 additions & 32 deletions src/dir_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,44 +142,49 @@ fn walk(dir: PathBuf, walk_data: &WalkData, depth: usize) -> Option<Node> {
.into_iter()
.par_bridge()
.filter_map(|entry| {
if let Ok(ref entry) = entry {
// uncommenting the below line gives simpler code but
// rayon doesn't parallelize as well giving a 3X performance drop
// hence we unravel the recursion a bit
match entry {
Ok(ref entry) => {
// uncommenting the below line gives simpler code but
// rayon doesn't parallelize as well giving a 3X performance drop
// hence we unravel the recursion a bit

// return walk(entry.path(), walk_data, depth)
// return walk(entry.path(), walk_data, depth)

if !ignore_file(entry, walk_data) {
if let Ok(data) = entry.file_type() {
if data.is_dir()
|| (walk_data.follow_links && data.is_symlink())
{
return walk(entry.path(), walk_data, depth + 1);
}
if !ignore_file(entry, walk_data) {
if let Ok(data) = entry.file_type() {
if data.is_dir()
|| (walk_data.follow_links && data.is_symlink())
{
return walk(entry.path(), walk_data, depth + 1);
}

let node = build_node(
entry.path(),
vec![],
walk_data.filter_regex,
walk_data.invert_filter_regex,
walk_data.use_apparent_size,
data.is_symlink(),
data.is_file(),
walk_data.by_filecount,
depth,
);
let node = build_node(
entry.path(),
vec![],
walk_data.filter_regex,
walk_data.invert_filter_regex,
walk_data.use_apparent_size,
data.is_symlink(),
data.is_file(),
walk_data.by_filecount,
depth,
);

prog_data.num_files.fetch_add(1, ORDERING);
if let Some(ref file) = node {
prog_data.total_file_size.fetch_add(file.size, ORDERING);
}
prog_data.num_files.fetch_add(1, ORDERING);
if let Some(ref file) = node {
prog_data
.total_file_size
.fetch_add(file.size, ORDERING);
}

return node;
return node;
}
}
}
} else {
let mut editable_error = errors.lock().unwrap();
editable_error.no_permissions = true
Err(ref failed) => {
let mut editable_error = errors.lock().unwrap();
editable_error.no_permissions.insert(failed.to_string());
}
}
None
})
Expand All @@ -189,7 +194,9 @@ fn walk(dir: PathBuf, walk_data: &WalkData, depth: usize) -> Option<Node> {
let mut editable_error = errors.lock().unwrap();
match failed.kind() {
std::io::ErrorKind::PermissionDenied => {
editable_error.no_permissions = true;
editable_error
.no_permissions
.insert(dir.to_string_lossy().into());
}
std::io::ErrorKind::NotFound => {
editable_error.file_not_found.insert(failed.to_string());
Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ fn main() {
}

let final_errors = walk_data.errors.lock().unwrap();
let failed_permissions = final_errors.no_permissions;
if !final_errors.file_not_found.is_empty() {
let err = final_errors
.file_not_found
Expand All @@ -279,8 +278,14 @@ fn main() {
.join(", ");
eprintln!("No such file or directory: {}", err);
}
if failed_permissions {
eprintln!("Did not have permissions for all directories");
if !final_errors.no_permissions.is_empty() {
let err = final_errors
.no_permissions
.iter()
.map(|a| a.as_ref())
.collect::<Vec<&str>>()
.join(", ");
eprintln!("Did not have permissions for directories: {}", err);
}
if !final_errors.unknown_error.is_empty() {
let err = final_errors
Expand Down
2 changes: 1 addition & 1 deletion src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl PAtomicInfo {

#[derive(Default)]
pub struct RuntimeErrors {
pub no_permissions: bool,
pub no_permissions: HashSet<String>,
pub file_not_found: HashSet<String>,
pub unknown_error: HashSet<String>,
pub abort: bool,
Expand Down
70 changes: 54 additions & 16 deletions tests/test_exact_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ fn initialize() {
});
}

fn exact_output_test<T: AsRef<OsStr>>(valid_outputs: Vec<String>, command_args: Vec<T>) {
fn exact_output_test<T: AsRef<OsStr>>(
command_args: Vec<T>,
valid_outputs: Vec<String>,
error_outputs: Vec<String>,
) {
initialize();

let mut a = &mut Command::cargo_bin("dust").unwrap();
Expand All @@ -50,25 +54,40 @@ fn exact_output_test<T: AsRef<OsStr>>(valid_outputs: Vec<String>, command_args:
a = a.arg(p);
}

let output = str::from_utf8(&a.unwrap().stdout).unwrap().to_owned();
if !valid_outputs.is_empty() {
let stdout_output = str::from_utf8(&a.unwrap().stdout).unwrap().to_owned();
let will_fail = valid_outputs.iter().any(|i| stdout_output.contains(i));
if !will_fail {
eprintln!(
"output(stdout):\n{}\ndoes not contain any of:\n{}",
stdout_output,
valid_outputs.join("\n\n")
);
}
assert!(will_fail);
}

let will_fail = valid_outputs.iter().any(|i| output.contains(i));
if !will_fail {
eprintln!(
"output:\n{}\ndoes not contain any of:\n{}",
output,
valid_outputs.join("\n\n")
);
// check stderr
if !error_outputs.is_empty() {
let stderr_output = str::from_utf8(&a.unwrap().stderr).unwrap().to_owned();
let will_fail = error_outputs.iter().any(|i| stderr_output.contains(i));
if !will_fail {
eprintln!(
"output(stderr):\n{}\ndoes not contain any of:\n{}",
stderr_output,
valid_outputs.join("\n\n")
);
}
assert!(will_fail);
}
assert!(will_fail)
}

// "windows" result data can vary by host (size seems to be variable by one byte); fix code vs test and re-enable
#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_main_basic() {
// -c is no color mode - This makes testing much simpler
exact_output_test(main_output(), vec!["-c", "-B", "/tmp/test_dir/"])
exact_output_test(vec!["-c", "-B", "/tmp/test_dir/"], main_output(), vec![]);
}

#[cfg_attr(target_os = "windows", ignore)]
Expand All @@ -81,7 +100,7 @@ pub fn test_main_multi_arg() {
"/tmp/test_dir",
"/tmp/test_dir",
];
exact_output_test(main_output(), command_args);
exact_output_test(command_args, main_output(), vec![]);
}

fn main_output() -> Vec<String> {
Expand Down Expand Up @@ -112,7 +131,7 @@ fn main_output() -> Vec<String> {
#[test]
pub fn test_main_long_paths() {
let command_args = vec!["-c", "-p", "-B", "/tmp/test_dir/"];
exact_output_test(main_output_long_paths(), command_args);
exact_output_test(command_args, main_output_long_paths(), vec![]);
}

fn main_output_long_paths() -> Vec<String> {
Expand Down Expand Up @@ -140,7 +159,7 @@ fn main_output_long_paths() -> Vec<String> {
#[test]
pub fn test_substring_of_names_and_long_names() {
let command_args = vec!["-c", "-B", "/tmp/test_dir2"];
exact_output_test(no_substring_of_names_output(), command_args);
exact_output_test(command_args, no_substring_of_names_output(), vec![]);
}

fn no_substring_of_names_output() -> Vec<String> {
Expand Down Expand Up @@ -174,7 +193,7 @@ fn no_substring_of_names_output() -> Vec<String> {
#[test]
pub fn test_unicode_directories() {
let command_args = vec!["-c", "-B", "/tmp/test_dir_unicode"];
exact_output_test(unicode_dir(), command_args);
exact_output_test(command_args, unicode_dir(), vec![]);
}

fn unicode_dir() -> Vec<String> {
Expand All @@ -201,7 +220,7 @@ fn unicode_dir() -> Vec<String> {
#[test]
pub fn test_apparent_size() {
let command_args = vec!["-c", "-s", "-b", "/tmp/test_dir"];
exact_output_test(apparent_size_output(), command_args);
exact_output_test(command_args, apparent_size_output(), vec![]);
}

fn apparent_size_output() -> Vec<String> {
Expand All @@ -222,3 +241,22 @@ fn apparent_size_output() -> Vec<String> {

vec![one_space_before, two_space_before]
}

#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_permission() {
Command::new("sh")
.arg("-c")
.arg("mkdir -p /tmp/unreadable_folder && chmod 000 /tmp/unreadable_folder")
.output()
.unwrap();
let command_args = vec!["/tmp/unreadable_folder"];
let permission_msg = r#"Did not have permissions"#.trim().to_string();
exact_output_test(command_args, vec![], vec![permission_msg]);

Command::new("sh")
.arg("-c")
.arg("chmod 555 /tmp/unreadable_folder")
.output()
.unwrap();
}

0 comments on commit 73b5072

Please sign in to comment.