Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is there a nice way to test click auto-completion functions? #1453

Closed
seebi opened this issue Jan 13, 2020 · 5 comments
Closed

is there a nice way to test click auto-completion functions? #1453

seebi opened this issue Jan 13, 2020 · 5 comments

Comments

@seebi
Copy link

seebi commented Jan 13, 2020

The current documentation is blank about this.
Is there maybe an example somewhere?

@davidism
Copy link
Member

davidism commented Jan 13, 2020

To trigger autocompletion, your shell calls your program with the _PROG_NAME_COMPLETE=source env var. If you're using CliRunner, you can call invoke(..., env={"_FOO_COMPLETE": "source"}, prog_name="foo") along with partial arguments and look at the output, the same way the shell would.

Or test your autocompletion function directly, since Click's internals are already tested. An autocompletion function takes a Context, list of full arguments, and the partial string being completed, all of which you can fake in a test.

Click's own tests use a somewhat internal function: https://github.com/pallets/click/blob/master/tests/test_bashcomplete.py.

@seebi
Copy link
Author

seebi commented Jan 14, 2020

When I start it like this, I actually do not get my function output but something like

_cmemc_completion() {
    local IFS=$'
'
    COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   _CMEMC_COMPLETE=complete $1 ) )
    return 0
}

_cmemc_completionetup() {
    local COMPLETION_OPTIONS=""
    local BASH_VERSION_ARR=(${BASH_VERSION//./ })
    # Only BASH version 4.4 and later have the nosort option.
    if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then
        COMPLETION_OPTIONS="-o nosort"
    fi

    complete $COMPLETION_OPTIONS -F _cmemc_completion cmemc
}

_cmemc_completionetup;

Do I miss something here?

@davidism
Copy link
Member

davidism commented Jan 14, 2020

Nope, that's what autocompletion does, then the your shell takes that and actually handles the completion. You probably want to test your custom function, which the other two options do.

@seebi
Copy link
Author

seebi commented Jan 14, 2020

Ok, got it ... using

from click._bashcomplete import get_choices

def choices_without_help(cli, args, incomplete):
    completions = get_choices(cli, 'cmemc', args, incomplete)
    return [c[0] for c in completions]

def test_delete_completion():
    assert choices_without_help(cli, ['graph', 'delete'], 'acc') == ['...']

actually does the job.... nice.

And thanks @davidism

@seebi seebi closed this as completed Jan 14, 2020
@davidism
Copy link
Member

Follow up to this, I mistyped. _PROG_NAME_COMPLETE=complete, not source, will make Click output the available completions. source outputs the shell function that enables completion, and you can see it calling Click with complete. Either way works for testing.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants