Skip to content

Commit ac1006c

Browse files
authored
Merge pull request #6197 from devjgm/greg/fix-clap-completer-issue-with-fish
test: Add unit test for fish env completer to clap_complete
2 parents 1a6c0de + d495145 commit ac1006c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

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)