Skip to content

Commit 83dd7d9

Browse files
[CI] Add Support for Parsing Ninja Logs to generate_test_report_lib
This patch adds in support for taking the CLI output from ninja and parsing it for failures. This is intended to be used in the cases where all tests pass (or none have run), but the build fails to easily surface where exactly the build failed. The actual integration will happen in a future patch. Reviewers: gburgessiv, dschuff, lnihlen, DavidSpickett, Keenuts, cmtice Reviewed By: DavidSpickett, cmtice Pull Request: #152620
1 parent 45b1594 commit 83dd7d9

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

.ci/generate_test_report_lib.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,65 @@
1212
"https://github.com/llvm/llvm-project/issues and add the "
1313
"`infrastructure` label."
1414
)
15+
# The maximum number of lines to pull from a ninja failure.
16+
NINJA_LOG_SIZE_THRESHOLD = 500
17+
18+
19+
def _parse_ninja_log(ninja_log: list[str]) -> list[tuple[str, str]]:
20+
"""Parses an individual ninja log."""
21+
failures = []
22+
index = 0
23+
while index < len(ninja_log):
24+
while index < len(ninja_log) and not ninja_log[index].startswith("FAILED:"):
25+
index += 1
26+
if index == len(ninja_log):
27+
# We hit the end of the log without finding a build failure, go to
28+
# the next log.
29+
return failures
30+
# We are trying to parse cases like the following:
31+
#
32+
# [4/5] test/4.stamp
33+
# FAILED: touch test/4.stamp
34+
# touch test/4.stamp
35+
#
36+
# index will point to the line that starts with Failed:. The progress
37+
# indicator is the line before this ([4/5] test/4.stamp) and contains a pretty
38+
# printed version of the target being built (test/4.stamp). We use this line
39+
# and remove the progress information to get a succinct name for the target.
40+
failing_action = ninja_log[index - 1].split("] ")[1]
41+
failure_log = []
42+
while (
43+
index < len(ninja_log)
44+
and not ninja_log[index].startswith("[")
45+
and not ninja_log[index].startswith("ninja: build stopped:")
46+
and len(failure_log) < NINJA_LOG_SIZE_THRESHOLD
47+
):
48+
failure_log.append(ninja_log[index])
49+
index += 1
50+
failures.append((failing_action, "\n".join(failure_log)))
51+
return failures
52+
53+
54+
def find_failure_in_ninja_logs(ninja_logs: list[list[str]]) -> list[tuple[str, str]]:
55+
"""Extracts failure messages from ninja output.
56+
57+
This function takes stdout/stderr from ninja in the form of a list of files
58+
represented as a list of lines. This function then returns tuples containing
59+
the name of the target and the error message.
60+
61+
Args:
62+
ninja_logs: A list of files in the form of a list of lines representing the log
63+
files captured from ninja.
64+
65+
Returns:
66+
A list of tuples. The first string is the name of the target that failed. The
67+
second string is the error message.
68+
"""
69+
failures = []
70+
for ninja_log in ninja_logs:
71+
log_failures = _parse_ninja_log(ninja_log)
72+
failures.extend(log_failures)
73+
return failures
1574

1675

1776
# Set size_limit to limit the byte size of the report. The default is 1MB as this

.ci/generate_test_report_lib_test.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,111 @@ def junit_from_xml(xml):
1919

2020

2121
class TestReports(unittest.TestCase):
22+
def test_find_failure_ninja_logs(self):
23+
failures = generate_test_report_lib.find_failure_in_ninja_logs(
24+
[
25+
[
26+
"[1/5] test/1.stamp",
27+
"[2/5] test/2.stamp",
28+
"[3/5] test/3.stamp",
29+
"[4/5] test/4.stamp",
30+
"FAILED: touch test/4.stamp",
31+
"Wow! This system is really broken!",
32+
"[5/5] test/5.stamp",
33+
],
34+
]
35+
)
36+
self.assertEqual(len(failures), 1)
37+
self.assertEqual(
38+
failures[0],
39+
(
40+
"test/4.stamp",
41+
dedent(
42+
"""\
43+
FAILED: touch test/4.stamp
44+
Wow! This system is really broken!"""
45+
),
46+
),
47+
)
48+
49+
def test_no_failure_ninja_log(self):
50+
failures = generate_test_report_lib.find_failure_in_ninja_logs(
51+
[
52+
[
53+
"[1/3] test/1.stamp",
54+
"[2/3] test/2.stamp",
55+
"[3/3] test/3.stamp",
56+
]
57+
]
58+
)
59+
self.assertEqual(failures, [])
60+
61+
def test_ninja_log_end(self):
62+
failures = generate_test_report_lib.find_failure_in_ninja_logs(
63+
[
64+
[
65+
"[1/3] test/1.stamp",
66+
"[2/3] test/2.stamp",
67+
"[3/3] test/3.stamp",
68+
"FAILED: touch test/3.stamp",
69+
"Wow! This system is really broken!",
70+
"ninja: build stopped: subcommand failed.",
71+
]
72+
]
73+
)
74+
self.assertEqual(len(failures), 1)
75+
self.assertEqual(
76+
failures[0],
77+
(
78+
"test/3.stamp",
79+
dedent(
80+
"""\
81+
FAILED: touch test/3.stamp
82+
Wow! This system is really broken!"""
83+
),
84+
),
85+
)
86+
87+
def test_ninja_log_multiple_failures(self):
88+
failures = generate_test_report_lib.find_failure_in_ninja_logs(
89+
[
90+
[
91+
"[1/5] test/1.stamp",
92+
"[2/5] test/2.stamp",
93+
"FAILED: touch test/2.stamp",
94+
"Wow! This system is really broken!",
95+
"[3/5] test/3.stamp",
96+
"[4/5] test/4.stamp",
97+
"FAILED: touch test/4.stamp",
98+
"Wow! This system is maybe broken!",
99+
"[5/5] test/5.stamp",
100+
]
101+
]
102+
)
103+
self.assertEqual(len(failures), 2)
104+
self.assertEqual(
105+
failures[0],
106+
(
107+
"test/2.stamp",
108+
dedent(
109+
"""\
110+
FAILED: touch test/2.stamp
111+
Wow! This system is really broken!"""
112+
),
113+
),
114+
)
115+
self.assertEqual(
116+
failures[1],
117+
(
118+
"test/4.stamp",
119+
dedent(
120+
"""\
121+
FAILED: touch test/4.stamp
122+
Wow! This system is maybe broken!"""
123+
),
124+
),
125+
)
126+
22127
def test_title_only(self):
23128
self.assertEqual(
24129
generate_test_report_lib.generate_report("Foo", 0, []),

0 commit comments

Comments
 (0)