click icon indicating copy to clipboard operation
click copied to clipboard

missing documentation: testing of shell completion

Open seebi opened this issue 2 years ago • 1 comments

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 :-)

seebi avatar May 28 '22 11:05 seebi

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",
    ]

juhoinkinen avatar Apr 04 '23 16:04 juhoinkinen