Lcov constructor

Lcov({
  1. required Config config,
  2. required List<Record> records,
})

Lcov constructor

Implementation

Lcov({required this.config, required this.records}) {
  try {
    coverage = Coverage(config.percentage);
    final fileMatcher = FileMatcherUtil();
    final fileSystemUtil = FileSystemUtil();

    records = records
        .where((record) => !fileMatcher.hasSuffix(
            file: record.file ?? '', excludeSuffixes: config.excludeSuffixes))
        .where((record) => !fileMatcher.hasPattern(
            value: record.file ?? '', patterns: config.excludeFiles))
        .toList();

    if (config.excludeContentsPath != null) {
      final excludeContentsByPathList = fileSystemUtil
          .readAsLinesSync(config.excludeContentsPath!)
          .mapRegex()
          .toList(growable: false);

      records = records.where((record) {
        if (record.file == null) return true;
        final file = File(record.file!);
        final loc = file.readAsLinesSync();
        final hasPatterns = fileMatcher.hasPatterns(
            values: loc, patterns: excludeContentsByPathList);
        return !hasPatterns;
      }).toList();
    } else if (config.excludeContents.isNotEmpty) {
      records = records.where((record) {
        if (record.file == null) return true;
        final file = File(record.file!);
        final loc = file.readAsLinesSync();
        final hasPatterns = fileMatcher.hasPatterns(
            values: loc, patterns: config.excludeContents);
        return !hasPatterns;
      }).toList();
    }

    records.forEach(updateTotals);

    final totalCoverage = (totalHits / totalFinds) * 100;
    coverage.totalCoverage = totalCoverage;
  } on FileSystemException catch (e) {
    print(AppConstants.fileSystemExceptionMessage);
    print(e);
    rethrow;
  } catch (e) {
    print(e);
    rethrow;
  }
}