• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

martineberlein / debugging-benchmark / 8171647283

06 Mar 2024 11:57AM UTC coverage: 72.671% (+2.0%) from 70.662%
8171647283

Pull #26

github

web-flow
Merge 08a98172c into f838bc853
Pull Request #26: Release 0.2.0

376 of 465 new or added lines in 22 files covered. (80.86%)

1 existing line in 1 file now uncovered.

1646 of 2265 relevant lines covered (72.67%)

0.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/debugging_framework/coverage.py
1
# from fuzzingbook.Coverage import Coverage, Location, BranchCoverage
2

3
from debugging_framework.input import Input
×
4

5

NEW
6
from typing import Any, Optional, Callable, List, Type, Set, Tuple
×
NEW
7
from types import FrameType, TracebackType
×
NEW
8
import inspect
×
NEW
9
import sys
×
10

11

NEW
12
Location = Tuple[str, int]
×
13

14

NEW
15
class Coverage:
×
16
    """Track coverage within a `with` block. Use as
17
    ```
18
    with Coverage() as cov:
19
        function_to_be_traced()
20
    c = cov.coverage()
21
    ```
22
    """
23

NEW
24
    def __init__(self) -> None:
×
25
        """Constructor"""
NEW
26
        self._trace: List[Location] = []
×
27

28
    # Trace function
NEW
29
    def traceit(self, frame: FrameType, event: str, arg: Any) -> Optional[Callable]:
×
30
        """Tracing function. To be overloaded in subclasses."""
NEW
31
        if self.original_trace_function is not None:
×
NEW
32
            self.original_trace_function(frame, event, arg)
×
33

NEW
34
        if event == "line":
×
NEW
35
            function_name = frame.f_code.co_name
×
NEW
36
            lineno = frame.f_lineno
×
NEW
37
            if function_name != '__exit__':  # avoid tracing ourselves:
×
NEW
38
                self._trace.append((function_name, lineno))
×
39

NEW
40
        return self.traceit
×
41

NEW
42
    def __enter__(self) -> Any:
×
43
        """Start of `with` block. Turn on tracing."""
NEW
44
        self.original_trace_function = sys.gettrace()
×
NEW
45
        sys.settrace(self.traceit)
×
NEW
46
        return self
×
47

NEW
48
    def __exit__(self, exc_type: Type, exc_value: BaseException,
×
49
                 tb: TracebackType) -> Optional[bool]:
50
        """End of `with` block. Turn off tracing."""
NEW
51
        sys.settrace(self.original_trace_function)
×
NEW
52
        return None  # default: pass all exceptions
×
53

NEW
54
    def trace(self) -> List[Location]:
×
55
        """The list of executed lines, as (function_name, line_number) pairs"""
NEW
56
        return self._trace
×
57

NEW
58
    def coverage(self) -> Set[Location]:
×
59
        """The set of executed lines, as (function_name, line_number) pairs"""
NEW
60
        return set(self.trace())
×
61

NEW
62
    def function_names(self) -> Set[str]:
×
63
        """The set of function names seen"""
NEW
64
        return set(function_name for (function_name, line_number) in self.coverage())
×
65

NEW
66
    def __repr__(self) -> str:
×
67
        """Return a string representation of this object.
68
           Show covered (and uncovered) program code"""
NEW
69
        t = ""
×
NEW
70
        for function_name in self.function_names():
×
71
            # Similar code as in the example above
NEW
72
            try:
×
NEW
73
                fun = eval(function_name)
×
NEW
74
            except Exception as exc:
×
NEW
75
                t += f"Skipping {function_name}: {exc}"
×
NEW
76
                continue
×
77

NEW
78
            source_lines, start_line_number = inspect.getsourcelines(fun)
×
NEW
79
            for lineno in range(start_line_number, start_line_number + len(source_lines)):
×
NEW
80
                if (function_name, lineno) in self.trace():
×
NEW
81
                    t += "# "
×
82
                else:
NEW
83
                    t += "  "
×
NEW
84
                t += "%2d  " % lineno
×
NEW
85
                t += source_lines[lineno - start_line_number]
×
86

NEW
87
        return t
×
88

89

NEW
90
class BranchCoverage(Coverage):
×
NEW
91
    def coverage(self):
×
92
        """The set of executed line pairs"""
NEW
93
        coverage = set()
×
NEW
94
        past_line = None
×
NEW
95
        for line in self.trace():
×
NEW
96
            if past_line is not None:
×
NEW
97
                coverage.add((past_line, line))
×
NEW
98
            past_line = line
×
99

NEW
100
        return coverage
×
101

102

UNCOV
103
def population_coverage(
×
104
    population: List[Input | str], function: Callable
105
) -> Tuple[Set[Location], List[int]]:
106
    cumulative_coverage: List[int] = []
×
107
    all_coverage: Set[Location] = set()
×
108

109
    for s in population:
×
110
        with Coverage() as cov:
×
111
            try:
×
112
                function(s)
×
113
            except:
×
114
                pass
×
115
        filtered_set = {
×
116
            (func, line)
117
            for (func, line) in cov.coverage()
118
            if "derivation_tree" not in func and "input" not in func
119
        }
120
        all_coverage |= filtered_set
×
121
        cumulative_coverage.append(len(all_coverage))
×
122

123
    return all_coverage, cumulative_coverage
×
124

125

126
def population_branch_coverage(
×
127
    population: List[Input | str], function: Callable
128
) -> Tuple[Set[Location], List[int]]:
129
    cumulative_coverage: List[int] = []
×
130
    all_coverage: Set[Location] = set()
×
131

132
    for s in population:
×
133
        with BranchCoverage() as cov:
×
134
            try:
×
135
                function(s)
×
136
            except:
×
137
                pass
×
138
        filtered_set = {
×
139
            (x, y)
140
            for (x, y) in cov.coverage()
141
            if "derivation_tree" not in x[0] and y[0] and "input" not in x[0] and y[0]
142
        }
143
        all_coverage |= filtered_set
×
144
        cumulative_coverage.append(len(all_coverage))
×
145

146
    return all_coverage, cumulative_coverage
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc