|
2 | 2 |
|
3 | 3 | import sys |
4 | 4 | from pathlib import Path |
| 5 | +from unittest.mock import AsyncMock, Mock |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 |
|
8 | 9 | import examples.run_examples as run_examples |
9 | 10 |
|
10 | 11 |
|
| 12 | +@pytest.mark.parametrize("auto_source", ["argument", "environment", "manual"]) |
| 13 | +def test_local_temporal_runner_selection( |
| 14 | + monkeypatch: pytest.MonkeyPatch, |
| 15 | + tmp_path: Path, |
| 16 | + capsys: pytest.CaptureFixture[str], |
| 17 | + auto_source: str, |
| 18 | +) -> None: |
| 19 | + monkeypatch.delenv("EXAMPLES_AUTO_SKIP", raising=False) |
| 20 | + monkeypatch.setenv( |
| 21 | + "EXAMPLES_INTERACTIVE_MODE", "auto" if auto_source == "environment" else "manual" |
| 22 | + ) |
| 23 | + monkeypatch.setattr(run_examples, "build_command_path", lambda: "") |
| 24 | + spawn = Mock(side_effect=AssertionError("The runner must not start this example")) |
| 25 | + monkeypatch.setattr(run_examples.subprocess, "Popen", spawn) |
| 26 | + args = [ |
| 27 | + "run_examples.py", |
| 28 | + "--filter", |
| 29 | + "local_hello_workflow", |
| 30 | + "--logs-dir", |
| 31 | + str(tmp_path / "logs"), |
| 32 | + "--main-log", |
| 33 | + str(tmp_path / "main.log"), |
| 34 | + "--artifacts-dir", |
| 35 | + str(tmp_path / "artifacts"), |
| 36 | + ] |
| 37 | + if auto_source == "argument": |
| 38 | + args.append("--auto-mode") |
| 39 | + elif auto_source == "manual": |
| 40 | + args.append("--dry-run") |
| 41 | + monkeypatch.setattr(sys, "argv", args) |
| 42 | + |
| 43 | + assert run_examples.main() == 0 |
| 44 | + |
| 45 | + output = capsys.readouterr().out |
| 46 | + relpath = "examples/sandbox/extensions/temporal/local_hello_workflow.py" |
| 47 | + assert f"- {'RUN ' if auto_source == 'manual' else 'SKIP'} {relpath}" in output |
| 48 | + if auto_source != "manual": |
| 49 | + assert "(skipped: auto-skip)" in output |
| 50 | + spawn.assert_not_called() |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize("mode", ["auto", "AUTO", "manual"]) |
| 54 | +@pytest.mark.skipif(sys.platform == "win32", reason="The example requires the Unix-only backend") |
| 55 | +@pytest.mark.asyncio |
| 56 | +async def test_local_temporal_entrypoint_refuses_auto_mode( |
| 57 | + monkeypatch: pytest.MonkeyPatch, mode: str |
| 58 | +) -> None: |
| 59 | + pytest.importorskip("temporalio") |
| 60 | + from examples.sandbox.extensions.temporal import local_hello_workflow |
| 61 | + |
| 62 | + monkeypatch.setenv("EXAMPLES_INTERACTIVE_MODE", mode) |
| 63 | + monkeypatch.setenv("OPENAI_API_KEY", "synthetic-test-key") |
| 64 | + # A custom skip list can select the example, but cannot authorize auto-mode execution. |
| 65 | + monkeypatch.setenv("EXAMPLES_AUTO_SKIP", "examples/basic/hello_world.py") |
| 66 | + start_server = AsyncMock(side_effect=RuntimeError("test server startup boundary")) |
| 67 | + monkeypatch.setattr( |
| 68 | + local_hello_workflow.WorkflowEnvironment, "start_time_skipping", start_server |
| 69 | + ) |
| 70 | + |
| 71 | + if mode.lower() == "auto": |
| 72 | + with pytest.raises(SystemExit, match="cannot run in auto mode"): |
| 73 | + await local_hello_workflow.main() |
| 74 | + start_server.assert_not_awaited() |
| 75 | + else: |
| 76 | + with pytest.raises(RuntimeError, match="test server startup boundary"): |
| 77 | + await local_hello_workflow.main() |
| 78 | + start_server.assert_awaited_once() |
| 79 | + |
| 80 | + |
11 | 81 | def test_default_auto_skip_excludes_prerequisite_bound_examples() -> None: |
12 | 82 | expected = { |
13 | 83 | "examples/sandbox/docker/mounts/azure_mount_read_write.py", |
|
0 commit comments