unit_coverage.dart 1.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';

/// Produces a per-library coverage summary when fed an lcov file, sorted by
/// increasing code coverage percentage.
///
/// Usage: `dart tool/unit_coverage lcov.info`
void main(List<String> args) {
  if (args.isEmpty || args.length > 1) {
    print('Usage: dart tool/unit_coverage lcov.info');
    return;
  }
  final List<String> lines = File(args.single).readAsLinesSync();
  final List<Coverage> coverages = <Coverage>[];
18
  Coverage? currentCoverage;
19 20 21 22 23 24 25 26

  for (final String line in lines) {
    if (line.startsWith('SF:')) {
      final String library = line.split('SF:')[1];
      currentCoverage = Coverage()..library = library;
      coverages.add(currentCoverage);
    }
    if (line.startsWith('DA')) {
27
      currentCoverage?.totalLines += 1;
28
      if (!line.endsWith(',0')) {
29
        currentCoverage?.testedLines += 1;
30 31 32 33 34 35 36 37 38 39 40
      }
    }
    if (line == 'end_of_record') {
      currentCoverage = null;
    }
  }
  coverages.sort((Coverage left, Coverage right) {
    final double leftPercent = left.testedLines / left.totalLines;
    final double rightPercent = right.testedLines / right.totalLines;
    return leftPercent.compareTo(rightPercent);
  });
41 42 43
  double overallNumerator = 0;
  double overallDenominator = 0;
  print('% | tested | total');
44
  for (final Coverage coverage in coverages) {
45 46
    overallNumerator += coverage.testedLines;
    overallDenominator += coverage.totalLines;
47
    final String coveragePercent = (coverage.testedLines / coverage.totalLines * 100).toStringAsFixed(2);
48
    print('${coverage.library}: $coveragePercent% | ${coverage.testedLines} | ${coverage.totalLines}');
49
  }
50
  print('OVERALL: ${overallNumerator/overallDenominator}');
51 52 53
}

class Coverage {
54
  String? library;
55 56 57
  int totalLines = 0;
  int testedLines = 0;
}