Skip to content

Commit 0a57e34

Browse files
authored
fix(coverage): allow empty coverage files from skipped test suites (#2667) (#2668)
1 parent e95958f commit 0a57e34

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎pkgs/coverage/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 1.16.1-wip
22

3+
- Fix `HitMap.parseFiles` to allow empty `{}` coverage files written by
4+
`dart test --coverage` for skipped test suites.
35
- `Resolver.resolve` rejects `package:` and `dart:` URIs outside known roots.
46

57
## 1.16.0

‎pkgs/coverage/lib/src/hitmap.dart‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ class HitMap {
172172
final ignoredLinesInFilesCache = <String, List<List<int>>?>{};
173173
for (var file in files) {
174174
final contents = file.readAsStringSync();
175-
switch (json.decode(contents)) {
175+
final decoded = json.decode(contents);
176+
switch (decoded) {
176177
case {'coverage': final List jsonResult}:
177178
globalHitmap.merge(
178179
HitMap.parseJsonSync(
@@ -186,7 +187,11 @@ class HitMap {
186187
scopeOutput: scopeOutput,
187188
),
188189
);
189-
case final decoded:
190+
case Map(isEmpty: true):
191+
// `dart test --coverage` outputs `{}` for suites where all tests
192+
// were skipped.
193+
break;
194+
default:
190195
throw FormatException(
191196
'Unrecognized coverage JSON in "${file.path}". Expected a '
192197
'{"coverage": [...]} report, but got ${decoded.runtimeType}.',

‎pkgs/coverage/test/chrome_test.dart‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ void main() {
208208
'{"coverage": null}',
209209
'{"coverage": {}}',
210210
'{"coverage": "nope"}',
211-
'{}',
212211
'"a string"',
213212
'42',
214213
'null',
@@ -235,6 +234,21 @@ void main() {
235234
}
236235
});
237236

237+
test(
238+
'HitMap.parseFiles ignores empty JSON file from skipped test suite',
239+
() async {
240+
final tempDir = Directory.systemTemp.createTempSync('hitmap_empty_');
241+
final tempFile = File('${tempDir.path}/empty.json')
242+
..writeAsStringSync('{}');
243+
try {
244+
final hitmap = await HitMap.parseFiles([tempFile]);
245+
expect(hitmap, isEmpty);
246+
} finally {
247+
tempDir.deleteSync(recursive: true);
248+
}
249+
},
250+
);
251+
238252
group('HitMap scopeOutput and ScopedOutput', () {
239253
late Directory tempDir;
240254
late String packagesPath;

‎pkgs/coverage/test/collect_coverage_test.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,23 @@ void main() {
186186
}
187187
});
188188

189+
test(
190+
'HitMap.parseFiles ignores empty JSON file from skipped test suite',
191+
() async {
192+
final tempDir = await Directory.systemTemp.createTemp('coverage.test.');
193+
194+
try {
195+
final outputFile = File(p.join(tempDir.path, 'coverage.json'));
196+
await outputFile.writeAsString('{}', flush: true);
197+
198+
final parsedResult = await HitMap.parseFiles([outputFile]);
199+
expect(parsedResult, isEmpty);
200+
} finally {
201+
await tempDir.delete(recursive: true);
202+
}
203+
},
204+
);
205+
189206
test('HitMap.parseFiles with packagesPath and checkIgnoredLines', () async {
190207
final tempDir = await Directory.systemTemp.createTemp('coverage.test.');
191208

0 commit comments

Comments
 (0)