File: test_cli_plugin.py

package info (click to toggle)
litestar 2.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,500 kB
  • sloc: python: 70,169; makefile: 254; javascript: 105; sh: 60
file content (77 lines) | stat: -rw-r--r-- 2,919 bytes parent folder | download
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
from click.testing import CliRunner

from litestar.cli._utils import LitestarExtensionGroup
from tests.unit.test_cli.conftest import CreateAppFileFixture

APPLICATION_WITH_CLI_PLUGIN = """
from litestar import Litestar
from litestar.plugins import CLIPluginProtocol

class CLIPlugin(CLIPluginProtocol):
    def on_cli_init(self, cli):
        @cli.command()
        def mycommand(app: Litestar):
            \"\"\"Description of plugin command\"\"\"
            print(f"App is loaded: {app is not None}")

app = Litestar(plugins=[CLIPlugin()])
"""


def test_basic_command(
    runner: CliRunner,
    create_app_file: CreateAppFileFixture,
    root_command: LitestarExtensionGroup,
) -> None:
    app_file = create_app_file("command_test_app.py", content=APPLICATION_WITH_CLI_PLUGIN)
    result = runner.invoke(root_command, ["--app", f"{app_file.stem}:app", "mycommand"])

    assert not result.exception
    assert "App is loaded: True" in result.output


def test_plugin_command_appears_in_help_message(
    runner: CliRunner,
    create_app_file: CreateAppFileFixture,
    root_command: LitestarExtensionGroup,
) -> None:
    app_file = create_app_file("command_test_app.py", content=APPLICATION_WITH_CLI_PLUGIN)
    result = runner.invoke(root_command, ["--app", f"{app_file.stem}:app", "--help"])

    assert not result.exception
    assert "mycommand" in result.output
    assert "Description of plugin command" in result.output


def test_format_help_loads_plugins_before_rendering(
    runner: CliRunner,
    create_app_file: CreateAppFileFixture,
    root_command: LitestarExtensionGroup,
) -> None:
    """Test that format_help method correctly loads plugins before rendering help.

    This specifically tests the fix for rich-click where format_help was called
    before _prepare(), causing plugin commands to not appear in help output.
    """
    app_file = create_app_file("format_help_test_app.py", content=APPLICATION_WITH_CLI_PLUGIN)

    # Test by invoking help which internally calls format_help
    result = runner.invoke(root_command, ["--app", f"{app_file.stem}:app", "--help"])

    # Ensure the command succeeded
    assert result.exit_code == 0, f"Command failed with output: {result.output}"

    # Verify that plugin commands are included in the help output
    assert "mycommand" in result.output, "Plugin command should appear in help output"
    assert "Description of plugin command" in result.output, "Plugin command description should appear in help output"

    # Additional verification: ensure the plugin command is in the Commands section
    lines = result.output.split("\n")
    commands_section_found = False
    for line in lines:
        if "Commands" in line:
            commands_section_found = True
        if commands_section_found and "mycommand" in line:
            break
    else:
        assert False, "Plugin command not found in Commands section of help output"