extract method
Runs the given package tests with the processRunner, parses the coverage output
with the hitmapReader and creates a CoverageReport with the coverageReportFactory
with the resulting hitmaps.
Return a CoverageExtractionResult
containing the coverage report and
the status of the tests executed as a TestResultStatus.
If the showTestOutput flag is true, the tests outputs will be shows in the console
Implementation
Future<CoverageExtractionResult> extract({
required Directory packageDirectory,
required bool showTestOutput,
List<String>? includeRegexes,
List<String>? excludeRegexes,
bool? ignoreBarrelFiles,
}) async {
if (!hasTestDirectory(packageDirectory)) {
throw Exception(
'Could not find "test" directory in "${packageDirectory.absolute.path}"');
}
final coverageOutputDirectory = _getCoverageOutputDir(packageDirectory);
final packageData = getPackageData(directory: packageDirectory);
if (packageData == null) {
throw Exception(
'Could not find package name in pubspec.yaml. Working directory: ${packageDirectory.absolute.path}');
}
print('Running tests for package ${packageData.name}...');
final testRunner = _createTestRunner(packageData);
final testResult = await testRunner.runTests(
packageData,
coverageOutputDirectory: coverageOutputDirectory,
showTestOutput: showTestOutput,
);
final coverageReport = coverageReportFactory.create(
hitmap: testResult.hitmap,
package: packageData.name,
packageDirectory: packageData.directory,
includeRegexes: includeRegexes,
excludeRegexes: excludeRegexes,
ignoreBarrelFiles: ignoreBarrelFiles,
);
if (coverageOutputDirectory.existsSync()) {
await coverageOutputDirectory.delete(recursive: true);
}
return CoverageExtractionResult(
testResultStatus: testResult.exitCode == 1
? TestResultStatus.ERROR
: TestResultStatus.SUCCESS,
coverageReport: coverageReport,
);
}