Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 5ccac01

Browse files
authored
Apply --scope-output filters after getSourceReport (#498)
1 parent f2e52fb commit 5ccac01

File tree

3 files changed

+376
-270
lines changed

3 files changed

+376
-270
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 1.8.1-wip
22

33
- Require Dart ^3.4
4+
- Fix bug where some ranges were able to bypass the `--scope-output` filters.
45

56
## 1.8.0
67

lib/src/collect.dart

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,8 @@ Future<Map<String, dynamic>> _getAllCoverage(
178178
continue;
179179
}
180180
for (final script in scripts.scripts!) {
181-
final uri = Uri.parse(script.uri!);
182-
if (uri.scheme != 'package') continue;
183-
final scope = uri.path.split('/').first;
184181
// Skip scripts which should not be included in the report.
185-
if (!scopedOutput.contains(scope)) continue;
182+
if (!scopedOutput.includesScript(script.uri)) continue;
186183
late final SourceReport scriptReport;
187184
try {
188185
scriptReport = await service.getSourceReport(
@@ -203,7 +200,8 @@ Future<Map<String, dynamic>> _getAllCoverage(
203200
includeDart,
204201
functionCoverage,
205202
reportLines,
206-
coverableLineCache);
203+
coverableLineCache,
204+
scopedOutput);
207205
allCoverage.addAll(coverage);
208206
}
209207
} else {
@@ -229,7 +227,8 @@ Future<Map<String, dynamic>> _getAllCoverage(
229227
includeDart,
230228
functionCoverage,
231229
reportLines,
232-
coverableLineCache);
230+
coverableLineCache,
231+
scopedOutput);
233232
allCoverage.addAll(coverage);
234233
}
235234
}
@@ -312,7 +311,8 @@ Future<List<Map<String, dynamic>>> _processSourceReport(
312311
bool includeDart,
313312
bool functionCoverage,
314313
bool reportLines,
315-
Map<String, Set<int>>? coverableLineCache) async {
314+
Map<String, Set<int>>? coverableLineCache,
315+
Set<String> scopedOutput) async {
316316
final hitMaps = <Uri, HitMap>{};
317317
final scripts = <ScriptRef, Script>{};
318318
final libraries = <LibraryRef>{};
@@ -362,8 +362,14 @@ Future<List<Map<String, dynamic>>> _processSourceReport(
362362

363363
for (var range in report.ranges!) {
364364
final scriptRef = report.scripts![range.scriptIndex!];
365-
final scriptUriString = scriptRef.uri!;
366-
final scriptUri = Uri.parse(scriptUriString);
365+
final scriptUriString = scriptRef.uri;
366+
if (!scopedOutput.includesScript(scriptUriString)) {
367+
// Sometimes a range's script can be different to the function's script
368+
// (eg mixins), so we have to re-check the scope filter.
369+
// See https://github.com/dart-lang/coverage/issues/495
370+
continue;
371+
}
372+
final scriptUri = Uri.parse(scriptUriString!);
367373

368374
// If we have a coverableLineCache, use it in the same way we use
369375
// SourceReportCoverage.misses: to add zeros to the coverage result for all
@@ -497,3 +503,19 @@ class StdoutLog extends Log {
497503
@override
498504
void severe(String message) => print(message);
499505
}
506+
507+
extension _ScopedOutput on Set<String> {
508+
bool includesScript(String? scriptUriString) {
509+
if (scriptUriString == null) return false;
510+
511+
// If the set is empty, it means the user didn't specify a --scope-output
512+
// flag, so allow everything.
513+
if (isEmpty) return true;
514+
515+
final scriptUri = Uri.parse(scriptUriString);
516+
if (scriptUri.scheme != 'package') return false;
517+
518+
final scope = scriptUri.pathSegments.first;
519+
return contains(scope);
520+
}
521+
}

0 commit comments

Comments
 (0)