File: test_coverage.py

package info (click to toggle)
graphviz 14.1.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,440 kB
  • sloc: ansic: 142,129; cpp: 11,960; python: 7,770; makefile: 4,043; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,388; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (156 lines) | stat: -rwxr-xr-x 4,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3

"""Graphviz test coverage analysis script"""

import argparse
import logging
import subprocess
import sys
from pathlib import Path

# logging output stream, setup in main()
log = None


def main(args: list[str]) -> int:
    """entry point"""

    # setup logging to print to stderr
    global log
    ch = logging.StreamHandler()
    log = logging.getLogger("test_coverage.py")
    log.addHandler(ch)

    # parse command line arguments
    parser = argparse.ArgumentParser(
        description="Graphviz test coverage analysis script"
    )
    parser.add_argument(
        "--init",
        action="store_true",
        help="Capture initial zero coverage data before running any test",
    )
    parser.add_argument(
        "--analyze",
        action="store_true",
        help="Analyze test coverage after running tests",
    )
    options = parser.parse_args(args[1:])

    if not options.init and not options.analyze:
        log.error("Must specify --init or --analyze; refusing to run")
        return -1

    cwd = Path.cwd()

    generated_files = [
        cwd / "build/cmd/tools/gmlparse.c",
        cwd / "build/cmd/tools/gmlscan.c",
        cwd / "build/lib/cgraph/grammar.c",
        cwd / "build/lib/cgraph/scan.c",
        cwd / "build/lib/common/htmlparse.c",
        cwd / "build/lib/expr/exparse.c",
        cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/moc_csettings.cpp",
        cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/moc_imageviewer.cpp",
        cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/moc_mainwindow.cpp",
        cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/moc_mdichild.cpp",
        cwd / "build/cmd/gvedit/gvedit_autogen/EWIEGA46WW/qrc_mdi.cpp",
    ]

    # files that are generated but only need to be excluded during init
    init_generated_files = [
        cwd / "build/tclpkg/gv/CMakeFiles/gv_d.dir/gvD_wrap.cxx",
        cwd / "build/tclpkg/gv/CMakeFiles/gv_go.dir/gvGO_wrap.cxx",
        cwd / "build/tclpkg/gv/CMakeFiles/gv_sharp.dir/gvCSHARP_wrap.cxx",
        cwd / "build/tclpkg/gv/CMakeFiles/gv_tcl.dir/gvTCL_wrap.cxx",
    ]

    excluded_files = generated_files
    init_excluded_files = init_generated_files

    exclude_options = [f"--exclude={f}" for f in excluded_files]
    init_exclude_options = [f"--exclude={f}" for f in init_excluded_files]

    if options.init:
        subprocess.check_call(
            [
                "lcov",
                "--capture",
                "--initial",
                "--directory",
                ".",
                "--branch-coverage",
                "--no-external",
            ]
            + exclude_options
            + init_exclude_options
            + ["--output-file", "app_base.info"]
        )

        return 0

    if options.analyze:
        # capture test coverage data
        subprocess.check_call(
            [
                "lcov",
                "--capture",
                "--directory",
                ".",
                "--branch-coverage",
                "--no-external",
            ]
            + exclude_options
            + ["--output-file", "app_test.info"]
        )
        # combine baseline and test coverage data
        subprocess.check_call(
            [
                "lcov",
                "--branch-coverage",
                "--add-tracefile",
                "app_base.info",
                "-add-tracefile",
                "app_test.info",
                "--output-file",
                "app_total.info",
            ]
        )
        # generate coverage html pages using lcov which are nicer than gcovr's
        Path("coverage/lcov").mkdir(parents=True, exist_ok=True)
        subprocess.check_call(
            [
                "genhtml",
                "--prefix",
                cwd,
                "--branch-coverage",
                "--output-directory",
                "coverage/lcov",
                "--show-details",
                "app_total.info",
            ]
        )
        # generate coverage info for GitLab's Test Coverage Visualization
        Path("coverage/gcovr").mkdir(parents=True, exist_ok=True)
        subprocess.check_call(
            ["gcovr"]
            + exclude_options
            + [f"--gcov-exclude={f}" for f in generated_files]
            + [
                "--xml-pretty",
                "--html-details=coverage/gcovr/index.html",
                "--exclude-unreachable-branches",
                "--gcov-ignore-errors=no_working_dir_found",
                "--print-summary",
                "--output",
                "coverage.xml",
                "--root",
                cwd,
            ]
        )

    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv))