diff --git a/packages/flutter_tools/bin/fuchsia_tester.dart b/packages/flutter_tools/bin/fuchsia_tester.dart index ee67b685f6..b78eea375d 100644 --- a/packages/flutter_tools/bin/fuchsia_tester.dart +++ b/packages/flutter_tools/bin/fuchsia_tester.dart @@ -117,9 +117,14 @@ Future run(List 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'); } diff --git a/packages/flutter_tools/lib/src/commands/test.dart b/packages/flutter_tools/lib/src/commands/test.dart index 341222c245..9b306f5ccc 100644 --- a/packages/flutter_tools/lib/src/commands/test.dart +++ b/packages/flutter_tools/lib/src/commands/test.dart @@ -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), ); } diff --git a/packages/flutter_tools/lib/src/test/coverage_collector.dart b/packages/flutter_tools/lib/src/test/coverage_collector.dart index 09c4c4087d..4a2b444a70 100644 --- a/packages/flutter_tools/lib/src/test/coverage_collector.dart +++ b/packages/flutter_tools/lib/src/test/coverage_collector.dart @@ -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 _globalHitmap; - final Directory coverageDirectory; - final FlutterProject flutterProject; + bool Function(String) libraryPredicate; @override Future handleFinishedTest(ProcessEvent event) async { @@ -50,13 +48,7 @@ class CoverageCollector extends TestWatcher { Future collectCoverageIsolate(Uri observatoryUri) async { assert(observatoryUri != null); print('collecting coverage data from $observatoryUri...'); - final Map 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 data = await collect(observatoryUri, libraryPredicate); if (data == null) { throw Exception('Failed to collect coverage.'); } @@ -84,17 +76,7 @@ class CoverageCollector extends TestWatcher { .then((int code) { throw Exception('Failed to collect coverage, process terminated prematurely with exit code $code.'); }); - final Future 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 collectionComplete = collect(observatoryUri, libraryPredicate) .then((Map result) { if (result == null) throw Exception('Failed to collect coverage.'); diff --git a/packages/flutter_tools/tool/tool_coverage.dart b/packages/flutter_tools/tool/tool_coverage.dart index bc2cf824cb..865ef6ca53 100644 --- a/packages/flutter_tools/tool/tool_coverage.dart +++ b/packages/flutter_tools/tool/tool_coverage.dart @@ -47,7 +47,7 @@ Future main(List 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> _pending = >{}; final String precompiledPath = path.join('.dart_tool', 'build', 'generated', 'flutter_tools');