|
| 1 | +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 2 | +# See https://llvm.org/LICENSE.txt for license information. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | +""" |
| 5 | +Pytest wrapper that exposes each GoogleTest case inside ESIRuntimeCppTests as |
| 6 | +an individual pytest item. |
| 7 | +
|
| 8 | +The binary is located by (in order): |
| 9 | + 1. The ``ESI_RUNTIME_TESTS_BIN`` environment variable (explicit override). |
| 10 | + 2. ``tests/cpp/ESIRuntimeCppTests`` relative to the ESI runtime root, which |
| 11 | + is derived from the ``esiaccel`` package location — the same convention |
| 12 | + used by the integration tests (three ``parent`` levels up from |
| 13 | + ``esiaccel.__file__``). |
| 14 | +
|
| 15 | +The entire module is skipped when the binary cannot be found. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import os |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +from tests.conftest import get_runtime_root |
| 28 | + |
| 29 | +# --------------------------------------------------------------------------- |
| 30 | +# Locate the test binary |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | + |
| 33 | +_BIN_NAME = "ESIRuntimeCppTests" + (".exe" if sys.platform == "win32" else "") |
| 34 | + |
| 35 | + |
| 36 | +def _find_binary() -> Path | None: |
| 37 | + # 1. Explicit override. |
| 38 | + env = os.environ.get("ESI_RUNTIME_TESTS_BIN") |
| 39 | + if env: |
| 40 | + p = Path(env) |
| 41 | + if p.is_file(): |
| 42 | + return p |
| 43 | + |
| 44 | + # 2. Relative to the ESI runtime root derived from the esiaccel package — |
| 45 | + # the same convention used by the integration tests. Works when the |
| 46 | + # standalone runtime build's esiaccel is the active one on PYTHONPATH. |
| 47 | + try: |
| 48 | + candidate = get_runtime_root() / "tests" / "cpp" / _BIN_NAME |
| 49 | + if candidate.is_file(): |
| 50 | + return candidate |
| 51 | + except ImportError: |
| 52 | + pass |
| 53 | + |
| 54 | + return None |
| 55 | + |
| 56 | + |
| 57 | +_BINARY = _find_binary() |
| 58 | + |
| 59 | +if _BINARY is None: |
| 60 | + pytest.skip( |
| 61 | + "ESIRuntimeCppTests binary not found – make sure you've built that " |
| 62 | + "target and/or set ESI_RUNTIME_TESTS_BIN", |
| 63 | + allow_module_level=True, |
| 64 | + ) |
| 65 | + |
| 66 | +# --------------------------------------------------------------------------- |
| 67 | +# Enumerate gtest cases |
| 68 | +# --------------------------------------------------------------------------- |
| 69 | + |
| 70 | + |
| 71 | +def _list_tests() -> list[str]: |
| 72 | + """Return a list of 'Suite.TestName' strings from --gtest_list_tests.""" |
| 73 | + result = subprocess.run( |
| 74 | + [str(_BINARY), "--gtest_list_tests"], |
| 75 | + capture_output=True, |
| 76 | + text=True, |
| 77 | + ) |
| 78 | + if result.returncode != 0: |
| 79 | + pytest.fail( |
| 80 | + f"Failed to list gtest cases (rc={result.returncode}):\n" |
| 81 | + f"--- stdout ---\n{result.stdout}\n" |
| 82 | + f"--- stderr ---\n{result.stderr}", |
| 83 | + pytrace=False, |
| 84 | + ) |
| 85 | + tests: list[str] = [] |
| 86 | + suite = "" |
| 87 | + for line in result.stdout.splitlines(): |
| 88 | + # Suite header line ends with '.', e.g. "ESITypesTest." |
| 89 | + # Use a non-indent check so parameterized suites like "Suite/0." also match. |
| 90 | + if not line.startswith(" ") and line.rstrip().endswith("."): |
| 91 | + suite = line.strip() |
| 92 | + # Individual test line is indented, e.g. " VoidTypeSerialization" |
| 93 | + elif line.startswith(" "): |
| 94 | + test_name = line.strip().split()[0] # strip trailing comments |
| 95 | + if suite and test_name: |
| 96 | + tests.append(f"{suite}{test_name}") |
| 97 | + return tests |
| 98 | + |
| 99 | + |
| 100 | +_ALL_TESTS = _list_tests() |
| 101 | + |
| 102 | +# --------------------------------------------------------------------------- |
| 103 | +# Parametrize |
| 104 | +# --------------------------------------------------------------------------- |
| 105 | + |
| 106 | + |
| 107 | +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: |
| 108 | + if "gtest_case" in metafunc.fixturenames: |
| 109 | + metafunc.parametrize("gtest_case", _ALL_TESTS, ids=_ALL_TESTS) |
| 110 | + |
| 111 | + |
| 112 | +def test_cpp_runtime(gtest_case: str) -> None: |
| 113 | + result = subprocess.run( |
| 114 | + [str(_BINARY), f"--gtest_filter={gtest_case}"], |
| 115 | + capture_output=True, |
| 116 | + text=True, |
| 117 | + ) |
| 118 | + if result.returncode != 0: |
| 119 | + pytest.fail( |
| 120 | + f"gtest case '{gtest_case}' failed:\n" |
| 121 | + f"--- stdout ---\n{result.stdout}\n" |
| 122 | + f"--- stderr ---\n{result.stderr}", |
| 123 | + pytrace=False, |
| 124 | + ) |
0 commit comments