|
1 | | -from unittest.mock import MagicMock |
| 1 | +import sys |
| 2 | +import types |
| 3 | +from unittest.mock import AsyncMock, MagicMock |
2 | 4 |
|
3 | 5 | import pytest |
| 6 | +from click.testing import CliRunner |
4 | 7 |
|
5 | | -from uipath._cli import cli_dev |
| 8 | +from uipath._cli import cli, cli_dev |
| 9 | +from uipath._cli.middlewares import MiddlewareResult |
| 10 | +from uipath.platform.common import UiPathExecutionContext |
6 | 11 |
|
7 | 12 |
|
8 | | -def test_create_dev_factory_propagates_execution_source( |
| 13 | +def test_create_dev_context_and_factory_uses_dev_command( |
9 | 14 | monkeypatch: pytest.MonkeyPatch, |
10 | 15 | ) -> None: |
11 | | - """_create_dev_factory sets the 'playground' source and returns the factory.""" |
12 | | - captured: dict[str, object] = {} |
13 | | - |
14 | | - def fake_set_execution_source(value: object) -> None: |
15 | | - captured["source"] = value |
16 | | - |
| 16 | + """The helper builds a 'dev' context (source 'playground') and its factory.""" |
17 | 17 | sentinel_factory = MagicMock(name="factory") |
| 18 | + captured: dict[str, object] = {} |
18 | 19 |
|
19 | 20 | def fake_get(context: object) -> object: |
20 | 21 | captured["command"] = context.command # type: ignore[attr-defined] |
21 | 22 | return sentinel_factory |
22 | 23 |
|
23 | | - monkeypatch.setattr(cli_dev, "set_execution_source", fake_set_execution_source) |
24 | 24 | monkeypatch.setattr( |
25 | 25 | "uipath._cli.cli_dev.UiPathRuntimeFactoryRegistry.get", fake_get |
26 | 26 | ) |
27 | 27 |
|
28 | | - factory = cli_dev._create_dev_factory(None) # type: ignore[arg-type] |
| 28 | + context, factory = cli_dev._create_dev_context_and_factory(None) # type: ignore[arg-type] |
29 | 29 |
|
30 | 30 | assert factory is sentinel_factory |
31 | 31 | assert captured["command"] == "dev" |
32 | | - assert captured["source"] == "playground" |
| 32 | + assert context.execution_source == "playground" |
| 33 | + |
| 34 | + |
| 35 | +def test_dev_terminal_sets_execution_source_during_run( |
| 36 | + monkeypatch: pytest.MonkeyPatch, |
| 37 | +) -> None: |
| 38 | + """Running `dev terminal` scopes the execution source to 'playground'.""" |
| 39 | + seen: dict[str, object] = {} |
| 40 | + |
| 41 | + async def fake_run_async() -> None: |
| 42 | + seen["source"] = UiPathExecutionContext().execution_source |
| 43 | + |
| 44 | + fake_console = MagicMock() |
| 45 | + fake_console.run_async = fake_run_async |
| 46 | + |
| 47 | + fake_module = types.ModuleType("uipath.dev") |
| 48 | + fake_module.UiPathDeveloperConsole = MagicMock(return_value=fake_console) # type: ignore[attr-defined] |
| 49 | + monkeypatch.setitem(sys.modules, "uipath.dev", fake_module) |
| 50 | + |
| 51 | + mock_factory = MagicMock() |
| 52 | + mock_factory.dispose = AsyncMock() |
| 53 | + |
| 54 | + monkeypatch.setattr(cli_dev, "_check_dev_dependency", lambda interface: None) |
| 55 | + monkeypatch.setattr(cli_dev, "setup_debugging", lambda debug, port: True) |
| 56 | + monkeypatch.setattr( |
| 57 | + "uipath._cli.cli_dev.Middlewares.next", |
| 58 | + lambda *a, **k: MiddlewareResult(should_continue=True), |
| 59 | + ) |
| 60 | + monkeypatch.setattr( |
| 61 | + "uipath._cli.cli_dev.UiPathRuntimeFactoryRegistry.get", |
| 62 | + lambda context: mock_factory, |
| 63 | + ) |
| 64 | + |
| 65 | + result = CliRunner().invoke(cli, ["dev", "terminal"]) |
| 66 | + |
| 67 | + assert result.exit_code == 0, result.output |
| 68 | + assert seen["source"] == "playground" |
| 69 | + # token released once the run completes |
| 70 | + assert UiPathExecutionContext().execution_source is None |
0 commit comments