Skip to content

Instantly share code, notes, and snippets.

@apaatsio
Last active May 23, 2022 13:26
Show Gist options
  • Save apaatsio/8a10e52bccc29a0d598f6a3841f8966f to your computer and use it in GitHub Desktop.
Save apaatsio/8a10e52bccc29a0d598f6a3841f8966f to your computer and use it in GitHub Desktop.
Calculate total test coverage from lcov.info file generated by `flutter test --coverage`
// NOTE: The preferred way is to install lcov and use command `lcov --summary path/to/lcov.info`
// Use this script only if you can't install lcov on your platform.
// Usage: dart coverage.dart path/to/lcov.info
import 'dart:io';
void main(List<String> args) async {
final lcovFile = args[0];
final lines = await File(lcovFile).readAsLines();
final coverage = lines.fold([0, 0], (List<int> data, line) {
var testedLines = data[0];
var totalLines = data[1];
if (line.startsWith('DA')) {
totalLines++;
if (!line.endsWith(',0')) {
testedLines++;
}
}
return [testedLines, totalLines];
});
final testedLines = coverage[0];
final totalLines = coverage[1];
print(
'Total test coverage: ${(testedLines / totalLines * 100).toStringAsFixed(2)}%');
}
@andrzejchm
Copy link

line number 16 should state:

 if (!line.endsWith(',0')) {

instead of

if (!line.endsWith('0')) {

otherwise lines reported like DA:103;10 will be considered as not covered at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment