blastdiff 0.1.0__py3-none-any.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.
@@ -0,0 +1,257 @@
1
+ """Coverage parser (Cobertura XML and LCOV) and deterministic proof mapper."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import xml.etree.ElementTree as ET
6
+
7
+ from blastdiff.core.blast_radius import is_test_path
8
+ from blastdiff.core.models import (
9
+ FileReview,
10
+ ProofStatus,
11
+ ProofSummary,
12
+ StoryBucket,
13
+ )
14
+
15
+ _NON_EXECUTABLE_LANGUAGES: frozenset[str] = frozenset(
16
+ {"markdown", "json", "yaml", "toml", "text", "html", "css"}
17
+ )
18
+
19
+
20
+ def _normalize_path(path: str) -> str:
21
+ norm = path.replace("\\", "/")
22
+ while norm.startswith("./"):
23
+ norm = norm[2:]
24
+ return norm
25
+
26
+
27
+ def parse_coverage_xml(xml_text: str) -> dict[str, dict[int, int]]:
28
+ """Parse Cobertura coverage.xml into {normalized_file_path: {lineno: hits}}."""
29
+ if not xml_text or not xml_text.strip():
30
+ return {}
31
+ try:
32
+ root = ET.fromstring(xml_text)
33
+ except ET.ParseError:
34
+ return {}
35
+
36
+ coverage_map: dict[str, dict[int, int]] = {}
37
+ for cls in root.iter("class"):
38
+ filename = cls.attrib.get("filename", "").strip()
39
+ if not filename:
40
+ continue
41
+ norm_file = _normalize_path(filename)
42
+ file_hits = coverage_map.setdefault(norm_file, {})
43
+
44
+ lines_elem = cls.find("lines")
45
+ line_nodes = lines_elem.findall("line") if lines_elem is not None else cls.findall(".//line")
46
+ for line_node in line_nodes:
47
+ num_raw = line_node.attrib.get("number")
48
+ hits_raw = line_node.attrib.get("hits", "0")
49
+ if num_raw is None:
50
+ continue
51
+ try:
52
+ lineno = int(num_raw)
53
+ hits = int(hits_raw)
54
+ except ValueError:
55
+ continue
56
+ prev = file_hits.get(lineno)
57
+ file_hits[lineno] = hits if prev is None else max(prev, hits)
58
+
59
+ return coverage_map
60
+
61
+
62
+ def parse_lcov(lcov_text: str) -> dict[str, dict[int, int]]:
63
+ """Parse LCOV tracefile (`SF:` and `DA:line,hits`) into {normalized_path: {lineno: hits}}."""
64
+ if not lcov_text or not lcov_text.strip():
65
+ return {}
66
+
67
+ coverage_map: dict[str, dict[int, int]] = {}
68
+ current_file: str | None = None
69
+
70
+ for raw_line in lcov_text.splitlines():
71
+ line = raw_line.strip()
72
+ if not line:
73
+ continue
74
+ if line.startswith("SF:"):
75
+ path = _normalize_path(line[3:].strip())
76
+ current_file = path or None
77
+ if current_file is not None:
78
+ coverage_map.setdefault(current_file, {})
79
+ elif line.startswith("DA:") and current_file is not None:
80
+ parts = line[3:].split(",")
81
+ if len(parts) < 2:
82
+ continue
83
+ try:
84
+ lineno = int(parts[0].strip())
85
+ hits = int(parts[1].strip())
86
+ except ValueError:
87
+ continue
88
+ file_hits = coverage_map[current_file]
89
+ prev = file_hits.get(lineno)
90
+ file_hits[lineno] = hits if prev is None else max(prev, hits)
91
+ elif line == "end_of_record":
92
+ current_file = None
93
+
94
+ return coverage_map
95
+
96
+
97
+ def _match_coverage_for_file(
98
+ file_path: str,
99
+ coverage_map: dict[str, dict[int, int]],
100
+ ) -> dict[int, int] | None:
101
+ """Match a repo-relative path against coverage keys (exact or longest normalized suffix)."""
102
+ if not coverage_map:
103
+ return None
104
+
105
+ norm_target = _normalize_path(file_path)
106
+ if norm_target in coverage_map:
107
+ return coverage_map[norm_target]
108
+
109
+ best_match: dict[int, int] | None = None
110
+ best_len = -1
111
+
112
+ for raw_key, hits in coverage_map.items():
113
+ norm_key = _normalize_path(raw_key)
114
+ if norm_key == norm_target:
115
+ return hits
116
+ if norm_key.endswith("/" + norm_target):
117
+ if len(norm_target) > best_len:
118
+ best_match = hits
119
+ best_len = len(norm_target)
120
+ elif norm_target.endswith("/" + norm_key):
121
+ if len(norm_key) > best_len:
122
+ best_match = hits
123
+ best_len = len(norm_key)
124
+
125
+ return best_match
126
+
127
+
128
+ def _is_non_executable(file_review: FileReview) -> bool:
129
+ if file_review.status == "deleted":
130
+ return True
131
+ if file_review.story_bucket in (StoryBucket.DOCS, StoryBucket.CONFIG):
132
+ return True
133
+ if file_review.language in _NON_EXECUTABLE_LANGUAGES:
134
+ return True
135
+ if file_review.additions == 0:
136
+ return True
137
+ return False
138
+
139
+
140
+ def apply_proof_to_files(
141
+ files: list[FileReview],
142
+ coverage_map: dict[str, dict[int, int]] | None,
143
+ coverage_source: str | None,
144
+ ) -> ProofSummary:
145
+ """Map coverage data and static test reachability onto FileReview and Hunk models."""
146
+ total_covered = 0
147
+ total_uncovered = 0
148
+ unmeasured_executable_added = 0
149
+
150
+ for file_review in files:
151
+ if _is_non_executable(file_review):
152
+ file_review.proof_status = ProofStatus.NON_EXECUTABLE
153
+ file_review.covered_added_lines = 0
154
+ file_review.uncovered_added_lines = 0
155
+ for hunk in file_review.hunks:
156
+ hunk.proof_status = ProofStatus.NON_EXECUTABLE
157
+ hunk.covered_lines = 0
158
+ hunk.uncovered_lines = 0
159
+ continue
160
+
161
+ line_hits = (
162
+ _match_coverage_for_file(file_review.path, coverage_map)
163
+ if coverage_map
164
+ else None
165
+ )
166
+
167
+ file_covered = 0
168
+ file_uncovered = 0
169
+
170
+ for hunk in file_review.hunks:
171
+ hunk_covered = 0
172
+ hunk_uncovered = 0
173
+ hunk_added_count = 0
174
+
175
+ for dl in hunk.lines:
176
+ if dl.kind != "add" or dl.new_lineno is None:
177
+ continue
178
+ hunk_added_count += 1
179
+ if line_hits is not None and dl.new_lineno in line_hits:
180
+ hits = line_hits[dl.new_lineno]
181
+ dl.coverage_hits = hits
182
+ if hits > 0:
183
+ hunk_covered += 1
184
+ else:
185
+ hunk_uncovered += 1
186
+
187
+ hunk.covered_lines = hunk_covered
188
+ hunk.uncovered_lines = hunk_uncovered
189
+ file_covered += hunk_covered
190
+ file_uncovered += hunk_uncovered
191
+
192
+ if hunk_added_count == 0:
193
+ hunk.proof_status = ProofStatus.NON_EXECUTABLE
194
+ elif line_hits is not None:
195
+ if hunk_uncovered > 0:
196
+ hunk.proof_status = ProofStatus.UNVERIFIED
197
+ elif hunk_covered > 0:
198
+ hunk.proof_status = ProofStatus.PROVEN_BY_COVERAGE
199
+ elif file_review.reaching_tests:
200
+ hunk.proof_status = ProofStatus.REACHED_BY_TESTS
201
+ else:
202
+ hunk.proof_status = ProofStatus.NON_EXECUTABLE
203
+ elif file_review.reaching_tests:
204
+ hunk.proof_status = ProofStatus.REACHED_BY_TESTS
205
+ else:
206
+ hunk.proof_status = ProofStatus.UNVERIFIED
207
+
208
+ file_review.covered_added_lines = file_covered
209
+ file_review.uncovered_added_lines = file_uncovered
210
+
211
+ if line_hits is not None:
212
+ if file_uncovered > 0:
213
+ file_review.proof_status = ProofStatus.UNVERIFIED
214
+ elif file_covered > 0:
215
+ file_review.proof_status = ProofStatus.PROVEN_BY_COVERAGE
216
+ elif file_review.reaching_tests:
217
+ file_review.proof_status = ProofStatus.REACHED_BY_TESTS
218
+ else:
219
+ file_review.proof_status = ProofStatus.NON_EXECUTABLE
220
+ elif file_review.reaching_tests:
221
+ file_review.proof_status = ProofStatus.REACHED_BY_TESTS
222
+ else:
223
+ file_review.proof_status = ProofStatus.UNVERIFIED
224
+
225
+ total_covered += file_covered
226
+ total_uncovered += file_uncovered
227
+ if line_hits is None:
228
+ unmeasured_executable_added += file_review.additions
229
+
230
+ measured_total = total_covered + total_uncovered
231
+ if measured_total > 0:
232
+ added_lines_total = measured_total
233
+ coverage_pct: float | None = round((total_covered / measured_total) * 100.0, 1)
234
+ else:
235
+ added_lines_total = unmeasured_executable_added
236
+ coverage_pct = None
237
+
238
+ statically_reached = 0
239
+ unreached = 0
240
+ for file_review in files:
241
+ if is_test_path(file_review.path):
242
+ continue
243
+ for sc in file_review.symbol_changes:
244
+ if sc.test_callers:
245
+ statically_reached += 1
246
+ else:
247
+ unreached += 1
248
+
249
+ return ProofSummary(
250
+ coverage_source=coverage_source,
251
+ added_lines_total=added_lines_total,
252
+ covered_added_lines=total_covered,
253
+ uncovered_added_lines=total_uncovered,
254
+ coverage_pct=coverage_pct,
255
+ statically_reached_symbols=statically_reached,
256
+ unreached_symbols=unreached,
257
+ )