Skip to content
24 changes: 22 additions & 2 deletions backends/test/suite/generate_markdown_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@
#


def escape_for_markdown(text: str) -> str:
"""
Modify a string to properly display in a markdown table cell.
"""
if not text:
return text

# Replace newlines with <br /> tags
escaped = text.replace("\n", "<br />")

# Escape backslashes.
escaped = escaped.replace("\\", "\\\\")

# Escape pipe characters that would break table structure
escaped = escaped.replace("|", "\\|")

return escaped


def generate_markdown(csv_path: str, exit_code: int = 0): # noqa (C901)
# Print warning if exit code is non-zero
if exit_code != 0:
Expand Down Expand Up @@ -46,7 +65,7 @@ def generate_markdown(csv_path: str, exit_code: int = 0): # noqa (C901)

for row in data_rows:
# Make a copy of the row to avoid modifying the original
processed_row = row.copy()
processed_row = [escape_for_markdown(cell) for cell in row]

# Count results and collect failed tests
if result_column_index is not None and result_column_index < len(row):
Expand Down Expand Up @@ -96,7 +115,8 @@ def generate_markdown(csv_path: str, exit_code: int = 0): # noqa (C901)
# Generate Failed Tests section
print("# Failed Tests\n")
if failed_tests:
print("| " + " | ".join(header) + " |")
escaped_header = [escape_for_markdown(col) for col in header]
print("| " + " | ".join(escaped_header) + " |")
print("|" + "|".join(["---"] * len(header)) + "|")
for row in failed_tests:
print("| " + " | ".join(row) + " |")
Expand Down
12 changes: 12 additions & 0 deletions backends/test/suite/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
]
)

CSV_FIELD_NAMES.append("Error")


# Operators that are excluded from the counts returned by count_ops. These are used to
# exclude operatations that are not logically relevant or delegatable to backends.
Expand Down Expand Up @@ -365,6 +367,15 @@ def write_csv_header(output: TextIO):
def write_csv_row(record: TestCaseSummary, output: TextIO):
writer = csv.DictWriter(output, CSV_FIELD_NAMES)

# Truncate error message if it's too long, keeping first and last 200 characters
error_message = ""
if record.error is not None:
error_str = str(record.error)
if len(error_str) > 400:
error_message = error_str[:200] + "..." + error_str[-200:]
else:
error_message = error_str

row = {
"Test ID": record.name,
"Test Case": record.base_name,
Expand All @@ -373,6 +384,7 @@ def write_csv_row(record: TestCaseSummary, output: TextIO):
"Params": _serialize_params(record.params),
"Result": record.result.to_short_str(),
"Result Detail": record.result.to_detail_str(),
"Error": error_message,
"Delegated": "True" if record.is_delegated() else "False",
"Quantize Time (s)": (
f"{record.quantize_time.total_seconds():.3f}"
Expand Down
Loading