diff options
author | nibon7 <nibon7@163.com> | 2022-07-27 23:53:00 +0800 |
---|---|---|
committer | JT <547158+jntrnr@users.noreply.github.com> | 2022-07-28 09:16:52 +1200 |
commit | 370639d7d7d3a4c2a57b0757f56c0f8a64241d0b (patch) | |
tree | 4a32ad0d0a1cae7d8f369a29937c6e70a6ddaab1 | |
parent | d326f6def6f9005963cea1c4164ef9114e2ad234 (diff) |
Fix ls panics when a file or directory not exists (#6148)0.66.1
* Fix ls panics when a file or directory not exists
Fixes #6146
Signed-off-by: nibon7 <nibon7@163.com>
* add test
Signed-off-by: nibon7 <nibon7@163.com>
-rw-r--r-- | crates/nu-command/src/filesystem/ls.rs | 10 | ||||
-rw-r--r-- | crates/nu-command/tests/commands/ls.rs | 8 |
2 files changed, 15 insertions, 3 deletions
diff --git a/crates/nu-command/src/filesystem/ls.rs b/crates/nu-command/src/filesystem/ls.rs index a9e98885e..0fa94e1cc 100644 --- a/crates/nu-command/src/filesystem/ls.rs +++ b/crates/nu-command/src/filesystem/ls.rs @@ -147,9 +147,13 @@ impl Command for Ls { Some(glob_options) }; let (prefix, paths) = - nu_engine::glob_from(&glob_path, &cwd, call_span, glob_options) - .expect("glob failure"); - + match nu_engine::glob_from(&glob_path, &cwd, call_span, glob_options) { + Ok((prefix, paths)) => (prefix, paths), + Err(e) => { + shell_errors.push(e); + return Vec::from([Value::nothing(call_span)]).into_iter(); + } + }; let mut paths_peek = paths.peekable(); if paths_peek.peek().is_none() { shell_errors.push(ShellError::GenericError( diff --git a/crates/nu-command/tests/commands/ls.rs b/crates/nu-command/tests/commands/ls.rs index 88703d168..93d09ea9e 100644 --- a/crates/nu-command/tests/commands/ls.rs +++ b/crates/nu-command/tests/commands/ls.rs @@ -527,3 +527,11 @@ fn can_list_system_folder() { )); assert_eq!(ls_with_filter.err, ""); } + +#[test] +fn list_a_directory_not_exists() { + Playground::setup("ls_test_directory_not_exists", |dirs, _sandbox| { + let actual = nu!(cwd: dirs.test(), "ls a_directory_not_exists"); + assert!(actual.err.contains("directory not found")); + }) +} |