|
1 | | -import textwrap |
2 | | - |
3 | 1 | from click.testing import CliRunner |
4 | 2 |
|
5 | | -from litestar.cli._utils import LitestarGroup |
| 3 | +from litestar.cli._utils import LitestarExtensionGroup |
6 | 4 | from tests.unit.test_cli.conftest import CreateAppFileFixture |
7 | 5 |
|
| 6 | +APPLICATION_WITH_CLI_PLUGIN = """ |
| 7 | +from litestar import Litestar |
| 8 | +from litestar.plugins import CLIPluginProtocol |
| 9 | +
|
| 10 | +class CLIPlugin(CLIPluginProtocol): |
| 11 | + def on_cli_init(self, cli): |
| 12 | + @cli.command() |
| 13 | + def mycommand(app: Litestar): |
| 14 | + \"\"\"Description of plugin command\"\"\" |
| 15 | + print(f"App is loaded: {app is not None}") |
8 | 16 |
|
9 | | -def test_basic_command(runner: CliRunner, create_app_file: CreateAppFileFixture, root_command: LitestarGroup) -> None: |
10 | | - app_file_content = textwrap.dedent( |
11 | | - """ |
12 | | - from litestar import Litestar |
13 | | - from litestar.plugins import CLIPluginProtocol |
| 17 | +app = Litestar(plugins=[CLIPlugin()]) |
| 18 | +""" |
14 | 19 |
|
15 | | - class CLIPlugin(CLIPluginProtocol): |
16 | | - def on_cli_init(self, cli): |
17 | | - @cli.command() |
18 | | - def foo(app: Litestar): |
19 | | - print(f"App is loaded: {app is not None}") |
20 | 20 |
|
21 | | - app = Litestar(plugins=[CLIPlugin()]) |
22 | | - """ |
23 | | - ) |
24 | | - app_file = create_app_file("command_test_app.py", content=app_file_content) |
25 | | - result = runner.invoke(root_command, ["--app", f"{app_file.stem}:app", "foo"]) |
| 21 | +def test_basic_command( |
| 22 | + runner: CliRunner, |
| 23 | + create_app_file: CreateAppFileFixture, |
| 24 | + root_command: LitestarExtensionGroup, |
| 25 | +) -> None: |
| 26 | + app_file = create_app_file("command_test_app.py", content=APPLICATION_WITH_CLI_PLUGIN) |
| 27 | + result = runner.invoke(root_command, ["--app", f"{app_file.stem}:app", "mycommand"]) |
26 | 28 |
|
27 | 29 | assert not result.exception |
28 | 30 | assert "App is loaded: True" in result.output |
| 31 | + |
| 32 | + |
| 33 | +def test_plugin_command_appears_in_help_message( |
| 34 | + runner: CliRunner, |
| 35 | + create_app_file: CreateAppFileFixture, |
| 36 | + root_command: LitestarExtensionGroup, |
| 37 | +) -> None: |
| 38 | + app_file = create_app_file("command_test_app.py", content=APPLICATION_WITH_CLI_PLUGIN) |
| 39 | + result = runner.invoke(root_command, ["--app", f"{app_file.stem}:app", "--help"]) |
| 40 | + |
| 41 | + assert not result.exception |
| 42 | + assert "mycommand" in result.output |
| 43 | + assert "Description of plugin command" in result.output |
0 commit comments