Skip to content

Commit ca4beb6

Browse files
committed
More launcher tests
1 parent c896cf8 commit ca4beb6

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

src/fastcs/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ def _launch(controller_class: type[Controller]) -> typer.Typer:
111111

112112
launch_typer = typer.Typer()
113113

114-
class _LaunchContext:
114+
class LaunchContext:
115115
def __init__(self, controller_class, fastcs_options):
116116
self.controller_class = controller_class
117117
self.fastcs_options = fastcs_options
118118

119119
@launch_typer.callback()
120120
def create_context(ctx: typer.Context):
121-
ctx.obj = _LaunchContext(
121+
ctx.obj = LaunchContext(
122122
controller_class,
123123
fastcs_options,
124124
)
@@ -158,9 +158,9 @@ def run(
158158
instance_options.transport,
159159
)
160160

161-
if hasattr(instance_options.transport, "gui"):
161+
if "gui" in options_yaml["transport"]:
162162
instance.create_gui()
163-
if hasattr(instance_options.transport, "docs"):
163+
if "docs" in options_yaml["transport"]:
164164
instance.create_docs()
165165
instance.run()
166166

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
from fastcs.mapping import Mapping
1616
from fastcs.wrappers import command, scan
1717

18+
DATA_PATH = Path(__file__).parent / "data"
19+
20+
21+
@pytest.fixture
22+
def data() -> Path:
23+
return DATA_PATH
24+
25+
1826
# Prevent pytest from catching exceptions when debugging in vscode so that break on
1927
# exception works correctly (see: https://github.com/pytest-dev/pytest/issues/7409)
2028
if os.getenv("PYTEST_RAISE", "0") == "1":

tests/data/config_full.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# yaml-language-server: $schema=schema.json
2+
transport:
3+
ioc: {}
4+
docs: {}
5+
gui: {}
6+
controller:
7+
name: controller-name

tests/data/config_minimal.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# yaml-language-server: $schema=schema.json
2+
transport:
3+
dsr: {}

tests/test_main.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55
from pydantic import create_model
6+
from pytest_mock import MockerFixture
67
from typer.testing import CliRunner
78

89
from fastcs.controller import Controller
@@ -40,7 +41,23 @@ def __init__(self, arg: SomeConfig, too_many):
4041
runner = CliRunner()
4142

4243

43-
def test_is_hinted_schema():
44+
def test_single_arg_schema():
45+
target_model = create_model(
46+
"SingleArg",
47+
transport=(EpicsOptions | TangoOptions, ...),
48+
__config__={"extra": "forbid"},
49+
)
50+
target_dict = target_model.model_json_schema()
51+
52+
app = _launch(SingleArg)
53+
result = runner.invoke(app, ["schema"])
54+
assert result.exit_code == 0
55+
result_dict = json.loads(result.stdout)
56+
57+
assert result_dict == target_dict
58+
59+
60+
def test_is_hinted_schema(data):
4461
target_model = create_model(
4562
"IsHinted",
4663
controller=(SomeConfig, ...),
@@ -56,6 +73,10 @@ def test_is_hinted_schema():
5673

5774
assert result_dict == target_dict
5875

76+
# # store a schema to use for debugging
77+
# with open(data / "schema.json", mode="w") as f:
78+
# json.dump(result_dict, f, indent=2)
79+
5980

6081
def test_not_hinted_schema():
6182
error = (
@@ -78,3 +99,31 @@ def test_over_defined_schema():
7899
with pytest.raises(LaunchError) as exc_info:
79100
launch(ManyArgs)
80101
assert str(exc_info.value) == error
102+
103+
104+
def test_launch_minimal(mocker: MockerFixture, data):
105+
run = mocker.patch("fastcs.main.FastCS.run")
106+
gui = mocker.patch("fastcs.main.FastCS.create_gui")
107+
docs = mocker.patch("fastcs.main.FastCS.create_docs")
108+
109+
app = _launch(SingleArg)
110+
result = runner.invoke(app, ["run", str(data / "config_minimal.yaml")])
111+
assert result.exit_code == 0
112+
113+
run.assert_called_once()
114+
gui.assert_not_called()
115+
docs.assert_not_called()
116+
117+
118+
def test_launch_full(mocker: MockerFixture, data):
119+
run = mocker.patch("fastcs.main.FastCS.run")
120+
gui = mocker.patch("fastcs.main.FastCS.create_gui")
121+
docs = mocker.patch("fastcs.main.FastCS.create_docs")
122+
123+
app = _launch(IsHinted)
124+
result = runner.invoke(app, ["run", str(data / "config_full.yaml")])
125+
assert result.exit_code == 0
126+
127+
run.assert_called_once()
128+
gui.assert_called_once()
129+
docs.assert_called_once()

0 commit comments

Comments
 (0)