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 { ...@@ -117,9 +117,14 @@ Future<void> run(List<String> args) async {
CoverageCollector collector; CoverageCollector collector;
if (argResults['coverage']) { if (argResults['coverage']) {
collector = CoverageCollector( collector = CoverageCollector(
flutterProject: FlutterProject.current(), libraryPredicate: (String libraryName) {
coverageDirectory: coverageDirectory, // 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)) { if (!argResults.options.contains(_kOptionTestDirectory)) {
throwToolExit('Use of --coverage requires setting --test-directory'); throwToolExit('Use of --coverage requires setting --test-directory');
} }
......
...@@ -185,8 +185,9 @@ class TestCommand extends FastFlutterCommand { ...@@ -185,8 +185,9 @@ class TestCommand extends FastFlutterCommand {
CoverageCollector collector; CoverageCollector collector;
if (argResults['coverage'] || argResults['merge-coverage']) { if (argResults['coverage'] || argResults['merge-coverage']) {
final String projectName = FlutterProject.current().manifest.appName;
collector = CoverageCollector( collector = CoverageCollector(
flutterProject: FlutterProject.current(), libraryPredicate: (String libraryName) => libraryName.contains(projectName),
); );
} }
......
...@@ -14,18 +14,16 @@ import '../base/platform.dart'; ...@@ -14,18 +14,16 @@ import '../base/platform.dart';
import '../base/process_manager.dart'; import '../base/process_manager.dart';
import '../dart/package_map.dart'; import '../dart/package_map.dart';
import '../globals.dart'; import '../globals.dart';
import '../project.dart';
import '../vmservice.dart'; import '../vmservice.dart';
import 'watcher.dart'; import 'watcher.dart';
/// A class that's used to collect coverage data during tests. /// A class that's used to collect coverage data during tests.
class CoverageCollector extends TestWatcher { class CoverageCollector extends TestWatcher {
CoverageCollector({this.flutterProject, this.coverageDirectory}); CoverageCollector({this.libraryPredicate});
Map<String, dynamic> _globalHitmap; Map<String, dynamic> _globalHitmap;
final Directory coverageDirectory; bool Function(String) libraryPredicate;
final FlutterProject flutterProject;
@override @override
Future<void> handleFinishedTest(ProcessEvent event) async { Future<void> handleFinishedTest(ProcessEvent event) async {
...@@ -50,13 +48,7 @@ class CoverageCollector extends TestWatcher { ...@@ -50,13 +48,7 @@ class CoverageCollector extends TestWatcher {
Future<void> collectCoverageIsolate(Uri observatoryUri) async { Future<void> collectCoverageIsolate(Uri observatoryUri) async {
assert(observatoryUri != null); assert(observatoryUri != null);
print('collecting coverage data from $observatoryUri...'); print('collecting coverage data from $observatoryUri...');
final Map<String, dynamic> data = await collect(observatoryUri, (String libraryName) { final Map<String, dynamic> data = await collect(observatoryUri, libraryPredicate);
// 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);
});
if (data == null) { if (data == null) {
throw Exception('Failed to collect coverage.'); throw Exception('Failed to collect coverage.');
} }
...@@ -84,17 +76,7 @@ class CoverageCollector extends TestWatcher { ...@@ -84,17 +76,7 @@ class CoverageCollector extends TestWatcher {
.then<void>((int code) { .then<void>((int code) {
throw Exception('Failed to collect coverage, process terminated prematurely with exit code $code.'); throw Exception('Failed to collect coverage, process terminated prematurely with exit code $code.');
}); });
final Future<void> collectionComplete = collect(observatoryUri, (String libraryName) { final Future<void> collectionComplete = collect(observatoryUri, libraryPredicate)
// 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);
})
.then<void>((Map<String, dynamic> result) { .then<void>((Map<String, dynamic> result) {
if (result == null) if (result == null)
throw Exception('Failed to collect coverage.'); throw Exception('Failed to collect coverage.');
......
...@@ -47,7 +47,7 @@ Future<void> main(List<String> arguments) async { ...@@ -47,7 +47,7 @@ Future<void> main(List<String> arguments) async {
/// A platform that loads tests in isolates spawned within this Dart process. /// A platform that loads tests in isolates spawned within this Dart process.
class VMPlatform extends PlatformPlugin { class VMPlatform extends PlatformPlugin {
final CoverageCollector coverageCollector = CoverageCollector( 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 Map<String, Future<void>> _pending = <String, Future<void>>{};
final String precompiledPath = path.join('.dart_tool', 'build', 'generated', 'flutter_tools'); 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