File: test_command.py

package info (click to toggle)
aioconsole 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 332 kB
  • sloc: python: 2,022; makefile: 6
file content (98 lines) | stat: -rw-r--r-- 2,499 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import io
import sys
import argparse

import pytest

from aioconsole import AsynchronousCli

testdata = {
    "simple_command": (
        "hello\n",
        """\
Welcome to the CLI interface of hello!
Try:
 * 'help' to display the help message
 * 'list' to display the command list.
[Hello!] Hello!
[Hello!] \n""",
    ),
    "simple_command_with_arg": (
        "hello -n Neil\n",
        """\
Welcome to the CLI interface of hello!
Try:
 * 'help' to display the help message
 * 'list' to display the command list.
[Hello!] Hello Neil!
[Hello!] \n""",
    ),
    "list_command": (
        "list\n",
        """\
Welcome to the CLI interface of hello!
Try:
 * 'help' to display the help message
 * 'list' to display the command list.
[Hello!] List of commands:
 * exit [-h]
 * hello [-h] [--name NAME]
 * help [-h]
 * list [-h]
[Hello!] \n""",
    ),
    "help_command": (
        "help\n",
        """\
Welcome to the CLI interface of hello!
Try:
 * 'help' to display the help message
 * 'list' to display the command list.
[Hello!] Type 'help' to display this message.
Type 'list' to display the command list.
Type '<command> -h' to display the help message of <command>.
[Hello!] \n""",
    ),
    "exit_command": (
        "exit\n",
        """\
Welcome to the CLI interface of hello!
Try:
 * 'help' to display the help message
 * 'list' to display the command list.
[Hello!] """,
    ),
    "wrong_command": (
        "hellooo\n",
        """\
Welcome to the CLI interface of hello!
Try:
 * 'help' to display the help message
 * 'list' to display the command list.
[Hello!] Command 'hellooo' does not exist.
[Hello!] \n""",
    ),
}


def make_cli(streams=None):
    async def say_hello(reader, writer, name=None):
        data = f"Hello {name}!" if name else "Hello!"
        writer.write(data.encode() + b"\n")

    parser = argparse.ArgumentParser(description="Say hello")
    parser.add_argument("--name", "-n", type=str)
    commands = {"hello": (say_hello, parser)}
    return AsynchronousCli(commands, streams, prog="hello")


@pytest.mark.parametrize(
    "input_string, expected", list(testdata.values()), ids=list(testdata.keys())
)
@pytest.mark.asyncio
async def test_async_cli(monkeypatch, input_string, expected):
    monkeypatch.setattr("sys.ps1", "[Hello!] ", raising=False)
    monkeypatch.setattr("sys.stdin", io.StringIO(input_string))
    monkeypatch.setattr("sys.stderr", io.StringIO())
    await make_cli().interact(stop=False)
    assert sys.stderr.getvalue() == expected