coverage 7.13.1__cp314-cp314t-musllinux_1_2_aarch64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- a1_coverage.pth +1 -0
- coverage/__init__.py +38 -0
- coverage/__main__.py +12 -0
- coverage/annotate.py +113 -0
- coverage/bytecode.py +197 -0
- coverage/cmdline.py +1220 -0
- coverage/collector.py +487 -0
- coverage/config.py +732 -0
- coverage/context.py +74 -0
- coverage/control.py +1514 -0
- coverage/core.py +139 -0
- coverage/data.py +251 -0
- coverage/debug.py +669 -0
- coverage/disposition.py +59 -0
- coverage/env.py +135 -0
- coverage/exceptions.py +85 -0
- coverage/execfile.py +329 -0
- coverage/files.py +553 -0
- coverage/html.py +860 -0
- coverage/htmlfiles/coverage_html.js +735 -0
- coverage/htmlfiles/favicon_32.png +0 -0
- coverage/htmlfiles/index.html +199 -0
- coverage/htmlfiles/keybd_closed.png +0 -0
- coverage/htmlfiles/pyfile.html +149 -0
- coverage/htmlfiles/style.css +389 -0
- coverage/htmlfiles/style.scss +844 -0
- coverage/inorout.py +590 -0
- coverage/jsonreport.py +200 -0
- coverage/lcovreport.py +218 -0
- coverage/misc.py +381 -0
- coverage/multiproc.py +120 -0
- coverage/numbits.py +146 -0
- coverage/parser.py +1215 -0
- coverage/patch.py +118 -0
- coverage/phystokens.py +197 -0
- coverage/plugin.py +617 -0
- coverage/plugin_support.py +299 -0
- coverage/pth_file.py +16 -0
- coverage/py.typed +1 -0
- coverage/python.py +272 -0
- coverage/pytracer.py +370 -0
- coverage/regions.py +127 -0
- coverage/report.py +298 -0
- coverage/report_core.py +117 -0
- coverage/results.py +502 -0
- coverage/sqldata.py +1212 -0
- coverage/sqlitedb.py +226 -0
- coverage/sysmon.py +509 -0
- coverage/templite.py +319 -0
- coverage/tomlconfig.py +212 -0
- coverage/tracer.cpython-314t-aarch64-linux-musl.so +0 -0
- coverage/tracer.pyi +43 -0
- coverage/types.py +214 -0
- coverage/version.py +35 -0
- coverage/xmlreport.py +263 -0
- coverage-7.13.1.dist-info/METADATA +200 -0
- coverage-7.13.1.dist-info/RECORD +61 -0
- coverage-7.13.1.dist-info/WHEEL +5 -0
- coverage-7.13.1.dist-info/entry_points.txt +4 -0
- coverage-7.13.1.dist-info/licenses/LICENSE.txt +177 -0
- coverage-7.13.1.dist-info/top_level.txt +1 -0
coverage/results.py
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
|
|
2
|
+
# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt
|
|
3
|
+
|
|
4
|
+
"""Results of coverage measurement."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import collections
|
|
9
|
+
import dataclasses
|
|
10
|
+
from collections.abc import Iterable
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from coverage.exceptions import ConfigError
|
|
14
|
+
from coverage.misc import nice_pair
|
|
15
|
+
from coverage.types import TArc, TLineNo
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from coverage.data import CoverageData
|
|
19
|
+
from coverage.plugin import FileReporter
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def analysis_from_file_reporter(
|
|
23
|
+
data: CoverageData,
|
|
24
|
+
precision: int,
|
|
25
|
+
file_reporter: FileReporter,
|
|
26
|
+
filename: str,
|
|
27
|
+
) -> Analysis:
|
|
28
|
+
"""Create an Analysis from a FileReporter."""
|
|
29
|
+
has_arcs = data.has_arcs()
|
|
30
|
+
statements = file_reporter.lines()
|
|
31
|
+
excluded = file_reporter.excluded_lines()
|
|
32
|
+
executed = file_reporter.translate_lines(data.lines(filename) or []) & statements
|
|
33
|
+
|
|
34
|
+
if has_arcs:
|
|
35
|
+
arc_possibilities_set = file_reporter.arcs()
|
|
36
|
+
arcs: Iterable[TArc] = data.arcs(filename) or []
|
|
37
|
+
arcs = file_reporter.translate_arcs(arcs)
|
|
38
|
+
|
|
39
|
+
# Reduce the set of arcs to the ones that could be branches.
|
|
40
|
+
dests = collections.defaultdict(set)
|
|
41
|
+
for fromno, tono in arc_possibilities_set:
|
|
42
|
+
dests[fromno].add(tono)
|
|
43
|
+
single_dests = {
|
|
44
|
+
fromno: list(tonos)[0] for fromno, tonos in dests.items() if len(tonos) == 1
|
|
45
|
+
}
|
|
46
|
+
new_arcs = set()
|
|
47
|
+
for fromno, tono in arcs:
|
|
48
|
+
if fromno != tono:
|
|
49
|
+
new_arcs.add((fromno, tono))
|
|
50
|
+
else:
|
|
51
|
+
if fromno in single_dests:
|
|
52
|
+
new_arcs.add((fromno, single_dests[fromno]))
|
|
53
|
+
|
|
54
|
+
arcs_executed_set = file_reporter.translate_arcs(new_arcs)
|
|
55
|
+
exit_counts = file_reporter.exit_counts()
|
|
56
|
+
no_branch = file_reporter.no_branch_lines()
|
|
57
|
+
else:
|
|
58
|
+
arc_possibilities_set = set()
|
|
59
|
+
arcs_executed_set = set()
|
|
60
|
+
exit_counts = {}
|
|
61
|
+
no_branch = set()
|
|
62
|
+
|
|
63
|
+
return Analysis(
|
|
64
|
+
precision=precision,
|
|
65
|
+
filename=filename,
|
|
66
|
+
has_arcs=has_arcs,
|
|
67
|
+
statements=statements,
|
|
68
|
+
excluded=excluded,
|
|
69
|
+
executed=executed,
|
|
70
|
+
arc_possibilities_set=arc_possibilities_set,
|
|
71
|
+
arcs_executed_set=arcs_executed_set,
|
|
72
|
+
exit_counts=exit_counts,
|
|
73
|
+
no_branch=no_branch,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclasses.dataclass
|
|
78
|
+
class Analysis:
|
|
79
|
+
"""The results of analyzing a FileReporter."""
|
|
80
|
+
|
|
81
|
+
precision: int
|
|
82
|
+
filename: str
|
|
83
|
+
has_arcs: bool
|
|
84
|
+
statements: set[TLineNo]
|
|
85
|
+
excluded: set[TLineNo]
|
|
86
|
+
executed: set[TLineNo]
|
|
87
|
+
arc_possibilities_set: set[TArc]
|
|
88
|
+
arcs_executed_set: set[TArc]
|
|
89
|
+
exit_counts: dict[TLineNo, int]
|
|
90
|
+
no_branch: set[TLineNo]
|
|
91
|
+
|
|
92
|
+
def __post_init__(self) -> None:
|
|
93
|
+
self.arc_possibilities = sorted(self.arc_possibilities_set)
|
|
94
|
+
self.arcs_executed = sorted(self.arcs_executed_set)
|
|
95
|
+
self.missing = self.statements - self.executed
|
|
96
|
+
|
|
97
|
+
if self.has_arcs:
|
|
98
|
+
n_branches = self._total_branches()
|
|
99
|
+
mba = self.missing_branch_arcs()
|
|
100
|
+
n_partial_branches = sum(len(v) for k, v in mba.items() if k not in self.missing)
|
|
101
|
+
n_missing_branches = sum(len(v) for k, v in mba.items())
|
|
102
|
+
else:
|
|
103
|
+
n_branches = n_partial_branches = n_missing_branches = 0
|
|
104
|
+
|
|
105
|
+
self.numbers = Numbers(
|
|
106
|
+
precision=self.precision,
|
|
107
|
+
n_files=1,
|
|
108
|
+
n_statements=len(self.statements),
|
|
109
|
+
n_excluded=len(self.excluded),
|
|
110
|
+
n_missing=len(self.missing),
|
|
111
|
+
n_branches=n_branches,
|
|
112
|
+
n_partial_branches=n_partial_branches,
|
|
113
|
+
n_missing_branches=n_missing_branches,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
def missing_formatted(self, branches: bool = False) -> str:
|
|
117
|
+
"""The missing line numbers, formatted nicely.
|
|
118
|
+
|
|
119
|
+
Returns a string like "1-2, 5-11, 13-14".
|
|
120
|
+
|
|
121
|
+
If `branches` is true, includes the missing branch arcs also.
|
|
122
|
+
|
|
123
|
+
"""
|
|
124
|
+
if branches and self.has_arcs:
|
|
125
|
+
arcs = self.missing_branch_arcs().items()
|
|
126
|
+
else:
|
|
127
|
+
arcs = None
|
|
128
|
+
|
|
129
|
+
return format_lines(self.statements, self.missing, arcs=arcs)
|
|
130
|
+
|
|
131
|
+
def arcs_missing(self) -> list[TArc]:
|
|
132
|
+
"""Returns a sorted list of the un-executed arcs in the code."""
|
|
133
|
+
missing = (
|
|
134
|
+
p
|
|
135
|
+
for p in self.arc_possibilities
|
|
136
|
+
if p not in self.arcs_executed_set
|
|
137
|
+
and p[0] not in self.no_branch
|
|
138
|
+
and p[1] not in self.excluded
|
|
139
|
+
)
|
|
140
|
+
return sorted(missing)
|
|
141
|
+
|
|
142
|
+
def _branch_lines(self) -> list[TLineNo]:
|
|
143
|
+
"""Returns a list of line numbers that have more than one exit."""
|
|
144
|
+
return [l1 for l1, count in self.exit_counts.items() if count > 1]
|
|
145
|
+
|
|
146
|
+
def _total_branches(self) -> int:
|
|
147
|
+
"""How many total branches are there?"""
|
|
148
|
+
return sum(count for count in self.exit_counts.values() if count > 1)
|
|
149
|
+
|
|
150
|
+
def missing_branch_arcs(self) -> dict[TLineNo, list[TLineNo]]:
|
|
151
|
+
"""Return arcs that weren't executed from branch lines.
|
|
152
|
+
|
|
153
|
+
Returns {l1:[l2a,l2b,...], ...}
|
|
154
|
+
|
|
155
|
+
"""
|
|
156
|
+
missing = self.arcs_missing()
|
|
157
|
+
branch_lines = set(self._branch_lines())
|
|
158
|
+
mba = collections.defaultdict(list)
|
|
159
|
+
for l1, l2 in missing:
|
|
160
|
+
assert l1 != l2, f"In {self.filename}, didn't expect {l1} == {l2}"
|
|
161
|
+
if l1 in branch_lines:
|
|
162
|
+
mba[l1].append(l2)
|
|
163
|
+
return mba
|
|
164
|
+
|
|
165
|
+
def executed_branch_arcs(self) -> dict[TLineNo, list[TLineNo]]:
|
|
166
|
+
"""Return arcs that were executed from branch lines.
|
|
167
|
+
|
|
168
|
+
Only include ones that we considered possible.
|
|
169
|
+
|
|
170
|
+
Returns {l1:[l2a,l2b,...], ...}
|
|
171
|
+
|
|
172
|
+
"""
|
|
173
|
+
branch_lines = set(self._branch_lines())
|
|
174
|
+
eba = collections.defaultdict(list)
|
|
175
|
+
for l1, l2 in self.arcs_executed:
|
|
176
|
+
assert l1 != l2, f"Oops: Didn't think this could happen: {l1 = }, {l2 = }"
|
|
177
|
+
if (l1, l2) not in self.arc_possibilities_set:
|
|
178
|
+
continue
|
|
179
|
+
if l1 in branch_lines:
|
|
180
|
+
eba[l1].append(l2)
|
|
181
|
+
return eba
|
|
182
|
+
|
|
183
|
+
def branch_stats(self) -> dict[TLineNo, tuple[int, int]]:
|
|
184
|
+
"""Get stats about branches.
|
|
185
|
+
|
|
186
|
+
Returns a dict mapping line numbers to a tuple:
|
|
187
|
+
(total_exits, taken_exits).
|
|
188
|
+
|
|
189
|
+
"""
|
|
190
|
+
|
|
191
|
+
missing_arcs = self.missing_branch_arcs()
|
|
192
|
+
stats = {}
|
|
193
|
+
for lnum in self._branch_lines():
|
|
194
|
+
exits = self.exit_counts[lnum]
|
|
195
|
+
missing = len(missing_arcs[lnum])
|
|
196
|
+
stats[lnum] = (exits, exits - missing)
|
|
197
|
+
return stats
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
TRegionLines = frozenset[TLineNo]
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class AnalysisNarrower:
|
|
204
|
+
"""
|
|
205
|
+
For reducing an `Analysis` to a subset of its lines.
|
|
206
|
+
|
|
207
|
+
Originally this was a simpler method on Analysis, but that led to quadratic
|
|
208
|
+
behavior. This class does the bulk of the work up-front to provide the
|
|
209
|
+
same results in linear time.
|
|
210
|
+
|
|
211
|
+
Create an AnalysisNarrower from an Analysis, bulk-add region lines to it
|
|
212
|
+
with `add_regions`, then individually request new narrowed Analysis objects
|
|
213
|
+
for each region with `narrow`. Doing most of the work in limited calls to
|
|
214
|
+
`add_regions` lets us avoid poor performance.
|
|
215
|
+
"""
|
|
216
|
+
|
|
217
|
+
# In this class, regions are represented by a frozenset of their lines.
|
|
218
|
+
|
|
219
|
+
def __init__(self, analysis: Analysis) -> None:
|
|
220
|
+
self.analysis = analysis
|
|
221
|
+
self.region2arc_possibilities: dict[TRegionLines, set[TArc]] = collections.defaultdict(set)
|
|
222
|
+
self.region2arc_executed: dict[TRegionLines, set[TArc]] = collections.defaultdict(set)
|
|
223
|
+
self.region2exit_counts: dict[TRegionLines, dict[TLineNo, int]] = collections.defaultdict(
|
|
224
|
+
dict
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
def add_regions(self, liness: Iterable[set[TLineNo]]) -> None:
|
|
228
|
+
"""
|
|
229
|
+
Pre-process a number of sets of line numbers. Later calls to `narrow`
|
|
230
|
+
with one of these sets will provide a narrowed Analysis.
|
|
231
|
+
"""
|
|
232
|
+
if self.analysis.has_arcs:
|
|
233
|
+
line2region: dict[TLineNo, TRegionLines] = {}
|
|
234
|
+
|
|
235
|
+
for lines in liness:
|
|
236
|
+
fzlines = frozenset(lines)
|
|
237
|
+
for line in lines:
|
|
238
|
+
line2region[line] = fzlines
|
|
239
|
+
|
|
240
|
+
def collect_arcs(
|
|
241
|
+
arc_set: set[TArc],
|
|
242
|
+
region2arcs: dict[TRegionLines, set[TArc]],
|
|
243
|
+
) -> None:
|
|
244
|
+
for a, b in arc_set:
|
|
245
|
+
if r := line2region.get(a):
|
|
246
|
+
region2arcs[r].add((a, b))
|
|
247
|
+
if r := line2region.get(b):
|
|
248
|
+
region2arcs[r].add((a, b))
|
|
249
|
+
|
|
250
|
+
collect_arcs(self.analysis.arc_possibilities_set, self.region2arc_possibilities)
|
|
251
|
+
collect_arcs(self.analysis.arcs_executed_set, self.region2arc_executed)
|
|
252
|
+
|
|
253
|
+
for lno, num in self.analysis.exit_counts.items():
|
|
254
|
+
if r := line2region.get(lno):
|
|
255
|
+
self.region2exit_counts[r][lno] = num
|
|
256
|
+
|
|
257
|
+
def narrow(self, lines: set[TLineNo]) -> Analysis:
|
|
258
|
+
"""Create a narrowed Analysis.
|
|
259
|
+
|
|
260
|
+
The current analysis is copied to make a new one that only considers
|
|
261
|
+
the lines in `lines`.
|
|
262
|
+
"""
|
|
263
|
+
|
|
264
|
+
# Technically, the set intersections in this method are still O(N**2)
|
|
265
|
+
# since this method is called N times, but they're very fast and moving
|
|
266
|
+
# them to `add_regions` won't avoid the quadratic time.
|
|
267
|
+
|
|
268
|
+
statements = self.analysis.statements & lines
|
|
269
|
+
excluded = self.analysis.excluded & lines
|
|
270
|
+
executed = self.analysis.executed & lines
|
|
271
|
+
|
|
272
|
+
if self.analysis.has_arcs:
|
|
273
|
+
fzlines = frozenset(lines)
|
|
274
|
+
arc_possibilities_set = self.region2arc_possibilities[fzlines]
|
|
275
|
+
arcs_executed_set = self.region2arc_executed[fzlines]
|
|
276
|
+
exit_counts = self.region2exit_counts[fzlines]
|
|
277
|
+
no_branch = self.analysis.no_branch & lines
|
|
278
|
+
else:
|
|
279
|
+
arc_possibilities_set = set()
|
|
280
|
+
arcs_executed_set = set()
|
|
281
|
+
exit_counts = {}
|
|
282
|
+
no_branch = set()
|
|
283
|
+
|
|
284
|
+
return Analysis(
|
|
285
|
+
precision=self.analysis.precision,
|
|
286
|
+
filename=self.analysis.filename,
|
|
287
|
+
has_arcs=self.analysis.has_arcs,
|
|
288
|
+
statements=statements,
|
|
289
|
+
excluded=excluded,
|
|
290
|
+
executed=executed,
|
|
291
|
+
arc_possibilities_set=arc_possibilities_set,
|
|
292
|
+
arcs_executed_set=arcs_executed_set,
|
|
293
|
+
exit_counts=exit_counts,
|
|
294
|
+
no_branch=no_branch,
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
@dataclasses.dataclass
|
|
299
|
+
class Numbers:
|
|
300
|
+
"""The numerical results of measuring coverage.
|
|
301
|
+
|
|
302
|
+
This holds the basic statistics from `Analysis`, and is used to roll
|
|
303
|
+
up statistics across files.
|
|
304
|
+
|
|
305
|
+
"""
|
|
306
|
+
|
|
307
|
+
precision: int = 0
|
|
308
|
+
n_files: int = 0
|
|
309
|
+
n_statements: int = 0
|
|
310
|
+
n_excluded: int = 0
|
|
311
|
+
n_missing: int = 0
|
|
312
|
+
n_branches: int = 0
|
|
313
|
+
n_partial_branches: int = 0
|
|
314
|
+
n_missing_branches: int = 0
|
|
315
|
+
|
|
316
|
+
@property
|
|
317
|
+
def n_executed(self) -> int:
|
|
318
|
+
"""Returns the number of executed statements."""
|
|
319
|
+
return self.n_statements - self.n_missing
|
|
320
|
+
|
|
321
|
+
@property
|
|
322
|
+
def n_executed_branches(self) -> int:
|
|
323
|
+
"""Returns the number of executed branches."""
|
|
324
|
+
return self.n_branches - self.n_missing_branches
|
|
325
|
+
|
|
326
|
+
@property
|
|
327
|
+
def ratio_statements(self) -> tuple[int, int]:
|
|
328
|
+
"""Return numerator/denominator for statement coverage."""
|
|
329
|
+
return self.n_executed, self.n_statements
|
|
330
|
+
|
|
331
|
+
@property
|
|
332
|
+
def ratio_branches(self) -> tuple[int, int]:
|
|
333
|
+
"""Return numerator/denominator for branch coverage."""
|
|
334
|
+
return self.n_executed_branches, self.n_branches
|
|
335
|
+
|
|
336
|
+
def _percent(self, numerator: int, denominator: int) -> float:
|
|
337
|
+
"""Helper for pc_* properties."""
|
|
338
|
+
if denominator > 0:
|
|
339
|
+
return (100.0 * numerator) / denominator
|
|
340
|
+
return 100.0
|
|
341
|
+
|
|
342
|
+
@property
|
|
343
|
+
def pc_covered(self) -> float:
|
|
344
|
+
"""Returns a single percentage value for coverage."""
|
|
345
|
+
return self._percent(*self.ratio_covered)
|
|
346
|
+
|
|
347
|
+
@property
|
|
348
|
+
def pc_statements(self) -> float:
|
|
349
|
+
"""Returns the percentage covered for statements."""
|
|
350
|
+
return self._percent(*self.ratio_statements)
|
|
351
|
+
|
|
352
|
+
@property
|
|
353
|
+
def pc_branches(self) -> float:
|
|
354
|
+
"""Returns the percentage covered for branches."""
|
|
355
|
+
return self._percent(*self.ratio_branches)
|
|
356
|
+
|
|
357
|
+
@property
|
|
358
|
+
def pc_covered_str(self) -> str:
|
|
359
|
+
"""Returns the percent covered, as a string, without a percent sign.
|
|
360
|
+
|
|
361
|
+
Note that "0" is only returned when the value is truly zero, and "100"
|
|
362
|
+
is only returned when the value is truly 100. Rounding can never
|
|
363
|
+
result in either "0" or "100".
|
|
364
|
+
|
|
365
|
+
"""
|
|
366
|
+
return display_covered(self.pc_covered, self.precision)
|
|
367
|
+
|
|
368
|
+
@property
|
|
369
|
+
def pc_statements_str(self) -> str:
|
|
370
|
+
"""Returns the statement percent covered without a percent sign."""
|
|
371
|
+
return display_covered(self.pc_statements, self.precision)
|
|
372
|
+
|
|
373
|
+
@property
|
|
374
|
+
def pc_branches_str(self) -> str:
|
|
375
|
+
"""Returns the branch percent covered without a percent sign."""
|
|
376
|
+
return display_covered(self.pc_branches, self.precision)
|
|
377
|
+
|
|
378
|
+
@property
|
|
379
|
+
def ratio_covered(self) -> tuple[int, int]:
|
|
380
|
+
"""Return a numerator and denominator for the coverage ratio."""
|
|
381
|
+
numerator = self.n_executed + self.n_executed_branches
|
|
382
|
+
denominator = self.n_statements + self.n_branches
|
|
383
|
+
return numerator, denominator
|
|
384
|
+
|
|
385
|
+
def __add__(self, other: Numbers) -> Numbers:
|
|
386
|
+
return Numbers(
|
|
387
|
+
self.precision,
|
|
388
|
+
self.n_files + other.n_files,
|
|
389
|
+
self.n_statements + other.n_statements,
|
|
390
|
+
self.n_excluded + other.n_excluded,
|
|
391
|
+
self.n_missing + other.n_missing,
|
|
392
|
+
self.n_branches + other.n_branches,
|
|
393
|
+
self.n_partial_branches + other.n_partial_branches,
|
|
394
|
+
self.n_missing_branches + other.n_missing_branches,
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
def __radd__(self, other: int) -> Numbers:
|
|
398
|
+
# Implementing 0+Numbers allows us to sum() a list of Numbers.
|
|
399
|
+
assert other == 0 # we only ever call it this way.
|
|
400
|
+
return self
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def display_covered(pc: float, precision: int) -> str:
|
|
404
|
+
"""Return a displayable total percentage, as a string.
|
|
405
|
+
|
|
406
|
+
Note that "0" is only returned when the value is truly zero, and "100"
|
|
407
|
+
is only returned when the value is truly 100. Rounding can never
|
|
408
|
+
result in either "0" or "100".
|
|
409
|
+
|
|
410
|
+
"""
|
|
411
|
+
near0 = 1.0 / 10**precision
|
|
412
|
+
if 0 < pc < near0:
|
|
413
|
+
pc = near0
|
|
414
|
+
elif (100.0 - near0) < pc < 100:
|
|
415
|
+
pc = 100.0 - near0
|
|
416
|
+
else:
|
|
417
|
+
pc = round(pc, precision)
|
|
418
|
+
return f"{pc:.{precision}f}"
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def _line_ranges(
|
|
422
|
+
statements: Iterable[TLineNo],
|
|
423
|
+
lines: Iterable[TLineNo],
|
|
424
|
+
) -> list[tuple[TLineNo, TLineNo]]:
|
|
425
|
+
"""Produce a list of ranges for `format_lines`."""
|
|
426
|
+
statements = sorted(statements)
|
|
427
|
+
lines = sorted(lines)
|
|
428
|
+
|
|
429
|
+
pairs = []
|
|
430
|
+
start: TLineNo | None = None
|
|
431
|
+
lidx = 0
|
|
432
|
+
for stmt in statements:
|
|
433
|
+
if lidx >= len(lines):
|
|
434
|
+
break
|
|
435
|
+
if stmt == lines[lidx]:
|
|
436
|
+
lidx += 1
|
|
437
|
+
if not start:
|
|
438
|
+
start = stmt
|
|
439
|
+
end = stmt
|
|
440
|
+
elif start:
|
|
441
|
+
pairs.append((start, end))
|
|
442
|
+
start = None
|
|
443
|
+
if start:
|
|
444
|
+
pairs.append((start, end))
|
|
445
|
+
return pairs
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
def format_lines(
|
|
449
|
+
statements: Iterable[TLineNo],
|
|
450
|
+
lines: Iterable[TLineNo],
|
|
451
|
+
arcs: Iterable[tuple[TLineNo, list[TLineNo]]] | None = None,
|
|
452
|
+
) -> str:
|
|
453
|
+
"""Nicely format a list of line numbers.
|
|
454
|
+
|
|
455
|
+
Format a list of line numbers for printing by coalescing groups of lines as
|
|
456
|
+
long as the lines represent consecutive statements. This will coalesce
|
|
457
|
+
even if there are gaps between statements.
|
|
458
|
+
|
|
459
|
+
For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and
|
|
460
|
+
`lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14".
|
|
461
|
+
|
|
462
|
+
Both `lines` and `statements` can be any iterable. All of the elements of
|
|
463
|
+
`lines` must be in `statements`, and all of the values must be positive
|
|
464
|
+
integers.
|
|
465
|
+
|
|
466
|
+
If `arcs` is provided, they are (start,[end,end,end]) pairs that will be
|
|
467
|
+
included in the output as long as start isn't in `lines`.
|
|
468
|
+
|
|
469
|
+
"""
|
|
470
|
+
line_items = [(pair[0], nice_pair(pair)) for pair in _line_ranges(statements, lines)]
|
|
471
|
+
if arcs is not None:
|
|
472
|
+
line_exits = sorted(arcs)
|
|
473
|
+
for line, exits in line_exits:
|
|
474
|
+
for ex in sorted(exits):
|
|
475
|
+
if line not in lines and ex not in lines:
|
|
476
|
+
dest = ex if ex > 0 else "exit"
|
|
477
|
+
line_items.append((line, f"{line}->{dest}"))
|
|
478
|
+
|
|
479
|
+
ret = ", ".join(t[-1] for t in sorted(line_items))
|
|
480
|
+
return ret
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
def should_fail_under(total: float, fail_under: float, precision: int) -> bool:
|
|
484
|
+
"""Determine if a total should fail due to fail-under.
|
|
485
|
+
|
|
486
|
+
`total` is a float, the coverage measurement total. `fail_under` is the
|
|
487
|
+
fail_under setting to compare with. `precision` is the number of digits
|
|
488
|
+
to consider after the decimal point.
|
|
489
|
+
|
|
490
|
+
Returns True if the total should fail.
|
|
491
|
+
|
|
492
|
+
"""
|
|
493
|
+
# We can never achieve higher than 100% coverage, or less than zero.
|
|
494
|
+
if not (0 <= fail_under <= 100.0):
|
|
495
|
+
msg = f"fail_under={fail_under} is invalid. Must be between 0 and 100."
|
|
496
|
+
raise ConfigError(msg)
|
|
497
|
+
|
|
498
|
+
# Special case for fail_under=100, it must really be 100.
|
|
499
|
+
if fail_under == 100.0 and total != 100.0:
|
|
500
|
+
return True
|
|
501
|
+
|
|
502
|
+
return round(total, precision) < fail_under
|