"""Coverage Report. Renders the HTML report of everything that is not covered and the summary table used in the documentation.""" #################################################################################################################################### import os TITLE = "pgBackRest Coverage Report" HTML_PRE = ( '\n' '\n' "\n" " " + TITLE + "\n" ' \n' ' \n" "\n" "\n" '
' + TITLE + "
\n" ) TOC_PRE = ( '
\n' ' \n' ' \n' ' \n' " \n" ) TOC_COVERED_PRE = ' \n \n \n" TOC_UNCOVERED_PRE = ' \n \n \n" TOC_POST = "
FILE
' TOC_COVERED_POST = "
\n ' TOC_UNCOVERED_POST = "\n
\n" RPT_PRE = '
' RPT_MID2 = ( "
\n" ' \n' ' \n' ' \n' ' \n' ' \n' " \n" ) RPT_LINE_PRE = ' \n \n \n' RPT_BRANCH_UNCOVERED_PRE = '\n \n" RPT_CODE = ' \n \n" RPT_SKIP = ' \n \n \n' RPT_POST = TOC_POST HTML_POST = "\n\n" #################################################################################################################################### def _context_render(no, text): """Render a context line, i.e. a covered line shown around one that is missing coverage.""" return RPT_LINE_PRE + str(no) + RPT_BRANCH_COVERED + RPT_CODE + text + RPT_LINE_POST #################################################################################################################################### def _context_trim(text): """Is this line one that may be trimmed from the context, i.e. it carries no information on its own?""" text = text.strip() return text == "" or text == "{" or text == "}" #################################################################################################################################### def _file_render(file, line_text_list): """Render the report for one file.""" result = RPT_PRE + file.name + RPT_MID1 + file.name + RPT_MID2 line_last = 0 for line_idx, line in enumerate(file.line_list): # Check branch coverage and build report string branch_covered = True branch_str = "[" if line.branch_list is not None: # A python branch is an arc to another line so name the line that was never jumped to, which says which path was # missed. A C branch has no such target and is shown as one mark per outcome. if line.target_list is not None: for branch, target in zip(line.branch_list, line.target_list): if branch != 0: continue if len(branch_str) > 1: branch_str += " " # A negative target is the end of the function the line is in, i.e. the branch that never returned. The value # is the line the function is defined on, which is of no use in the report. branch_str += "-> return" if target < 0 else "-> %d" % target branch_covered = False else: for branch in line.branch_list: if len(branch_str) > 1: branch_str += " " if branch == 0: branch_str += "-" branch_covered = False else: branch_str += "+" branch_str += "]" # Add before context context_begin = line.no - 5 if line.no > 5 else 1 context_end = line.no - 1 if line.no > 1 else 0 if line_last != 0 and context_begin < line_last + 1: context_begin = line_last + 1 if line_last != 0 and context_begin > line_last + 1: result += RPT_SKIP # Trim uninteresting lines from the top of the context for context_idx in range(context_begin, context_end + 1): if not _context_trim(line_text_list[context_idx - 1]): break context_begin = context_idx + 1 for context_idx in range(context_begin, context_end + 1): result += _context_render(context_idx, line_text_list[context_idx - 1]) # Output coverage result += RPT_LINE_PRE + str(line.no) if not branch_covered: result += RPT_BRANCH_UNCOVERED_PRE + branch_str + RPT_BRANCH_UNCOVERED_POST else: result += RPT_BRANCH_COVERED result += RPT_CODE_UNCOVERED if line.hit == 0 else RPT_CODE result += line_text_list[line.no - 1] + RPT_LINE_POST line_last = line.no # Add after context context_begin = line.no + 1 context_end = line.no + 5 if line.no <= len(line_text_list) - 5 else len(line_text_list) if line_idx < len(file.line_list) - 1: line_next = file.line_list[line_idx + 1] if context_end >= line_next.no: context_end = line_next.no - 1 # Trim uninteresting lines from the bottom of the context for context_idx in range(context_end, context_begin - 1, -1): if not _context_trim(line_text_list[context_idx - 1]): break context_end = context_idx - 1 for context_idx in range(context_begin, context_end + 1): result += _context_render(context_idx, line_text_list[context_idx - 1]) line_last = context_end return result + RPT_POST #################################################################################################################################### def report_render(coverage, source_read): """Render the coverage report. Files that are fully covered are listed in the table of contents but have no report section.""" result = HTML_PRE + TOC_PRE # Build table of contents for file in coverage.file_list: if not file.line_list: result += TOC_COVERED_PRE + file.name + TOC_COVERED_POST else: result += TOC_UNCOVERED_PRE + file.name + TOC_UNCOVERED_MID + file.name + TOC_UNCOVERED_POST result += TOC_POST # Build files that are missing coverage for file in coverage.file_list: if file.line_list: result += _file_render(file, source_read(file.name).split("\n")) return result + HTML_POST #################################################################################################################################### # Coverage summary for the documentation SUMMARY_ROW = ( "\n" " %s\n" " %s\n" " %s\n" " %s\n" "\n" ) #################################################################################################################################### def summary_value(hit, total): """Render hit/total and the percentage, matching cvtPctToZ() in src/common/type/convert.c.""" if total == 0: return "---" # A ratio of one is fixed at 100% so rounding cannot throw off the result if hit == total: percent = 10000 else: percent = (int(hit / total * 100000) + 5) // 10 percent = "%03u" % percent return "%u/%u (%s.%s%%)" % (hit, total, percent[:-2], percent[-2:]) #################################################################################################################################### def summary_render(coverage): """Render the summary table, aggregating the files of each code module into a single row.""" result = "" module_list = {} total = [0, 0, 0, 0, 0, 0] for file in coverage.file_list: # The summary is of the core code, which is what the documentation reports coverage for. Naming what belongs rather than # what does not keeps a library added elsewhere, e.g. the python under build/lib, from arriving here uninvited. if not file.name.startswith("src/"): continue name = os.path.dirname(file.name)[len("src/") :] module = module_list.setdefault(name, [0, 0, 0, 0, 0, 0]) value = (file.function_hit, file.function_total, file.branch_hit, file.branch_total, file.line_hit, file.line_total) for index, item in enumerate(value): module[index] += item total[index] += item for name in sorted(module_list): module = module_list[name] result += ( SUMMARY_ROW % ( name, summary_value(module[0], module[1]), summary_value(module[2], module[3]), summary_value(module[4], module[5]), ) + "\n" ) return result + SUMMARY_ROW % ( "TOTAL", summary_value(total[0], total[1]), summary_value(total[2], total[3]), summary_value(total[4], total[5]), )
LINEBRANCHCODE
' RPT_BRANCH_COVERED = '' RPT_BRANCH_UNCOVERED_POST = "' RPT_CODE_UNCOVERED = ' ' RPT_LINE_POST = "
...