| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
"""Graphviz test coverage analysis script""" |
| 4 |
|
| 5 |
import argparse |
| 6 |
import logging |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
from pathlib import Path |
| 10 |
|
| 11 |
# logging output stream, setup in main() |
| 12 |
log = None |
| 13 |
|
| 14 |
|
| 15 |
def main(args: list[str]) -> int: |
| 16 |
"""entry point""" |
| 17 |
|
| 18 |
# setup logging to print to stderr |
| 19 |
global log |
| 20 |
ch = logging.StreamHandler() |
| 21 |
log = logging.getLogger("test_coverage.py") |
| 22 |
log.addHandler(ch) |
| 23 |
|
| 24 |
# parse command line arguments |
| 25 |
parser = argparse.ArgumentParser( |
| 26 |
description="Graphviz test coverage analysis script" |
| 27 |
) |
| 28 |
parser.add_argument( |
| 29 |
"--init", |
| 30 |
action="store_true", |
| 31 |
help="Capture initial zero coverage data before running any test", |
| 32 |
) |
| 33 |
parser.add_argument( |
| 34 |
"--analyze", |
| 35 |
action="store_true", |
| 36 |
help="Analyze test coverage after running tests", |
| 37 |
) |
| 38 |
options = parser.parse_args(args[1:]) |
| 39 |
|
| 40 |
if not options.init and not options.analyze: |
| 41 |
log.error("Must specify --init or --analyze; refusing to run") |
| 42 |
return -1 |
| 43 |
|
| 44 |
cwd = Path.cwd() |
| 45 |
|
| 46 |
generated_files = [ |
| 47 |
cwd / "build/cmd/tools/gmlparse.c", |
| 48 |
cwd / "build/cmd/tools/gmlscan.c", |
| 49 |
cwd / "build/lib/cgraph/grammar.c", |
| 50 |
cwd / "build/lib/cgraph/scan.c", |
| 51 |
cwd / "build/lib/common/htmlparse.c", |
| 52 |
cwd / "build/lib/expr/exparse.c", |
| 53 |
cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/moc_csettings.cpp", |
| 54 |
cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/moc_imageviewer.cpp", |
| 55 |
cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/moc_mainwindow.cpp", |
| 56 |
cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/moc_mdichild.cpp", |
| 57 |
cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/qrc_mdi.cpp", |
| 58 |
] |
| 59 |
|
| 60 |
# files that are generated but only need to be excluded during init |
| 61 |
init_generated_files = [ |
| 62 |
cwd / "build/tclpkg/gv/CMakeFiles/gv_d.dir/gvD_wrap.cxx", |
| 63 |
cwd / "build/tclpkg/gv/CMakeFiles/gv_go.dir/gvGO_wrap.cxx", |
| 64 |
cwd / "build/tclpkg/gv/CMakeFiles/gv_sharp.dir/gvCSHARP_wrap.cxx", |
| 65 |
cwd / "build/tclpkg/gv/CMakeFiles/gv_tcl.dir/gvTCL_wrap.cxx", |
| 66 |
] |
| 67 |
|
| 68 |
excluded_files = generated_files |
| 69 |
init_excluded_files = init_generated_files |
| 70 |
|
| 71 |
exclude_options = [f"--exclude={f}" for f in excluded_files] |
| 72 |
init_exclude_options = [f"--exclude={f}" for f in init_excluded_files] |
| 73 |
|
| 74 |
if options.init: |
| 75 |
subprocess.check_call( |
| 76 |
[ |
| 77 |
"lcov", |
| 78 |
"--capture", |
| 79 |
"--initial", |
| 80 |
"--directory", |
| 81 |
".", |
| 82 |
"--branch-coverage", |
| 83 |
"--no-external", |
| 84 |
] |
| 85 |
+ exclude_options |
| 86 |
+ init_exclude_options |
| 87 |
+ ["--output-file", "app_base.info"] |
| 88 |
) |
| 89 |
|
| 90 |
return 0 |
| 91 |
|
| 92 |
if options.analyze: |
| 93 |
# capture test coverage data |
| 94 |
subprocess.check_call( |
| 95 |
[ |
| 96 |
"lcov", |
| 97 |
"--capture", |
| 98 |
"--directory", |
| 99 |
".", |
| 100 |
"--branch-coverage", |
| 101 |
"--no-external", |
| 102 |
] |
| 103 |
+ exclude_options |
| 104 |
+ ["--output-file", "app_test.info"] |
| 105 |
) |
| 106 |
# combine baseline and test coverage data |
| 107 |
subprocess.check_call( |
| 108 |
[ |
| 109 |
"lcov", |
| 110 |
"--branch-coverage", |
| 111 |
"--add-tracefile", |
| 112 |
"app_base.info", |
| 113 |
"-add-tracefile", |
| 114 |
"app_test.info", |
| 115 |
"--output-file", |
| 116 |
"app_total.info", |
| 117 |
] |
| 118 |
) |
| 119 |
# generate coverage html pages using lcov which are nicer than gcovr's |
| 120 |
Path("coverage/lcov").mkdir(parents=True, exist_ok=True) |
| 121 |
subprocess.check_call( |
| 122 |
[ |
| 123 |
"genhtml", |
| 124 |
"--prefix", |
| 125 |
cwd, |
| 126 |
"--branch-coverage", |
| 127 |
"--output-directory", |
| 128 |
"coverage/lcov", |
| 129 |
"--show-details", |
| 130 |
"app_total.info", |
| 131 |
] |
| 132 |
) |
| 133 |
# generate coverage info for GitLab's Test Coverage Visualization |
| 134 |
Path("coverage/gcovr").mkdir(parents=True, exist_ok=True) |
| 135 |
subprocess.check_call( |
| 136 |
["gcovr"] |
| 137 |
+ exclude_options |
| 138 |
+ [f"--gcov-exclude={f}" for f in generated_files] |
| 139 |
+ [ |
| 140 |
"--xml-pretty", |
| 141 |
"--html-details=coverage/gcovr/index.html", |
| 142 |
"--exclude-unreachable-branches", |
| 143 |
"--gcov-ignore-errors=no_working_dir_found", |
| 144 |
"--print-summary", |
| 145 |
"--output", |
| 146 |
"coverage.xml", |
| 147 |
"--root", |
| 148 |
cwd, |
| 149 |
] |
| 150 |
) |
| 151 |
|
| 152 |
return 0 |
| 153 |
|
| 154 |
|
| 155 |
if __name__ == "__main__": |
| 156 |
sys.exit(main(sys.argv)) |