| 1 | import io |
| 2 | import sys |
| 3 | import argparse |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from aioconsole import AsynchronousCli |
| 8 | |
| 9 | testdata = { |
| 10 | "simple_command": ( |
| 11 | "hello\n", |
| 12 | """\ |
| 13 | Welcome to the CLI interface of hello! |
| 14 | Try: |
| 15 | * 'help' to display the help message |
| 16 | * 'list' to display the command list. |
| 17 | [Hello!] Hello! |
| 18 | [Hello!] \n""", |
| 19 | ), |
| 20 | "simple_command_with_arg": ( |
| 21 | "hello -n Neil\n", |
| 22 | """\ |
| 23 | Welcome to the CLI interface of hello! |
| 24 | Try: |
| 25 | * 'help' to display the help message |
| 26 | * 'list' to display the command list. |
| 27 | [Hello!] Hello Neil! |
| 28 | [Hello!] \n""", |
| 29 | ), |
| 30 | "list_command": ( |
| 31 | "list\n", |
| 32 | """\ |
| 33 | Welcome to the CLI interface of hello! |
| 34 | Try: |
| 35 | * 'help' to display the help message |
| 36 | * 'list' to display the command list. |
| 37 | [Hello!] List of commands: |
| 38 | * exit [-h] |
| 39 | * hello [-h] [--name NAME] |
| 40 | * help [-h] |
| 41 | * list [-h] |
| 42 | [Hello!] \n""", |
| 43 | ), |
| 44 | "help_command": ( |
| 45 | "help\n", |
| 46 | """\ |
| 47 | Welcome to the CLI interface of hello! |
| 48 | Try: |
| 49 | * 'help' to display the help message |
| 50 | * 'list' to display the command list. |
| 51 | [Hello!] Type 'help' to display this message. |
| 52 | Type 'list' to display the command list. |
| 53 | Type '<command> -h' to display the help message of <command>. |
| 54 | [Hello!] \n""", |
| 55 | ), |
| 56 | "exit_command": ( |
| 57 | "exit\n", |
| 58 | """\ |
| 59 | Welcome to the CLI interface of hello! |
| 60 | Try: |
| 61 | * 'help' to display the help message |
| 62 | * 'list' to display the command list. |
| 63 | [Hello!] """, |
| 64 | ), |
| 65 | "wrong_command": ( |
| 66 | "hellooo\n", |
| 67 | """\ |
| 68 | Welcome to the CLI interface of hello! |
| 69 | Try: |
| 70 | * 'help' to display the help message |
| 71 | * 'list' to display the command list. |
| 72 | [Hello!] Command 'hellooo' does not exist. |
| 73 | [Hello!] \n""", |
| 74 | ), |
| 75 | } |
| 76 | |
| 77 | |
| 78 | def make_cli(streams=None): |
| 79 | async def say_hello(reader, writer, name=None): |
| 80 | data = f"Hello {name}!" if name else "Hello!" |
| 81 | writer.write(data.encode() + b"\n") |
| 82 | |
| 83 | parser = argparse.ArgumentParser(description="Say hello") |
| 84 | parser.add_argument("--name", "-n", type=str) |
| 85 | commands = {"hello": (say_hello, parser)} |
| 86 | return AsynchronousCli(commands, streams, prog="hello") |
| 87 | |
| 88 | |
| 89 | @pytest.mark.parametrize( |
| 90 | "input_string, expected", list(testdata.values()), ids=list(testdata.keys()) |
| 91 | ) |
| 92 | @pytest.mark.asyncio |
| 93 | async def test_async_cli(monkeypatch, input_string, expected): |
| 94 | monkeypatch.setattr("sys.ps1", "[Hello!] ", raising=False) |
| 95 | monkeypatch.setattr("sys.stdin", io.StringIO(input_string)) |
| 96 | monkeypatch.setattr("sys.stderr", io.StringIO()) |
| 97 | await make_cli().interact(stop=False) |
| 98 | assert sys.stderr.getvalue() == expected |