|
| 1 | +from io import StringIO |
| 2 | +from textwrap import dedent |
1 | 3 | from types import FunctionType |
| 4 | +from unittest.mock import Mock |
2 | 5 |
|
3 | 6 | import pytest |
4 | 7 |
|
5 | | -from blueapi.cli.stubgen import _docstring, _type_string |
| 8 | +from blueapi.cli.stubgen import ( |
| 9 | + _docstring, |
| 10 | + _type_string, |
| 11 | + generate_stubs, |
| 12 | + render_stub_file, |
| 13 | +) |
| 14 | +from blueapi.client.cache import DeviceRef, Plan |
| 15 | +from blueapi.service.model import DeviceModel, PlanModel |
6 | 16 |
|
7 | 17 |
|
8 | 18 | def single_line(): |
@@ -76,3 +86,129 @@ def test_docstring_filter(input: FunctionType, expected: str): |
76 | 86 | ) |
77 | 87 | def test_type_string(typ: dict, expected: str): |
78 | 88 | assert _type_string(typ) == expected |
| 89 | + |
| 90 | + |
| 91 | +def test_render_empty(): |
| 92 | + output = StringIO() |
| 93 | + |
| 94 | + render_stub_file(output, [], []) |
| 95 | + plan_text, device_text = _extract_rendered(output) |
| 96 | + |
| 97 | + assert plan_text == "" |
| 98 | + assert device_text == "" |
| 99 | + |
| 100 | + |
| 101 | +FOO = PlanModel(name="empty", description="Doc string for empty", schema={}) |
| 102 | + |
| 103 | +BAR = PlanModel( |
| 104 | + name="two_args", |
| 105 | + description="Doc string for two_args", |
| 106 | + schema={ |
| 107 | + "properties": { |
| 108 | + "one": {"type": "integer"}, |
| 109 | + "two": {"type": "string"}, |
| 110 | + }, |
| 111 | + "required": ["one"], |
| 112 | + }, |
| 113 | +) |
| 114 | + |
| 115 | + |
| 116 | +def test_render_empty_plan_function(): |
| 117 | + output = StringIO() |
| 118 | + plans = [Plan(model=FOO, runner=Mock())] |
| 119 | + render_stub_file(output, plans, []) |
| 120 | + plan_text, device_text = _extract_rendered(output) |
| 121 | + |
| 122 | + assert device_text == "" |
| 123 | + |
| 124 | + assert ( |
| 125 | + plan_text |
| 126 | + == """\ |
| 127 | + def empty(self, |
| 128 | + ) -> WorkerEvent: |
| 129 | + \""" |
| 130 | + Doc string for empty |
| 131 | + \""" |
| 132 | + ...\n""" |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +def test_render_multiple_plan_functions(): |
| 137 | + output = StringIO() |
| 138 | + runner = Mock() |
| 139 | + plans = [Plan(FOO, runner), Plan(BAR, runner)] |
| 140 | + render_stub_file(output, plans, []) |
| 141 | + plan_text, device_text = _extract_rendered(output) |
| 142 | + assert device_text == "" |
| 143 | + |
| 144 | + assert ( |
| 145 | + plan_text |
| 146 | + == """\ |
| 147 | + def empty(self, |
| 148 | + ) -> WorkerEvent: |
| 149 | + \""" |
| 150 | + Doc string for empty |
| 151 | + \""" |
| 152 | + ... |
| 153 | + def two_args(self, |
| 154 | + one: int, |
| 155 | + two: str | None = None, |
| 156 | + ) -> WorkerEvent: |
| 157 | + \""" |
| 158 | + Doc string for two_args |
| 159 | + \""" |
| 160 | + ...\n""" |
| 161 | + ) |
| 162 | + |
| 163 | + |
| 164 | +def test_device_fields(): |
| 165 | + output = StringIO() |
| 166 | + cache = Mock() |
| 167 | + devices = [ |
| 168 | + DeviceRef("one", cache, DeviceModel(name="one", protocols=[])), |
| 169 | + DeviceRef("two", cache, DeviceModel(name="two", protocols=[])), |
| 170 | + ] |
| 171 | + render_stub_file(output, [], devices) |
| 172 | + |
| 173 | + plan_text, device_text = _extract_rendered(output) |
| 174 | + assert plan_text == "" |
| 175 | + assert device_text == " one: DeviceRef\n two: DeviceRef\n" |
| 176 | + |
| 177 | + |
| 178 | +def test_package_creation(tmp_path): |
| 179 | + generate_stubs(tmp_path / "blueapi-stubs", [], []) |
| 180 | + with open(tmp_path / "blueapi-stubs" / "pyproject.toml") as pyproj: |
| 181 | + assert pyproj.read().startswith( |
| 182 | + dedent(""" |
| 183 | + [project] |
| 184 | + name = "blueapi-stubs" |
| 185 | + version = "0.1.0" |
| 186 | + """) |
| 187 | + ) |
| 188 | + with open( |
| 189 | + tmp_path / "blueapi-stubs" / "src" / "blueapi-stubs" / "py.typed" |
| 190 | + ) as typed: |
| 191 | + assert typed.read() == "partial\n" |
| 192 | + |
| 193 | + assert ( |
| 194 | + tmp_path / "blueapi-stubs" / "src" / "blueapi-stubs" / "client" / "cache.pyi" |
| 195 | + ).exists() |
| 196 | + |
| 197 | + |
| 198 | +def _extract_rendered(src: StringIO) -> tuple[str, str]: |
| 199 | + src.seek(0) |
| 200 | + _read_until_line(src, "### Generated plans") |
| 201 | + plan_text = _read_until_line(src, "### End") |
| 202 | + _read_until_line(src, "### Generated devices") |
| 203 | + device_text = _read_until_line(src, "### End") |
| 204 | + return plan_text, device_text |
| 205 | + |
| 206 | + |
| 207 | +def _read_until_line(src: StringIO, match: str) -> str: |
| 208 | + text = "" |
| 209 | + for line in src: |
| 210 | + if line.startswith(match): |
| 211 | + break |
| 212 | + text += line |
| 213 | + |
| 214 | + return text |
0 commit comments