diff --git a/packages/flutter_tools/bin/fuchsia_tester.dart b/packages/flutter_tools/bin/fuchsia_tester.dart index 2f0dbf815ae..8d9797270c4 100644 --- a/packages/flutter_tools/bin/fuchsia_tester.dart +++ b/packages/flutter_tools/bin/fuchsia_tester.dart @@ -30,6 +30,7 @@ const String _kOptionTestDirectory = 'test-directory'; const String _kOptionSdkRoot = 'sdk-root'; const String _kOptionIcudtl = 'icudtl'; const String _kOptionTests = 'tests'; +const String _kOptionCoverageDirectory = 'coverage-directory'; const List _kRequiredOptions = [ _kOptionPackages, _kOptionShell, @@ -55,6 +56,7 @@ Future run(List args) async { ..addOption(_kOptionSdkRoot, help: 'Path to the SDK platform files') ..addOption(_kOptionIcudtl, help: 'Path to the ICU data file') ..addOption(_kOptionTests, help: 'Path to json file that maps Dart test files to precompiled dill files') + ..addOption(_kOptionCoverageDirectory, help: 'The path to the directory that will have coverage collected') ..addFlag(_kOptionCoverage, defaultsTo: false, negatable: false, @@ -85,6 +87,14 @@ Future run(List args) async { if (!fs.isDirectorySync(sdkRootSrc.path)) { throwToolExit('Cannot find SDK files at ${sdkRootSrc.path}'); } + Directory coverageDirectory; + final String coverageDirectoryPath = argResults[_kOptionCoverageDirectory]; + if (coverageDirectoryPath != null) { + if (!fs.isDirectorySync(coverageDirectoryPath)) { + throwToolExit('Cannot find coverage directory at $coverageDirectoryPath'); + } + coverageDirectory = fs.directory(coverageDirectoryPath); + } // Put the tester shell where runTests expects it. // TODO(tvolkert,garymm): Switch to a Fuchsia-specific Artifacts impl. @@ -131,9 +141,14 @@ Future run(List args) async { if (collector != null) { // collector expects currentDirectory to be the root of the dart - // package (i.e. contains lib/ and test/ sub-dirs). - fs.currentDirectory = testDirectory.parent; - if (!await collector.collectCoverageData(argResults[_kOptionCoveragePath])) + // package (i.e. contains lib/ and test/ sub-dirs). In some cases, + // test files may appear to be in the root directory. + if (coverageDirectory == null) { + fs.currentDirectory = testDirectory.parent; + } else { + fs.currentDirectory = testDirectory; + } + if (!await collector.collectCoverageData(argResults[_kOptionCoveragePath], coverageDirectory: coverageDirectory)) throwToolExit('Failed to collect coverage data'); } } finally { diff --git a/packages/flutter_tools/lib/src/test/coverage_collector.dart b/packages/flutter_tools/lib/src/test/coverage_collector.dart index 8d16526f943..580b3933ceb 100644 --- a/packages/flutter_tools/lib/src/test/coverage_collector.dart +++ b/packages/flutter_tools/lib/src/test/coverage_collector.dart @@ -83,6 +83,7 @@ class CoverageCollector extends TestWatcher { Future finalizeCoverage({ coverage.Formatter formatter, Duration timeout, + Directory coverageDirectory, }) async { printTrace('formating coverage data'); if (_globalHitmap == null) @@ -90,7 +91,9 @@ class CoverageCollector extends TestWatcher { if (formatter == null) { final coverage.Resolver resolver = coverage.Resolver(packagesPath: PackageMap.globalPackagesPath); final String packagePath = fs.currentDirectory.path; - final List reportOn = [fs.path.join(packagePath, 'lib')]; + final List reportOn = coverageDirectory == null + ? [fs.path.join(packagePath, 'lib')] + : [coverageDirectory.path]; formatter = coverage.LcovFormatter(resolver, reportOn: reportOn, basePath: packagePath); } final String result = await formatter.format(_globalHitmap); @@ -98,10 +101,11 @@ class CoverageCollector extends TestWatcher { return result; } - Future collectCoverageData(String coveragePath, { bool mergeCoverageData = false }) async { + Future collectCoverageData(String coveragePath, { bool mergeCoverageData = false, Directory coverageDirectory }) async { final Status status = logger.startProgress('Collecting coverage information...'); final String coverageData = await finalizeCoverage( timeout: const Duration(seconds: 30), + coverageDirectory: coverageDirectory, ); status.stop(); printTrace('coverage information collection complete');