|
1 | 1 | from abc import ABC |
2 | 2 | from unittest.mock import patch |
3 | 3 |
|
| 4 | +import click |
4 | 5 | import pytest |
5 | 6 | from ellar.app import App |
6 | 7 | from ellar.common import ( |
|
23 | 24 | from ..main import router |
24 | 25 |
|
25 | 26 |
|
| 27 | +@click.command(name="command-one") |
| 28 | +def command_one(): |
| 29 | + click.echo("Hello World command one") |
| 30 | + |
| 31 | + |
| 32 | +@click.command(name="command-two") |
| 33 | +def command_two(): |
| 34 | + click.echo("Hello World command two") |
| 35 | + |
| 36 | + |
26 | 37 | class IDynamic(ABC): |
27 | 38 | a: int |
28 | 39 | b: float |
@@ -116,6 +127,17 @@ class LazyModuleImportWithSetup(ModuleBase): |
116 | 127 | pass |
117 | 128 |
|
118 | 129 |
|
| 130 | +@Module(commands=[command_one]) |
| 131 | +class DynamicModuleRegisterCommand(ModuleBase, IModuleSetup): |
| 132 | + @classmethod |
| 133 | + def setup(cls, command_three_text: str) -> DynamicModule: |
| 134 | + @click.command |
| 135 | + def command_three(): |
| 136 | + click.echo(command_three_text) |
| 137 | + |
| 138 | + return DynamicModule(cls, commands=[command_one, command_two, command_three]) |
| 139 | + |
| 140 | + |
119 | 141 | def test_invalid_lazy_module_import(): |
120 | 142 | with pytest.raises(ImproperConfiguration) as ex: |
121 | 143 | LazyModuleImport("tests.test_modules.test_module_config:IDynamic").get_module() |
@@ -303,3 +325,24 @@ def test_can_not_apply_dynamic_module_twice(): |
303 | 325 | with patch.object(reflect.__class__, "define_metadata") as mock_define_metadata: |
304 | 326 | dynamic_module.apply_configuration() |
305 | 327 | assert mock_define_metadata.called is False |
| 328 | + |
| 329 | + |
| 330 | +def test_dynamic_command_register_command(cli_runner): |
| 331 | + commands = reflect.get_metadata( |
| 332 | + MODULE_METADATA.COMMANDS, DynamicModuleRegisterCommand |
| 333 | + ) |
| 334 | + assert len(commands) == 1 |
| 335 | + res = cli_runner.invoke(commands[0], []) |
| 336 | + assert res.stdout == "Hello World command one\n" |
| 337 | + |
| 338 | + with reflect.context(): |
| 339 | + DynamicModuleRegisterCommand.setup("Command Three Here").apply_configuration() |
| 340 | + commands = reflect.get_metadata( |
| 341 | + MODULE_METADATA.COMMANDS, DynamicModuleRegisterCommand |
| 342 | + ) |
| 343 | + assert len(commands) == 3 |
| 344 | + |
| 345 | + res = cli_runner.invoke(commands[2], []) |
| 346 | + assert res.stdout == "Command Three Here\n" |
| 347 | + |
| 348 | + assert len(reflect._meta_data) > 10 |
0 commit comments