mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Make tool coverage collection resilient to sentinel coverage data (#35186)
This commit is contained in:
parent
24eabe1f91
commit
a0b2878e76
@ -184,13 +184,17 @@ class CoverageCollector extends TestWatcher {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> collect(Uri serviceUri, bool Function(String) libraryPredicate) async {
|
||||
final VMService vmService = await VMService.connect(serviceUri, compression: CompressionOptions.compressionOff);
|
||||
Future<VMService> _defaultConnect(Uri serviceUri) {
|
||||
return VMService.connect(serviceUri, compression: CompressionOptions.compressionOff);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> collect(Uri serviceUri, bool Function(String) libraryPredicate,
|
||||
[Future<VMService> Function(Uri) connector = _defaultConnect]) async {
|
||||
final VMService vmService = await connector(serviceUri);
|
||||
await vmService.getVM();
|
||||
return _getAllCoverage(vmService, libraryPredicate);
|
||||
}
|
||||
|
||||
|
||||
Future<Map<String, dynamic>> _getAllCoverage(VMService service, bool Function(String) libraryPredicate) async {
|
||||
await service.getVM();
|
||||
final List<Map<String, dynamic>> coverage = <Map<String, dynamic>>[];
|
||||
@ -203,6 +207,13 @@ Future<Map<String, dynamic>> _getAllCoverage(VMService service, bool Function(St
|
||||
final Map<String, Map<String, dynamic>> sourceReports = <String, Map<String, dynamic>>{};
|
||||
// For each ScriptRef loaded into the VM, load the corresponding Script and
|
||||
// SourceReport object.
|
||||
|
||||
// We may receive such objects as
|
||||
// {type: Sentinel, kind: Collected, valueAsString: <collected>}
|
||||
// that need to be skipped.
|
||||
if (scriptList['scripts'] == null) {
|
||||
continue;
|
||||
}
|
||||
for (Map<String, dynamic> script in scriptList['scripts']) {
|
||||
if (!libraryPredicate(script['uri'])) {
|
||||
continue;
|
||||
|
||||
51
packages/flutter_tools/test/coverage_collector_test.dart
Normal file
51
packages/flutter_tools/test/coverage_collector_test.dart
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright 2019 The Chromium 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:async';
|
||||
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/test/coverage_collector.dart';
|
||||
import 'package:flutter_tools/src/vmservice.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'src/common.dart';
|
||||
|
||||
void main() {
|
||||
MockVMService mockVMService;
|
||||
|
||||
setUp(() {
|
||||
mockVMService = MockVMService();
|
||||
});
|
||||
|
||||
test('Coverage collector Can handle coverage sentinenl data', () async {
|
||||
when(mockVMService.vm.isolates.first.invokeRpcRaw('getScripts', params: anyNamed('params')))
|
||||
.thenAnswer((Invocation invocation) async {
|
||||
return <String, Object>{'type': 'Sentinel', 'kind': 'Collected', 'valueAsString': '<collected>'};
|
||||
});
|
||||
final Map<String, Object> result = await collect(null, (String predicate) => true, (Uri uri) async {
|
||||
return mockVMService;
|
||||
});
|
||||
|
||||
expect(result, <String, Object>{'type': 'CodeCoverage', 'coverage': <Object>[]});
|
||||
});
|
||||
}
|
||||
|
||||
class MockVMService extends Mock implements VMService {
|
||||
@override
|
||||
final MockVM vm = MockVM();
|
||||
}
|
||||
|
||||
class MockVM extends Mock implements VM {
|
||||
@override
|
||||
final List<MockIsolate> isolates = <MockIsolate>[ MockIsolate() ];
|
||||
}
|
||||
|
||||
class MockIsolate extends Mock implements Isolate {}
|
||||
|
||||
class MockProcess extends Mock implements Process {
|
||||
final Completer<int>completer = Completer<int>();
|
||||
|
||||
@override
|
||||
Future<int> get exitCode => completer.future;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user