|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:coverage/src/util.dart'; |
| 6 | +import 'package:source_maps/parser.dart'; |
| 7 | + |
| 8 | +/// Returns a Dart based hit-map containing coverage report for the provided |
| 9 | +/// Chrome [preciseCoverage]. |
| 10 | +/// |
| 11 | +/// [sourceProvider] returns the source content for the Chrome scriptId, or null |
| 12 | +/// if not available. |
| 13 | +/// |
| 14 | +/// [sourceMapProvider] returns the associated source map content for the Chrome |
| 15 | +/// scriptId, or null if not available. |
| 16 | +/// |
| 17 | +/// [sourceUriProvider] returns the uri for the provided sourceUrl and |
| 18 | +/// associated scriptId. |
| 19 | +/// |
| 20 | +/// Chrome coverage information for which the corresponding source map or source |
| 21 | +/// content is null will be ignored. |
| 22 | +Future<Map<String, dynamic>> parseChromeCoverage( |
| 23 | + List<Map<String, dynamic>> preciseCoverage, |
| 24 | + Future<String> Function(String scriptId) sourceProvider, |
| 25 | + Future<String> Function(String scriptId) sourceMapProvider, |
| 26 | + Future<Uri> Function(String sourceUrl, String scriptId) sourceUriProvider, |
| 27 | +) async { |
| 28 | + final coverageReport = <Uri, Map<int, bool>>{}; |
| 29 | + for (Map<String, dynamic> entry in preciseCoverage) { |
| 30 | + final String scriptId = entry['scriptId']; |
| 31 | + |
| 32 | + final mapResponse = await sourceMapProvider(scriptId); |
| 33 | + if (mapResponse == null) continue; |
| 34 | + |
| 35 | + final SingleMapping mapping = parse(mapResponse); |
| 36 | + |
| 37 | + final compiledSource = await sourceProvider(scriptId); |
| 38 | + if (compiledSource == null) continue; |
| 39 | + |
| 40 | + final coverageInfo = _coverageInfoFor(entry); |
| 41 | + final offsetCoverage = _offsetCoverage(coverageInfo, compiledSource.length); |
| 42 | + final coveredPositions = _coveredPositions(compiledSource, offsetCoverage); |
| 43 | + |
| 44 | + for (var lineEntry in mapping.lines) { |
| 45 | + for (var columnEntry in lineEntry.entries) { |
| 46 | + if (columnEntry.sourceUrlId == null) continue; |
| 47 | + final sourceUrl = mapping.urls[columnEntry.sourceUrlId]; |
| 48 | + |
| 49 | + // Ignore coverage information for the SDK. |
| 50 | + if (sourceUrl.startsWith('org-dartlang-sdk:')) continue; |
| 51 | + |
| 52 | + final uri = await sourceUriProvider(sourceUrl, scriptId); |
| 53 | + final coverage = coverageReport.putIfAbsent(uri, () => <int, bool>{}); |
| 54 | + |
| 55 | + coverage[columnEntry.sourceLine + 1] = coveredPositions |
| 56 | + .contains(_Position(lineEntry.line + 1, columnEntry.column + 1)); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + final coverageHitMaps = <Uri, Map<int, int>>{}; |
| 62 | + coverageReport.forEach((uri, coverage) { |
| 63 | + final hitMap = <int, int>{}; |
| 64 | + for (var line in coverage.keys.toList()..sort()) { |
| 65 | + hitMap[line] = coverage[line] ? 1 : 0; |
| 66 | + } |
| 67 | + coverageHitMaps[uri] = hitMap; |
| 68 | + }); |
| 69 | + |
| 70 | + final allCoverage = <Map<String, dynamic>>[]; |
| 71 | + coverageHitMaps.forEach((uri, hitMap) { |
| 72 | + allCoverage.add(toScriptCoverageJson(uri, hitMap)); |
| 73 | + }); |
| 74 | + return <String, dynamic>{'type': 'CodeCoverage', 'coverage': allCoverage}; |
| 75 | +} |
| 76 | + |
| 77 | +/// Returns all covered positions in a provided source. |
| 78 | +Set<_Position> _coveredPositions( |
| 79 | + String compiledSource, List<bool> offsetCoverage) { |
| 80 | + final positions = Set<_Position>(); |
| 81 | + // Line is 1 based. |
| 82 | + var line = 1; |
| 83 | + // Column is 1 based. |
| 84 | + var column = 0; |
| 85 | + for (var offset = 0; offset < compiledSource.length; offset++) { |
| 86 | + if (compiledSource[offset] == '\n') { |
| 87 | + line++; |
| 88 | + column = 0; |
| 89 | + } else { |
| 90 | + column++; |
| 91 | + } |
| 92 | + if (offsetCoverage[offset]) positions.add(_Position(line, column)); |
| 93 | + } |
| 94 | + return positions; |
| 95 | +} |
| 96 | + |
| 97 | +/// Returns coverage information for a Chrome entry. |
| 98 | +List<_CoverageInfo> _coverageInfoFor(Map<String, dynamic> entry) { |
| 99 | + final result = <_CoverageInfo>[]; |
| 100 | + for (Map<String, dynamic> functions in entry['functions']) { |
| 101 | + for (Map<String, dynamic> range in functions['ranges']) { |
| 102 | + result.add(_CoverageInfo( |
| 103 | + range['startOffset'], |
| 104 | + range['endOffset'], |
| 105 | + range['count'] > 0, |
| 106 | + )); |
| 107 | + } |
| 108 | + } |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | +/// Returns the coverage information for each offset. |
| 113 | +List<bool> _offsetCoverage(List<_CoverageInfo> coverageInfo, int sourceLength) { |
| 114 | + final offsetCoverage = List.filled(sourceLength, false); |
| 115 | + |
| 116 | + // Sort coverage information by their size. |
| 117 | + // Coverage information takes granularity as precedence. |
| 118 | + coverageInfo.sort((a, b) => |
| 119 | + (b.endOffset - b.startOffset).compareTo(a.endOffset - a.startOffset)); |
| 120 | + |
| 121 | + for (var range in coverageInfo) { |
| 122 | + for (var i = range.startOffset; i < range.endOffset; i++) { |
| 123 | + offsetCoverage[i] = range.isCovered; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return offsetCoverage; |
| 128 | +} |
| 129 | + |
| 130 | +class _CoverageInfo { |
| 131 | + _CoverageInfo(this.startOffset, this.endOffset, this.isCovered); |
| 132 | + |
| 133 | + /// 0 based byte offset. |
| 134 | + final int startOffset; |
| 135 | + |
| 136 | + /// 0 based byte offset. |
| 137 | + final int endOffset; |
| 138 | + |
| 139 | + final bool isCovered; |
| 140 | +} |
| 141 | + |
| 142 | +/// A covered position in a source file where [line] and [column] are 1 based. |
| 143 | +class _Position { |
| 144 | + _Position(this.line, this.column); |
| 145 | + |
| 146 | + final int line; |
| 147 | + final int column; |
| 148 | + |
| 149 | + @override |
| 150 | + int get hashCode => hash2(line, column); |
| 151 | + |
| 152 | + @override |
| 153 | + bool operator ==(dynamic o) => |
| 154 | + o is _Position && o.line == line && o.column == column; |
| 155 | +} |
0 commit comments