| 1 |
import contextlib |
| 2 |
from typing import Tuple |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
from click.testing import CliRunner |
| 7 |
|
| 8 |
from celery.bin.celery import celery |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture(autouse=True) |
| 12 |
def reset_command_params_between_each_test(): |
| 13 |
with contextlib.ExitStack() as stack: |
| 14 |
for command in celery.commands.values(): |
| 15 |
# We only need shallow copy -- preload options are appended to the list, |
| 16 |
# existing options are kept as-is |
| 17 |
params_copy = command.params[:] |
| 18 |
patch_instance = patch.object(command, "params", params_copy) |
| 19 |
stack.enter_context(patch_instance) |
| 20 |
|
| 21 |
yield |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize( |
| 25 |
"subcommand_with_params", |
| 26 |
[ |
| 27 |
("purge", "-f"), |
| 28 |
("shell",), |
| 29 |
] |
| 30 |
) |
| 31 |
def test_preload_options(subcommand_with_params: Tuple[str, ...], isolated_cli_runner: CliRunner): |
| 32 |
# Verify commands like shell and purge can accept preload options. |
| 33 |
# Projects like Pyramid-Celery's ini option should be valid preload |
| 34 |
# options. |
| 35 |
res_without_preload = isolated_cli_runner.invoke( |
| 36 |
celery, |
| 37 |
["-A", "t.unit.bin.proj.app", *subcommand_with_params, "--ini", "some_ini.ini"], |
| 38 |
catch_exceptions=False, |
| 39 |
) |
| 40 |
|
| 41 |
assert "No such option: --ini" in res_without_preload.output |
| 42 |
assert res_without_preload.exit_code == 2 |
| 43 |
|
| 44 |
res_with_preload = isolated_cli_runner.invoke( |
| 45 |
celery, |
| 46 |
[ |
| 47 |
"-A", |
| 48 |
"t.unit.bin.proj.pyramid_celery_app", |
| 49 |
*subcommand_with_params, |
| 50 |
"--ini", |
| 51 |
"some_ini.ini", |
| 52 |
], |
| 53 |
catch_exceptions=False, |
| 54 |
) |
| 55 |
|
| 56 |
assert res_with_preload.exit_code == 0, res_with_preload.output |