forked from extern/nushell
Escape path that could be a flag (#10721)
# Description Files that begin with dashes can be ambiguous when passed to commands like `ls`. For example if there exists a file `--help`, it might be considered a flag if not properly escaped. This PR escapes any file that begins with a dash. # User-Facing Changes Files beginning with dashes will be escaped. # Tests + Formatting Tests are added.
This commit is contained in:
parent
9c70c68914
commit
7162d4d9aa
@ -152,7 +152,9 @@ pub fn complete_item(
|
|||||||
pub fn escape_path(path: String, dir: bool) -> String {
|
pub fn escape_path(path: String, dir: bool) -> String {
|
||||||
let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']);
|
let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']);
|
||||||
let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']);
|
let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']);
|
||||||
if filename_contaminated || dirname_contaminated || path.parse::<f64>().is_ok() {
|
let maybe_flag = path.starts_with('-');
|
||||||
|
let maybe_number = path.parse::<f64>().is_ok();
|
||||||
|
if filename_contaminated || dirname_contaminated || maybe_flag || maybe_number {
|
||||||
format!("`{path}`")
|
format!("`{path}`")
|
||||||
} else {
|
} else {
|
||||||
path
|
path
|
||||||
|
@ -527,6 +527,10 @@ fn file_completion_quoted() {
|
|||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"`--help`".to_string(),
|
||||||
|
"`-42`".to_string(),
|
||||||
|
"`-inf`".to_string(),
|
||||||
|
"`4.2`".to_string(),
|
||||||
"`te st.txt`".to_string(),
|
"`te st.txt`".to_string(),
|
||||||
"`te#st.txt`".to_string(),
|
"`te#st.txt`".to_string(),
|
||||||
"`te'st.txt`".to_string(),
|
"`te'st.txt`".to_string(),
|
||||||
|
0
tests/fixtures/quoted_completions/--help
vendored
Normal file
0
tests/fixtures/quoted_completions/--help
vendored
Normal file
0
tests/fixtures/quoted_completions/-42
vendored
Normal file
0
tests/fixtures/quoted_completions/-42
vendored
Normal file
0
tests/fixtures/quoted_completions/-inf
vendored
Normal file
0
tests/fixtures/quoted_completions/-inf
vendored
Normal file
0
tests/fixtures/quoted_completions/4.2
vendored
Normal file
0
tests/fixtures/quoted_completions/4.2
vendored
Normal file
Loading…
Reference in New Issue
Block a user