9
9
import json
10
10
import sys
11
11
12
- from typing import Any , IO , Iterable , TYPE_CHECKING
12
+ from typing import Any , Dict , IO , Iterable , TYPE_CHECKING
13
13
14
14
from coverage import __version__
15
15
from coverage .report_core import get_analysis_to_report
22
22
from coverage .plugin import FileReporter
23
23
24
24
25
+ # A type for data that can be JSON-serialized.
26
+ JsonObj = Dict [str , Any ]
27
+
25
28
# "Version 1" had no format number at all.
26
29
# 2: add the meta.format field.
27
30
# 3: add region information (functions, classes)
@@ -36,7 +39,27 @@ def __init__(self, coverage: Coverage) -> None:
36
39
self .coverage = coverage
37
40
self .config = self .coverage .config
38
41
self .total = Numbers (self .config .precision )
39
- self .report_data : dict [str , Any ] = {}
42
+ self .report_data : JsonObj = {}
43
+
44
+ def make_summary (self , nums : Numbers ) -> JsonObj :
45
+ """Create a dict summarizing `nums`."""
46
+ return {
47
+ "covered_lines" : nums .n_executed ,
48
+ "num_statements" : nums .n_statements ,
49
+ "percent_covered" : nums .pc_covered ,
50
+ "percent_covered_display" : nums .pc_covered_str ,
51
+ "missing_lines" : nums .n_missing ,
52
+ "excluded_lines" : nums .n_excluded ,
53
+ }
54
+
55
+ def make_branch_summary (self , nums : Numbers ) -> JsonObj :
56
+ """Create a dict summarizing the branch info in `nums`."""
57
+ return {
58
+ "num_branches" : nums .n_branches ,
59
+ "num_partial_branches" : nums .n_partial_branches ,
60
+ "covered_branches" : nums .n_executed_branches ,
61
+ "missing_branches" : nums .n_missing_branches ,
62
+ }
40
63
41
64
def report (self , morfs : Iterable [TMorf ] | None , outfile : IO [str ]) -> float :
42
65
"""Generate a json report for `morfs`.
@@ -66,23 +89,10 @@ def report(self, morfs: Iterable[TMorf] | None, outfile: IO[str]) -> float:
66
89
)
67
90
68
91
self .report_data ["files" ] = measured_files
69
-
70
- self .report_data ["totals" ] = {
71
- "covered_lines" : self .total .n_executed ,
72
- "num_statements" : self .total .n_statements ,
73
- "percent_covered" : self .total .pc_covered ,
74
- "percent_covered_display" : self .total .pc_covered_str ,
75
- "missing_lines" : self .total .n_missing ,
76
- "excluded_lines" : self .total .n_excluded ,
77
- }
92
+ self .report_data ["totals" ] = self .make_summary (self .total )
78
93
79
94
if coverage_data .has_arcs ():
80
- self .report_data ["totals" ].update ({
81
- "num_branches" : self .total .n_branches ,
82
- "num_partial_branches" : self .total .n_partial_branches ,
83
- "covered_branches" : self .total .n_executed_branches ,
84
- "missing_branches" : self .total .n_missing_branches ,
85
- })
95
+ self .report_data ["totals" ].update (self .make_branch_summary (self .total ))
86
96
87
97
json .dump (
88
98
self .report_data ,
@@ -94,19 +104,12 @@ def report(self, morfs: Iterable[TMorf] | None, outfile: IO[str]) -> float:
94
104
95
105
def report_one_file (
96
106
self , coverage_data : CoverageData , analysis : Analysis , file_reporter : FileReporter
97
- ) -> dict [ str , Any ] :
107
+ ) -> JsonObj :
98
108
"""Extract the relevant report data for a single file."""
99
109
nums = analysis .numbers
100
110
self .total += nums
101
- summary = {
102
- "covered_lines" : nums .n_executed ,
103
- "num_statements" : nums .n_statements ,
104
- "percent_covered" : nums .pc_covered ,
105
- "percent_covered_display" : nums .pc_covered_str ,
106
- "missing_lines" : nums .n_missing ,
107
- "excluded_lines" : nums .n_excluded ,
108
- }
109
- reported_file : dict [str , Any ] = {
111
+ summary = self .make_summary (nums )
112
+ reported_file : JsonObj = {
110
113
"executed_lines" : sorted (analysis .executed ),
111
114
"summary" : summary ,
112
115
"missing_lines" : sorted (analysis .missing ),
@@ -115,12 +118,7 @@ def report_one_file(
115
118
if self .config .json_show_contexts :
116
119
reported_file ["contexts" ] = coverage_data .contexts_by_lineno (analysis .filename )
117
120
if coverage_data .has_arcs ():
118
- summary .update ({
119
- "num_branches" : nums .n_branches ,
120
- "num_partial_branches" : nums .n_partial_branches ,
121
- "covered_branches" : nums .n_executed_branches ,
122
- "missing_branches" : nums .n_missing_branches ,
123
- })
121
+ summary .update (self .make_branch_summary (nums ))
124
122
reported_file ["executed_branches" ] = list (
125
123
_convert_branch_arcs (analysis .executed_branch_arcs ()),
126
124
)
@@ -136,14 +134,7 @@ def report_one_file(
136
134
outside_lines -= region .lines
137
135
narrowed_analysis = analysis .narrow (region .lines )
138
136
narrowed_nums = narrowed_analysis .numbers
139
- narrowed_summary = {
140
- "covered_lines" : narrowed_nums .n_executed ,
141
- "num_statements" : narrowed_nums .n_statements ,
142
- "percent_covered" : narrowed_nums .pc_covered ,
143
- "percent_covered_display" : narrowed_nums .pc_covered_str ,
144
- "missing_lines" : narrowed_nums .n_missing ,
145
- "excluded_lines" : narrowed_nums .n_excluded ,
146
- }
137
+ narrowed_summary = self .make_summary (narrowed_nums )
147
138
reported_file [region .kind ][region .name ] = {
148
139
"executed_lines" : sorted (narrowed_analysis .executed ),
149
140
"summary" : narrowed_summary ,
@@ -154,18 +145,14 @@ def report_one_file(
154
145
contexts = coverage_data .contexts_by_lineno (narrowed_analysis .filename )
155
146
reported_file [region .kind ][region .name ]["contexts" ] = contexts
156
147
if coverage_data .has_arcs ():
157
- narrowed_summary .update ({
158
- "num_branches" : narrowed_nums .n_branches ,
159
- "num_partial_branches" : narrowed_nums .n_partial_branches ,
160
- "covered_branches" : narrowed_nums .n_executed_branches ,
161
- "missing_branches" : narrowed_nums .n_missing_branches ,
162
- })
148
+ narrowed_summary .update (self .make_branch_summary (narrowed_nums ))
163
149
reported_file [region .kind ][region .name ]["executed_branches" ] = list (
164
150
_convert_branch_arcs (narrowed_analysis .executed_branch_arcs ()),
165
151
)
166
152
reported_file [region .kind ][region .name ]["missing_branches" ] = list (
167
153
_convert_branch_arcs (narrowed_analysis .missing_branch_arcs ()),
168
154
)
155
+
169
156
return reported_file
170
157
171
158
0 commit comments