diff --git a/crates/nu-command/src/filesystem/glob.rs b/crates/nu-command/src/filesystem/glob.rs index 885f4a4e64..7df54469d4 100644 --- a/crates/nu-command/src/filesystem/glob.rs +++ b/crates/nu-command/src/filesystem/glob.rs @@ -99,6 +99,16 @@ impl Command for Glob { let glob_pattern: Spanned = call.req(engine_state, stack, 0)?; let depth = call.get_flag(engine_state, stack, "depth")?; + if glob_pattern.item.is_empty() { + return Err(ShellError::GenericError( + "glob pattern must not be empty".to_string(), + "".to_string(), + Some(glob_pattern.span), + Some("add characters to the glob pattern".to_string()), + Vec::new(), + )); + } + let folder_depth = if let Some(depth) = depth { depth } else { diff --git a/crates/nu-command/tests/commands/glob.rs b/crates/nu-command/tests/commands/glob.rs new file mode 100644 index 0000000000..7a5e628e9f --- /dev/null +++ b/crates/nu-command/tests/commands/glob.rs @@ -0,0 +1,39 @@ +use nu_test_support::fs::Stub::EmptyFile; +use nu_test_support::playground::Playground; +use nu_test_support::{nu, pipeline}; + +#[test] +fn empty_glob_pattern_triggers_error() { + Playground::setup("glob_test_1", |dirs, sandbox| { + sandbox.with_files(vec![ + EmptyFile("yehuda.txt"), + EmptyFile("jonathan.txt"), + EmptyFile("andres.txt"), + ]); + + let actual = nu!( + cwd: dirs.test(), + "glob ''", + ); + + assert!(actual.err.contains("must not be empty")); + }) +} + +#[test] +fn nonempty_glob_lists_matching_paths() { + Playground::setup("glob_sanity_star", |dirs, sandbox| { + sandbox.with_files(vec![ + EmptyFile("yehuda.txt"), + EmptyFile("jonathan.txt"), + EmptyFile("andres.txt"), + ]); + + let actual = nu!( + cwd: dirs.test(), + pipeline("glob '*' | length"), + ); + + assert_eq!(actual.out, "3"); + }) +} diff --git a/crates/nu-command/tests/commands/mod.rs b/crates/nu-command/tests/commands/mod.rs index 1be98ba91d..dc06030b78 100644 --- a/crates/nu-command/tests/commands/mod.rs +++ b/crates/nu-command/tests/commands/mod.rs @@ -24,6 +24,7 @@ mod flatten; mod format; mod g; mod get; +mod glob; mod group_by; mod hash_; mod headers;