Unverified Commit 4de9b446 authored by Jiahao's avatar Jiahao Committed by GitHub

Parameterize CoverageCollector with a library name predicate (#36774)

An optimization to the coverage collection speed was added in #30811. This commit further expands on it to parameterize the CoverageCollector with a custom predicate, allowing internal use cases to filter the RPC calls to the Dart VM based on scripts of interest to coverage collection.
parent e8d73068
......@@ -117,9 +117,14 @@ Future<void> run(List<String> args) async {
CoverageCollector collector;
if (argResults['coverage']) {
collector = CoverageCollector(
flutterProject: FlutterProject.current(),
coverageDirectory: coverageDirectory,
);
libraryPredicate: (String libraryName) {
// If we have a specified coverage directory then accept all libraries.
if (coverageDirectory != null) {
return true;
}
final String projectName = FlutterProject.current().manifest.appName;
return libraryName.contains(projectName);
});
if (!argResults.options.contains(_kOptionTestDirectory)) {
throwToolExit('Use of --coverage requires setting --test-directory');
}
......
......@@ -185,8 +185,9 @@ class TestCommand extends FastFlutterCommand {
CoverageCollector collector;
if (argResults['coverage'] || argResults['merge-coverage']) {
final String projectName = FlutterProject.current().manifest.appName;
collector = CoverageCollector(
flutterProject: FlutterProject.current(),
libraryPredicate: (String libraryName) => libraryName.contains(projectName),
);
}
......
......@@ -14,18 +14,16 @@ import '../base/platform.dart';
import '../base/process_manager.dart';
import '../dart/package_map.dart';
import '../globals.dart';
import '../project.dart';
import '../vmservice.dart';
import 'watcher.dart';
/// A class that's used to collect coverage data during tests.
class CoverageCollector extends TestWatcher {
CoverageCollector({this.flutterProject, this.coverageDirectory});
CoverageCollector({this.libraryPredicate});
Map<String, dynamic> _globalHitmap;
final Directory coverageDirectory;
final FlutterProject flutterProject;
bool Function(String) libraryPredicate;
@override
Future<void> handleFinishedTest(ProcessEvent event) async {
......@@ -50,13 +48,7 @@ class CoverageCollector extends TestWatcher {
Future<void> collectCoverageIsolate(Uri observatoryUri) async {
assert(observatoryUri != null);
print('collecting coverage data from $observatoryUri...');
final Map<String, dynamic> data = await collect(observatoryUri, (String libraryName) {
// If we have a specified coverage directory or could not find the package name, then
// accept all libraries.
return (coverageDirectory != null)
|| (flutterProject == null)
|| libraryName.contains(flutterProject.manifest.appName);
});
final Map<String, dynamic> data = await collect(observatoryUri, libraryPredicate);
if (data == null) {
throw Exception('Failed to collect coverage.');
}
......@@ -84,17 +76,7 @@ class CoverageCollector extends TestWatcher {
.then<void>((int code) {
throw Exception('Failed to collect coverage, process terminated prematurely with exit code $code.');
});
final Future<void> collectionComplete = collect(observatoryUri, (String libraryName) {
// If we have a specified coverage directory or could not find the package name, then
// accept all libraries.
if (coverageDirectory != null) {
return true;
}
if (flutterProject == null) {
return true;
}
return libraryName.contains(flutterProject.manifest.appName);
})
final Future<void> collectionComplete = collect(observatoryUri, libraryPredicate)
.then<void>((Map<String, dynamic> result) {
if (result == null)
throw Exception('Failed to collect coverage.');
......
......@@ -47,7 +47,7 @@ Future<void> main(List<String> arguments) async {
/// A platform that loads tests in isolates spawned within this Dart process.
class VMPlatform extends PlatformPlugin {
final CoverageCollector coverageCollector = CoverageCollector(
flutterProject: FlutterProject.current(),
libraryPredicate: (String libraryName) => libraryName.contains(FlutterProject.current().manifest.appName),
);
final Map<String, Future<void>> _pending = <String, Future<void>>{};
final String precompiledPath = path.join('.dart_tool', 'build', 'generated', 'flutter_tools');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment