Skip to content

Commit d495145

Browse files
committed
clap_complete: add unit test for fish env completer
Related to: #6196 This PR adds a unit test showing the current behavior of the the Fish shell env completer, that quotes paths incorrectly when there are spaces in the path. The issue should be demonstrated in this PR, and a follow-up PR could have a fix for the issue.
1 parent 1a6c0de commit d495145

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

clap_complete/src/env/shells.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,55 @@ impl Zsh {
446446
string.replace('\\', "\\\\")
447447
}
448448
}
449+
450+
#[cfg(test)]
451+
mod tests {
452+
use super::*;
453+
use snapbox::assert_data_eq;
454+
455+
// This test verifies that fish shell path quoting works with or without spaces in the path.
456+
#[test]
457+
#[cfg(all(unix, feature = "unstable-dynamic"))]
458+
#[cfg(feature = "unstable-shell-tests")]
459+
fn fish_env_completer_path_quoting_works() {
460+
// Returns the dynamic registration line for the fish shell, for example:
461+
// complete --keep-order --exclusive --command my-bin --arguments "(COMPLETE=fish /path/to/my-bin ... )"
462+
let get_fish_registration = |completer_bin: &str| {
463+
let mut buf = Vec::new();
464+
let fish = Fish;
465+
fish.write_registration(
466+
"IGNORED_VAR",
467+
"ignored-name",
468+
"/ignored/bin",
469+
completer_bin,
470+
&mut buf,
471+
)
472+
.expect("write_registration failed");
473+
return String::from_utf8(buf).expect("Invalid UTF-8");
474+
};
475+
476+
let script = get_fish_registration("completer");
477+
assert_data_eq!(
478+
script.trim(),
479+
snapbox::str![r#"complete [..] "([..] "'completer'"[..])""#]
480+
);
481+
482+
let script = get_fish_registration("/path/completer");
483+
assert_data_eq!(
484+
script.trim(),
485+
snapbox::str![r#"complete [..] "([..] "'/path/completer'"[..])""#]
486+
);
487+
488+
// This case demonstrates the existing bug when handling paths with spaces as described in
489+
// https://github.com/clap-rs/clap/issues/6196
490+
// The problem shown here is:
491+
// * The double quote started at `"(` is closed by the double quote before the path
492+
// * Then we have an empty pair of single quotes
493+
// * Then we have the _unquoted_ path with a space, which is problematic
494+
let script = get_fish_registration("/path with a space/completer");
495+
assert_data_eq!(
496+
script.trim(),
497+
snapbox::str![r#"complete [..] "([..] "''/path with a space/completer''"[..])""#]
498+
);
499+
}
500+
}

0 commit comments

Comments
 (0)