1# SPDX-License-Identifier: GPL-2.0 2 3import builtins 4from .consts import KSFT_MAIN_NAME 5 6KSFT_RESULT = None 7 8 9class KsftSkipEx(Exception): 10 pass 11 12 13class KsftXfailEx(Exception): 14 pass 15 16 17def ksft_pr(*objs, **kwargs): 18 print("#", *objs, **kwargs) 19 20 21def ksft_eq(a, b, comment=""): 22 global KSFT_RESULT 23 if a != b: 24 KSFT_RESULT = False 25 ksft_pr("Check failed", a, "!=", b, comment) 26 27 28def ksft_true(a, comment=""): 29 global KSFT_RESULT 30 if not a: 31 KSFT_RESULT = False 32 ksft_pr("Check failed", a, "does not eval to True", comment) 33 34 35def ksft_in(a, b, comment=""): 36 global KSFT_RESULT 37 if a not in b: 38 KSFT_RESULT = False 39 ksft_pr("Check failed", a, "not in", b, comment) 40 41 42def ksft_ge(a, b, comment=""): 43 global KSFT_RESULT 44 if a < b: 45 KSFT_RESULT = False 46 ksft_pr("Check failed", a, "<", b, comment) 47 48 49def ktap_result(ok, cnt=1, case="", comment=""): 50 res = "" 51 if not ok: 52 res += "not " 53 res += "ok " 54 res += str(cnt) + " " 55 res += KSFT_MAIN_NAME 56 if case: 57 res += "." + str(case.__name__) 58 if comment: 59 res += " # " + comment 60 print(res) 61 62 63def ksft_run(cases, args=()): 64 totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0} 65 66 print("KTAP version 1") 67 print("1.." + str(len(cases))) 68 69 global KSFT_RESULT 70 cnt = 0 71 for case in cases: 72 KSFT_RESULT = True 73 cnt += 1 74 try: 75 case(*args) 76 except KsftSkipEx as e: 77 ktap_result(True, cnt, case, comment="SKIP " + str(e)) 78 totals['skip'] += 1 79 continue 80 except KsftXfailEx as e: 81 ktap_result(True, cnt, case, comment="XFAIL " + str(e)) 82 totals['xfail'] += 1 83 continue 84 except Exception as e: 85 for line in str(e).split('\n'): 86 ksft_pr("Exception|", line) 87 ktap_result(False, cnt, case) 88 totals['fail'] += 1 89 continue 90 91 ktap_result(KSFT_RESULT, cnt, case) 92 totals['pass'] += 1 93 94 print( 95 f"# Totals: pass:{totals['pass']} fail:{totals['fail']} xfail:{totals['xfail']} xpass:0 skip:{totals['skip']} error:0" 96 ) 97