Skip to content

Commit fd3ca29

Browse files
authored
[lit] Support GoogleTest test discovery through prefixes, too (#137423)
1 parent 826f237 commit fd3ca29

File tree

5 files changed

+134
-3
lines changed

5 files changed

+134
-3
lines changed

llvm/utils/lit/lit/formats/googletest.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
class GoogleTest(TestFormat):
18-
def __init__(self, test_sub_dirs, test_suffix, run_under=[]):
18+
def __init__(self, test_sub_dirs, test_suffix, run_under=[], test_prefix=None):
1919
self.seen_executables = set()
2020
self.test_sub_dirs = str(test_sub_dirs).split(";")
2121

@@ -26,6 +26,7 @@ def __init__(self, test_sub_dirs, test_suffix, run_under=[]):
2626

2727
# Also check for .py files for testing purposes.
2828
self.test_suffixes = {exe_suffix, test_suffix + ".py"}
29+
self.test_prefixes = {test_prefix} if test_prefix else None
2930
self.run_under = run_under
3031

3132
def get_num_tests(self, path, litConfig, localConfig):
@@ -55,7 +56,9 @@ def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig):
5556
dir_path = os.path.join(source_path, subdir)
5657
if not os.path.isdir(dir_path):
5758
continue
58-
for fn in lit.util.listdir_files(dir_path, suffixes=self.test_suffixes):
59+
for fn in lit.util.listdir_files(
60+
dir_path, suffixes=self.test_suffixes, prefixes=self.test_prefixes
61+
):
5962
# Discover the tests in this executable.
6063
execpath = os.path.join(source_path, subdir, fn)
6164
if execpath in self.seen_executables:

llvm/utils/lit/lit/util.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def mkdir_p(path):
182182
mkdir(path)
183183

184184

185-
def listdir_files(dirname, suffixes=None, exclude_filenames=None):
185+
def listdir_files(dirname, suffixes=None, exclude_filenames=None, prefixes=None):
186186
"""Yields files in a directory.
187187
188188
Filenames that are not excluded by rules below are yielded one at a time, as
@@ -214,12 +214,15 @@ def listdir_files(dirname, suffixes=None, exclude_filenames=None):
214214
exclude_filenames = set()
215215
if suffixes is None:
216216
suffixes = {""}
217+
if prefixes is None:
218+
prefixes = {""}
217219
for filename in os.listdir(dirname):
218220
if (
219221
os.path.isdir(os.path.join(dirname, filename))
220222
or filename.startswith(".")
221223
or filename in exclude_filenames
222224
or not any(filename.endswith(sfx) for sfx in suffixes)
225+
or not any(filename.startswith(pfx) for pfx in prefixes)
223226
):
224227
continue
225228
yield filename
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import sys
5+
6+
if len(sys.argv) == 3 and sys.argv[1] == "--gtest_list_tests":
7+
if sys.argv[2] != "--gtest_filter=-*DISABLED_*":
8+
raise ValueError("unexpected argument: %s" % (sys.argv[2]))
9+
print(
10+
"""\
11+
FirstTest.
12+
subTestA
13+
subTestB
14+
subTestC
15+
subTestD
16+
ParameterizedTest/0.
17+
subTest
18+
ParameterizedTest/1.
19+
subTest"""
20+
)
21+
sys.exit(0)
22+
elif len(sys.argv) != 1:
23+
# sharding and json output are specified using environment variables
24+
raise ValueError("unexpected argument: %r" % (" ".join(sys.argv[1:])))
25+
26+
for e in ["GTEST_TOTAL_SHARDS", "GTEST_SHARD_INDEX", "GTEST_OUTPUT"]:
27+
if e not in os.environ:
28+
raise ValueError("missing environment variables: " + e)
29+
30+
if not os.environ["GTEST_OUTPUT"].startswith("json:"):
31+
raise ValueError("must emit json output: " + os.environ["GTEST_OUTPUT"])
32+
33+
output = """\
34+
{
35+
"random_seed": 123,
36+
"testsuites": [
37+
{
38+
"name": "FirstTest",
39+
"testsuite": [
40+
{
41+
"name": "subTestA",
42+
"result": "COMPLETED",
43+
"time": "0.001s"
44+
},
45+
{
46+
"name": "subTestB",
47+
"result": "COMPLETED",
48+
"time": "0.001s",
49+
"failures": [
50+
{
51+
"failure": "I am subTest B, I FAIL\\nAnd I have two lines of output",
52+
"type": ""
53+
}
54+
]
55+
},
56+
{
57+
"name": "subTestC",
58+
"result": "SKIPPED",
59+
"time": "0.001s"
60+
},
61+
{
62+
"name": "subTestD",
63+
"result": "UNRESOLVED",
64+
"time": "0.001s"
65+
}
66+
]
67+
},
68+
{
69+
"name": "ParameterizedTest/0",
70+
"testsuite": [
71+
{
72+
"name": "subTest",
73+
"result": "COMPLETED",
74+
"time": "0.001s"
75+
}
76+
]
77+
},
78+
{
79+
"name": "ParameterizedTest/1",
80+
"testsuite": [
81+
{
82+
"name": "subTest",
83+
"result": "COMPLETED",
84+
"time": "0.001s"
85+
}
86+
]
87+
}
88+
]
89+
}"""
90+
91+
dummy_output = """\
92+
{
93+
"testsuites": [
94+
]
95+
}"""
96+
97+
json_filename = os.environ["GTEST_OUTPUT"].split(":", 1)[1]
98+
with open(json_filename, "w", encoding="utf-8") as f:
99+
if os.environ["GTEST_TOTAL_SHARDS"] == "1":
100+
print("[ RUN ] FirstTest.subTestB", flush=True)
101+
print("I am subTest B output", file=sys.stderr, flush=True)
102+
print("[ FAILED ] FirstTest.subTestB (8 ms)", flush=True)
103+
104+
f.write(output)
105+
exit_code = 1
106+
else:
107+
f.write(dummy_output)
108+
exit_code = 0
109+
110+
sys.exit(exit_code)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import lit.formats
2+
3+
config.name = "googletest-format"
4+
config.test_format = lit.formats.GoogleTest("DummySubDir", test_suffix="", test_prefix="test_")
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# RUN: env GTEST_TOTAL_SHARDS=1 GTEST_SHARD_INDEX=0 \
2+
# RUN: %{lit} -v --order=random --no-gtest-sharding %{inputs}/googletest-prefix --show-tests > %t.out
3+
# FIXME: Temporarily dump test output so we can debug failing tests on
4+
# buildbots.
5+
# RUN: cat %t.out
6+
# RUN: FileCheck < %t.out %s
7+
#
8+
# END.
9+
10+
# CHECK: -- Available Tests --
11+
# CHECK-NEXT: googletest-format :: DummySubDir/test_one.py

0 commit comments

Comments
 (0)