From 16f85f32a223274f725594057ae2187eaea086a3 Mon Sep 17 00:00:00 2001 From: Abhi V Date: Sat, 5 Sep 2020 01:02:58 +0530 Subject: [PATCH] `ls **/*` does not show hidden files without the `-a` flag (#2407) * fixed: .*.(ext|*) * ls **/* does not return hidden files without the -a flag * fixed formatting * fixed clippy issues * fixed clippy issues, v2 * added `#[cfg(unix)]` to windows-failing test --- crates/nu-cli/src/shell/filesystem_shell.rs | 20 +++++++++++ crates/nu-cli/tests/commands/ls.rs | 38 +++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/crates/nu-cli/src/shell/filesystem_shell.rs b/crates/nu-cli/src/shell/filesystem_shell.rs index ec4d75551..98f825fd0 100644 --- a/crates/nu-cli/src/shell/filesystem_shell.rs +++ b/crates/nu-cli/src/shell/filesystem_shell.rs @@ -139,6 +139,8 @@ impl Shell for FilesystemShell { )); } + let mut hidden_dirs = vec![]; + // Generated stream: impl Stream Ok(futures::stream::iter(paths.filter_map(move |path| { @@ -147,7 +149,14 @@ impl Shell for FilesystemShell { Err(err) => return Some(Err(err)), }; + if path_contains_hidden_folder(&path, &hidden_dirs) { + return None; + } + if !all && !hidden_dir_specified && is_hidden_dir(&path) { + if path.is_dir() { + hidden_dirs.push(path); + } return None; } @@ -1001,3 +1010,14 @@ pub(crate) fn dir_entry_dict( Ok(dict.into_value()) } + +fn path_contains_hidden_folder(path: &PathBuf, folders: &[PathBuf]) -> bool { + let path_str = path.to_str().expect("failed to read path"); + if folders + .iter() + .any(|p| path_str.starts_with(&p.to_str().expect("failed to read hidden paths"))) + { + return true; + } + false +} diff --git a/crates/nu-cli/tests/commands/ls.rs b/crates/nu-cli/tests/commands/ls.rs index e442586fb..94829aa2e 100644 --- a/crates/nu-cli/tests/commands/ls.rs +++ b/crates/nu-cli/tests/commands/ls.rs @@ -214,6 +214,44 @@ fn lists_all_hidden_files_when_glob_contains_dot() { }) } +#[test] +// TODO Remove this cfg value when we have an OS-agnostic way +// of creating hidden files using the playground. +#[cfg(unix)] +fn lists_all_hidden_files_when_glob_does_not_contain_dot() { + Playground::setup("ls_test_8", |dirs, sandbox| { + sandbox + .with_files(vec![ + EmptyFile("root1.txt"), + EmptyFile("root2.txt"), + EmptyFile(".dotfile1"), + ]) + .within("dir_a") + .with_files(vec![ + EmptyFile("yehuda.10.txt"), + EmptyFile("jonathan.10.txt"), + EmptyFile(".dotfile2"), + ]) + .within(".dir_b") + .with_files(vec![ + EmptyFile("andres.10.txt"), + EmptyFile("chicken_not_to_be_picked_up.100.txt"), + EmptyFile(".dotfile3"), + ]); + + let actual = nu!( + cwd: dirs.test(), pipeline( + r#" + ls **/* + | count + | echo $it + "# + )); + + assert_eq!(actual.out, "5"); + }) +} + #[test] fn list_all_columns() { Playground::setup(