|
| 1 | +#!/bin/python3 |
| 2 | +# prints a summary of the tests to both the console and the job summary: |
| 3 | +# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary |
| 4 | +# |
| 5 | +# To test the output of the GH test summary: |
| 6 | +# python3 run-qemu/print_test_summary.py -j run-qemu/fixtures/test_progs.json -s /dev/stderr > /dev/null |
| 7 | +# To test the output of the console: |
| 8 | +# python3 run-qemu/print_test_summary.py -j run-qemu/fixtures/test_progs.json -s /dev/stderr 2> /dev/null |
| 9 | + |
| 10 | +import argparse |
| 11 | +import json |
| 12 | + |
| 13 | + |
| 14 | +def parse_args(): |
| 15 | + parser = argparse.ArgumentParser() |
| 16 | + parser.add_argument( |
| 17 | + "-j", |
| 18 | + "--json-summary", |
| 19 | + required=True, |
| 20 | + metavar="FILE", |
| 21 | + help="test_progs's json summary file", |
| 22 | + ) |
| 23 | + parser.add_argument( |
| 24 | + "-s", |
| 25 | + "--step-summary", |
| 26 | + required=True, |
| 27 | + metavar="FILE", |
| 28 | + help="Github step summary file", |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "-a", "--append", action="store_true", help="Append to github step summary file" |
| 32 | + ) |
| 33 | + return parser.parse_args() |
| 34 | + |
| 35 | + |
| 36 | +def notice(text: str) -> str: |
| 37 | + return f"::notice::{text}" |
| 38 | + |
| 39 | + |
| 40 | +def error(text: str) -> str: |
| 41 | + return f"::error::{text}" |
| 42 | + |
| 43 | + |
| 44 | +def markdown_summary(json_summary: json): |
| 45 | + return f"""- :heavy_check_mark: Success: {json_summary['success']}/{json_summary['success_subtest']} |
| 46 | +- :next_track_button: Skipped: ${json_summary['skipped']} |
| 47 | +- :x: Failed: {json_summary['failed']}""" |
| 48 | + |
| 49 | + |
| 50 | +def console_summary(json_summary: json): |
| 51 | + return f"Success: {json_summary['success']}/{json_summary['success_subtest']}, Skipped: {json_summary['skipped']}, Failed: {json_summary['failed']}" |
| 52 | + |
| 53 | + |
| 54 | +def log_gh_summary(file, text: str): |
| 55 | + print(text, file=file) |
| 56 | + |
| 57 | + |
| 58 | +def log_console(text: str): |
| 59 | + print(text) |
| 60 | + |
| 61 | + |
| 62 | +def group(text: str, title: str = "", error: bool = False) -> str: |
| 63 | + if error and title: |
| 64 | + title = f"\033[1;31mError:\033[0m {title}" |
| 65 | + return f"""::group::{title} |
| 66 | +{text} |
| 67 | +::endgroup::""" |
| 68 | + |
| 69 | + |
| 70 | +def test_error_console_log(test_error: str, test_message: str) -> str: |
| 71 | + error_msg = error(test_error) |
| 72 | + if test_message: |
| 73 | + error_msg += "\n" + test_message.strip() |
| 74 | + return group(error_msg, title=test_error, error=True) |
| 75 | + else: |
| 76 | + return error_msg |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + args = parse_args() |
| 81 | + step_open_mode = "a" if args.append else "w" |
| 82 | + json_summary = None |
| 83 | + |
| 84 | + with open(args.json_summary, "r") as f: |
| 85 | + json_summary = json.load(f) |
| 86 | + |
| 87 | + with open(args.step_summary, step_open_mode) as f: |
| 88 | + log_gh_summary(f, "# Tests summary") |
| 89 | + log_gh_summary(f, markdown_summary(json_summary)) |
| 90 | + |
| 91 | + log_console(notice(console_summary(json_summary))) |
| 92 | + |
| 93 | + for test in json_summary["results"]: |
| 94 | + test_name = test["name"] |
| 95 | + test_number = test["number"] |
| 96 | + if test["failed"]: |
| 97 | + test_log = f"#{test_number} {test_name}" |
| 98 | + log_gh_summary(f, test_log) |
| 99 | + log_console(test_error_console_log(test_log, test["message"])) |
| 100 | + |
| 101 | + for subtest in test["subtests"]: |
| 102 | + if subtest["failed"]: |
| 103 | + subtest_log = f"#{test_number}/{subtest['number']} {test_name}/{subtest['name']}" |
| 104 | + log_gh_summary(f, subtest_log) |
| 105 | + log_console(test_error_console_log(subtest_log, subtest["message"])) |
0 commit comments