diff --git a/crates/nu-cli/src/completions/dotnu_completions.rs b/crates/nu-cli/src/completions/dotnu_completions.rs index 1ab38ddef5..debc6c7c0e 100644 --- a/crates/nu-cli/src/completions/dotnu_completions.rs +++ b/crates/nu-cli/src/completions/dotnu_completions.rs @@ -58,7 +58,7 @@ impl Completer for DotNuCompletion { }; // Check if the base_dir is a folder - if base_dir != "./" { + if base_dir != format!(".{}", SEP) { // Add the base dir into the directories to be searched search_dirs.push(base_dir.clone()); diff --git a/crates/nu-cli/tests/test_completions.rs b/crates/nu-cli/tests/test_completions.rs index 209edd6824..0807902fbc 100644 --- a/crates/nu-cli/tests/test_completions.rs +++ b/crates/nu-cli/tests/test_completions.rs @@ -2,11 +2,51 @@ use std::path::PathBuf; use nu_cli::NuCompleter; use nu_command::create_default_context; -use nu_protocol::engine::{EngineState, Stack}; +use nu_protocol::{ + engine::{EngineState, Stack, StateDelta}, + Value, +}; use nu_test_support::fs; use reedline::{Completer, Suggestion}; const SEP: char = std::path::MAIN_SEPARATOR; +#[test] +fn dotnu_completions() { + // Create a new engine + let (_, dir_str, engine) = new_engine(); + + let mut stack = Stack::new(); + + // Add pwd as env var + stack.add_env_var( + "PWD".to_string(), + Value::String { + val: dir_str.clone(), + span: nu_protocol::Span { + start: 0, + end: dir_str.len(), + }, + }, + ); + + // Instatiate a new completer + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + // Test source completion + let completion_str = "source ".to_string(); + let suggestions = completer.complete(&completion_str, completion_str.len()); + + assert_eq!(1, suggestions.len()); + assert_eq!("test_dotnu.nu", suggestions.get(0).unwrap().value); + + // Test use completion + let completion_str = "use ".to_string(); + let suggestions = completer.complete(&completion_str, completion_str.len()); + + assert_eq!(1, suggestions.len()); + assert_eq!("test_dotnu.nu", suggestions.get(0).unwrap().value); +} + #[test] fn flag_completions() { // Create a new engine @@ -16,7 +56,6 @@ fn flag_completions() { // Instatiate a new completer let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - // Test completions for the 'ls' flags let suggestions = completer.complete("ls -", 4); @@ -61,6 +100,7 @@ fn file_completions() { folder(dir.join("test_a")), folder(dir.join("test_b")), folder(dir.join("another")), + file(dir.join("test_dotnu.nu")), file(dir.join(".hidden_file")), folder(dir.join(".hidden_folder")), ]; @@ -116,8 +156,31 @@ pub fn new_engine() -> (PathBuf, String, EngineState) { .unwrap_or_default(); dir_str.push(SEP); - // Create a default engine - (dir.clone(), dir_str, create_default_context(dir)) + // Create a new engine with default context + let mut engine_state = create_default_context(&dir); + + // New stack + let mut stack = Stack::new(); + + // New delta + let delta = StateDelta::new(&engine_state); + + // Add pwd as env var + stack.add_env_var( + "PWD".to_string(), + Value::String { + val: dir_str.clone(), + span: nu_protocol::Span { + start: 0, + end: dir_str.len(), + }, + }, + ); + + // Merge delta + let _ = engine_state.merge_delta(delta, Some(&mut stack), &dir); + + (dir.clone(), dir_str, engine_state) } // match a list of suggestions with the expected values diff --git a/tests/fixtures/completions/test_dotnu.nu b/tests/fixtures/completions/test_dotnu.nu new file mode 100644 index 0000000000..00f113ee9b --- /dev/null +++ b/tests/fixtures/completions/test_dotnu.nu @@ -0,0 +1,3 @@ +def test[] { + Just a test +}