|
1 | 1 | # type: ignore |
2 | 2 | import os |
3 | | -from unittest.mock import patch |
| 3 | +from contextlib import asynccontextmanager |
| 4 | +from unittest.mock import AsyncMock, Mock, patch |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 | from click.testing import CliRunner |
|
9 | 10 | from uipath._cli.middlewares import MiddlewareResult |
10 | 11 |
|
11 | 12 |
|
| 13 | +def _middleware_continue(): |
| 14 | + return MiddlewareResult( |
| 15 | + should_continue=True, |
| 16 | + error_message=None, |
| 17 | + should_include_stacktrace=False, |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +async def _empty_async_gen(*args, **kwargs): |
| 22 | + """An async generator that yields nothing (simulates empty runtime.stream).""" |
| 23 | + return |
| 24 | + yield # noqa: unreachable - makes this an async generator |
| 25 | + |
| 26 | + |
| 27 | +def _make_mock_factory(entrypoints: list[str]): |
| 28 | + """Create a mock runtime factory with given entrypoints.""" |
| 29 | + mock_factory = Mock() |
| 30 | + mock_factory.discover_entrypoints.return_value = entrypoints |
| 31 | + mock_factory.get_settings = AsyncMock(return_value=None) |
| 32 | + mock_factory.dispose = AsyncMock() |
| 33 | + |
| 34 | + mock_runtime = Mock() |
| 35 | + mock_runtime.execute = AsyncMock(return_value=Mock(status="SUCCESSFUL")) |
| 36 | + mock_runtime.stream = Mock(side_effect=_empty_async_gen) |
| 37 | + mock_runtime.dispose = AsyncMock() |
| 38 | + mock_factory.new_runtime = AsyncMock(return_value=mock_runtime) |
| 39 | + |
| 40 | + return mock_factory |
| 41 | + |
| 42 | + |
| 43 | +@asynccontextmanager |
| 44 | +async def _mock_resource_overwrites_context(*args, **kwargs): |
| 45 | + yield |
| 46 | + |
| 47 | + |
12 | 48 | @pytest.fixture |
13 | 49 | def entrypoint(): |
14 | 50 | return "main" |
@@ -142,14 +178,81 @@ def test_run_input_file_success( |
142 | 178 | assert "Successful execution." in result.output |
143 | 179 |
|
144 | 180 | class TestMiddleware: |
145 | | - def test_no_entrypoint(self, runner: CliRunner, temp_dir: str): |
| 181 | + def test_autodiscover_entrypoint(self, runner: CliRunner, temp_dir: str): |
| 182 | + """When exactly one entrypoint exists, it is auto-resolved.""" |
146 | 183 | with runner.isolated_filesystem(temp_dir=temp_dir): |
147 | | - result = runner.invoke(cli, ["run"]) |
148 | | - assert result.exit_code == 1 |
149 | | - assert ( |
150 | | - "No entrypoint specified" in result.output |
151 | | - or "Missing argument" in result.output |
| 184 | + mock_factory = _make_mock_factory(["my_agent"]) |
| 185 | + |
| 186 | + with ( |
| 187 | + patch( |
| 188 | + "uipath._cli.cli_run.Middlewares.next", |
| 189 | + return_value=_middleware_continue(), |
| 190 | + ), |
| 191 | + patch( |
| 192 | + "uipath._cli.cli_run.UiPathRuntimeFactoryRegistry.get", |
| 193 | + return_value=mock_factory, |
| 194 | + ), |
| 195 | + patch( |
| 196 | + "uipath._cli.cli_run.ResourceOverwritesContext", |
| 197 | + side_effect=_mock_resource_overwrites_context, |
| 198 | + ), |
| 199 | + ): |
| 200 | + result = runner.invoke(cli, ["run"]) |
| 201 | + |
| 202 | + assert result.exit_code == 0, ( |
| 203 | + f"output: {result.output!r}, exception: {result.exception}" |
152 | 204 | ) |
| 205 | + assert "Successful execution." in result.output |
| 206 | + mock_factory.new_runtime.assert_awaited_once() |
| 207 | + assert mock_factory.new_runtime.call_args[0][0] == "my_agent" |
| 208 | + |
| 209 | + def test_no_entrypoint_multiple_available( |
| 210 | + self, runner: CliRunner, temp_dir: str |
| 211 | + ): |
| 212 | + """When multiple entrypoints exist and none specified, show usage help.""" |
| 213 | + with runner.isolated_filesystem(temp_dir=temp_dir): |
| 214 | + mock_factory = _make_mock_factory(["agent_a", "agent_b"]) |
| 215 | + |
| 216 | + with ( |
| 217 | + patch( |
| 218 | + "uipath._cli.cli_run.Middlewares.next", |
| 219 | + return_value=_middleware_continue(), |
| 220 | + ), |
| 221 | + patch( |
| 222 | + "uipath._cli.cli_run.UiPathRuntimeFactoryRegistry.get", |
| 223 | + return_value=mock_factory, |
| 224 | + ), |
| 225 | + ): |
| 226 | + result = runner.invoke(cli, ["run"]) |
| 227 | + |
| 228 | + assert result.exit_code == 0 |
| 229 | + assert "Available entrypoints:" in result.output |
| 230 | + assert "agent_a" in result.output |
| 231 | + assert "agent_b" in result.output |
| 232 | + assert "Usage: uipath run" in result.output |
| 233 | + mock_factory.new_runtime.assert_not_awaited() |
| 234 | + |
| 235 | + def test_no_entrypoint_none_available(self, runner: CliRunner, temp_dir: str): |
| 236 | + """When no entrypoints exist and none specified, show usage help.""" |
| 237 | + with runner.isolated_filesystem(temp_dir=temp_dir): |
| 238 | + mock_factory = _make_mock_factory([]) |
| 239 | + |
| 240 | + with ( |
| 241 | + patch( |
| 242 | + "uipath._cli.cli_run.Middlewares.next", |
| 243 | + return_value=_middleware_continue(), |
| 244 | + ), |
| 245 | + patch( |
| 246 | + "uipath._cli.cli_run.UiPathRuntimeFactoryRegistry.get", |
| 247 | + return_value=mock_factory, |
| 248 | + ), |
| 249 | + ): |
| 250 | + result = runner.invoke(cli, ["run"]) |
| 251 | + |
| 252 | + assert result.exit_code == 0 |
| 253 | + assert "No entrypoints found" in result.output |
| 254 | + assert "Usage: uipath run" in result.output |
| 255 | + mock_factory.new_runtime.assert_not_awaited() |
153 | 256 |
|
154 | 257 | def test_script_not_found( |
155 | 258 | self, runner: CliRunner, temp_dir: str, entrypoint: str |
|
0 commit comments