|
| 1 | +"""Smoke tests for examples/tutorials Python files.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from typing import Iterator, Tuple |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | + |
| 12 | +SMOKE_TEST_TIMEOUT = 90 |
| 13 | +PROJECT_ROOT = Path(__file__).parent.parent.parent |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope="module", autouse=True) |
| 17 | +def ray_disable_uv_run() -> Iterator[None]: |
| 18 | + """Disable Ray's `uv run` runtime environment for smoke tests.""" |
| 19 | + # uv run environment will prevent tests from running outside of the project root |
| 20 | + # This is necessary because the smoke tests run in a separate process |
| 21 | + os.environ["RAY_ENABLE_UV_RUN_RUNTIME_ENV"] = "0" |
| 22 | + yield |
| 23 | + os.environ.pop("RAY_ENABLE_UV_RUN_RUNTIME_ENV", None) |
| 24 | + |
| 25 | + |
| 26 | +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: |
| 27 | + """Dynamically generate test parameters for each tutorial file.""" |
| 28 | + if "file_and_dir" in metafunc.fixturenames: |
| 29 | + # Get tutorial files |
| 30 | + tutorials_dir = PROJECT_ROOT / "examples" / "tutorials" |
| 31 | + |
| 32 | + if not tutorials_dir.exists(): |
| 33 | + pytest.skip(f"Tutorials directory not found: {tutorials_dir}") |
| 34 | + |
| 35 | + tutorial_files = [] |
| 36 | + for py_file in tutorials_dir.rglob("*.py"): |
| 37 | + working_dir = py_file.parent |
| 38 | + tutorial_files.append((py_file, working_dir)) |
| 39 | + |
| 40 | + if not tutorial_files: |
| 41 | + pytest.skip("No Python files found in examples/tutorials") |
| 42 | + |
| 43 | + # Create test IDs for better test output |
| 44 | + test_ids = [str(py_file.relative_to(PROJECT_ROOT)) for py_file, _ in tutorial_files] |
| 45 | + |
| 46 | + metafunc.parametrize("file_and_dir", tutorial_files, ids=test_ids) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.smoke |
| 50 | +def test_tutorial_file_runs(file_and_dir: Tuple[Path, Path]) -> None: |
| 51 | + """Test that a tutorial file runs without errors.""" |
| 52 | + py_file, working_dir = file_and_dir |
| 53 | + |
| 54 | + try: |
| 55 | + process = subprocess.Popen( # noqa: S603 |
| 56 | + [sys.executable, py_file.name], |
| 57 | + cwd=working_dir, |
| 58 | + stdout=subprocess.PIPE, |
| 59 | + stderr=subprocess.PIPE, |
| 60 | + text=True, |
| 61 | + ) |
| 62 | + try: |
| 63 | + stdout, stderr = process.communicate(timeout=SMOKE_TEST_TIMEOUT) |
| 64 | + except subprocess.TimeoutExpired: |
| 65 | + process.kill() |
| 66 | + stdout, stderr = process.communicate() |
| 67 | + pytest.skip( |
| 68 | + f"{py_file.relative_to(PROJECT_ROOT)} timed out after {SMOKE_TEST_TIMEOUT} seconds" |
| 69 | + ) |
| 70 | + |
| 71 | + if process.returncode != 0: |
| 72 | + error_msg = ( |
| 73 | + f"Tutorial file {py_file.relative_to(PROJECT_ROOT)} " |
| 74 | + f"failed to run successfully.\n" |
| 75 | + f"Return code: {process.returncode}\n" |
| 76 | + f"STDOUT:\n{stdout}\n" |
| 77 | + f"STDERR:\n{stderr}" |
| 78 | + ) |
| 79 | + pytest.fail(error_msg) |
| 80 | + except Exception as e: |
| 81 | + pytest.fail(f"Error running tutorial file {py_file.relative_to(PROJECT_ROOT)}: {e}") |
0 commit comments