Fix path_contains_hidden_folder (#6173)

* Fix path_contains_hidden_folder

Signed-off-by: nibon7 <nibon7@163.com>

* add test

Signed-off-by: nibon7 <nibon7@163.com>
This commit is contained in:
nibon7 2022-08-04 09:59:57 +08:00 committed by GitHub
parent 87823b0cb5
commit 7c49a42b68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View File

@ -343,11 +343,7 @@ fn is_hidden_dir(dir: impl AsRef<Path>) -> bool {
}
fn path_contains_hidden_folder(path: &Path, 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")))
{
if folders.iter().any(|p| path.starts_with(p.as_path())) {
return true;
}
false

View File

@ -513,3 +513,28 @@ fn list_a_directory_not_exists() {
assert!(actual.err.contains("directory not found"));
})
}
#[cfg(target_os = "linux")]
#[test]
fn list_directory_contains_invalid_utf8() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
Playground::setup(
"ls_test_directory_contains_invalid_utf8",
|dirs, _sandbox| {
let v: [u8; 4] = [7, 196, 144, 188];
let s = OsStr::from_bytes(&v);
let cwd = dirs.test();
let path = cwd.join(s);
std::fs::create_dir_all(&path).expect("failed to create directory");
let actual = nu!(cwd, "ls");
assert!(actual.out.contains("warning: get non-utf8 filename"));
assert!(actual.err.contains("No matches found for"));
},
)
}