CIRCT
23.0.0git
Loading...
Searching...
No Matches
lib
Dialect
ESI
runtime
tests
unit
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
"""
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 using the shared
12
resolver from ``tests.conftest``.
13
14
The entire module is skipped when the binary cannot be found.
15
"""
16
17
from
__future__
import
annotations
18
19
import
os
20
import
subprocess
21
import
sys
22
from
pathlib
import
Path
23
24
import
pytest
25
26
from
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
35
def
_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
60
if
_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
72
def
_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
108
def
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
113
def
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
)
test_cpp_runtime
Definition
test_cpp_runtime.py:1
test_cpp_runtime._find_binary
Path|None _find_binary()
Definition
test_cpp_runtime.py:35
test_cpp_runtime.pytest_generate_tests
None pytest_generate_tests(pytest.Metafunc metafunc)
Definition
test_cpp_runtime.py:108
test_cpp_runtime._list_tests
list[str] _list_tests()
Definition
test_cpp_runtime.py:72
tests.conftest
Definition
conftest.py:1
Generated on Tue Apr 21 2026 00:18:10 for CIRCT by
1.9.8