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