LCOV Reports for Dart

Release License Build

Parse and format LCOV coverage reports, in Dart.

Requirements

The latest Dart SDK and Pub versions. If you plan to play with the sources, you will also need the latest Grinder version.

Installing via Pub

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  lcov: *

2. Install it

Install this package and its dependencies from a command prompt:

$ pub get

3. Import it

Now in your Dart code, you can use:

import 'package:lcov/lcov.dart';

Usage

This package provides a set of classes representing a coverage report and its data. The Report class, the main one, provides the parsing and formatting features.

Parse coverage data from a LCOV file

The Report.parse() static method parses a coverage report provided as string, and returns a Report instance giving detailed information about this coverage report:

try {
  var coverage = await new File('lcov.info').readAsString();
  var report = Report.parse(coverage);
  print('The coverage report contains ${report.records.length} records:');
  print(report.toJson());
}

on FormatException {
  print('The LCOV report has an invalid format.');
}

The Report.toJson() instance method will return a map like this:

{
  "testName": "Example",
  "records": [
    {
      "sourceFile": "/home/cedx/lcov.dart/fixture.dart",
      "branches": {
        "data": [],
        "found": 0,
        "hit": 0
      },
      "functions": {
        "data": [
          {"executionCount": 2, "functionName": "main", "lineNumber": 4}
        ],
        "found": 1,
        "hit": 1
      },
      "lines": {
        "data": [
          {"checksum": "PF4Rz2r7RTliO9u6bZ7h6g", "executionCount": 2, "lineNumber": 6},
          {"checksum": "y7GE3Y4FyXCeXcrtqgSVzw", "executionCount": 2, "lineNumber": 9}
        ],
        "found": 2,
        "hit": 2
      }
    }
  ]
}

Format coverage data to the LCOV format

Each provided class has a dedicated toString() instance method returning the corresponding data formatted as LCOV string. All you have to do is to create the adequate structure using these different classes, and to export the final result:

var lineCoverage = new LineCoverage(4, 4)
  ..data.add(new LineData(6, 2, 'PF4Rz2r7RTliO9u6bZ7h6g'));

var record = new Record('/home/cedx/lcov.dart/fixture.dart')
  ..functions = new FunctionCoverage(1, 1)
  ..lines = lineCoverage;

var report = new Report('Example')
  ..records.add(record);

print(report);

The Report.toString() method will return a LCOV report formatted like this:

TN:Example
SF:/home/cedx/lcov.dart/fixture.dart
FNF:1
FNH:1
DA:6,2,PF4Rz2r7RTliO9u6bZ7h6g
LF:4
LH:4
end_of_record

See also

License

LCOV Reports for Dart is distributed under the Apache License, version 2.0.

Libraries

lcov

Parse and format LCOV coverage reports.