-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
215 lines (174 loc) · 7.03 KB
/
base.py
File metadata and controls
215 lines (174 loc) · 7.03 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from __future__ import annotations
import dataclasses
import datetime
import decimal
import json
import pathlib
from abc import ABC, abstractmethod
from collections import deque
from collections.abc import Sequence
from codecov.exceptions import ConfigurationException
from codecov.log import log
@dataclasses.dataclass
class CoverageMetadata:
version: str
timestamp: datetime.datetime
branch_coverage: bool
show_contexts: bool
@dataclasses.dataclass
class CoverageInfo: # pylint: disable=too-many-instance-attributes
covered_lines: int
num_statements: int
percent_covered: decimal.Decimal
percent_covered_display: str
missing_lines: int
excluded_lines: int
num_branches: int | None
num_partial_branches: int | None
covered_branches: int | None
missing_branches: int | None
@dataclasses.dataclass
class FileCoverage:
path: pathlib.Path
executed_lines: list[int]
missing_lines: list[int]
excluded_lines: list[int]
executed_branches: list[list[int]] | None
missing_branches: list[list[int]] | None
info: CoverageInfo
@dataclasses.dataclass
class Coverage:
meta: CoverageMetadata
info: CoverageInfo
files: dict[pathlib.Path, FileCoverage]
@dataclasses.dataclass
class FileDiffCoverage:
path: pathlib.Path
percent_covered: decimal.Decimal
covered_statements: list[int]
missing_statements: list[int]
added_statements: list[int]
# Added lines tracks all the lines that were added in the diff, not just
# the statements (so it includes comments, blank lines, etc.)
added_lines: list[int]
@dataclasses.dataclass
class DiffCoverage:
total_num_lines: int
total_num_violations: int
total_percent_covered: decimal.Decimal
num_changed_lines: int
files: dict[pathlib.Path, FileDiffCoverage]
class BaseCoverage(ABC):
def compute_coverage(self, num_covered: int, num_total: int) -> decimal.Decimal:
if num_total == 0:
return decimal.Decimal('1')
return decimal.Decimal(num_covered) / decimal.Decimal(num_total)
def get_coverage_info(self, coverage_path: pathlib.Path) -> Coverage:
try:
with coverage_path.open() as coverage_data:
json_coverage = json.loads(coverage_data.read())
except FileNotFoundError as exc:
log.error('Coverage report file not found at the specified location: %s', coverage_path)
raise ConfigurationException from exc
except json.JSONDecodeError as exc:
log.error('Invalid JSON format in coverage report file: %s', coverage_path)
raise ConfigurationException from exc
return self.extract_info(data=json_coverage)
@abstractmethod
def extract_info(self, data: dict) -> Coverage:
raise NotImplementedError # pragma: no cover
def get_diff_coverage_info( # pylint: disable=too-many-locals
self,
added_lines: dict[pathlib.Path, list[int]],
coverage: Coverage,
) -> DiffCoverage:
files = {}
total_num_lines = 0
total_num_violations = 0
num_changed_lines = 0
for path, added_lines_for_file in added_lines.items():
num_changed_lines += len(added_lines_for_file)
try:
file = coverage.files[path]
except KeyError:
continue
executed = set(file.executed_lines) & set(added_lines_for_file)
count_executed = len(executed)
missing = set(file.missing_lines) & set(added_lines_for_file)
count_missing = len(missing)
added = executed | missing
count_total = len(added)
total_num_lines += count_total
total_num_violations += count_missing
percent_covered = self.compute_coverage(num_covered=count_executed, num_total=count_total)
files[path] = FileDiffCoverage(
path=path,
percent_covered=percent_covered,
covered_statements=sorted(executed),
missing_statements=sorted(missing),
added_statements=sorted(added),
added_lines=added_lines_for_file,
)
final_percentage = self.compute_coverage(
num_covered=total_num_lines - total_num_violations,
num_total=total_num_lines,
)
return DiffCoverage(
total_num_lines=total_num_lines,
total_num_violations=total_num_violations,
total_percent_covered=final_percentage,
num_changed_lines=num_changed_lines,
files=files,
)
def parse_diff_output(self, diff: str) -> dict[pathlib.Path, list[int]]:
current_file: pathlib.Path | None = None
added_filename_prefix = '+++ b/'
result: dict[pathlib.Path, list[int]] = {}
diff_lines: deque[str] = deque()
diff_lines.extend(diff.splitlines())
while diff_lines:
line = diff_lines.popleft()
if line.startswith(added_filename_prefix):
current_file = pathlib.Path(line.removeprefix(added_filename_prefix))
continue
if not line.startswith('@@'):
continue
def parse_line_number_diff_line(diff_line: str) -> Sequence[int]:
"""
Parse the "added" part of the line number diff text:
@@ -60,0 +61 @@ def compute_files( -> [64]
@@ -60,0 +61,9 @@ def compute_files( -> [64, 65, 66]
Github API returns default context lines 3 at start and end, we need to remove them.
"""
start, _ = (int(i) for i in (diff_line.split()[2][1:] + ',1').split(',')[:2])
line_start = line_end = start
while diff_lines:
next_line = diff_lines.popleft()
if next_line.startswith(' '):
line_start += 1
line_end += 1
continue
if next_line.startswith('-'):
continue
diff_lines.appendleft(next_line)
break
last_added_line = line_end
while diff_lines:
next_line = diff_lines.popleft()
if next_line.startswith(' ') or next_line.startswith('+'):
line_end += 1
if next_line.startswith('+'):
last_added_line = line_end
continue
if next_line.startswith('-'):
continue
diff_lines.appendleft(next_line)
break
return range(line_start, last_added_line)
lines = parse_line_number_diff_line(diff_line=line)
if len(lines) > 0:
if current_file is None:
log.error('Diff output format is invalid: %s', diff)
raise ValueError
result.setdefault(current_file, []).extend(lines)
return result