Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quotes to hash file autocomplete #7398

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
add test case and fmt
  • Loading branch information
merelymyself committed Dec 8, 2022
commit cea815053b1e0e8df97b6d8442688e1ae2953734
6 changes: 5 additions & 1 deletion crates/nu-cli/src/completions/directory_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ pub fn directory_completion(
}

// Fix files or folders with quotes or hash
if path.contains('\'') || path.contains('"') || path.contains(' ') || path.contains('#') {
if path.contains('\'')
|| path.contains('"')
|| path.contains(' ')
|| path.contains('#')
{
path = format!("`{}`", path);
}

Expand Down
6 changes: 5 additions & 1 deletion crates/nu-cli/src/completions/file_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ pub fn file_path_completion(
}

// Fix files or folders with quotes or hashes
if path.contains('\'') || path.contains('"') || path.contains(' ') || path.contains('#') {
if path.contains('\'')
|| path.contains('"')
|| path.contains(' ')
|| path.contains('#')
{
path = format!("`{}`", path);
}

Expand Down
20 changes: 19 additions & 1 deletion crates/nu-cli/tests/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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};
use support::{completions_helpers::new_quote_engine, file, folder, match_suggestions, new_engine};

#[fixture]
fn completer() -> NuCompleter {
Expand Down Expand Up @@ -411,6 +411,24 @@ fn command_watch_with_filecompletion() {
match_suggestions(expected_paths, suggestions)
}

#[test]
fn file_completion_quoted() {
let (_, _, engine, stack) = new_quote_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());

let expected_paths: Vec<String> = vec![
"`te st.txt`".to_string(),
"`te#st.txt`".to_string(),
"`te'st.txt`".to_string(),
];

match_suggestions(expected_paths, suggestions)
}

#[test]
fn flag_completions() {
// Create a new engine
Expand Down
39 changes: 39 additions & 0 deletions crates/nu-cli/tests/support/completions_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,45 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
(dir, dir_str, engine_state, stack)
}

pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) {
// Target folder inside assets
let dir = fs::fixtures().join("quoted_completions");
let mut dir_str = dir
.clone()
.into_os_string()
.into_string()
.unwrap_or_default();
dir_str.push(SEP);

// Create a new engine with default context
let mut engine_state = create_default_context();

// New stack
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::new(0, dir_str.len()),
},
);
stack.add_env_var(
"TEST".to_string(),
Value::String {
val: "NUSHELL".to_string(),
span: nu_protocol::Span::new(0, dir_str.len()),
},
);

// Merge environment into the permanent state
let merge_result = engine_state.merge_env(&mut stack, &dir);
assert!(merge_result.is_ok());

(dir, dir_str, engine_state, stack)
}

// match a list of suggestions with the expected values
pub fn match_suggestions(expected: Vec<String>, suggestions: Vec<Suggestion>) {
let expected_len = expected.len();
Expand Down
Empty file.
Empty file.
Empty file.