91 lines · python
1# SPDX-License-Identifier: GPL-2.02#3# Copyright (c) 2023 Collabora Ltd4#5# Kselftest helpers for outputting in KTAP format. Based on kselftest.h.6#7 8import sys9 10ksft_cnt = {"pass": 0, "fail": 0, "skip": 0}11ksft_num_tests = 012ksft_test_number = 113 14KSFT_PASS = 015KSFT_FAIL = 116KSFT_SKIP = 417 18 19def print_header():20 print("TAP version 13")21 22 23def set_plan(num_tests):24 global ksft_num_tests25 ksft_num_tests = num_tests26 print("1..{}".format(num_tests))27 28 29def print_cnts():30 print(31 f"# Totals: pass:{ksft_cnt['pass']} fail:{ksft_cnt['fail']} xfail:0 xpass:0 skip:{ksft_cnt['skip']} error:0"32 )33 34 35def print_msg(msg):36 print(f"# {msg}")37 38 39def _test_print(result, description, directive=None):40 if directive:41 directive_str = f"# {directive}"42 else:43 directive_str = ""44 45 global ksft_test_number46 print(f"{result} {ksft_test_number} {description} {directive_str}")47 ksft_test_number += 148 49 50def test_result_pass(description):51 _test_print("ok", description)52 ksft_cnt["pass"] += 153 54 55def test_result_fail(description):56 _test_print("not ok", description)57 ksft_cnt["fail"] += 158 59 60def test_result_skip(description):61 _test_print("ok", description, "SKIP")62 ksft_cnt["skip"] += 163 64 65def test_result(condition, description=""):66 if condition:67 test_result_pass(description)68 else:69 test_result_fail(description)70 71 72def finished():73 if ksft_cnt["pass"] + ksft_cnt["skip"] == ksft_num_tests:74 exit_code = KSFT_PASS75 else:76 exit_code = KSFT_FAIL77 78 print_cnts()79 80 sys.exit(exit_code)81 82 83def exit_fail():84 print_cnts()85 sys.exit(KSFT_FAIL)86 87 88def exit_pass():89 print_cnts()90 sys.exit(KSFT_PASS)91