-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
225740 Add pluggable system to the CLI tool
- Loading branch information
Showing
7 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from importlib.metadata import EntryPoint, entry_points | ||
|
||
from typer import Typer | ||
|
||
PLUGINS_PACKAGE = "swo.mpt.cli.plugins" | ||
|
||
|
||
def list_entrypoints() -> list[EntryPoint]: # pragma: no cover | ||
entrypoints = entry_points().select(group=PLUGINS_PACKAGE) | ||
if not entrypoints: | ||
return [] | ||
|
||
return entrypoints | ||
|
||
|
||
def load_plugins(app: Typer): | ||
for entrypoint in list_entrypoints(): | ||
plugin = entrypoint.load() | ||
app.add_typer(plugin.app, name=entrypoint.name) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .app import app # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import typer | ||
from swo.mpt.cli.core.console import console | ||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def test(): | ||
console.print("I'm a test plugin") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
27 changes: 27 additions & 0 deletions
27
tests/test_swo/test_mpt/test_cli/test_core/test_plugins.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from importlib.metadata import EntryPoint | ||
|
||
from swo.mpt.cli.core.plugins import load_plugins | ||
from swo.mpt.cli.swocli import app | ||
from typer.testing import CliRunner | ||
|
||
from tests.plugins.test_plugin import app as plugin_app | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_load_plugins(mocker): | ||
mocker.patch.object( | ||
EntryPoint, | ||
"load", | ||
side_effect=lambda: mocker.MagicMock(app=plugin_app), | ||
) | ||
mocker.patch( | ||
"swo.mpt.cli.core.plugins.list_entrypoints", | ||
return_value=[EntryPoint("tests", "tests.plugins.test_plugin", None)], | ||
) | ||
|
||
load_plugins(app) | ||
result = runner.invoke(app, ["tests", "test"]) | ||
|
||
assert result.exit_code == 0, result.stdout | ||
assert "I'm a test plugin" in result.stdout |