|
| 1 | +import sys |
| 2 | +from collections.abc import Generator |
| 3 | +from importlib.metadata import EntryPoint |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +import typer as _typer |
| 9 | +from fastapi_cli.cli import _load_plugins |
| 10 | + |
| 11 | +PLUGINS_ASSET_PATH: Path = Path(__file__).parent / "assets" |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def test_app() -> _typer.Typer: |
| 16 | + return _typer.Typer() |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def plugins_on_path() -> Generator[None, None, None]: |
| 21 | + original_path = sys.path.copy() |
| 22 | + sys.path.insert(0, str(PLUGINS_ASSET_PATH)) |
| 23 | + try: |
| 24 | + yield |
| 25 | + finally: |
| 26 | + sys.path[:] = original_path |
| 27 | + for key in list(sys.modules.keys()): |
| 28 | + if key.startswith("plugins."): |
| 29 | + del sys.modules[key] |
| 30 | + |
| 31 | + |
| 32 | +def _entry_point(name: str, module_attr: str) -> EntryPoint: |
| 33 | + return EntryPoint( |
| 34 | + name=name, |
| 35 | + value=f"plugins.{module_attr}", |
| 36 | + group="fastapi_cli.plugins", |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def test_load_plugins_happy_path(plugins_on_path: None, test_app: _typer.Typer) -> None: |
| 41 | + """Plugin registers its command on the Typer app.""" |
| 42 | + |
| 43 | + ep = _entry_point("sample", "sample:register") |
| 44 | + |
| 45 | + with patch("fastapi_cli.cli._entry_points", return_value=[ep]): |
| 46 | + _load_plugins(test_app) |
| 47 | + |
| 48 | + names = {ci.name for ci in test_app.registered_commands} |
| 49 | + assert "ping" in names |
| 50 | + |
| 51 | + |
| 52 | +def test_load_plugins_logs_on_failure(plugins_on_path: None, test_app: _typer.Typer) -> None: |
| 53 | + """A plugin that raises is skipped and a warning is logged.""" |
| 54 | + |
| 55 | + ep = _entry_point("broken", "broken:register") |
| 56 | + |
| 57 | + with ( |
| 58 | + patch("fastapi_cli.cli._entry_points", return_value=[ep]), |
| 59 | + patch("fastapi_cli.cli.logger") as mock_logger, |
| 60 | + ): |
| 61 | + _load_plugins(test_app) |
| 62 | + |
| 63 | + mock_logger.warning.assert_called_once() |
| 64 | + _fmt, ep_name, *_ = mock_logger.warning.call_args.args |
| 65 | + assert "broken" in ep_name |
| 66 | + |
| 67 | + |
| 68 | +def test_load_plugins_warns_on_collision_with_builtin( |
| 69 | + plugins_on_path: None, test_app: _typer.Typer |
| 70 | +) -> None: |
| 71 | + """Plugin registering a name already on the app triggers a collision warning.""" |
| 72 | + |
| 73 | + @test_app.command("dev") |
| 74 | + def existing() -> None: |
| 75 | + pass # pragma: no cover |
| 76 | + |
| 77 | + ep = _entry_point("colliding", "colliding:register") |
| 78 | + |
| 79 | + with ( |
| 80 | + patch("fastapi_cli.cli._entry_points", return_value=[ep]), |
| 81 | + patch("fastapi_cli.cli.logger") as mock_logger, |
| 82 | + ): |
| 83 | + _load_plugins(test_app) |
| 84 | + |
| 85 | + mock_logger.warning.assert_called_once() |
| 86 | + _fmt, ep_name, collisions = mock_logger.warning.call_args.args |
| 87 | + assert "colliding" in ep_name |
| 88 | + assert "dev" in collisions |
| 89 | + |
| 90 | + |
| 91 | +def test_load_plugins_warns_on_cross_plugin_collision( |
| 92 | + plugins_on_path: None, test_app: _typer.Typer |
| 93 | +) -> None: |
| 94 | + """Two plugins registering the same name: only the second gets a warning.""" |
| 95 | + |
| 96 | + ep_a = _entry_point("sample", "sample:register") # registers "ping" |
| 97 | + ep_b = _entry_point("colliding2", "sample:register") # also tries to register "ping" |
| 98 | + |
| 99 | + with ( |
| 100 | + patch("fastapi_cli.cli._entry_points", return_value=[ep_a, ep_b]), |
| 101 | + patch("fastapi_cli.cli.logger") as mock_logger, |
| 102 | + ): |
| 103 | + _load_plugins(test_app) |
| 104 | + |
| 105 | + assert mock_logger.warning.call_count == 1 |
| 106 | + _fmt, ep_name, collisions = mock_logger.warning.call_args.args |
| 107 | + assert "colliding2" in ep_name |
| 108 | + assert "ping" in collisions |
0 commit comments