Skip to content

Commit 8287831

Browse files
authored
Reland "[llvm-lit] Process ANSI color codes in test output when forma… (#108107)
…tting" (#108104)" This recommits 0f56ba1 (reverted by 6007ad7). In the original patch llvm/utils/lit/tests/escape-color.py failed on Windows because it diffed llvm-lit output with a file containing '\n' newlines rather than '\r\n'. This issue is avoided by calling 'diff --strip-trailing-cr'. Original description below: Test output that carried color across newlines previously resulted in the formatting around the output also being colored. Detect the current ANSI color and reset it when printing formatting, and then reapply it. As an added bonus an unterminated color code is also detected, preventing it from leaking out into the rest of the terminal. Fixes #106633
1 parent ccc7a07 commit 8287831

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

llvm/utils/lit/lit/TestRunner.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,20 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
10171017
return exitCode
10181018

10191019

1020+
def findColor(line, curr_color):
1021+
start = line.rfind("\33[")
1022+
if start == -1:
1023+
return curr_color
1024+
end = line.find("m", start + 2)
1025+
if end == -1:
1026+
return curr_color
1027+
match = line[start : end + 1]
1028+
# "\33[0m" means "reset all formatting". Sometimes the 0 is skipped.
1029+
if match == "\33[m" or match == "\33[0m":
1030+
return None
1031+
return match
1032+
1033+
10201034
def formatOutput(title, data, limit=None):
10211035
if not data.strip():
10221036
return ""
@@ -1027,8 +1041,18 @@ def formatOutput(title, data, limit=None):
10271041
msg = ""
10281042
ndashes = 30
10291043
# fmt: off
1030-
out = f"# .---{title}{'-' * (ndashes - 4 - len(title))}\n"
1031-
out += f"# | " + "\n# | ".join(data.splitlines()) + "\n"
1044+
out = f"# .---{title}{'-' * (ndashes - 4 - len(title))}\n"
1045+
curr_color = None
1046+
for line in data.splitlines():
1047+
if curr_color:
1048+
out += "\33[0m"
1049+
out += "# | "
1050+
if curr_color:
1051+
out += curr_color
1052+
out += line + "\n"
1053+
curr_color = findColor(line, curr_color)
1054+
if curr_color:
1055+
out += "\33[0m" # prevent unterminated formatting from leaking
10321056
out += f"# `---{msg}{'-' * (ndashes - 4 - len(msg))}\n"
10331057
# fmt: on
10341058
return out
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# .---command stdout------------
2+
# | # RUN: cat %s
3+
# | red
4+
# | still red(B
5+
# | plain
6+
# | green
7+
# | still green (never terminated)
8+
# `-----------------------------
9+
10+
--
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# RUN: cat %s
2+
red
3+
still red(B
4+
plain
5+
green
6+
still green (never terminated)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import lit.formats
2+
3+
config.name = "escape-color"
4+
config.suffixes = [".txt"]
5+
config.test_format = lit.formats.ShTest()
6+
config.test_source_root = None
7+
config.test_exec_root = None
8+

llvm/utils/lit/tests/escape-color.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# cut off the first 9 lines to avoid absolute file paths in the output
2+
# then keep only the next 10 lines to avoid test timing in the output
3+
# RUN: %{lit} %{inputs}/escape-color/color.txt -a | tail -n +10 | head -n 10 > %t
4+
# RUN: diff --strip-trailing-cr %{inputs}/escape-color/color-escaped.txt %t

0 commit comments

Comments
 (0)