Skip to content

Commit d126883

Browse files
committed
Refined filtering functionality for collect function #529
1 parent f7a6cc8 commit d126883

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

pkgs/coverage/lib/src/collect.dart

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const _debugTokenPositions = bool.fromEnvironment('DEBUG_COVERAGE');
4343
/// scripts that start with any of the provided paths are considered.
4444
///
4545
/// If [isolateIds] is set, coverage gathering **will not be restricted** to
46-
/// only those VM isolates. Instead, coverage will be collected for **all isolates
46+
/// only those VM isolates. Instead, coverage will be collected for
47+
/// **all isolates
4748
/// in the same isolate group** as the provided isolate(s).
4849
///
4950
/// If [coverableLineCache] is set, the collector will avoid recompiling
@@ -57,11 +58,12 @@ const _debugTokenPositions = bool.fromEnvironment('DEBUG_COVERAGE');
5758
Future<Map<String, dynamic>> collect(Uri serviceUri, bool resume,
5859
bool waitPaused, bool includeDart, Set<String>? scopedOutput,
5960
{Set<String>? isolateIds,
60-
Duration? timeout,
61-
bool functionCoverage = false,
62-
bool branchCoverage = false,
63-
Map<String, Set<int>>? coverableLineCache,
64-
VmService? serviceOverrideForTesting}) async {
61+
Duration? timeout,
62+
bool functionCoverage = false,
63+
bool branchCoverage = false,
64+
Map<String, Set<int>>? coverableLineCache,
65+
VmService? serviceOverrideForTesting,
66+
bool Function(String)? filter}) async { // Correct function type
6567
scopedOutput ??= <String>{};
6668

6769
late VmService service;
@@ -95,7 +97,7 @@ Future<Map<String, dynamic>> collect(Uri serviceUri, bool resume,
9597
}
9698

9799
try {
98-
return await _getAllCoverage(
100+
final coverageData = await _getAllCoverage(
99101
service,
100102
includeDart,
101103
functionCoverage,
@@ -104,6 +106,13 @@ Future<Map<String, dynamic>> collect(Uri serviceUri, bool resume,
104106
isolateIds,
105107
coverableLineCache,
106108
waitPaused);
109+
110+
// Apply filtering if a filter function is provided
111+
if (filter != null) {
112+
coverageData.removeWhere((key, value) => !filter(key));
113+
}
114+
115+
return coverageData;
107116
} finally {
108117
if (resume && !waitPaused) {
109118
await _resumeIsolates(service);
@@ -114,6 +123,8 @@ Future<Map<String, dynamic>> collect(Uri serviceUri, bool resume,
114123
}
115124
}
116125

126+
127+
117128
Future<Map<String, dynamic>> _getAllCoverage(
118129
VmService service,
119130
bool includeDart,

pkgs/coverage/lib/src/resolver.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ import 'package:path/path.dart' as p;
99

1010
/// [Resolver] resolves imports with respect to a given environment.
1111
class Resolver {
12+
13+
@Deprecated('Use Resolver.create')
14+
15+
Resolver
16+
({this.packagesPath, this.sdkRoot})
17+
: _packages = packagesPath != null ? _parsePackages(packagesPath) : null,
18+
packagePath = null;
19+
20+
Resolver._
21+
(
22+
{this.packagesPath,
23+
this.packagePath,
24+
this.sdkRoot,
25+
Map<String, Uri>? packages})
26+
: _packages = packages;
1227
/// Returns a list of all Dart files in the project.
1328
List<String> listAllDartFiles({String directoryPath = '.'}) {
1429
final dir = Directory(directoryPath);
@@ -22,18 +37,6 @@ class Resolver {
2237
.toList();
2338
}
2439

25-
@Deprecated('Use Resolver.create')
26-
Resolver({this.packagesPath, this.sdkRoot})
27-
: _packages = packagesPath != null ? _parsePackages(packagesPath) : null,
28-
packagePath = null;
29-
30-
Resolver._(
31-
{this.packagesPath,
32-
this.packagePath,
33-
this.sdkRoot,
34-
Map<String, Uri>? packages})
35-
: _packages = packages;
36-
3740
static Future<Resolver> create({
3841
String? packagesPath,
3942
String? packagePath,

0 commit comments

Comments
 (0)