From f16a1a76bcec71098200c429c30990acb260d4b7 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Mon, 24 Jul 2023 22:56:51 +0700 Subject: [PATCH 1/3] chore: Clippy --- clap_complete/src/dynamic/completer.rs | 2 +- clap_complete/tests/testsuite/common.rs | 2 +- tests/builder/help.rs | 2 +- tests/derive/occurrences.rs | 11 ++++------- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/clap_complete/src/dynamic/completer.rs b/clap_complete/src/dynamic/completer.rs index 1f03843c..4fadf2f6 100644 --- a/clap_complete/src/dynamic/completer.rs +++ b/clap_complete/src/dynamic/completer.rs @@ -241,7 +241,7 @@ fn complete_path( .flatten() .filter_map(Result::ok) { - let raw_file_name = OsString::from(entry.file_name()); + let raw_file_name = entry.file_name(); if !raw_file_name.starts_with(&prefix) { continue; } diff --git a/clap_complete/tests/testsuite/common.rs b/clap_complete/tests/testsuite/common.rs index 6a00593b..b2fe11fe 100644 --- a/clap_complete/tests/testsuite/common.rs +++ b/clap_complete/tests/testsuite/common.rs @@ -369,7 +369,7 @@ pub fn register_example(name: &str, shell: completest::Shell) { runtime.register(name, registration).unwrap(); - snapbox::assert_subset_eq(&home, scratch_path); + snapbox::assert_subset_eq(home, scratch_path); scratch.close().unwrap(); } diff --git a/tests/builder/help.rs b/tests/builder/help.rs index de13fe04..14ff54bc 100644 --- a/tests/builder/help.rs +++ b/tests/builder/help.rs @@ -441,7 +441,7 @@ Options: } #[test] -#[cfg(all(feature = "wrap_help"))] +#[cfg(feature = "wrap_help")] fn possible_value_wrapped_help() { static WRAPPED_HELP: &str = "\ Usage: test [OPTIONS] diff --git a/tests/derive/occurrences.rs b/tests/derive/occurrences.rs index 4df6e98f..d49624d9 100644 --- a/tests/derive/occurrences.rs +++ b/tests/derive/occurrences.rs @@ -13,7 +13,7 @@ fn test_vec_of_vec() { Opt { points: vec![vec![1, 2], vec![0, 0]] }, - Opt::try_parse_from(&["test", "-p", "1", "2", "-p", "0", "0"]).unwrap() + Opt::try_parse_from(["test", "-p", "1", "2", "-p", "0", "0"]).unwrap() ); } @@ -47,7 +47,7 @@ fn test_vec_vec_empty() { assert_eq!( Opt { points: vec![] }, - Opt::try_parse_from(&["test"]).unwrap() + Opt::try_parse_from(["test"]).unwrap() ); } @@ -63,7 +63,7 @@ fn test_option_vec_vec() { Opt { points: Some(vec![vec![1, 2], vec![3, 4]]) }, - Opt::try_parse_from(&["test", "-p", "1", "2", "-p", "3", "4"]).unwrap() + Opt::try_parse_from(["test", "-p", "1", "2", "-p", "3", "4"]).unwrap() ); } @@ -75,8 +75,5 @@ fn test_option_vec_vec_empty() { points: Option>>, } - assert_eq!( - Opt { points: None }, - Opt::try_parse_from(&["test"]).unwrap() - ); + assert_eq!(Opt { points: None }, Opt::try_parse_from(["test"]).unwrap()); } From ab1407f1ae04b53bb270e1d3ab0a015b7d264c89 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Wed, 26 Jul 2023 21:25:21 +0700 Subject: [PATCH 2/3] chore: Fix dynamic completion tests --- .../tests/snapshots/register_minimal.bash | 14 +++-- clap_complete/tests/testsuite/bash.rs | 8 +-- clap_complete/tests/testsuite/dynamic.rs | 52 +++---------------- 3 files changed, 19 insertions(+), 55 deletions(-) diff --git a/clap_complete/tests/snapshots/register_minimal.bash b/clap_complete/tests/snapshots/register_minimal.bash index 41ef0b10..3779a382 100644 --- a/clap_complete/tests/snapshots/register_minimal.bash +++ b/clap_complete/tests/snapshots/register_minimal.bash @@ -1,16 +1,14 @@ _clap_complete_my_app() { - local IFS=$'/013' - local SUPPRESS_SPACE=0 + export _CLAP_COMPLETE_INDEX=${COMP_CWORD} + export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE} if compopt +o nospace 2> /dev/null; then - SUPPRESS_SPACE=1 - fi - if [[ ${SUPPRESS_SPACE} == 1 ]]; then - SPACE_ARG="--no-space" + export _CLAP_COMPLETE_SPACE=false else - SPACE_ARG="--space" + export _CLAP_COMPLETE_SPACE=true fi - COMPREPLY=( $("my-app" complete --index ${COMP_CWORD} --type ${COMP_TYPE} ${SPACE_ARG} --ifs="$IFS" -- "${COMP_WORDS[@]}") ) + export _CLAP_COMPLETE_IFS=$'/013' + COMPREPLY=( $("my-app" complete --shell bash -- "${COMP_WORDS[@]}") ) if [[ $? != 0 ]]; then unset COMPREPLY elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then diff --git a/clap_complete/tests/testsuite/bash.rs b/clap_complete/tests/testsuite/bash.rs index db03d2bb..38b05ea9 100644 --- a/clap_complete/tests/testsuite/bash.rs +++ b/clap_complete/tests/testsuite/bash.rs @@ -99,13 +99,15 @@ fn value_terminator() { #[cfg(feature = "unstable-dynamic")] #[test] fn register_minimal() { + use clap_complete::dynamic::Completer; + let name = "my-app"; - let executables = [name]; + let bin = name; let completer = name; - let behavior = clap_complete::dynamic::bash::Behavior::Minimal; let mut buf = Vec::new(); - clap_complete::dynamic::bash::register(name, executables, completer, &behavior, &mut buf) + clap_complete::dynamic::shells::Bash + .write_registration(name, bin, completer, &mut buf) .unwrap(); snapbox::Assert::new() .action_env("SNAPSHOTS") diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/dynamic.rs index f3a27c26..40ae27a1 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/dynamic.rs @@ -13,19 +13,10 @@ fn suggest_subcommand_subset() { let args = IntoIterator::into_iter(args) .map(std::ffi::OsString::from) .collect::>(); - let comp_type = clap_complete::dynamic::bash::CompType::default(); - let trailing_space = true; let current_dir = None; - let completions = clap_complete::dynamic::bash::complete( - &mut cmd, - args, - arg_index, - comp_type, - trailing_space, - current_dir, - ) - .unwrap(); + let completions = + clap_complete::dynamic::complete(&mut cmd, args, arg_index, current_dir).unwrap(); let completions = completions .into_iter() .map(|s| s.to_string_lossy().into_owned()) @@ -59,19 +50,10 @@ fn suggest_long_flag_subset() { let args = IntoIterator::into_iter(args) .map(std::ffi::OsString::from) .collect::>(); - let comp_type = clap_complete::dynamic::bash::CompType::default(); - let trailing_space = true; let current_dir = None; - let completions = clap_complete::dynamic::bash::complete( - &mut cmd, - args, - arg_index, - comp_type, - trailing_space, - current_dir, - ) - .unwrap(); + let completions = + clap_complete::dynamic::complete(&mut cmd, args, arg_index, current_dir).unwrap(); let completions = completions .into_iter() .map(|s| s.to_string_lossy().into_owned()) @@ -94,19 +76,10 @@ fn suggest_possible_value_subset() { let args = IntoIterator::into_iter(args) .map(std::ffi::OsString::from) .collect::>(); - let comp_type = clap_complete::dynamic::bash::CompType::default(); - let trailing_space = true; let current_dir = None; - let completions = clap_complete::dynamic::bash::complete( - &mut cmd, - args, - arg_index, - comp_type, - trailing_space, - current_dir, - ) - .unwrap(); + let completions = + clap_complete::dynamic::complete(&mut cmd, args, arg_index, current_dir).unwrap(); let completions = completions .into_iter() .map(|s| s.to_string_lossy().into_owned()) @@ -140,19 +113,10 @@ fn suggest_additional_short_flags() { let args = IntoIterator::into_iter(args) .map(std::ffi::OsString::from) .collect::>(); - let comp_type = clap_complete::dynamic::bash::CompType::default(); - let trailing_space = true; let current_dir = None; - let completions = clap_complete::dynamic::bash::complete( - &mut cmd, - args, - arg_index, - comp_type, - trailing_space, - current_dir, - ) - .unwrap(); + let completions = + clap_complete::dynamic::complete(&mut cmd, args, arg_index, current_dir).unwrap(); let completions = completions .into_iter() .map(|s| s.to_string_lossy().into_owned()) From 505046bf88d64f5eb2b27f9c4d9763fa66421ad5 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Wed, 26 Jul 2023 23:40:03 +0700 Subject: [PATCH 3/3] chore(ci): Test dynamic completions --- .github/workflows/ci.yml | 2 ++ .github/workflows/rust-next.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 67d3bf78..f941080e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -73,6 +73,8 @@ jobs: - name: Test (ultra-minimal) if: matrix.build == 'minimal' run: make test-minimal ARGS='--manifest-path Cargo.toml' + - name: Test dynamic completions + run: cargo test -p clap_complete -F unstable-dynamic check: name: Check runs-on: ubuntu-latest diff --git a/.github/workflows/rust-next.yml b/.github/workflows/rust-next.yml index 996f0977..90da41f7 100644 --- a/.github/workflows/rust-next.yml +++ b/.github/workflows/rust-next.yml @@ -70,3 +70,5 @@ jobs: - name: Test (ultra-minimal) if: matrix.build == 'minimal' run: make test-minimal ARGS='--manifest-path Cargo.toml' + - name: Test dynamic completions + run: cargo test -p clap_complete -F unstable-dynamic