Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional version command to launcher #96

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/fastcs/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from pydantic import BaseModel, create_model
from ruamel.yaml import YAML

from fastcs.__main__ import __version__

from .backend import Backend
from .controller import Controller
from .exceptions import LaunchError
Expand Down Expand Up @@ -62,7 +64,10 @@ def run(self) -> None:
self._transport.run()


def launch(controller_class: type[Controller]) -> None:
def launch(
controller_class: type[Controller],
version: str | None = None,
) -> None:
"""
Serves as an entry point for starting FastCS applications.

Expand All @@ -72,7 +77,9 @@ def launch(controller_class: type[Controller]) -> None:

Args:
controller_class (type[Controller]): The FastCS Controller to instantiate.
It must have a type-hinted __init__ method and no more than 2 arguments.
It must have a type-hinted __init__ method and no more than 2 arguments.
version (Optional[str]): The version of the FastCS Controller.
Optional

Raises:
LaunchError: If the class's __init__ is not as expected
Expand All @@ -86,10 +93,13 @@ def __init__(self, my_arg: MyControllerOptions) -> None:
if __name__ == "__main__":
launch(MyController)
"""
_launch(controller_class)()
_launch(controller_class, version)()


def _launch(controller_class: type[Controller]) -> typer.Typer:
def _launch(
controller_class: type[Controller],
version: str | None = None,
) -> typer.Typer:
fastcs_options = _extract_options_model(controller_class)
launch_typer = typer.Typer()

Expand Down Expand Up @@ -146,6 +156,12 @@ def run(
instance.create_docs()
instance.run()

@launch_typer.command(name="version", help=f"{controller_class.__name__} version")
def version_command():
if version:
print(f"{controller_class.__name__}: {version}")
print(f"FastCS: {__version__}")

return launch_typer


Expand Down
18 changes: 18 additions & 0 deletions tests/test_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pytest_mock import MockerFixture
from typer.testing import CliRunner

from fastcs.__main__ import __version__
from fastcs.controller import Controller
from fastcs.exceptions import LaunchError
from fastcs.launch import TransportOptions, _launch, launch
Expand Down Expand Up @@ -99,6 +100,23 @@ def test_over_defined_schema():
assert str(exc_info.value) == error


def test_version():
impl_version = "0.0.1"
expected = f"SingleArg: {impl_version}\n" f"FastCS: {__version__}\n"
app = _launch(SingleArg, version=impl_version)
result = runner.invoke(app, ["version"])
assert result.exit_code == 0
assert result.stdout == expected


def test_no_version():
expected = f"FastCS: {__version__}\n"
app = _launch(SingleArg)
result = runner.invoke(app, ["version"])
assert result.exit_code == 0
assert result.stdout == expected


def test_launch_minimal(mocker: MockerFixture, data):
run = mocker.patch("fastcs.launch.FastCS.run")
gui = mocker.patch("fastcs.launch.FastCS.create_gui")
Expand Down
Loading