Skip to content

Commit

Permalink
Merge pull request #5717 from epage/dyn-tests
Browse files Browse the repository at this point in the history
test(complete): Verify more shell integration cases
  • Loading branch information
epage authored Sep 4, 2024
2 parents 31a4450 + 931d9ac commit e09f793
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 48 deletions.
1 change: 1 addition & 0 deletions clap_complete/examples/exhaustive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn cli() -> clap::Command {
.long("choice")
.action(clap::ArgAction::Set)
.value_parser(clap::builder::PossibleValuesParser::new([
PossibleValue::new("another shell").help("something with a space"),
PossibleValue::new("bash").help("bash (shell)"),
PossibleValue::new("fish").help("fish shell"),
PossibleValue::new("zsh").help("zsh shell"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ _exhaustive() {
fi
case "${prev}" in
--choice)
COMPREPLY=($(compgen -W "bash fish zsh" -- "${cur}"))
COMPREPLY=($(compgen -W "another shell bash fish zsh" -- "${cur}"))
return 0
;;
*)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ complete -c exhaustive -n "__fish_exhaustive_using_subcommand action" -l count -
complete -c exhaustive -n "__fish_exhaustive_using_subcommand action" -l global -d 'everywhere'
complete -c exhaustive -n "__fish_exhaustive_using_subcommand action" -s h -l help -d 'Print help'
complete -c exhaustive -n "__fish_exhaustive_using_subcommand action" -s V -l version -d 'Print version'
complete -c exhaustive -n "__fish_exhaustive_using_subcommand quote; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions escape-help help" -l choice -r -f -a "{bash\t'bash (shell)',fish\t'fish shell',zsh\t'zsh shell'}"
complete -c exhaustive -n "__fish_exhaustive_using_subcommand quote; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions escape-help help" -l choice -r -f -a "{another shell\t'something with a space',bash\t'bash (shell)',fish\t'fish shell',zsh\t'zsh shell'}"
complete -c exhaustive -n "__fish_exhaustive_using_subcommand quote; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions escape-help help" -l single-quotes -d 'Can be \'always\', \'auto\', or \'never\''
complete -c exhaustive -n "__fish_exhaustive_using_subcommand quote; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions escape-help help" -l double-quotes -d 'Can be "always", "auto", or "never"'
complete -c exhaustive -n "__fish_exhaustive_using_subcommand quote; and not __fish_seen_subcommand_from cmd-single-quotes cmd-double-quotes cmd-backticks cmd-backslash cmd-brackets cmd-expansions escape-help help" -l backticks -d 'For more information see `echo test`'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ _arguments "${_arguments_options[@]}" : \
;;
(quote)
_arguments "${_arguments_options[@]}" : \
'--choice=[]: :((bash\:"bash (shell)"
'--choice=[]: :((another\ shell\:"something with a space"
bash\:"bash (shell)"
fish\:"fish shell"
zsh\:"zsh shell"))' \
'--single-quotes[Can be '\''always'\'', '\''auto'\'', or '\''never'\'']' \
Expand Down
70 changes: 59 additions & 11 deletions clap_complete/tests/testsuite/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ use snapbox::assert_data_eq;

use crate::common;

#[cfg(unix)]
const CMD: &str = "bash";
#[cfg(unix)]
type RuntimeBuilder = completest_pty::BashRuntimeBuilder;

#[test]
fn basic() {
let name = "my-app";
Expand Down Expand Up @@ -138,19 +143,18 @@ fn subcommand_last() {
#[test]
#[cfg(unix)]
fn register_completion() {
common::register_example::<completest_pty::BashRuntimeBuilder>("static", "exhaustive");
common::register_example::<RuntimeBuilder>("static", "exhaustive");
}

#[test]
#[cfg(unix)]
fn complete() {
if !common::has_command("bash") {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime =
common::load_runtime::<completest_pty::BashRuntimeBuilder>("static", "exhaustive");
let mut runtime = common::load_runtime::<RuntimeBuilder>("static", "exhaustive");

let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
Expand Down Expand Up @@ -235,19 +239,18 @@ fn complete() {
#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn register_dynamic_env() {
common::register_example::<completest_pty::BashRuntimeBuilder>("dynamic-env", "exhaustive");
common::register_example::<RuntimeBuilder>("dynamic-env", "exhaustive");
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn complete_dynamic_env_toplevel() {
if !common::has_command("bash") {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime =
common::load_runtime::<completest_pty::BashRuntimeBuilder>("dynamic-env", "exhaustive");
let mut runtime = common::load_runtime::<RuntimeBuilder>("dynamic-env", "exhaustive");

let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
Expand All @@ -262,13 +265,12 @@ fn complete_dynamic_env_toplevel() {
#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn complete_dynamic_env_quoted_help() {
if !common::has_command("bash") {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime =
common::load_runtime::<completest_pty::BashRuntimeBuilder>("dynamic-env", "exhaustive");
let mut runtime = common::load_runtime::<RuntimeBuilder>("dynamic-env", "exhaustive");

let input = "exhaustive quote \t\t";
let expected = snapbox::str![[r#"
Expand All @@ -281,3 +283,49 @@ fn complete_dynamic_env_quoted_help() {
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn complete_dynamic_env_option_value() {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime = common::load_runtime::<RuntimeBuilder>("dynamic-env", "exhaustive");

let input = "exhaustive action --choice=\t\t";
let expected = snapbox::str!["% "];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);

let input = "exhaustive action --choice=f\t";
let expected = snapbox::str!["exhaustive action --choice=f % exhaustive action --choice=f"];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn complete_dynamic_env_quoted_value() {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime = common::load_runtime::<RuntimeBuilder>("dynamic-env", "exhaustive");

let input = "exhaustive quote --choice \t\t";
let expected = snapbox::str![[r#"
%
another shell bash fish zsh
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);

let input = "exhaustive quote --choice an\t";
let expected =
snapbox::str!["exhaustive quote --choice an % exhaustive quote --choice another shell "];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
}
82 changes: 71 additions & 11 deletions clap_complete/tests/testsuite/elvish.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use crate::common;
use snapbox::assert_data_eq;

#[cfg(unix)]
const CMD: &str = "elvish";
#[cfg(unix)]
type RuntimeBuilder = completest_pty::ElvishRuntimeBuilder;

#[test]
fn basic() {
let name = "my-app";
Expand Down Expand Up @@ -137,19 +142,18 @@ fn subcommand_last() {
#[test]
#[cfg(unix)]
fn register_completion() {
common::register_example::<completest_pty::ElvishRuntimeBuilder>("static", "exhaustive");
common::register_example::<RuntimeBuilder>("static", "exhaustive");
}

#[test]
#[cfg(unix)]
fn complete_static_toplevel() {
if !common::has_command("elvish") {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime =
common::load_runtime::<completest_pty::ElvishRuntimeBuilder>("static", "exhaustive");
let mut runtime = common::load_runtime::<RuntimeBuilder>("static", "exhaustive");

let input = "exhaustive \t";
let expected = snapbox::str![[r#"
Expand Down Expand Up @@ -177,19 +181,18 @@ value value
#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn register_dynamic_env() {
common::register_example::<completest_pty::ElvishRuntimeBuilder>("dynamic-env", "exhaustive");
common::register_example::<RuntimeBuilder>("dynamic-env", "exhaustive");
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn complete_dynamic_env_toplevel() {
if !common::has_command("elvish") {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime =
common::load_runtime::<completest_pty::ElvishRuntimeBuilder>("dynamic-env", "exhaustive");
let mut runtime = common::load_runtime::<RuntimeBuilder>("dynamic-env", "exhaustive");

let input = "exhaustive \t";
let expected = snapbox::str![[r#"
Expand All @@ -205,13 +208,12 @@ fn complete_dynamic_env_toplevel() {
#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn complete_dynamic_env_quoted_help() {
if !common::has_command("elvish") {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime =
common::load_runtime::<completest_pty::ElvishRuntimeBuilder>("dynamic-env", "exhaustive");
let mut runtime = common::load_runtime::<RuntimeBuilder>("dynamic-env", "exhaustive");

let input = "exhaustive quote \t";
let expected = snapbox::str![[r#"
Expand All @@ -225,3 +227,61 @@ fn complete_dynamic_env_quoted_help() {
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn complete_dynamic_env_option_value() {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime = common::load_runtime::<RuntimeBuilder>("dynamic-env", "exhaustive");

let input = "exhaustive action --choice=\t";
let expected = snapbox::str![[r#"
% exhaustive action '--choice=first'
COMPLETING argument
--choice=first --choice=second
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);

let input = "exhaustive action --choice=f\t";
let expected = snapbox::str![[r#"
% exhaustive action '--choice=first'
COMPLETING argument
--choice=first
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
fn complete_dynamic_env_quoted_value() {
if !common::has_command(CMD) {
return;
}

let term = completest::Term::new();
let mut runtime = common::load_runtime::<RuntimeBuilder>("dynamic-env", "exhaustive");

let input = "exhaustive quote --choice \t";
let expected = snapbox::str![[r#"
% exhaustive quote --choice 'another shell'
COMPLETING argument
another shell bash fish zsh
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);

let input = "exhaustive quote --choice an\t";
let expected = snapbox::str![[r#"
% exhaustive quote --choice 'another shell'
COMPLETING argument
another shell
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
}
Loading

0 comments on commit e09f793

Please sign in to comment.