pallets / click

Python composable command line interface toolkit
https://click.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
15.8k stars 1.4k forks source link

missing documentation: testing of shell completion #2299

Open seebi opened 2 years ago

seebi commented 2 years ago

I currently migrate an application to click 8.x and have to migrate all the completion functions as well as the test code for shell completion.

My working code for click 7.x shell completion testing is based on @davidism answer on this post https://github.com/pallets/click/issues/1453 2 years ago and worked fine.

However, with click 8.x, there is no get_choices anymore and the documentation at https://click.palletsprojects.com/en/8.1.x/testing/ does not mention shell completion testing.

Can you give me a new example or a link please?

This time I will prepare an MR for https://github.com/pallets/click/blob/8.1.x/docs/testing.rst :-)

juhoinkinen commented 1 year ago

I mimicked the completion tests in Click itself to make up this kind of testing for annif CLI application, basically just the following:

from click.shell_completion import ShellComplete

def _get_completions(cli, args, incomplete):
    comp = ShellComplete(cli, {}, cli.name, "_ANNIF_COMPLETE")
    return comp.get_completions(args, incomplete)

def _get_words(cli, args, incomplete):
    return [c.value for c in _get_completions(cli, args, incomplete)]

def test_completion_base_all_options():
    completions = _get_words(annif.cli.cli, [""], "--")
    assert completions == [
        "--env-file",
        "--app",
        "--debug",
        "--no-debug",
        "--version",
        "--help",
    ]

def test_completion_base_version_option():
    completions = _get_words(annif.cli.cli, [""], "--ver")
    assert completions == [
        "--version",
    ]
Rowlando13 commented 3 weeks ago

@seebi or @juhoinkinen Any interest in turning this into a PR for the documentation fix?