From 58cf0c56f8cefc3c1bf9d4a39d3fdb19890b8681 Mon Sep 17 00:00:00 2001 From: Wind Date: Thu, 23 May 2024 10:47:06 +0800 Subject: [PATCH] add some completion tests (#12908) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description ```nushell ❯ ls ╭───┬───────┬──────┬──────┬──────────╮ │ # │ name │ type │ size │ modified │ ├───┼───────┼──────┼──────┼──────────┤ │ 0 │ a.txt │ file │ 0 B │ now │ ╰───┴───────┴──────┴──────┴──────────╯ ❯ ls a. NO RECORDS FOUND ``` There is a completion issue on previous version, I think @amtoine have reproduced it before. But currently I can't reproduce it on latest main. To avoid such regression, I added some tests for completion. --------- Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com> --- crates/nu-cli/tests/completions/mod.rs | 46 +++++++++++++++++++ .../partial_a/have_ext.exe | 0 .../partial_a/have_ext.txt | 0 3 files changed, 46 insertions(+) create mode 100644 tests/fixtures/partial_completions/partial_a/have_ext.exe create mode 100644 tests/fixtures/partial_completions/partial_a/have_ext.txt diff --git a/crates/nu-cli/tests/completions/mod.rs b/crates/nu-cli/tests/completions/mod.rs index a3eb692adf..eb245e2fb2 100644 --- a/crates/nu-cli/tests/completions/mod.rs +++ b/crates/nu-cli/tests/completions/mod.rs @@ -292,6 +292,8 @@ fn partial_completions() { // Create the expected values let expected_paths: Vec = vec![ + file(dir.join("partial_a").join("have_ext.exe")), + file(dir.join("partial_a").join("have_ext.txt")), file(dir.join("partial_a").join("hello")), file(dir.join("partial_a").join("hola")), file(dir.join("partial_b").join("hello_b")), @@ -310,6 +312,8 @@ fn partial_completions() { // Create the expected values let expected_paths: Vec = vec![ file(dir.join("partial_a").join("anotherfile")), + file(dir.join("partial_a").join("have_ext.exe")), + file(dir.join("partial_a").join("have_ext.txt")), file(dir.join("partial_a").join("hello")), file(dir.join("partial_a").join("hola")), file(dir.join("partial_b").join("hello_b")), @@ -360,6 +364,34 @@ fn partial_completions() { // Match the results match_suggestions(expected_paths, suggestions); + + // Test completion for all files under directories whose names begin with "pa" + let file_str = file(dir.join("partial_a").join("have")); + let target_file = format!("rm {file_str}"); + let suggestions = completer.complete(&target_file, target_file.len()); + + // Create the expected values + let expected_paths: Vec = vec![ + file(dir.join("partial_a").join("have_ext.exe")), + file(dir.join("partial_a").join("have_ext.txt")), + ]; + + // Match the results + match_suggestions(expected_paths, suggestions); + + // Test completion for all files under directories whose names begin with "pa" + let file_str = file(dir.join("partial_a").join("have_ext.")); + let file_dir = format!("rm {file_str}"); + let suggestions = completer.complete(&file_dir, file_dir.len()); + + // Create the expected values + let expected_paths: Vec = vec![ + file(dir.join("partial_a").join("have_ext.exe")), + file(dir.join("partial_a").join("have_ext.txt")), + ]; + + // Match the results + match_suggestions(expected_paths, suggestions); } #[test] @@ -394,6 +426,13 @@ fn command_ls_with_filecompletion() { ".hidden_folder/".to_string(), ]; + match_suggestions(expected_paths, suggestions); + + let target_dir = "ls custom_completion."; + let suggestions = completer.complete(target_dir, target_dir.len()); + + let expected_paths: Vec = vec!["custom_completion.nu".to_string()]; + match_suggestions(expected_paths, suggestions) } #[test] @@ -428,6 +467,13 @@ fn command_open_with_filecompletion() { ".hidden_folder/".to_string(), ]; + match_suggestions(expected_paths, suggestions); + + let target_dir = "open custom_completion."; + let suggestions = completer.complete(target_dir, target_dir.len()); + + let expected_paths: Vec = vec!["custom_completion.nu".to_string()]; + match_suggestions(expected_paths, suggestions) } diff --git a/tests/fixtures/partial_completions/partial_a/have_ext.exe b/tests/fixtures/partial_completions/partial_a/have_ext.exe new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures/partial_completions/partial_a/have_ext.txt b/tests/fixtures/partial_completions/partial_a/have_ext.txt new file mode 100644 index 0000000000..e69de29bb2