From 530e2505733f4bd759193d0a685cdd265129abbe Mon Sep 17 00:00:00 2001 From: Herlon Aguiar Date: Tue, 23 Aug 2022 16:24:24 +0200 Subject: [PATCH] nu-cli: merge completions tests into one file (#6389) This PR merges all the completions tests into one file. The reason for them to be separated was organization, so we wouldn't need to scroll a huge file. But that came with another issue, because rust generates a new binary for each completion test file and each completion test depends on Nu looks like all the dataframes were coming into each test file as well (as pointed by @rgwood --- crates/nu-cli/tests/alias.rs | 65 -- crates/nu-cli/tests/completions.rs | 627 +++++++++++++++++++ crates/nu-cli/tests/custom_completions.rs | 69 -- crates/nu-cli/tests/dotnu_completions.rs | 28 - crates/nu-cli/tests/external_completions.rs | 73 --- crates/nu-cli/tests/file_completions.rs | 272 -------- crates/nu-cli/tests/flag_completions.rs | 38 -- crates/nu-cli/tests/folder_completions.rs | 29 - crates/nu-cli/tests/variables_completions.rs | 88 --- 9 files changed, 627 insertions(+), 662 deletions(-) delete mode 100644 crates/nu-cli/tests/alias.rs create mode 100644 crates/nu-cli/tests/completions.rs delete mode 100644 crates/nu-cli/tests/custom_completions.rs delete mode 100644 crates/nu-cli/tests/dotnu_completions.rs delete mode 100644 crates/nu-cli/tests/external_completions.rs delete mode 100644 crates/nu-cli/tests/file_completions.rs delete mode 100644 crates/nu-cli/tests/flag_completions.rs delete mode 100644 crates/nu-cli/tests/folder_completions.rs delete mode 100644 crates/nu-cli/tests/variables_completions.rs diff --git a/crates/nu-cli/tests/alias.rs b/crates/nu-cli/tests/alias.rs deleted file mode 100644 index 78d1dd0199..0000000000 --- a/crates/nu-cli/tests/alias.rs +++ /dev/null @@ -1,65 +0,0 @@ -pub mod support; - -use nu_cli::NuCompleter; -use reedline::Completer; -use support::{match_suggestions, new_engine}; - -#[test] -fn alias_of_command_and_flags() { - let (dir, _, mut engine, mut stack) = new_engine(); - - // Create an alias - let alias = r#"alias ll = ls -l"#; - assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let suggestions = completer.complete("ll t", 4); - #[cfg(windows)] - let expected_paths: Vec = vec!["test_a\\".to_string(), "test_b\\".to_string()]; - #[cfg(not(windows))] - let expected_paths: Vec = vec!["test_a/".to_string(), "test_b/".to_string()]; - - match_suggestions(expected_paths, suggestions) -} - -#[test] -fn alias_of_basic_command() { - let (dir, _, mut engine, mut stack) = new_engine(); - - // Create an alias - let alias = r#"alias ll = ls "#; - assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let suggestions = completer.complete("ll t", 4); - #[cfg(windows)] - let expected_paths: Vec = vec!["test_a\\".to_string(), "test_b\\".to_string()]; - #[cfg(not(windows))] - let expected_paths: Vec = vec!["test_a/".to_string(), "test_b/".to_string()]; - - match_suggestions(expected_paths, suggestions) -} - -#[test] -fn alias_of_another_alias() { - let (dir, _, mut engine, mut stack) = new_engine(); - - // Create an alias - let alias = r#"alias ll = ls -la"#; - assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir.clone()).is_ok()); - // Create the second alias - let alias = r#"alias lf = ll -f"#; - assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let suggestions = completer.complete("lf t", 4); - #[cfg(windows)] - let expected_paths: Vec = vec!["test_a\\".to_string(), "test_b\\".to_string()]; - #[cfg(not(windows))] - let expected_paths: Vec = vec!["test_a/".to_string(), "test_b/".to_string()]; - - match_suggestions(expected_paths, suggestions) -} diff --git a/crates/nu-cli/tests/completions.rs b/crates/nu-cli/tests/completions.rs new file mode 100644 index 0000000000..c947e43122 --- /dev/null +++ b/crates/nu-cli/tests/completions.rs @@ -0,0 +1,627 @@ +pub mod support; + +use nu_cli::NuCompleter; +use nu_parser::parse; +use nu_protocol::engine::StateWorkingSet; +use reedline::{Completer, Suggestion}; +use rstest::{fixture, rstest}; +use support::{file, folder, match_suggestions, new_engine}; + +#[fixture] +fn completer() -> NuCompleter { + // Create a new engine + let (dir, _, mut engine, mut stack) = new_engine(); + + // Add record value as example + let record = "def tst [--mod -s] {}"; + assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); + + // Instantiate a new completer + NuCompleter::new(std::sync::Arc::new(engine), stack) +} + +#[fixture] +fn completer_strings() -> NuCompleter { + // Create a new engine + let (dir, _, mut engine, mut stack) = new_engine(); + + // Add record value as example + let record = r#"def animals [] { ["cat", "dog", "eel" ] } + def my-command [animal: string@animals] { print $animal }"#; + assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); + + // Instantiate a new completer + NuCompleter::new(std::sync::Arc::new(engine), stack) +} + +#[rstest] +fn variables_completions_double_dash_argument(mut completer: NuCompleter) { + let suggestions = completer.complete("tst --", 6); + let expected: Vec = vec!["--help".into(), "--mod".into()]; + // dbg!(&expected, &suggestions); + match_suggestions(expected, suggestions); +} + +#[rstest] +fn variables_completions_single_dash_argument(mut completer: NuCompleter) { + let suggestions = completer.complete("tst -", 5); + let expected: Vec = vec!["--help".into(), "--mod".into(), "-h".into(), "-s".into()]; + match_suggestions(expected, suggestions); +} + +#[rstest] +fn variables_completions_command(mut completer_strings: NuCompleter) { + let suggestions = completer_strings.complete("my-command ", 9); + let expected: Vec = vec!["my-command".into()]; + match_suggestions(expected, suggestions); +} + +#[rstest] +fn variables_completions_subcommands(mut completer_strings: NuCompleter) { + let suggestions = completer_strings.complete("my-command ", 11); + let expected: Vec = vec!["cat".into(), "dog".into(), "eel".into()]; + match_suggestions(expected, suggestions); +} + +#[rstest] +fn variables_completions_subcommands_2(mut completer_strings: NuCompleter) { + let suggestions = completer_strings.complete("my-command ", 11); + let expected: Vec = vec!["cat".into(), "dog".into(), "eel".into()]; + match_suggestions(expected, suggestions); +} + +#[test] +fn dotnu_completions() { + // Create a new engine + let (_, _, engine, stack) = new_engine(); + + // 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!("custom_completion.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!("custom_completion.nu", suggestions.get(0).unwrap().value); +} + +#[test] +#[ignore] +fn external_completer_trailing_space() { + // https://github.com/nushell/nushell/issues/6378 + let block = "let external_completer = {|spans| $spans}"; + let input = "gh alias ".to_string(); + + let suggestions = run_external_completion(&block, &input); + assert_eq!(3, suggestions.len()); + assert_eq!("gh", suggestions.get(0).unwrap().value); + assert_eq!("alias", suggestions.get(1).unwrap().value); + assert_eq!("", suggestions.get(2).unwrap().value); +} + +#[test] +fn external_completer_no_trailing_space() { + let block = "let external_completer = {|spans| $spans}"; + let input = "gh alias".to_string(); + + let suggestions = run_external_completion(&block, &input); + assert_eq!(2, suggestions.len()); + assert_eq!("gh", suggestions.get(0).unwrap().value); + assert_eq!("alias", suggestions.get(1).unwrap().value); +} + +#[test] +fn external_completer_pass_flags() { + let block = "let external_completer = {|spans| $spans}"; + let input = "gh api --".to_string(); + + let suggestions = run_external_completion(&block, &input); + assert_eq!(3, suggestions.len()); + assert_eq!("gh", suggestions.get(0).unwrap().value); + assert_eq!("api", suggestions.get(1).unwrap().value); + assert_eq!("--", suggestions.get(2).unwrap().value); +} + +#[test] +fn file_completions() { + // Create a new engine + let (dir, dir_str, engine, stack) = new_engine(); + + // Instatiate a new completer + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + // Test completions for the current folder + let target_dir = format!("cp {}", dir_str); + let suggestions = completer.complete(&target_dir, target_dir.len()); + + // Create the expected values + let expected_paths: Vec = vec![ + file(dir.join("nushell")), + folder(dir.join("test_a")), + folder(dir.join("test_b")), + folder(dir.join("another")), + file(dir.join("custom_completion.nu")), + file(dir.join(".hidden_file")), + folder(dir.join(".hidden_folder")), + ]; + + // Match the results + match_suggestions(expected_paths, suggestions); + + // Test completions for a file + let target_dir = format!("cp {}", folder(dir.join("another"))); + let suggestions = completer.complete(&target_dir, target_dir.len()); + + // Create the expected values + let expected_paths: Vec = vec![file(dir.join("another").join("newfile"))]; + + // Match the results + match_suggestions(expected_paths, suggestions); +} + +#[test] +fn command_ls_completion() { + let (_, _, engine, stack) = new_engine(); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let target_dir = "ls "; + let suggestions = completer.complete(target_dir, target_dir.len()); + + #[cfg(windows)] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a\\".to_string(), + "test_b\\".to_string(), + "another\\".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder\\".to_string(), + ]; + #[cfg(not(windows))] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a/".to_string(), + "test_b/".to_string(), + "another/".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder/".to_string(), + ]; + + match_suggestions(expected_paths, suggestions) +} +#[test] +fn command_open_completion() { + let (_, _, engine, stack) = new_engine(); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let target_dir = "open "; + let suggestions = completer.complete(target_dir, target_dir.len()); + + #[cfg(windows)] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a\\".to_string(), + "test_b\\".to_string(), + "another\\".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder\\".to_string(), + ]; + #[cfg(not(windows))] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a/".to_string(), + "test_b/".to_string(), + "another/".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder/".to_string(), + ]; + + match_suggestions(expected_paths, suggestions) +} + +#[test] +fn command_rm_completion() { + let (_, _, engine, stack) = new_engine(); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let target_dir = "rm "; + let suggestions = completer.complete(target_dir, target_dir.len()); + + #[cfg(windows)] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a\\".to_string(), + "test_b\\".to_string(), + "another\\".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder\\".to_string(), + ]; + #[cfg(not(windows))] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a/".to_string(), + "test_b/".to_string(), + "another/".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder/".to_string(), + ]; + + match_suggestions(expected_paths, suggestions) +} + +#[test] +fn command_cp_completion() { + let (_, _, engine, stack) = new_engine(); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let target_dir = "cp "; + let suggestions = completer.complete(target_dir, target_dir.len()); + + #[cfg(windows)] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a\\".to_string(), + "test_b\\".to_string(), + "another\\".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder\\".to_string(), + ]; + #[cfg(not(windows))] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a/".to_string(), + "test_b/".to_string(), + "another/".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder/".to_string(), + ]; + + match_suggestions(expected_paths, suggestions) +} + +#[test] +fn command_save_completion() { + let (_, _, engine, stack) = new_engine(); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let target_dir = "save "; + let suggestions = completer.complete(target_dir, target_dir.len()); + + #[cfg(windows)] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a\\".to_string(), + "test_b\\".to_string(), + "another\\".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder\\".to_string(), + ]; + #[cfg(not(windows))] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a/".to_string(), + "test_b/".to_string(), + "another/".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder/".to_string(), + ]; + + match_suggestions(expected_paths, suggestions) +} + +#[test] +fn command_touch_completion() { + let (_, _, engine, stack) = new_engine(); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let target_dir = "touch "; + let suggestions = completer.complete(target_dir, target_dir.len()); + + #[cfg(windows)] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a\\".to_string(), + "test_b\\".to_string(), + "another\\".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder\\".to_string(), + ]; + #[cfg(not(windows))] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a/".to_string(), + "test_b/".to_string(), + "another/".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder/".to_string(), + ]; + + match_suggestions(expected_paths, suggestions) +} + +#[test] +fn command_watch_completion() { + let (_, _, engine, stack) = new_engine(); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let target_dir = "watch "; + let suggestions = completer.complete(target_dir, target_dir.len()); + + #[cfg(windows)] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a\\".to_string(), + "test_b\\".to_string(), + "another\\".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder\\".to_string(), + ]; + #[cfg(not(windows))] + let expected_paths: Vec = vec![ + "nushell".to_string(), + "test_a/".to_string(), + "test_b/".to_string(), + "another/".to_string(), + "custom_completion.nu".to_string(), + ".hidden_file".to_string(), + ".hidden_folder/".to_string(), + ]; + + match_suggestions(expected_paths, suggestions) +} + +#[test] +fn flag_completions() { + // Create a new engine + let (_, _, engine, stack) = new_engine(); + + // 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); + + assert_eq!(14, suggestions.len()); + + let expected: Vec = vec![ + "--all".into(), + "--directory".into(), + "--du".into(), + "--full-paths".into(), + "--help".into(), + "--long".into(), + "--short-names".into(), + "-D".into(), + "-a".into(), + "-d".into(), + "-f".into(), + "-h".into(), + "-l".into(), + "-s".into(), + ]; + + // Match results + match_suggestions(expected, suggestions); +} + +#[test] +fn folder_completions() { + // Create a new engine + let (dir, dir_str, engine, stack) = new_engine(); + + // Instatiate a new completer + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + // Test completions for the current folder + let target_dir = format!("cd {}", dir_str); + let suggestions = completer.complete(&target_dir, target_dir.len()); + + // Create the expected values + let expected_paths: Vec = vec![ + folder(dir.join("test_a")), + folder(dir.join("test_b")), + folder(dir.join("another")), + folder(dir.join(".hidden_folder")), + ]; + + // Match the results + match_suggestions(expected_paths, suggestions); +} + +#[test] +fn variables_completions() { + // Create a new engine + let (dir, _, mut engine, mut stack) = new_engine(); + + // Add record value as example + let record = "let actor = { name: 'Tom Hardy', age: 44 }"; + assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); + + // Instatiate a new completer + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + // Test completions for $nu + let suggestions = completer.complete("$nu.", 4); + + assert_eq!(9, suggestions.len()); + + let expected: Vec = vec![ + "config-path".into(), + "env-path".into(), + "history-path".into(), + "home-path".into(), + "loginshell-path".into(), + "os-info".into(), + "pid".into(), + "scope".into(), + "temp-path".into(), + ]; + + // Match results + match_suggestions(expected, suggestions); + + // Test completions for $nu.h (filter) + let suggestions = completer.complete("$nu.h", 5); + + assert_eq!(2, suggestions.len()); + + let expected: Vec = vec!["history-path".into(), "home-path".into()]; + + // Match results + match_suggestions(expected, suggestions); + + // Test completions for custom var + let suggestions = completer.complete("$actor.", 7); + + assert_eq!(2, suggestions.len()); + + let expected: Vec = vec!["age".into(), "name".into()]; + + // Match results + match_suggestions(expected, suggestions); + + // Test completions for custom var (filtering) + let suggestions = completer.complete("$actor.n", 8); + + assert_eq!(1, suggestions.len()); + + let expected: Vec = vec!["name".into()]; + + // Match results + match_suggestions(expected, suggestions); + + // Test completions for $env + let suggestions = completer.complete("$env.", 5); + + assert_eq!(2, suggestions.len()); + + let expected: Vec = vec!["PWD".into(), "TEST".into()]; + + // Match results + match_suggestions(expected, suggestions); + + // Test completions for $env + let suggestions = completer.complete("$env.T", 6); + + assert_eq!(1, suggestions.len()); + + let expected: Vec = vec!["TEST".into()]; + + // Match results + match_suggestions(expected, suggestions); +} + +#[test] +fn alias_of_command_and_flags() { + let (dir, _, mut engine, mut stack) = new_engine(); + + // Create an alias + let alias = r#"alias ll = ls -l"#; + assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let suggestions = completer.complete("ll t", 4); + #[cfg(windows)] + let expected_paths: Vec = vec!["test_a\\".to_string(), "test_b\\".to_string()]; + #[cfg(not(windows))] + let expected_paths: Vec = vec!["test_a/".to_string(), "test_b/".to_string()]; + + match_suggestions(expected_paths, suggestions) +} + +#[test] +fn alias_of_basic_command() { + let (dir, _, mut engine, mut stack) = new_engine(); + + // Create an alias + let alias = r#"alias ll = ls "#; + assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let suggestions = completer.complete("ll t", 4); + #[cfg(windows)] + let expected_paths: Vec = vec!["test_a\\".to_string(), "test_b\\".to_string()]; + #[cfg(not(windows))] + let expected_paths: Vec = vec!["test_a/".to_string(), "test_b/".to_string()]; + + match_suggestions(expected_paths, suggestions) +} + +#[test] +fn alias_of_another_alias() { + let (dir, _, mut engine, mut stack) = new_engine(); + + // Create an alias + let alias = r#"alias ll = ls -la"#; + assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir.clone()).is_ok()); + // Create the second alias + let alias = r#"alias lf = ll -f"#; + assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); + + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + let suggestions = completer.complete("lf t", 4); + #[cfg(windows)] + let expected_paths: Vec = vec!["test_a\\".to_string(), "test_b\\".to_string()]; + #[cfg(not(windows))] + let expected_paths: Vec = vec!["test_a/".to_string(), "test_b/".to_string()]; + + match_suggestions(expected_paths, suggestions) +} + +fn run_external_completion(block: &str, input: &str) -> Vec { + // Create a new engine + let (dir, _, mut engine_state, mut stack) = new_engine(); + let (_, delta) = { + let mut working_set = StateWorkingSet::new(&engine_state); + let (block, err) = parse(&mut working_set, None, block.as_bytes(), false, &[]); + assert!(err.is_none()); + + (block, working_set.render()) + }; + + assert!(engine_state.merge_delta(delta).is_ok()); + + // Merge environment into the permanent state + assert!(engine_state.merge_env(&mut stack, &dir).is_ok()); + + let latest_block_id = engine_state.num_blocks() - 1; + + // Change config adding the external completer + let mut config = engine_state.get_config().clone(); + config.external_completer = Some(latest_block_id); + engine_state.set_config(&config); + + // Instatiate a new completer + let mut completer = NuCompleter::new(std::sync::Arc::new(engine_state), stack); + + completer.complete(&input, input.len()) +} diff --git a/crates/nu-cli/tests/custom_completions.rs b/crates/nu-cli/tests/custom_completions.rs deleted file mode 100644 index 8f24cbfd4e..0000000000 --- a/crates/nu-cli/tests/custom_completions.rs +++ /dev/null @@ -1,69 +0,0 @@ -pub mod support; - -use nu_cli::NuCompleter; -use reedline::Completer; -use rstest::{fixture, rstest}; -use support::{match_suggestions, new_engine}; - -#[fixture] -fn completer() -> NuCompleter { - // Create a new engine - let (dir, _, mut engine, mut stack) = new_engine(); - - // Add record value as example - let record = "def tst [--mod -s] {}"; - assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); - - // Instantiate a new completer - NuCompleter::new(std::sync::Arc::new(engine), stack) -} - -#[fixture] -fn completer_strings() -> NuCompleter { - // Create a new engine - let (dir, _, mut engine, mut stack) = new_engine(); - - // Add record value as example - let record = r#"def animals [] { ["cat", "dog", "eel" ] } - def my-command [animal: string@animals] { print $animal }"#; - assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); - - // Instantiate a new completer - NuCompleter::new(std::sync::Arc::new(engine), stack) -} - -#[rstest] -fn variables_completions_double_dash_argument(mut completer: NuCompleter) { - let suggestions = completer.complete("tst --", 6); - let expected: Vec = vec!["--help".into(), "--mod".into()]; - // dbg!(&expected, &suggestions); - match_suggestions(expected, suggestions); -} - -#[rstest] -fn variables_completions_single_dash_argument(mut completer: NuCompleter) { - let suggestions = completer.complete("tst -", 5); - let expected: Vec = vec!["--help".into(), "--mod".into(), "-h".into(), "-s".into()]; - match_suggestions(expected, suggestions); -} - -#[rstest] -fn variables_completions_command(mut completer_strings: NuCompleter) { - let suggestions = completer_strings.complete("my-command ", 9); - let expected: Vec = vec!["my-command".into()]; - match_suggestions(expected, suggestions); -} - -#[rstest] -fn variables_completions_subcommands(mut completer_strings: NuCompleter) { - let suggestions = completer_strings.complete("my-command ", 11); - let expected: Vec = vec!["cat".into(), "dog".into(), "eel".into()]; - match_suggestions(expected, suggestions); -} - -#[rstest] -fn variables_completions_subcommands_2(mut completer_strings: NuCompleter) { - let suggestions = completer_strings.complete("my-command ", 11); - let expected: Vec = vec!["cat".into(), "dog".into(), "eel".into()]; - match_suggestions(expected, suggestions); -} diff --git a/crates/nu-cli/tests/dotnu_completions.rs b/crates/nu-cli/tests/dotnu_completions.rs deleted file mode 100644 index 676e43fb78..0000000000 --- a/crates/nu-cli/tests/dotnu_completions.rs +++ /dev/null @@ -1,28 +0,0 @@ -pub mod support; - -use nu_cli::NuCompleter; -use reedline::Completer; -use support::new_engine; - -#[test] -fn dotnu_completions() { - // Create a new engine - let (_, _, engine, stack) = new_engine(); - - // 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!("custom_completion.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!("custom_completion.nu", suggestions.get(0).unwrap().value); -} diff --git a/crates/nu-cli/tests/external_completions.rs b/crates/nu-cli/tests/external_completions.rs deleted file mode 100644 index a741e4b3f8..0000000000 --- a/crates/nu-cli/tests/external_completions.rs +++ /dev/null @@ -1,73 +0,0 @@ -pub mod support; - -use nu_cli::NuCompleter; -use nu_parser::parse; -use nu_protocol::engine::StateWorkingSet; -use reedline::{Completer, Suggestion}; -use support::new_engine; - -#[test] -#[ignore] -fn external_completer_trailing_space() { - // https://github.com/nushell/nushell/issues/6378 - let block = "let external_completer = {|spans| $spans}"; - let input = "gh alias ".to_string(); - - let suggestions = run_completion(&block, &input); - assert_eq!(3, suggestions.len()); - assert_eq!("gh", suggestions.get(0).unwrap().value); - assert_eq!("alias", suggestions.get(1).unwrap().value); - assert_eq!("", suggestions.get(2).unwrap().value); -} - -#[test] -fn external_completer_no_trailing_space() { - let block = "let external_completer = {|spans| $spans}"; - let input = "gh alias".to_string(); - - let suggestions = run_completion(&block, &input); - assert_eq!(2, suggestions.len()); - assert_eq!("gh", suggestions.get(0).unwrap().value); - assert_eq!("alias", suggestions.get(1).unwrap().value); -} - -#[test] -fn external_completer_pass_flags() { - let block = "let external_completer = {|spans| $spans}"; - let input = "gh api --".to_string(); - - let suggestions = run_completion(&block, &input); - assert_eq!(3, suggestions.len()); - assert_eq!("gh", suggestions.get(0).unwrap().value); - assert_eq!("api", suggestions.get(1).unwrap().value); - assert_eq!("--", suggestions.get(2).unwrap().value); -} - -fn run_completion(block: &str, input: &str) -> Vec { - // Create a new engine - let (dir, _, mut engine_state, mut stack) = new_engine(); - let (_, delta) = { - let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = parse(&mut working_set, None, block.as_bytes(), false, &[]); - assert!(err.is_none()); - - (block, working_set.render()) - }; - - assert!(engine_state.merge_delta(delta).is_ok()); - - // Merge environment into the permanent state - assert!(engine_state.merge_env(&mut stack, &dir).is_ok()); - - let latest_block_id = engine_state.num_blocks() - 1; - - // Change config adding the external completer - let mut config = engine_state.get_config().clone(); - config.external_completer = Some(latest_block_id); - engine_state.set_config(&config); - - // Instatiate a new completer - let mut completer = NuCompleter::new(std::sync::Arc::new(engine_state), stack); - - completer.complete(&input, input.len()) -} diff --git a/crates/nu-cli/tests/file_completions.rs b/crates/nu-cli/tests/file_completions.rs deleted file mode 100644 index ac7d909886..0000000000 --- a/crates/nu-cli/tests/file_completions.rs +++ /dev/null @@ -1,272 +0,0 @@ -pub mod support; - -use nu_cli::NuCompleter; -use reedline::Completer; -use support::{file, folder, match_suggestions, new_engine}; - -#[test] -fn file_completions() { - // Create a new engine - let (dir, dir_str, engine, stack) = new_engine(); - - // Instatiate a new completer - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - // Test completions for the current folder - let target_dir = format!("cp {}", dir_str); - let suggestions = completer.complete(&target_dir, target_dir.len()); - - // Create the expected values - let expected_paths: Vec = vec![ - file(dir.join("nushell")), - folder(dir.join("test_a")), - folder(dir.join("test_b")), - folder(dir.join("another")), - file(dir.join("custom_completion.nu")), - file(dir.join(".hidden_file")), - folder(dir.join(".hidden_folder")), - ]; - - // Match the results - match_suggestions(expected_paths, suggestions); - - // Test completions for a file - let target_dir = format!("cp {}", folder(dir.join("another"))); - let suggestions = completer.complete(&target_dir, target_dir.len()); - - // Create the expected values - let expected_paths: Vec = vec![file(dir.join("another").join("newfile"))]; - - // Match the results - match_suggestions(expected_paths, suggestions); -} - -#[test] -fn command_ls_completion() { - let (_, _, engine, stack) = new_engine(); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let target_dir = "ls "; - let suggestions = completer.complete(target_dir, target_dir.len()); - - #[cfg(windows)] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a\\".to_string(), - "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder\\".to_string(), - ]; - #[cfg(not(windows))] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a/".to_string(), - "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder/".to_string(), - ]; - - match_suggestions(expected_paths, suggestions) -} -#[test] -fn command_open_completion() { - let (_, _, engine, stack) = new_engine(); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let target_dir = "open "; - let suggestions = completer.complete(target_dir, target_dir.len()); - - #[cfg(windows)] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a\\".to_string(), - "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder\\".to_string(), - ]; - #[cfg(not(windows))] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a/".to_string(), - "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder/".to_string(), - ]; - - match_suggestions(expected_paths, suggestions) -} - -#[test] -fn command_rm_completion() { - let (_, _, engine, stack) = new_engine(); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let target_dir = "rm "; - let suggestions = completer.complete(target_dir, target_dir.len()); - - #[cfg(windows)] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a\\".to_string(), - "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder\\".to_string(), - ]; - #[cfg(not(windows))] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a/".to_string(), - "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder/".to_string(), - ]; - - match_suggestions(expected_paths, suggestions) -} - -#[test] -fn command_cp_completion() { - let (_, _, engine, stack) = new_engine(); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let target_dir = "cp "; - let suggestions = completer.complete(target_dir, target_dir.len()); - - #[cfg(windows)] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a\\".to_string(), - "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder\\".to_string(), - ]; - #[cfg(not(windows))] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a/".to_string(), - "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder/".to_string(), - ]; - - match_suggestions(expected_paths, suggestions) -} - -#[test] -fn command_save_completion() { - let (_, _, engine, stack) = new_engine(); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let target_dir = "save "; - let suggestions = completer.complete(target_dir, target_dir.len()); - - #[cfg(windows)] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a\\".to_string(), - "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder\\".to_string(), - ]; - #[cfg(not(windows))] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a/".to_string(), - "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder/".to_string(), - ]; - - match_suggestions(expected_paths, suggestions) -} - -#[test] -fn command_touch_completion() { - let (_, _, engine, stack) = new_engine(); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let target_dir = "touch "; - let suggestions = completer.complete(target_dir, target_dir.len()); - - #[cfg(windows)] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a\\".to_string(), - "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder\\".to_string(), - ]; - #[cfg(not(windows))] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a/".to_string(), - "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder/".to_string(), - ]; - - match_suggestions(expected_paths, suggestions) -} - -#[test] -fn command_watch_completion() { - let (_, _, engine, stack) = new_engine(); - - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - let target_dir = "watch "; - let suggestions = completer.complete(target_dir, target_dir.len()); - - #[cfg(windows)] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a\\".to_string(), - "test_b\\".to_string(), - "another\\".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder\\".to_string(), - ]; - #[cfg(not(windows))] - let expected_paths: Vec = vec![ - "nushell".to_string(), - "test_a/".to_string(), - "test_b/".to_string(), - "another/".to_string(), - "custom_completion.nu".to_string(), - ".hidden_file".to_string(), - ".hidden_folder/".to_string(), - ]; - - match_suggestions(expected_paths, suggestions) -} diff --git a/crates/nu-cli/tests/flag_completions.rs b/crates/nu-cli/tests/flag_completions.rs deleted file mode 100644 index 6e816bb4b5..0000000000 --- a/crates/nu-cli/tests/flag_completions.rs +++ /dev/null @@ -1,38 +0,0 @@ -pub mod support; - -use nu_cli::NuCompleter; -use reedline::Completer; -use support::{match_suggestions, new_engine}; - -#[test] -fn flag_completions() { - // Create a new engine - let (_, _, engine, stack) = new_engine(); - - // 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); - - assert_eq!(14, suggestions.len()); - - let expected: Vec = vec![ - "--all".into(), - "--directory".into(), - "--du".into(), - "--full-paths".into(), - "--help".into(), - "--long".into(), - "--short-names".into(), - "-D".into(), - "-a".into(), - "-d".into(), - "-f".into(), - "-h".into(), - "-l".into(), - "-s".into(), - ]; - - // Match results - match_suggestions(expected, suggestions); -} diff --git a/crates/nu-cli/tests/folder_completions.rs b/crates/nu-cli/tests/folder_completions.rs deleted file mode 100644 index edfd572e1a..0000000000 --- a/crates/nu-cli/tests/folder_completions.rs +++ /dev/null @@ -1,29 +0,0 @@ -pub mod support; - -use nu_cli::NuCompleter; -use reedline::Completer; -use support::{folder, match_suggestions, new_engine}; - -#[test] -fn folder_completions() { - // Create a new engine - let (dir, dir_str, engine, stack) = new_engine(); - - // Instatiate a new completer - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - // Test completions for the current folder - let target_dir = format!("cd {}", dir_str); - let suggestions = completer.complete(&target_dir, target_dir.len()); - - // Create the expected values - let expected_paths: Vec = vec![ - folder(dir.join("test_a")), - folder(dir.join("test_b")), - folder(dir.join("another")), - folder(dir.join(".hidden_folder")), - ]; - - // Match the results - match_suggestions(expected_paths, suggestions); -} diff --git a/crates/nu-cli/tests/variables_completions.rs b/crates/nu-cli/tests/variables_completions.rs deleted file mode 100644 index 942eebbc25..0000000000 --- a/crates/nu-cli/tests/variables_completions.rs +++ /dev/null @@ -1,88 +0,0 @@ -pub mod support; - -use nu_cli::NuCompleter; -use reedline::Completer; -use support::{match_suggestions, new_engine}; - -#[test] -fn variables_completions() { - // Create a new engine - let (dir, _, mut engine, mut stack) = new_engine(); - - // Add record value as example - let record = "let actor = { name: 'Tom Hardy', age: 44 }"; - assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); - - // Instatiate a new completer - let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); - - // Test completions for $nu - let suggestions = completer.complete("$nu.", 4); - - assert_eq!(9, suggestions.len()); - - let expected: Vec = vec![ - "config-path".into(), - "env-path".into(), - "history-path".into(), - "home-path".into(), - "loginshell-path".into(), - "os-info".into(), - "pid".into(), - "scope".into(), - "temp-path".into(), - ]; - - // Match results - match_suggestions(expected, suggestions); - - // Test completions for $nu.h (filter) - let suggestions = completer.complete("$nu.h", 5); - - assert_eq!(2, suggestions.len()); - - let expected: Vec = vec!["history-path".into(), "home-path".into()]; - - // Match results - match_suggestions(expected, suggestions); - - // Test completions for custom var - let suggestions = completer.complete("$actor.", 7); - - assert_eq!(2, suggestions.len()); - - let expected: Vec = vec!["age".into(), "name".into()]; - - // Match results - match_suggestions(expected, suggestions); - - // Test completions for custom var (filtering) - let suggestions = completer.complete("$actor.n", 8); - - assert_eq!(1, suggestions.len()); - - let expected: Vec = vec!["name".into()]; - - // Match results - match_suggestions(expected, suggestions); - - // Test completions for $env - let suggestions = completer.complete("$env.", 5); - - assert_eq!(2, suggestions.len()); - - let expected: Vec = vec!["PWD".into(), "TEST".into()]; - - // Match results - match_suggestions(expected, suggestions); - - // Test completions for $env - let suggestions = completer.complete("$env.T", 6); - - assert_eq!(1, suggestions.len()); - - let expected: Vec = vec!["TEST".into()]; - - // Match results - match_suggestions(expected, suggestions); -}